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

Modification d'Entity

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