Select Git revision
ModelElement.java
Forked from
COUETOUX Basile / FirefighterStarter
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Kernel.java 6.27 KiB
package engine;
import engine.graphic.GraphicEngine;
import engine.physic.Coordinates2D;
import engine.physic.Entity;
import engine.sound.SoundEngine;
import engine.physic.PhysicEngine;
import pong.PongObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class Kernel {
ArrayList<Engine> engines = new ArrayList<Engine>();
PhysicEngine physicEngine;
GraphicEngine graphicEngine;
SoundEngine soundEngine =new SoundEngine();
public static boolean isCollision = false;
//public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>();
public static ArrayList<PongObject> gameObjects;
public HashMap<PongObject, Entity> pongObjectEntityHashMap;
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.drawRect();
engines.add(this.graphicEngine);//
this.physicEngine = new PhysicEngine();
engines.add(physicEngine);
engines.add(soundEngine);
pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap;
this.update();
this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane());
soundEngine = new SoundEngine();
start();
}
/**
* Start the game. Associate a Movable to each GameObject and draw them.
*
* @throws IOException
*/
public void start() throws IOException {
graphicEngine.start();
}
public void restart() throws IOException {
for (Engine engine: engines
) {
engine.restart();
}
//pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap;
}
/**
* One step of the game. Update all objects positions and repaint the grid.
*/
public void update() {
for (Engine engine: engines
) {
engine.update();
}
}
/**
* Listen to keyboard events.
*/
public void setKeysAndKeyReleasedListeners(JPanel mainPanel) {
InputMap inputMap = mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = mainPanel.getActionMap();
// Définir les actions pour les touches de pression
Action upAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, -3));
}
};
Action downAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 3));
}
};
Action zAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, -3));
}
};
Action sAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 3));
}
};
// Mapper les touches de pression aux actions
inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
inputMap.put(KeyStroke.getKeyStroke("pressed Z"), "zAction");
inputMap.put(KeyStroke.getKeyStroke("pressed S"), "sAction");
// Mapper les actions aux méthodes correspondantes
actionMap.put("upAction", upAction);
actionMap.put("downAction", downAction);
actionMap.put("zAction", zAction);
actionMap.put("sAction", sAction);
// Définir les actions pour les touches de relâchement
Action upReleaseAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 0));
}
};
Action downReleaseAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 0));
}
};
Action zReleaseAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 0));
}
};
Action sReleaseAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 0));
}
};
// Mapper les touches de relâchement aux actions
inputMap.put(KeyStroke.getKeyStroke("released UP"), "upReleaseAction");
inputMap.put(KeyStroke.getKeyStroke("released DOWN"), "downReleaseAction");
inputMap.put(KeyStroke.getKeyStroke("released Z"), "zReleaseAction");
inputMap.put(KeyStroke.getKeyStroke("released S"), "sReleaseAction");
// Mapper les actions aux méthodes correspondantes pour le relâchement
actionMap.put("upReleaseAction", upReleaseAction);
actionMap.put("downReleaseAction", downReleaseAction);
actionMap.put("zReleaseAction", zReleaseAction);
actionMap.put("sReleaseAction", sReleaseAction);
mainPanel.setFocusable(true);
}
public void setGameObject(ArrayList<PongObject> gameObject) {
gameObjects = gameObject;
}
public void addButton(JButton button){
button.setSize(50, 30);
graphicEngine.addButton(button);
}
}