Skip to content
Snippets Groups Projects
Commit 4f3658a3 authored by bosskkev's avatar bosskkev
Browse files

Modification d'Entity

parent ee6f9170
Branches
No related tags found
No related merge requests found
Pipeline #23010 failed
...@@ -16,29 +16,23 @@ import java.util.Random; ...@@ -16,29 +16,23 @@ import java.util.Random;
public class Kernel { public class Kernel {
ArrayList<Engine> engines = new ArrayList<Engine>(); ArrayList<Engine> engines = new ArrayList<>();
GraphicEngine graphicEngine;
//public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>();
public static ArrayList<PongObject> gameObjects; public static ArrayList<PongObject> gameObjects;
public HashMap<PongObject, Entity> pongObjectEntityHashMap = new HashMap<>();
Movable movable; Movable movable;
public Kernel(String gameName, int width, int height, ArrayList<PongObject> gameObjects) throws IOException { public Kernel(String gameName, int width, int height, ArrayList<PongObject> gameObjects) throws IOException {
this.setGameObject(gameObjects); this.setGameObject(gameObjects);
System.out.println("Model created"); System.out.println("Model created");
this.graphicEngine = new GraphicEngine(); // Will create a new grid and a new kernel GraphicEngine graphicEngine = new GraphicEngine();
graphicEngine.init(gameName, width, height); // Will start the kernel and set the keys listeners graphicEngine.init(gameName, width, height);
engines.add(this.graphicEngine);// engines.add(graphicEngine); //Adding graphic engine
engines.add(new PhysicEngine()); //Adding physic engine
engines.add(new PhysicEngine());
this.start(); this.start();
this.setKeysListeners((JPanel) this.graphicEngine.getContentPane()); this.setKeysListeners((JPanel) graphicEngine.getContentPane());
} }
/** /**
...@@ -47,11 +41,10 @@ public class Kernel { ...@@ -47,11 +41,10 @@ public class Kernel {
* @throws IOException * @throws IOException
*/ */
public void start() throws IOException { public void start() throws IOException {
Random random = new Random(); // For random speed for (Engine engine: engines
) {
engine.update();
graphicEngine.update(); }
} }
...@@ -60,15 +53,10 @@ public class Kernel { ...@@ -60,15 +53,10 @@ public class Kernel {
*/ */
public void update() { public void update() {
System.out.println("One step"); System.out.println("One step");
// Updating all objects positions
for (Engine engine: engines for (Engine engine: engines
) { ) {
engine.update(); engine.update();
} }
// Repainting the grid
graphicEngine.repaint();
graphicEngine.revalidate();
} }
/** /**
...@@ -78,7 +66,6 @@ public class Kernel { ...@@ -78,7 +66,6 @@ public class Kernel {
mainPanel.addKeyListener(new java.awt.event.KeyAdapter() { mainPanel.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) { public void keyPressed(java.awt.event.KeyEvent evt) {
switch (evt.getKeyCode()) { switch (evt.getKeyCode()) {
// J'ai essayé mais je me perds pour gerer les debordements
case java.awt.event.KeyEvent.VK_UP -> { case java.awt.event.KeyEvent.VK_UP -> {
movable.setSpeed(new Coordinates2D(0, -1)); movable.setSpeed(new Coordinates2D(0, -1));
} }
......
...@@ -40,8 +40,9 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -40,8 +40,9 @@ public class GraphicEngine extends JFrame implements Engine {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
repaint();
revalidate();
} }
......
...@@ -78,4 +78,7 @@ public interface Entity { ...@@ -78,4 +78,7 @@ public interface Entity {
*/ */
public void halt(); public void halt();
int getWidth();
int getHeight();
} }
...@@ -97,4 +97,14 @@ public class Movable implements Entity { ...@@ -97,4 +97,14 @@ public class Movable implements Entity {
} }
} }
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
} }
...@@ -68,4 +68,14 @@ public class NotMovable implements Entity { ...@@ -68,4 +68,14 @@ public class NotMovable implements Entity {
} }
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
} }
...@@ -9,6 +9,8 @@ import java.util.HashMap; ...@@ -9,6 +9,8 @@ import java.util.HashMap;
public class PhysicEngine implements Engine { public class PhysicEngine implements Engine {
public HashMap<PongObject, Entity> pongObjectEntityHashMap = new HashMap<>(); public HashMap<PongObject, Entity> pongObjectEntityHashMap = new HashMap<>();
private HashMap<Coordinates2D, PongObject> positions = new HashMap<>();
public PhysicEngine(){ public PhysicEngine(){
for (PongObject pongObject: Kernel.gameObjects) { for (PongObject pongObject: Kernel.gameObjects) {
Coordinates2D speed = new Coordinates2D(0, 0); Coordinates2D speed = new Coordinates2D(0, 0);
...@@ -17,7 +19,7 @@ public class PhysicEngine implements Engine { ...@@ -17,7 +19,7 @@ public class PhysicEngine implements Engine {
} }
//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);
} }
} }
...@@ -25,7 +27,13 @@ public class PhysicEngine implements Engine { ...@@ -25,7 +27,13 @@ public class PhysicEngine implements Engine {
public void update(){ public void update(){
for (PongObject pongObject: Kernel.gameObjects) { for (PongObject pongObject: Kernel.gameObjects) {
pongObjectEntityHashMap.get(pongObject).updatePosition(); // Updating of the associated movable position pongObjectEntityHashMap.get(pongObject).updatePosition(); // Updating of the associated movable position
//checkCollision();
pongObject.setPosition(pongObjectEntityHashMap.get(pongObject).getPosition()); // Updating of the pong object position pongObject.setPosition(pongObjectEntityHashMap.get(pongObject).getPosition()); // Updating of the pong object position
} }
} }
public void checkCollision(Entity firstObject, Entity secondObject){
//TODO: check collision between two objects
}
} }
...@@ -30,10 +30,11 @@ public class PongApp { ...@@ -30,10 +30,11 @@ public class PongApp {
Kernel kernel = new Kernel("Pong", width, height, components); Kernel kernel = new Kernel("Pong", width, height, components);
//kernel.setGameObject(components); //kernel.setGameObject(components);
while(true){ while(true){
try { try {
kernel.update(); kernel.update();
Thread.sleep(10); Thread.sleep(1);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment