Skip to content
Snippets Groups Projects
Commit 0b279ab1 authored by bosskkev's avatar bosskkev
Browse files

Rajout de l'interface Engine.java

parent b4e6977f
No related branches found
No related tags found
No related merge requests found
Pipeline #22935 failed
package engine;
public interface Engine {
void update();
}
package engine; package engine;
import pong.PongObject; import pong.PongObject;
import sprint2_demo.Hero;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
public class Grid extends JFrame { public class Grid extends JFrame implements Engine {
public Kernel kernel;
public Grid() throws IOException { public Grid() throws IOException {
this.kernel = new Kernel(this);
//components.add()
} }
public void init(String title, int width, int height) throws IOException { public void init(String title, int width, int height) throws IOException {
...@@ -30,14 +24,24 @@ public class Grid extends JFrame { ...@@ -30,14 +24,24 @@ public class Grid extends JFrame {
this.setResizable(false); this.setResizable(false);
this.setVisible(true); this.setVisible(true);
this.setLayout(this.getLayout()); this.setLayout(this.getLayout());
kernel.start();
kernel.setKeysListeners((JPanel) getContentPane());
this.revalidate(); this.revalidate();
} }
public void draw(ImageComponent ic) throws IOException { public void draw(ImageComponent ic) throws IOException {
getContentPane().add(ic); getContentPane().add(ic);
} }
@Override
public void update(){
for (PongObject po : Kernel.gameObjects
) {
try {
this.draw(new ImageComponent(po.getImage(), po.getPosition(), po.getWidth(), po.getHeight()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} }
...@@ -5,20 +5,33 @@ import pong.PongObject; ...@@ -5,20 +5,33 @@ import pong.PongObject;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.Random;
public class Kernel { public class Kernel {
ArrayList<Engine> engines = new ArrayList<Engine>();
engine.Grid grid; engine.Grid grid;
//public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>(); //public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>();
public HashMap<PongObject, Movable> pongObjectEntityHashMap = new HashMap<>();
protected static ArrayList<PongObject> gameObjects;
protected HashMap<PongObject, Entity> pongObjectEntityHashMap = new HashMap<>();
Movable movable; Movable movable;
public Kernel(Grid grid) { public Kernel(String gameName, int width, int height, ArrayList<PongObject> gameObjects) throws IOException {
this.setGameObject(gameObjects);
System.out.println("Model created"); System.out.println("Model created");
this.grid = grid;
this.grid = new Grid(); // Will create a new grid and a new kernel
grid.init(gameName, width, height); // Will start the kernel and set the keys listeners
engines.add(this.grid);
this.start();
this.setKeysListeners((JPanel) this.grid.getContentPane());
} }
/** /**
...@@ -30,11 +43,16 @@ public class Kernel { ...@@ -30,11 +43,16 @@ public class Kernel {
Random random = new Random(); // For random speed Random random = new Random(); // For random speed
for (PongObject pongObject: PongApp.components) { for (PongObject pongObject: PongApp.components) {
Coordinates2D speed = new Coordinates2D(0, 0);
if(pongObject.getName().equals("Ball")){
speed = new Coordinates2D(1, 1);
}
//Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1); //Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1);
Coordinates2D speed = new Coordinates2D(0, 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()));
grid.draw(new ImageComponent(pongObject.getImage(), pongObject.getPosition(), pongObject.getWidth(), pongObject.getHeight()));
} }
grid.update();
} }
...@@ -48,13 +66,8 @@ public class Kernel { ...@@ -48,13 +66,8 @@ public class Kernel {
pongObjectEntityHashMap.get(pongObject).updatePosition(); // Updating of the associated movable position pongObjectEntityHashMap.get(pongObject).updatePosition(); // Updating of the associated movable position
pongObject.setPosition(pongObjectEntityHashMap.get(pongObject).getPosition()); // Updating of the pong object position pongObject.setPosition(pongObjectEntityHashMap.get(pongObject).getPosition()); // Updating of the pong object position
try {
grid.draw(new ImageComponent(pongObject.getImage(), pongObject.getPosition(), pongObject.getWidth(), pongObject.getHeight()));
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
grid.update();
// Repainting the grid // Repainting the grid
grid.repaint(); grid.repaint();
grid.revalidate(); grid.revalidate();
...@@ -86,5 +99,7 @@ public class Kernel { ...@@ -86,5 +99,7 @@ public class Kernel {
mainPanel.setFocusable(true); mainPanel.setFocusable(true);
} }
public void setGameObject(ArrayList<PongObject> gameObject) {
this.gameObjects = gameObject;
}
} }
...@@ -6,6 +6,7 @@ import engine.ImageComponent; ...@@ -6,6 +6,7 @@ import engine.ImageComponent;
import engine.Kernel; import engine.Kernel;
import sprint2_demo.Hero; import sprint2_demo.Hero;
import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -30,11 +31,12 @@ public class PongApp { ...@@ -30,11 +31,12 @@ public class PongApp {
PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(100, 10), 64, 64); PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(100, 10), 64, 64);
components.add(pongBall); components.add(pongBall);
Grid grid = new Grid(); // Will create a new grid and a new kernel Kernel kernel = new Kernel("Pong", width, height, components);
grid.init("Pong", width, height); // Will start the kernel and set the keys listeners //kernel.setGameObject(components);
while(true){ while(true){
try { try {
grid.kernel.update(); kernel.update();
Thread.sleep(10); Thread.sleep(10);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -49,4 +49,9 @@ public class PongBall implements PongObject { ...@@ -49,4 +49,9 @@ public class PongBall implements PongObject {
public void setPosition(Coordinates2D position) { public void setPosition(Coordinates2D position) {
this.position = position; this.position = position;
} }
@Override
public String getName() {
return name;
}
} }
...@@ -11,4 +11,6 @@ public interface PongObject { ...@@ -11,4 +11,6 @@ public interface PongObject {
public int getHeight(); public int getHeight();
public int getResistance(); public int getResistance();
public void setPosition(Coordinates2D position); public void setPosition(Coordinates2D position);
public String getName();
} }
...@@ -48,4 +48,9 @@ public class PongRacket implements PongObject { ...@@ -48,4 +48,9 @@ public class PongRacket implements PongObject {
public void setPosition(Coordinates2D position) { public void setPosition(Coordinates2D position) {
this.position = position; this.position = position;
} }
@Override
public String getName() {
return name;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment