Skip to content
Snippets Groups Projects
Commit 9d486682 authored by bosskkev's avatar bosskkev
Browse files

Rajout de l'interface Game2D

parent 787760b5
Branches
No related tags found
No related merge requests found
Showing
with 306 additions and 217 deletions
...@@ -46,7 +46,7 @@ test { ...@@ -46,7 +46,7 @@ test {
} }
application { application {
mainClass = 'sprint2_demo.Grid' mainClass = 'pong.PongApp'
} }
spotbugs { spotbugs {
......
package engine; 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 { public interface Engine {
/**
* Method that update the state of an engine
*/
void update(); void update();
/**
* Method that restart the state of an engine
*/
void restart(); void restart();
} }
...@@ -5,7 +5,7 @@ import engine.physic.CartesianVector; ...@@ -5,7 +5,7 @@ import engine.physic.CartesianVector;
import engine.physic.Entity; import engine.physic.Entity;
import engine.sound.SoundEngine; import engine.sound.SoundEngine;
import engine.physic.PhysicEngine; import engine.physic.PhysicEngine;
import pong.PongObject; import pong.GameObject2D;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
...@@ -22,10 +22,10 @@ public class Kernel { ...@@ -22,10 +22,10 @@ public class Kernel {
public int isFirstUpdate = 0; public int isFirstUpdate = 0;
public static ArrayList<PongObject> gameObjects; public static ArrayList<GameObject2D> gameObjects;
public HashMap<PongObject, Entity> pongObjectEntityHashMap; 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); this.setGameObject(gameObjects);
System.out.println("Model created"); System.out.println("Model created");
...@@ -41,7 +41,7 @@ public class Kernel { ...@@ -41,7 +41,7 @@ public class Kernel {
engines.add(this.graphicEngine);// engines.add(this.graphicEngine);//
this.physicEngine = new PhysicEngine(soundEngine); this.physicEngine = new PhysicEngine(soundEngine);
engines.add(physicEngine); engines.add(physicEngine);
pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; gameObject2DEntityHashMap = physicEngine.gameObject2DEntityHashMap;
this.update(); this.update();
this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane()); this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane());
...@@ -50,7 +50,7 @@ public class Kernel { ...@@ -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 * @throws IOException
*/ */
...@@ -66,7 +66,7 @@ public class Kernel { ...@@ -66,7 +66,7 @@ public class Kernel {
engine.restart(); engine.restart();
} }
isFirstUpdate = 0; isFirstUpdate = 0;
//pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; //GameObject2DEntityHashMap = physicEngine.GameObject2DEntityHashMap;
} }
...@@ -102,28 +102,28 @@ public class Kernel { ...@@ -102,28 +102,28 @@ public class Kernel {
Action upAction = new AbstractAction() { Action upAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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() { Action downAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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() { Action zAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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() { Action sAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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 { ...@@ -143,28 +143,28 @@ public class Kernel {
Action upReleaseAction = new AbstractAction() { Action upReleaseAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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() { Action downReleaseAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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() { Action zReleaseAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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() { Action sReleaseAction = new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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 { ...@@ -183,23 +183,19 @@ public class Kernel {
mainPanel.setFocusable(true); mainPanel.setFocusable(true);
} }
public void setGameObject(ArrayList<PongObject> gameObject) { public void setGameObject(ArrayList<GameObject2D> gameObject) {
gameObjects = gameObject; gameObjects = gameObject;
} }
/**
* Add a new button to the game.
* @param button the JButton to add
*/
public void addButton(JButton button){ public void addButton(JButton button){
button.setSize(50, 30); button.setSize(50, 30);
graphicEngine.addButton(button); 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 * Play a sound
...@@ -208,10 +204,6 @@ public class Kernel { ...@@ -208,10 +204,6 @@ public class Kernel {
soundEngine.playMusic(soundPath); soundEngine.playMusic(soundPath);
} }
public boolean isItACollision(){
return physicEngine.isCollision;
}
public void muteSoundEngine(){ public void muteSoundEngine(){
soundEngine.mute(); soundEngine.mute();
} }
......
...@@ -3,6 +3,7 @@ package engine.graphic; ...@@ -3,6 +3,7 @@ package engine.graphic;
import engine.Engine; import engine.Engine;
import engine.Kernel; import engine.Kernel;
import engine.physic.CartesianVector; import engine.physic.CartesianVector;
import pong.GameObject2D;
import pong.PongApp; import pong.PongApp;
import pong.PongObject; import pong.PongObject;
...@@ -10,33 +11,37 @@ import javax.swing.*; ...@@ -10,33 +11,37 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.IOException; 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 { public class GraphicEngine extends JFrame implements Engine {
JPanel controlPanel = new JPanel(); JPanel controlPanel = new JPanel(); // Panel which contains the control buttons
JPanel gamePanel = new JPanel();
public GraphicEngine() throws IOException { 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.setTitle(windowTitle);
// this.setContentPane(mainPanel);
this.setMinimumSize(new Dimension(width, height)); this.setMinimumSize(new Dimension(width, height));
// this.setContentPane(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlPanel.setPreferredSize(new Dimension(width, 30)); controlPanel.setPreferredSize(new Dimension(width, 30));
controlPanel.setBackground(Color.BLACK); controlPanel.setBackground(Color.BLACK);
// Ajoutez controlPanel au nord et gamePanel au centre du mainPanel
this.getContentPane().add(controlPanel, BorderLayout.NORTH); this.getContentPane().add(controlPanel, BorderLayout.NORTH);
this.getContentPane().setBackground(Color.BLACK); 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.setLocationRelativeTo(null);
this.pack(); this.pack();
...@@ -44,18 +49,28 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -44,18 +49,28 @@ public class GraphicEngine extends JFrame implements Engine {
this.setVisible(true); this.setVisible(true);
this.setLayout(this.getLayout()); 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 { public void draw(ImageComponent ic) throws IOException {
this.getContentPane().add(ic); this.getContentPane().add(ic);
repaint(); repaint();
revalidate(); revalidate();
} }
/**
* Method which starts the graphic engine.
* Associates the ImageComponents to the PongObjects and draws them on the window.
*/
public void start(){ public void start(){
for (GameObject2D go : Kernel.gameObjects
for (PongObject po : Kernel.gameObjects
) { ) {
try { 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) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -67,13 +82,19 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -67,13 +82,19 @@ public class GraphicEngine extends JFrame implements Engine {
} }
/**
* Method which updates the graphic engine.
* @see Engine
*/
@Override @Override
public void update(){ public void update(){
//System.out.println(gamePanel.isVisible());
repaint(); repaint();
revalidate(); revalidate();
} }
/**
* Method which restarts the graphic engine.
*/
@Override @Override
public void restart() { public void restart() {
for(Component c : this.getContentPane().getComponents()){ for(Component c : this.getContentPane().getComponents()){
...@@ -82,9 +103,11 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -82,9 +103,11 @@ public class GraphicEngine extends JFrame implements Engine {
} }
} }
start(); 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(){ public void drawRect(){
this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight)); this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight));
repaint(); repaint();
...@@ -95,10 +118,10 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -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)); 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){ public void addButton(JButton button){
//TODO: add button action
controlPanel.add(button); controlPanel.add(button);
repaint(); repaint();
revalidate(); revalidate();
......
...@@ -6,43 +6,38 @@ import pong.PongApp; ...@@ -6,43 +6,38 @@ import pong.PongApp;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
/**
* ImageComponent class which extends JComponent and allows to draw an image on the window.
* @see JComponent
*/
public class ImageComponent extends JComponent { public class ImageComponent extends JComponent {
private Image image; private Image image;
private CartesianVector position; private CartesianVector position;
private int width; private int width;
private int height; 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) { public ImageComponent(Image image, CartesianVector position, int width, int height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.image = image; this.image = image;
this.position = position; this.position = position;
//this.setBorder(BorderFactory.createLineBorder(Color.black)); this.setSize(PongApp.width + 20, PongApp.height + 100); //IMPORTANT ! setting the size of the frame
this.setSize(PongApp.width + 20, PongApp.height + 100);
this.setVisible(true); this.setVisible(true);
this.setLayout(null); this.setLayout(null);
//repaint(); //repaint();
} }
public Image getImage() {
return image;
}
public CartesianVector getPosition() {
return position;
}
public void setPosition(CartesianVector position) {
this.position = position;
}
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
g.drawImage(image, position.getX(), position.getY(), this.width, this.height, this); g.drawImage(image, position.getX(), position.getY(), this.width, this.height, this);
//setOpaque(false);
} }
} }
\ No newline at end of file
...@@ -4,6 +4,10 @@ import pong.PongApp; ...@@ -4,6 +4,10 @@ import pong.PongApp;
import javax.swing.*; import javax.swing.*;
/**
* RectangleComponent class which extends JComponent and allows to draw a rectangle on the window.
* @see JComponent
*/
public class RectangleComponent extends JComponent { public class RectangleComponent extends JComponent {
private int width; private int width;
private int height; private int height;
......
...@@ -6,6 +6,10 @@ import pong.PongApp; ...@@ -6,6 +6,10 @@ import pong.PongApp;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
/**
* ScoreComponent class which extends JComponent and allows to draw the score on the window.
* @see JComponent
*/
public class ScoreComponent extends JComponent { public class ScoreComponent extends JComponent {
private int score1; private int score1;
private int score2; private int score2;
......
package engine.physic; package engine.physic;
/**
* CartesianVector class which represents a vector in a Cartesian space.
*/
public class CartesianVector { public class CartesianVector {
private int x; private int x; // x coordinate
private int y; private int y; // y coordinate
public CartesianVector(int x, int y){ public CartesianVector(int x, int y){
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
/**
* Add a vector to the current vector.
* @param otherCartesianVector The vector to add.
*/
public void add(CartesianVector otherCartesianVector){ public void add(CartesianVector otherCartesianVector){
this.x += otherCartesianVector.getX(); this.x += otherCartesianVector.getX();
this.y += otherCartesianVector.getY(); 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){ public void mul(int scalar){
this.x *= scalar; this.x *= scalar;
this.y *= scalar; this.y *= scalar;
} }
/**
* Increment positively or negatively the vector.
*/
public void increment(){ public void increment(){
if(this.x < 0){ if(this.x < 0){
this.x--; this.x--;
...@@ -51,8 +61,13 @@ public class CartesianVector { ...@@ -51,8 +61,13 @@ public class CartesianVector {
this.x = x; 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){ 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(); return this.x == otherCartesianVector.getX() && this.y == otherCartesianVector.getY();
} }
} }
\ No newline at end of file
...@@ -76,7 +76,17 @@ public interface Entity { ...@@ -76,7 +76,17 @@ public interface Entity {
*/ */
public void halt(); public void halt();
/**
* Return the width of the entity
*
* @return width
*/
int getWidth(); int getWidth();
/**
* Return the height of the entity
*
* @return height
*/
int getHeight(); int getHeight();
} }
...@@ -2,6 +2,10 @@ package engine.physic; ...@@ -2,6 +2,10 @@ package engine.physic;
import pong.PongApp; 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 { public class Movable implements Entity {
private int weight; private int weight;
...@@ -12,7 +16,15 @@ public class Movable implements Entity { ...@@ -12,7 +16,15 @@ public class Movable implements Entity {
private CartesianVector acceleration; 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) { public Movable(int weight, CartesianVector position, CartesianVector speed, CartesianVector acceleration, int width, int height) {
this.weight = weight; this.weight = weight;
this.speed = speed; this.speed = speed;
...@@ -22,41 +34,72 @@ public class Movable implements Entity { ...@@ -22,41 +34,72 @@ public class Movable implements Entity {
this.height = height; this.height = height;
} }
/**
* get the weight of the object
* @return weight
*/
@Override @Override
public int getWeight() { public int getWeight() {
return weight; return weight;
} }
/**
* get the speed of the object
* @return speed
*/
@Override @Override
public CartesianVector getSpeed() { public CartesianVector getSpeed() {
return speed; return speed;
} }
/**
* get the position of the object
* @return position
*/
@Override @Override
public CartesianVector getPosition() { public CartesianVector getPosition() {
return position; return position;
} }
/**
* get the acceleration of the object
* @return acceleration
*/
@Override @Override
public CartesianVector getAcceleration() { public CartesianVector getAcceleration() {
return acceleration; return acceleration;
} }
/**
* set the speed of the object
* @param speed the new speed of the object
*/
@Override @Override
public void setSpeed(CartesianVector speed) { public void setSpeed(CartesianVector speed) {
this.speed = speed; this.speed = speed;
} }
/**
* set the position of the object
* @param position the new position of the object
*/
@Override @Override
public void setPosition(CartesianVector position) { public void setPosition(CartesianVector position) {
this.position = position; this.position = position;
} }
/**
* set the acceleration of the object
* @param acceleration the new acceleration of the object
*/
@Override @Override
public void setAcceleration(CartesianVector acceleration) { public void setAcceleration(CartesianVector acceleration) {
} }
/**
* update the position of the object
*/
@Override @Override
public void updatePosition() { public void updatePosition() {
CartesianVector newPosition = new CartesianVector(this.position.getX(), this.position.getY()); CartesianVector newPosition = new CartesianVector(this.position.getX(), this.position.getY());
...@@ -76,38 +119,48 @@ public class Movable implements Entity { ...@@ -76,38 +119,48 @@ public class Movable implements Entity {
this.position.add(this.speed); // Modifying the position (x = x + vx ; y = y + vy) 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 @Override
public int getLeftResistance() { public int getLeftResistance() {
return 0; return 0;
} }
/**
* set the left resistance of the object
* @param leftLife the new left resistance of the object
*/
@Override @Override
public void setResistance(int leftLife) { public void setResistance(int leftLife) {
} }
/**
* get the right resistance of the object
* @return right resistance
*/
@Override @Override
public void halt() { public void halt() {
this.speed = new CartesianVector(0, 0); // Making the speed null this.speed = new CartesianVector(0, 0); // Making the speed null
updatePosition(); // Updating the position with a null acceleration 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 @Override
public int getWidth() { public int getWidth() {
return width; return width;
} }
/**
* get the height of the object
* @return height
*/
@Override @Override
public int getHeight() { public int getHeight() {
return height; return height;
......
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;
}
}
...@@ -3,67 +3,91 @@ package engine.physic; ...@@ -3,67 +3,91 @@ package engine.physic;
import engine.Engine; import engine.Engine;
import engine.Kernel; import engine.Kernel;
import engine.sound.SoundEngine; import engine.sound.SoundEngine;
import pong.GameObject2D;
import pong.PongObject; import pong.PongObject;
import java.util.HashMap; 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 { public class PhysicEngine implements Engine {
int i = 0; 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; public boolean isCollision = false;
SoundEngine soundEngine; 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) { public PhysicEngine(SoundEngine soundEngine) {
this.soundEngine = soundEngine; this.soundEngine = soundEngine;
restart(); restart();
} }
/**
* Update the physic engine.
* It will update the position of all the movable objects and check if there is a collision.
*/
@Override @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" 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) { for (GameObject2D gameObject2D: Kernel.gameObjects) {
CartesianVector previousPosition = pongObjectEntityHashMap.get(pongObject).getPosition(); CartesianVector previousPosition = gameObject2DEntityHashMap.get(gameObject2D).getPosition();
pongObjectEntityHashMap.get(pongObject).updatePosition();// Updating of the associated movable position gameObject2DEntityHashMap.get(gameObject2D).updatePosition();// Updating of the associated movable position
if(pongObject.getName().equals("Ball")){ if(gameObject2D.getName().equals("Ball")){
for (PongObject po2: pongObjectEntityHashMap.keySet() for (GameObject2D po2: gameObject2DEntityHashMap.keySet()
) { ) {
if(!po2.getName().equals("Ball")){ if(!po2.getName().equals("Ball")){
if(checkCollision(pongObjectEntityHashMap.get(pongObject), pongObjectEntityHashMap.get(po2))){ if(checkCollision(gameObject2DEntityHashMap.get(gameObject2D), gameObject2DEntityHashMap.get(po2))){
i++; i++;
//String collisionPath = "src/main/resources/Sound/collision.wav";
//Sound.playMusic(collisionPath);
soundEngine.playMusic("src/main/resources/Sound/hit_racket.wav"); soundEngine.playMusic("src/main/resources/Sound/hit_racket.wav");
System.out.println("Collision " + i); 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 //TODO: add a method to Entity to increment the speed
if(i % 10 == 0){ 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 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 //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 @Override
public void restart() { public void restart() {
pongObjectEntityHashMap = new HashMap<>(); gameObject2DEntityHashMap = new HashMap<>();
for (PongObject pongObject: Kernel.gameObjects) { for (GameObject2D gameObject2D: Kernel.gameObjects) {
pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), pongObject.getInitSpeed(), new CartesianVector(0, 0), pongObject.getWidth(), pongObject.getHeight())); 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){ public boolean checkCollision(Entity firstObject, Entity secondObject){
CartesianVector pos1 = firstObject.getPosition(); CartesianVector pos1 = firstObject.getPosition();
CartesianVector pos2 = secondObject.getPosition(); CartesianVector pos2 = secondObject.getPosition();
......
...@@ -10,6 +10,10 @@ import javax.sound.sampled.AudioSystem; ...@@ -10,6 +10,10 @@ import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
import javax.swing.*; 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 { public class SoundEngine implements Engine {
private static Clip clip; // Ajout de cette variable pour stocker le clip en cours de lecture 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 { ...@@ -18,6 +22,11 @@ public class SoundEngine implements Engine {
private boolean isMuted = false; private boolean isMuted = false;
public SoundEngine() { public SoundEngine() {
} }
/**
* Play a music from a location.
* @param location the path to the music
*/
public void playMusic(String location){ public void playMusic(String location){
if(isMuted) return; if(isMuted) return;
try { try {
...@@ -37,12 +46,9 @@ public class SoundEngine implements Engine { ...@@ -37,12 +46,9 @@ public class SoundEngine implements Engine {
} }
} }
public static void stopMusic() { /**
if (clip != null && clip.isRunning()) { * Update the sound engine.
clip.stop(); */
}
}
@Override @Override
public void update() { public void update() {
if(isMuted) return; if(isMuted) return;
...@@ -54,17 +60,27 @@ public class SoundEngine implements Engine { ...@@ -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) { public void addSound(String soundPath, boolean isPlaying) {
System.out.println("SoundEngine addSound " + isPlaying); System.out.println("SoundEngine addSound " + isPlaying);
soundMap.put(soundPath, isPlaying); soundMap.put(soundPath, isPlaying);
} }
/**
* Mute or unmute the sound engine.
*/
public void mute(){ public void mute(){
isMuted = !isMuted; isMuted = !isMuted;
} }
/**
* Restart the sound engine.
* TODO: implement this method
*/
@Override @Override
public void restart() { public void restart() {
......
package pong;
public interface Game2D {
}
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);
}
...@@ -9,7 +9,7 @@ import javax.swing.*; ...@@ -9,7 +9,7 @@ import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
public class PongApp { public class PongApp implements Game2D {
public static int numberOfMatchs = 5; public static int numberOfMatchs = 5;
public static int score1 = 0; public static int score1 = 0;
...@@ -17,7 +17,7 @@ public class PongApp { ...@@ -17,7 +17,7 @@ public class PongApp {
public static boolean isGoal = false; 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 width = 800;
public static int height = 600; public static int height = 600;
...@@ -46,8 +46,7 @@ public class PongApp { ...@@ -46,8 +46,7 @@ public class PongApp {
components.add(pongBall); components.add(pongBall);
Kernel kernel = new Kernel("Pong", width, height, components); Kernel kernel = new Kernel("Pong", width, height, components);
kernel.playSound("src/main/resources/Sound/minecraft.wav"); //kernel.playSound("src/main/resources/Sound/minecraft.wav");
kernel.addSound("src/main/resources/Sound/hit_racket.wav", kernel.isItACollision());
JButton button = new JButton("Restart"); JButton button = new JButton("Restart");
JButton button2 = new JButton("Mute"); JButton button2 = new JButton("Mute");
......
...@@ -4,6 +4,10 @@ import engine.physic.CartesianVector; ...@@ -4,6 +4,10 @@ import engine.physic.CartesianVector;
import java.awt.*; import java.awt.*;
/**
* Class which implements the pong ball
* @see PongObject
*/
public class PongBall implements PongObject { public class PongBall implements PongObject {
public String name; public String name;
...@@ -89,10 +93,11 @@ public class PongBall implements PongObject { ...@@ -89,10 +93,11 @@ public class PongBall implements PongObject {
} }
@Override @Override
public void processCollision(PongObject po2) { public void processCollision(GameObject2D po) {
} }
@Override @Override
public CartesianVector getInitSpeed(){ public CartesianVector getInitSpeed(){
return this.initSpeed; return this.initSpeed;
......
...@@ -4,24 +4,9 @@ import engine.physic.CartesianVector; ...@@ -4,24 +4,9 @@ import engine.physic.CartesianVector;
import java.awt.*; 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);
} }
...@@ -94,7 +94,7 @@ public class PongRacket implements PongObject { ...@@ -94,7 +94,7 @@ public class PongRacket implements PongObject {
* *
*/ */
@Override @Override
public void processCollision(PongObject po2) { public void processCollision(GameObject2D po2) {
boolean isUpperHalf = po2.getPosition().getY() < this.position.getY() + this.height / 2; boolean isUpperHalf = po2.getPosition().getY() < this.position.getY() + this.height / 2;
if(!isLeftRacket){ if(!isLeftRacket){
System.out.println("isLeftRacket"); System.out.println("isLeftRacket");
......
src/main/resources/heroIDLE/bas-50px.png

2.63 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment