diff --git a/build.gradle b/build.gradle index 2231cf1f4a1af4ab246ffc2cefcdb5caadafeae3..06fb1feaba0f9d312ec6c050114ccf59214b5ee5 100644 --- a/build.gradle +++ b/build.gradle @@ -46,7 +46,7 @@ test { } application { - mainClass = 'sprint2_demo.Grid' + mainClass = 'pong.PongApp' } spotbugs { diff --git a/src/main/java/engine/Engine.java b/src/main/java/engine/Engine.java index b01463f4d833bddf6c9d68dd59785e99e551291d..78c16c4c1b694f713fa90b23bf0b2b2b5cb123e2 100644 --- a/src/main/java/engine/Engine.java +++ b/src/main/java/engine/Engine.java @@ -1,6 +1,18 @@ package engine; +/** + * Interface for the engine which will be used to run the game. + * The core-kernel of the game will use implemented objects of this interface to run the game. + */ public interface Engine { + + /** + * Method that update the state of an engine + */ void update(); + + /** + * Method that restart the state of an engine + */ void restart(); } diff --git a/src/main/java/engine/Kernel.java b/src/main/java/engine/Kernel.java index 1d6d684783f7bf2f99ada1c3cd564afb75706c01..7a4ca426ed9aea3bf470be3fe5f79bf5a021e379 100644 --- a/src/main/java/engine/Kernel.java +++ b/src/main/java/engine/Kernel.java @@ -5,7 +5,7 @@ import engine.physic.CartesianVector; import engine.physic.Entity; import engine.sound.SoundEngine; import engine.physic.PhysicEngine; -import pong.PongObject; +import pong.GameObject2D; import javax.swing.*; import java.awt.event.ActionEvent; @@ -22,10 +22,10 @@ public class Kernel { public int isFirstUpdate = 0; - public static ArrayList<PongObject> gameObjects; - public HashMap<PongObject, Entity> pongObjectEntityHashMap; + public static ArrayList<GameObject2D> gameObjects; + public HashMap<GameObject2D, Entity> gameObject2DEntityHashMap; - public Kernel(String gameName, int width, int height, ArrayList<PongObject> gameObjects) throws IOException { + public Kernel(String gameName, int width, int height, ArrayList<GameObject2D> gameObjects) throws IOException { this.setGameObject(gameObjects); System.out.println("Model created"); @@ -41,7 +41,7 @@ public class Kernel { engines.add(this.graphicEngine);// this.physicEngine = new PhysicEngine(soundEngine); engines.add(physicEngine); - pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; + gameObject2DEntityHashMap = physicEngine.gameObject2DEntityHashMap; this.update(); this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane()); @@ -50,7 +50,7 @@ public class Kernel { } /** - * Start the game. Associate a Movable to each GameObject and draw them. + * Start the game. Associate a Movable to each pong.GameObject2D and draw them. * * @throws IOException */ @@ -66,7 +66,7 @@ public class Kernel { engine.restart(); } isFirstUpdate = 0; - //pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; + //GameObject2DEntityHashMap = physicEngine.GameObject2DEntityHashMap; } @@ -102,28 +102,28 @@ public class Kernel { Action upAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, -3)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, -3)); } }; Action downAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, 3)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, 3)); } }; Action zAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, -3)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, -3)); } }; Action sAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, 3)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, 3)); } }; @@ -143,28 +143,28 @@ public class Kernel { Action upReleaseAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, 0)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, 0)); } }; Action downReleaseAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, 0)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(1)).setSpeed(new CartesianVector(0, 0)); } }; Action zReleaseAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, 0)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, 0)); } }; Action sReleaseAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, 0)); + physicEngine.gameObject2DEntityHashMap.get(gameObjects.get(0)).setSpeed(new CartesianVector(0, 0)); } }; @@ -183,23 +183,19 @@ public class Kernel { mainPanel.setFocusable(true); } - public void setGameObject(ArrayList<PongObject> gameObject) { + public void setGameObject(ArrayList<GameObject2D> gameObject) { gameObjects = gameObject; } + /** + * Add a new button to the game. + * @param button the JButton to add + */ public void addButton(JButton button){ button.setSize(50, 30); graphicEngine.addButton(button); } - /** - * Add a new sound to the game. - * @param soundPath - * @param isPlaying - */ - public void addSound(String soundPath, boolean isPlaying){ - soundEngine.addSound(soundPath, isPlaying); - } /** * Play a sound @@ -208,10 +204,6 @@ public class Kernel { soundEngine.playMusic(soundPath); } - public boolean isItACollision(){ - return physicEngine.isCollision; - } - public void muteSoundEngine(){ soundEngine.mute(); } diff --git a/src/main/java/engine/graphic/GraphicEngine.java b/src/main/java/engine/graphic/GraphicEngine.java index 9dda229f705567a2c459999bdbe026e03292a5ac..d801024c52d68e5db22d400237540704fd15920e 100644 --- a/src/main/java/engine/graphic/GraphicEngine.java +++ b/src/main/java/engine/graphic/GraphicEngine.java @@ -3,6 +3,7 @@ package engine.graphic; import engine.Engine; import engine.Kernel; import engine.physic.CartesianVector; +import pong.GameObject2D; import pong.PongApp; import pong.PongObject; @@ -10,33 +11,37 @@ import javax.swing.*; import java.awt.*; import java.io.IOException; +/** + * Class which implements the Engine interface and allows to create a graphic engine. + * A graphic engine is a window which displays the game, and allows to interact with it, adding buttons, etc. + * @see Engine + */ public class GraphicEngine extends JFrame implements Engine { - JPanel controlPanel = new JPanel(); - JPanel gamePanel = new JPanel(); - public GraphicEngine() throws IOException { + JPanel controlPanel = new JPanel(); // Panel which contains the control buttons + + public GraphicEngine(){ } - public void init(String windowTitle, int width, int height) throws IOException { - // mainPanel.setBounds(850,500,122,124); + /** + * Method which initializes the graphic engine. + * @param windowTitle The title of the window. + * @param width The width of the window. + * @param height The height of the window. + */ + public void init(String windowTitle, int width, int height){ + this.setTitle(windowTitle); - // this.setContentPane(mainPanel); + this.setMinimumSize(new Dimension(width, height)); - // this.setContentPane(mainPanel); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); controlPanel.setPreferredSize(new Dimension(width, 30)); controlPanel.setBackground(Color.BLACK); - // Ajoutez controlPanel au nord et gamePanel au centre du mainPanel this.getContentPane().add(controlPanel, BorderLayout.NORTH); this.getContentPane().setBackground(Color.BLACK); - //mainPanel.add(gamePanel, BorderLayout.CENTER); - - - // Ajoutez mainPanel comme contenu principal - //this.setContentPane(mainPanel); - //this.setContentPane(mainPanel); this.setLocationRelativeTo(null); this.pack(); @@ -44,18 +49,28 @@ public class GraphicEngine extends JFrame implements Engine { this.setVisible(true); this.setLayout(this.getLayout()); } + + /** + * Method which draws an image on the window. + * @param ic The ImageComponent to draw. + * @see ImageComponent + * @throws IOException + */ public void draw(ImageComponent ic) throws IOException { this.getContentPane().add(ic); repaint(); revalidate(); } + /** + * Method which starts the graphic engine. + * Associates the ImageComponents to the PongObjects and draws them on the window. + */ public void start(){ - - for (PongObject po : Kernel.gameObjects + for (GameObject2D go : Kernel.gameObjects ) { try { - this.draw(new ImageComponent(po.getImage(), po.getPosition(), po.getWidth(), po.getHeight())); + this.draw(new ImageComponent(go.getImage(), go.getPosition(), go.getWidth(), go.getHeight())); } catch (IOException e) { throw new RuntimeException(e); } @@ -67,13 +82,19 @@ public class GraphicEngine extends JFrame implements Engine { } + /** + * Method which updates the graphic engine. + * @see Engine + */ @Override public void update(){ - //System.out.println(gamePanel.isVisible()); repaint(); revalidate(); } + /** + * Method which restarts the graphic engine. + */ @Override public void restart() { for(Component c : this.getContentPane().getComponents()){ @@ -82,9 +103,11 @@ public class GraphicEngine extends JFrame implements Engine { } } start(); - //TODO: Trouver un moyen de ne pas avoir à redessiner le rectangle à chaque restart et rendre le restart plus générique } + /** + * Method which draws a rectangle on the window. + */ public void drawRect(){ this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight)); repaint(); @@ -95,10 +118,10 @@ public class GraphicEngine extends JFrame implements Engine { this.getContentPane().add(new ScoreComponent(new CartesianVector(100, 100), new CartesianVector(500, 100), score1, score2)); } + /** + *Method which add a button to the control panel. + */ public void addButton(JButton button){ - //TODO: add button action - - controlPanel.add(button); repaint(); revalidate(); diff --git a/src/main/java/engine/graphic/ImageComponent.java b/src/main/java/engine/graphic/ImageComponent.java index 310360fcdb16c14f3e27a73a9002d17b7cfffd9f..08b615a0399397ed974eac9e08abc2dabc1fb697 100644 --- a/src/main/java/engine/graphic/ImageComponent.java +++ b/src/main/java/engine/graphic/ImageComponent.java @@ -6,43 +6,38 @@ import pong.PongApp; import javax.swing.*; import java.awt.*; +/** + * ImageComponent class which extends JComponent and allows to draw an image on the window. + * @see JComponent + */ public class ImageComponent extends JComponent { private Image image; private CartesianVector position; - private int width; private int height; + /** + * Constructor of the ImageComponent class. + * @param image The image to draw. + * @param position The position of the image. + * @param width The width of the image. + * @param height The height of the image. + */ public ImageComponent(Image image, CartesianVector position, int width, int height) { this.width = width; this.height = height; this.image = image; this.position = position; - //this.setBorder(BorderFactory.createLineBorder(Color.black)); - this.setSize(PongApp.width + 20, PongApp.height + 100); + this.setSize(PongApp.width + 20, PongApp.height + 100); //IMPORTANT ! setting the size of the frame this.setVisible(true); this.setLayout(null); //repaint(); } - public Image getImage() { - return image; - } - - public CartesianVector getPosition() { - return position; - } - - public void setPosition(CartesianVector position) { - this.position = position; - } - @Override protected void paintComponent(Graphics g) { super.paintComponent(g); - g.drawImage(image, position.getX(), position.getY(), this.width, this.height, this); - //setOpaque(false); } } \ No newline at end of file diff --git a/src/main/java/engine/graphic/RectangleComponent.java b/src/main/java/engine/graphic/RectangleComponent.java index 5a823103024bcc2832fd8a2b46b716b25e729e5d..6e7ecf49fa479bb8b727ec763acb800c6b996bd9 100644 --- a/src/main/java/engine/graphic/RectangleComponent.java +++ b/src/main/java/engine/graphic/RectangleComponent.java @@ -4,6 +4,10 @@ import pong.PongApp; import javax.swing.*; +/** + * RectangleComponent class which extends JComponent and allows to draw a rectangle on the window. + * @see JComponent + */ public class RectangleComponent extends JComponent { private int width; private int height; diff --git a/src/main/java/engine/graphic/ScoreComponent.java b/src/main/java/engine/graphic/ScoreComponent.java index e1640f29ae67c7bd752dc8ffe5b52516d3066997..27b068f42e918754ac79ee68ac996004faee6d44 100644 --- a/src/main/java/engine/graphic/ScoreComponent.java +++ b/src/main/java/engine/graphic/ScoreComponent.java @@ -6,6 +6,10 @@ import pong.PongApp; import javax.swing.*; import java.awt.*; +/** + * ScoreComponent class which extends JComponent and allows to draw the score on the window. + * @see JComponent + */ public class ScoreComponent extends JComponent { private int score1; private int score2; diff --git a/src/main/java/engine/physic/CartesianVector.java b/src/main/java/engine/physic/CartesianVector.java index d2a608935b60d019f0d01c3a7e87e94f355b5bb9..2344be5d8d383eadb9fd8d5ae931d930f521f9f7 100644 --- a/src/main/java/engine/physic/CartesianVector.java +++ b/src/main/java/engine/physic/CartesianVector.java @@ -1,27 +1,37 @@ package engine.physic; +/** + * CartesianVector class which represents a vector in a Cartesian space. + */ public class CartesianVector { - private int x; - private int y; + private int x; // x coordinate + private int y; // y coordinate public CartesianVector(int x, int y){ this.x = x; this.y = y; } + /** + * Add a vector to the current vector. + * @param otherCartesianVector The vector to add. + */ public void add(CartesianVector otherCartesianVector){ this.x += otherCartesianVector.getX(); this.y += otherCartesianVector.getY(); } - public void sub(CartesianVector otherCartesianVector){ - this.x -= otherCartesianVector.getX(); - this.y -= otherCartesianVector.getY(); - } + /** + * Multiply the vector by a scalar. + * @param scalar The scalar to multiply the vector by. + */ public void mul(int scalar){ this.x *= scalar; this.y *= scalar; } + /** + * Increment positively or negatively the vector. + */ public void increment(){ if(this.x < 0){ this.x--; @@ -51,8 +61,13 @@ public class CartesianVector { this.x = x; } + /** + * Compare two vectors with their coordinates. + * @param otherCartesianVector The vector to compare with. + * @return True if the vectors are equals, false otherwise. + */ public boolean equals(CartesianVector otherCartesianVector){ - System.out.println("Comparing " + this.x + " " + this.y + " with " + otherCartesianVector.getX() + " " + otherCartesianVector.getY()); + //System.out.println("Comparing " + this.x + " " + this.y + " with " + otherCartesianVector.getX() + " " + otherCartesianVector.getY()); return this.x == otherCartesianVector.getX() && this.y == otherCartesianVector.getY(); } } \ No newline at end of file diff --git a/src/main/java/engine/physic/Entity.java b/src/main/java/engine/physic/Entity.java index d159b3097332e1e56d72483aaa2c66a39f135aaf..298cadcd2b9e7094fe19086faa31f23d9e9e2ba8 100644 --- a/src/main/java/engine/physic/Entity.java +++ b/src/main/java/engine/physic/Entity.java @@ -76,7 +76,17 @@ public interface Entity { */ public void halt(); + /** + * Return the width of the entity + * + * @return width + */ int getWidth(); + /** + * Return the height of the entity + * + * @return height + */ int getHeight(); } diff --git a/src/main/java/engine/physic/Movable.java b/src/main/java/engine/physic/Movable.java index b2a9c46630794b125a4a1c65b9f13d8da53db032..538789f6c51e9478004f60fe7e8d8f2eb2023dcb 100644 --- a/src/main/java/engine/physic/Movable.java +++ b/src/main/java/engine/physic/Movable.java @@ -2,6 +2,10 @@ package engine.physic; import pong.PongApp; +/** + * A Movable class which implements the Entity interface and allows to create a movable object. + * @see Entity + */ public class Movable implements Entity { private int weight; @@ -12,7 +16,15 @@ public class Movable implements Entity { private CartesianVector acceleration; - + /** + * Constructor of the Movable class. + * @param weight The weight of the object. + * @param position The position of the object. + * @param speed The speed of the object. + * @param acceleration The acceleration of the object. + * @param width The width of the object. + * @param height The height of the object. + */ public Movable(int weight, CartesianVector position, CartesianVector speed, CartesianVector acceleration, int width, int height) { this.weight = weight; this.speed = speed; @@ -22,41 +34,72 @@ public class Movable implements Entity { this.height = height; } + /** + * get the weight of the object + * @return weight + */ @Override public int getWeight() { return weight; } + /** + * get the speed of the object + * @return speed + */ @Override public CartesianVector getSpeed() { return speed; } + /** + * get the position of the object + * @return position + */ @Override public CartesianVector getPosition() { return position; } + /** + * get the acceleration of the object + * @return acceleration + */ @Override public CartesianVector getAcceleration() { return acceleration; } + /** + * set the speed of the object + * @param speed the new speed of the object + */ @Override public void setSpeed(CartesianVector speed) { this.speed = speed; } + /** + * set the position of the object + * @param position the new position of the object + */ @Override public void setPosition(CartesianVector position) { this.position = position; } + /** + * set the acceleration of the object + * @param acceleration the new acceleration of the object + */ @Override public void setAcceleration(CartesianVector acceleration) { } + /** + * update the position of the object + */ @Override public void updatePosition() { CartesianVector newPosition = new CartesianVector(this.position.getX(), this.position.getY()); @@ -76,38 +119,48 @@ public class Movable implements Entity { this.position.add(this.speed); // Modifying the position (x = x + vx ; y = y + vy) } + /** + * get the left resistance of the object + * @return left resistance + */ @Override public int getLeftResistance() { return 0; } + /** + * set the left resistance of the object + * @param leftLife the new left resistance of the object + */ @Override public void setResistance(int leftLife) { } + /** + * get the right resistance of the object + * @return right resistance + */ @Override public void halt() { this.speed = new CartesianVector(0, 0); // Making the speed null updatePosition(); // Updating the position with a null acceleration } - public void setDirection(Movable movable, CartesianVector direction) throws InterruptedException { - while (true) { - this.speed = direction; - this.position.add(this.speed); - if (movable.getSpeed() != direction) { - break; - } - } - - } + /** + * get the width of the object + * @return width + */ @Override public int getWidth() { return width; } + /** + * get the height of the object + * @return height + */ @Override public int getHeight() { return height; diff --git a/src/main/java/engine/physic/NotMovable.java b/src/main/java/engine/physic/NotMovable.java deleted file mode 100644 index 35d79daeb339d17e1d35416bded70b83f891bf5b..0000000000000000000000000000000000000000 --- a/src/main/java/engine/physic/NotMovable.java +++ /dev/null @@ -1,78 +0,0 @@ -package engine.physic; - -/** - * Class for all obstacles in the game. - * Obstacles are static entities. - */ -public class NotMovable implements Entity { - - private int weight; - private final CartesianVector speed = new CartesianVector(0, 0); // Our obstacle is static - private CartesianVector position; - - @Override - public int getWeight() { - return 0; - } - - @Override - public CartesianVector getSpeed() { - return null; - } - - @Override - public CartesianVector getPosition() { - return null; - } - - @Override - public CartesianVector getAcceleration() { - return null; - } - - @Override - public void setSpeed(CartesianVector speed) { - - } - - @Override - public void setPosition(CartesianVector position) { - - } - - @Override - public void setAcceleration(CartesianVector acceleration) { - - } - - @Override - public void updatePosition() { - - } - - @Override - public int getLeftResistance() { - return 0; - } - - @Override - public void setResistance(int leftLife) { - - } - - @Override - public void halt() { - - } - - @Override - public int getWidth() { - return 0; - } - - @Override - public int getHeight() { - return 0; - } - -} diff --git a/src/main/java/engine/physic/PhysicEngine.java b/src/main/java/engine/physic/PhysicEngine.java index 329d8cfb7eb8be385ca2779ec9f05e4af23efc2d..cdd4bd2e0e66cec61b035f1ec7c71e8bf471cc2b 100644 --- a/src/main/java/engine/physic/PhysicEngine.java +++ b/src/main/java/engine/physic/PhysicEngine.java @@ -3,67 +3,91 @@ package engine.physic; import engine.Engine; import engine.Kernel; import engine.sound.SoundEngine; +import pong.GameObject2D; import pong.PongObject; import java.util.HashMap; +/** + * PhysicEngine is the class that will manage the physic of the game. + * It will be used by Kernel. + */ public class PhysicEngine implements Engine { int i = 0; - public HashMap<PongObject, Entity> pongObjectEntityHashMap; // Map of the pong objects and their associated entity + public HashMap<GameObject2D, Entity> gameObject2DEntityHashMap; // Map of the pong objects and their associated entity public boolean isCollision = false; SoundEngine soundEngine; - //private HashMap<CartesianVector, PongObject> positions; // Map of the positions and the associated pong object + + /** + * Constructor of the PhysicEngine. + * + * @param soundEngine the sound engine of the game which will be used to play some sounds + */ public PhysicEngine(SoundEngine soundEngine) { this.soundEngine = soundEngine; restart(); } + + /** + * Update the physic engine. + * It will update the position of all the movable objects and check if there is a collision. + */ @Override public void update(){ //TODO: we cannot have the mention of "Ball" here so we need to find a way to do it without mentioning "Ball" - for (PongObject pongObject: Kernel.gameObjects) { - CartesianVector previousPosition = pongObjectEntityHashMap.get(pongObject).getPosition(); - pongObjectEntityHashMap.get(pongObject).updatePosition();// Updating of the associated movable position - if(pongObject.getName().equals("Ball")){ - for (PongObject po2: pongObjectEntityHashMap.keySet() + for (GameObject2D gameObject2D: Kernel.gameObjects) { + CartesianVector previousPosition = gameObject2DEntityHashMap.get(gameObject2D).getPosition(); + gameObject2DEntityHashMap.get(gameObject2D).updatePosition();// Updating of the associated movable position + if(gameObject2D.getName().equals("Ball")){ + for (GameObject2D po2: gameObject2DEntityHashMap.keySet() ) { if(!po2.getName().equals("Ball")){ - if(checkCollision(pongObjectEntityHashMap.get(pongObject), pongObjectEntityHashMap.get(po2))){ + if(checkCollision(gameObject2DEntityHashMap.get(gameObject2D), gameObject2DEntityHashMap.get(po2))){ i++; - //String collisionPath = "src/main/resources/Sound/collision.wav"; - //Sound.playMusic(collisionPath); soundEngine.playMusic("src/main/resources/Sound/hit_racket.wav"); System.out.println("Collision " + i); - pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition); + gameObject2DEntityHashMap.get(gameObject2D).setPosition(previousPosition); //TODO: add a method to Entity to increment the speed if(i % 10 == 0){ - pongObjectEntityHashMap.get(pongObject).getSpeed().increment(); + gameObject2DEntityHashMap.get(gameObject2D).getSpeed().increment(); } - pongObject.setSpeed(pongObjectEntityHashMap.get(pongObject).getSpeed()); // Updating of the pong object speed with the latest speed of the entity + gameObject2D.setSpeed(gameObject2DEntityHashMap.get(gameObject2D).getSpeed()); // Updating of the pong object speed with the latest speed of the entity //po2 a racket will process the collision with the ball - po2.processCollision(pongObject); + po2.processCollision(gameObject2D); //Updating the Entity speed with the new speed of the pong object Ball - pongObjectEntityHashMap.get(pongObject).setSpeed(pongObject.getSpeed()); + gameObject2DEntityHashMap.get(gameObject2D).setSpeed(gameObject2D.getSpeed()); - pongObjectEntityHashMap.get(pongObject).updatePosition(); + gameObject2DEntityHashMap.get(gameObject2D).updatePosition(); } } } } - pongObject.setPosition(pongObjectEntityHashMap.get(pongObject).getPosition()); // Updating of the pong object position + gameObject2D.setPosition(gameObject2DEntityHashMap.get(gameObject2D).getPosition()); // Updating of the pong object position } } + /** + * Restart the physic engine. + * It will reset the position and the speed of all the movable objects. + */ @Override public void restart() { - pongObjectEntityHashMap = new HashMap<>(); - for (PongObject pongObject: Kernel.gameObjects) { - pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), pongObject.getInitSpeed(), new CartesianVector(0, 0), pongObject.getWidth(), pongObject.getHeight())); + gameObject2DEntityHashMap = new HashMap<>(); + for (GameObject2D gameObject2D: Kernel.gameObjects) { + gameObject2DEntityHashMap.put(gameObject2D, new Movable(1, gameObject2D.getPosition(), gameObject2D.getInitSpeed(), new CartesianVector(0, 0), gameObject2D.getWidth(), gameObject2D.getHeight())); } } + /** + * Check if there is a collision between two entities. + * + * @param firstObject the first entity + * @param secondObject the second entity + * @return true if there is a collision, false otherwise + */ public boolean checkCollision(Entity firstObject, Entity secondObject){ CartesianVector pos1 = firstObject.getPosition(); CartesianVector pos2 = secondObject.getPosition(); diff --git a/src/main/java/engine/sound/SoundEngine.java b/src/main/java/engine/sound/SoundEngine.java index 259479125eb3e7519bc9557e90c9621eb9ae3274..db674717451c0cdc69ab7d091691496862aadb3c 100644 --- a/src/main/java/engine/sound/SoundEngine.java +++ b/src/main/java/engine/sound/SoundEngine.java @@ -10,6 +10,10 @@ import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.swing.*; +/** + * SoundEngine is the class that will play the sound of the game. + * It will be used by Kernel and sometimes by PhysicEngine. + */ public class SoundEngine implements Engine { private static Clip clip; // Ajout de cette variable pour stocker le clip en cours de lecture @@ -18,6 +22,11 @@ public class SoundEngine implements Engine { private boolean isMuted = false; public SoundEngine() { } + + /** + * Play a music from a location. + * @param location the path to the music + */ public void playMusic(String location){ if(isMuted) return; try { @@ -37,12 +46,9 @@ public class SoundEngine implements Engine { } } - public static void stopMusic() { - if (clip != null && clip.isRunning()) { - clip.stop(); - } - } - + /** + * Update the sound engine. + */ @Override public void update() { if(isMuted) return; @@ -54,17 +60,27 @@ public class SoundEngine implements Engine { } } + /** + * Add a sound to the sound engine. + * @param soundPath the path to the sound + * @param isPlaying if the sound will be played + */ public void addSound(String soundPath, boolean isPlaying) { System.out.println("SoundEngine addSound " + isPlaying); soundMap.put(soundPath, isPlaying); } + /** + * Mute or unmute the sound engine. + */ public void mute(){ isMuted = !isMuted; } - - + /** + * Restart the sound engine. + * TODO: implement this method + */ @Override public void restart() { diff --git a/src/main/java/pong/Game2D.java b/src/main/java/pong/Game2D.java new file mode 100644 index 0000000000000000000000000000000000000000..158c5a991276e52c78b2bc879fa78b751446be7b --- /dev/null +++ b/src/main/java/pong/Game2D.java @@ -0,0 +1,4 @@ +package pong; + +public interface Game2D { +} diff --git a/src/main/java/pong/GameObject2D.java b/src/main/java/pong/GameObject2D.java new file mode 100644 index 0000000000000000000000000000000000000000..c45651f4a51c65f2739e02aa06f5950702a982ee --- /dev/null +++ b/src/main/java/pong/GameObject2D.java @@ -0,0 +1,26 @@ +package pong; + +import engine.physic.CartesianVector; + +import java.awt.*; + +public interface GameObject2D { + public CartesianVector getPosition(); + + public CartesianVector getSpeed(); + public Image getImage(); + public int getWidth(); + public int getHeight(); + public int getResistance(); + public void setPosition(CartesianVector position); + + public void setSpeed(CartesianVector speed); + + public CartesianVector getInitSpeed(); + public String getName(); + + CartesianVector getInitPosition(); + + boolean isLeftRacket(); + void processCollision(GameObject2D po); +} diff --git a/src/main/java/pong/PongApp.java b/src/main/java/pong/PongApp.java index 3e3fa3bfcb6110545a926a7ba922148885a000ac..feabf79536f299ad4fd3eb99f2e15fcd26be5ba0 100644 --- a/src/main/java/pong/PongApp.java +++ b/src/main/java/pong/PongApp.java @@ -9,7 +9,7 @@ import javax.swing.*; import java.io.IOException; import java.util.ArrayList; -public class PongApp { +public class PongApp implements Game2D { public static int numberOfMatchs = 5; public static int score1 = 0; @@ -17,7 +17,7 @@ public class PongApp { public static boolean isGoal = false; - public static ArrayList<PongObject> components = new ArrayList<>(); + public static ArrayList<GameObject2D> components = new ArrayList<>(); public static int width = 800; public static int height = 600; @@ -46,8 +46,7 @@ public class PongApp { components.add(pongBall); Kernel kernel = new Kernel("Pong", width, height, components); - kernel.playSound("src/main/resources/Sound/minecraft.wav"); - kernel.addSound("src/main/resources/Sound/hit_racket.wav", kernel.isItACollision()); + //kernel.playSound("src/main/resources/Sound/minecraft.wav"); JButton button = new JButton("Restart"); JButton button2 = new JButton("Mute"); diff --git a/src/main/java/pong/PongBall.java b/src/main/java/pong/PongBall.java index 60d186ef66b515dcdee4dd997d233e7e93fec378..8f84f1e5cda376fed91639bf9e64026067c129fb 100644 --- a/src/main/java/pong/PongBall.java +++ b/src/main/java/pong/PongBall.java @@ -4,6 +4,10 @@ import engine.physic.CartesianVector; import java.awt.*; +/** + * Class which implements the pong ball + * @see PongObject + */ public class PongBall implements PongObject { public String name; @@ -89,10 +93,11 @@ public class PongBall implements PongObject { } @Override - public void processCollision(PongObject po2) { + public void processCollision(GameObject2D po) { } + @Override public CartesianVector getInitSpeed(){ return this.initSpeed; diff --git a/src/main/java/pong/PongObject.java b/src/main/java/pong/PongObject.java index 3011cf72032f7def7ac3c851ce9bb17a860dbcc4..60e609fcb04808fcf1eec6bb0e6a1bebcb71fbba 100644 --- a/src/main/java/pong/PongObject.java +++ b/src/main/java/pong/PongObject.java @@ -4,24 +4,9 @@ import engine.physic.CartesianVector; import java.awt.*; -public interface PongObject { - public CartesianVector getPosition(); +/** + * Interface which implements the pong objects + */ +public interface PongObject extends GameObject2D { - public CartesianVector getSpeed(); - public Image getImage(); - public int getWidth(); - public int getHeight(); - public int getResistance(); - public void setPosition(CartesianVector position); - - public void setSpeed(CartesianVector speed); - - public CartesianVector getInitSpeed(); - public String getName(); - - CartesianVector getInitPosition(); - - - boolean isLeftRacket(); - void processCollision(PongObject po); } diff --git a/src/main/java/pong/PongRacket.java b/src/main/java/pong/PongRacket.java index b5190445ac6e510159ac2aae5e64fd15fc519019..4a3484f40d828f95d2c2fa2df1ea23af453c4cd2 100644 --- a/src/main/java/pong/PongRacket.java +++ b/src/main/java/pong/PongRacket.java @@ -94,7 +94,7 @@ public class PongRacket implements PongObject { * */ @Override - public void processCollision(PongObject po2) { + public void processCollision(GameObject2D po2) { boolean isUpperHalf = po2.getPosition().getY() < this.position.getY() + this.height / 2; if(!isLeftRacket){ System.out.println("isLeftRacket"); diff --git a/src/main/resources/heroIDLE/bas-50px.png b/src/main/resources/heroIDLE/bas-50px.png deleted file mode 100644 index 46eb476dde892503ee6d92a1d039b0b795470d42..0000000000000000000000000000000000000000 Binary files a/src/main/resources/heroIDLE/bas-50px.png and /dev/null differ diff --git a/src/main/resources/heroIDLE/droite-50px.png b/src/main/resources/heroIDLE/droite-50px.png deleted file mode 100644 index 0229a64b1c31e152a56950052166ff0f9e17aa48..0000000000000000000000000000000000000000 Binary files a/src/main/resources/heroIDLE/droite-50px.png and /dev/null differ diff --git a/src/main/resources/heroIDLE/gauche-50px.png b/src/main/resources/heroIDLE/gauche-50px.png deleted file mode 100644 index 18f800ea03951a7b0bfff75b56958e813c7792e0..0000000000000000000000000000000000000000 Binary files a/src/main/resources/heroIDLE/gauche-50px.png and /dev/null differ diff --git a/src/main/resources/heroIDLE/haut-50px.png b/src/main/resources/heroIDLE/haut-50px.png deleted file mode 100644 index 3cddbc7c4a0b034271e8cd696ca51c2d645c06d7..0000000000000000000000000000000000000000 Binary files a/src/main/resources/heroIDLE/haut-50px.png and /dev/null differ