Skip to content
Snippets Groups Projects
Commit 6800db13 authored by bosskkev's avatar bosskkev
Browse files

Implémentation du boutton restart

parent ee424dc9
No related branches found
No related tags found
No related merge requests found
Pipeline #24498 failed
...@@ -22,6 +22,8 @@ public class Kernel { ...@@ -22,6 +22,8 @@ public class Kernel {
SoundEngine soundEngine =new SoundEngine(); SoundEngine soundEngine =new SoundEngine();
public static boolean isCollision = false; public static boolean isCollision = false;
public int isFirstUpdate = 0;
//public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>(); //public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>();
...@@ -57,13 +59,16 @@ public class Kernel { ...@@ -57,13 +59,16 @@ public class Kernel {
*/ */
public void start() throws IOException { public void start() throws IOException {
graphicEngine.start(); graphicEngine.start();
} }
public void restart() throws IOException { public void restart() throws IOException {
for (Engine engine: engines for (Engine engine: engines
) { ) {
engine.restart(); engine.restart();
} }
isFirstUpdate = 0;
//pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; //pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap;
} }
...@@ -72,11 +77,20 @@ public class Kernel { ...@@ -72,11 +77,20 @@ public class Kernel {
* One step of the game. Update all objects positions and repaint the grid. * One step of the game. Update all objects positions and repaint the grid.
*/ */
public void update() { public void update() {
//System.out.println(gameObjects.get(0).getInitPosition().getX() + " " + gameObjects.get(2).getInitPosition().getY());
if(isFirstUpdate == 1){
try {
Thread.sleep(1000);
//isFirstUpdate = false;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
for (Engine engine: engines for (Engine engine: engines
) { ) {
engine.update(); engine.update();
} }
isFirstUpdate++;
} }
......
...@@ -10,20 +10,11 @@ import java.util.Random; ...@@ -10,20 +10,11 @@ import java.util.Random;
public class PhysicEngine implements Engine { public class PhysicEngine implements Engine {
int i = 0; int i = 0;
public HashMap<PongObject, Entity> pongObjectEntityHashMap = new HashMap<>(); public HashMap<PongObject, Entity> pongObjectEntityHashMap; // Map of the pong objects and their associated entity
private HashMap<Coordinates2D, PongObject> positions = new HashMap<>(); //private HashMap<Coordinates2D, PongObject> positions; // Map of the positions and the associated pong object
public PhysicEngine(){ public PhysicEngine(){
for (PongObject pongObject: Kernel.gameObjects) { restart();
Coordinates2D speed = new Coordinates2D(0, 0);
if(pongObject.getName().equals("Ball")){
speed = new Coordinates2D(2, 2);
}
//Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1);
pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), speed, new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight()));
positions.put(pongObject.getPosition(), pongObject);
}
} }
@Override @Override
public void update(){ public void update(){
...@@ -31,6 +22,8 @@ public class PhysicEngine implements Engine { ...@@ -31,6 +22,8 @@ public class PhysicEngine implements Engine {
Coordinates2D previousPosition = pongObjectEntityHashMap.get(pongObject).getPosition(); Coordinates2D previousPosition = pongObjectEntityHashMap.get(pongObject).getPosition();
pongObjectEntityHashMap.get(pongObject).updatePosition();// Updating of the associated movable position pongObjectEntityHashMap.get(pongObject).updatePosition();// Updating of the associated movable position
if(pongObject.getName().equals("Ball")){ if(pongObject.getName().equals("Ball")){
System.out.println("Ball position: " + pongObjectEntityHashMap.get(pongObject).getPosition().getX() + " ; " + pongObjectEntityHashMap.get(pongObject).getPosition().getY());
//System.out.println("Ball init position: " + pongObjectEntityHashMap.get(pongObject).getInitPosition());
for (PongObject po2: pongObjectEntityHashMap.keySet() for (PongObject po2: pongObjectEntityHashMap.keySet()
) { ) {
if(!po2.getName().equals("Ball")){ if(!po2.getName().equals("Ball")){
...@@ -60,15 +53,16 @@ public class PhysicEngine implements Engine { ...@@ -60,15 +53,16 @@ public class PhysicEngine implements Engine {
@Override @Override
public void restart() { public void restart() {
//pongObjectEntityHashMap = new HashMap<>(); pongObjectEntityHashMap = new HashMap<>();
//positions = new HashMap<>();
for (PongObject pongObject: Kernel.gameObjects) { for (PongObject pongObject: Kernel.gameObjects) {
Coordinates2D speed = new Coordinates2D(0, 0); Coordinates2D speed = new Coordinates2D(0, 0);
if(pongObject.getName().equals("Ball")){ if(pongObject.getName().equals("Ball")){
//pongObject.setPosition(pongObject.getInitPosition());
speed = new Coordinates2D(2, 2); speed = new Coordinates2D(2, 2);
} }
//Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1); //Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1);
pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), speed, new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight())); pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), speed, new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight()));
positions.put(pongObject.getPosition(), pongObject);
} }
} }
......
...@@ -51,6 +51,8 @@ public class PongApp { ...@@ -51,6 +51,8 @@ public class PongApp {
score2 = 0; score2 = 0;
System.out.println("Restarting"); System.out.println("Restarting");
try { try {
restart();
//kernel.setGameObject(components);
kernel.restart(); kernel.restart();
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
...@@ -69,7 +71,7 @@ public class PongApp { ...@@ -69,7 +71,7 @@ public class PongApp {
if(isGoal){ if(isGoal){
System.out.println(score1 + " : " + score2); System.out.println(score1 + " : " + score2);
restart(); restart();
kernel.setGameObject(components); //kernel.setGameObject(components);
kernel.restart(); kernel.restart();
} }
......
...@@ -10,7 +10,7 @@ public class PongBall implements PongObject { ...@@ -10,7 +10,7 @@ public class PongBall implements PongObject {
private final int width; private final int width;
private final int height; private final int height;
private Coordinates2D position; private Coordinates2D position;
private final Coordinates2D initPosition; private Coordinates2D initPosition;
private final Image image; private final Image image;
PongBall(String name, String imagePath, Coordinates2D position, int width, int height){ PongBall(String name, String imagePath, Coordinates2D position, int width, int height){
...@@ -60,6 +60,6 @@ public class PongBall implements PongObject { ...@@ -60,6 +60,6 @@ public class PongBall implements PongObject {
@Override @Override
public Coordinates2D getInitPosition() { public Coordinates2D getInitPosition() {
return initPosition; return this.initPosition;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment