Skip to content
Snippets Groups Projects
Commit 7d3533fc authored by AUGIER Yoann's avatar AUGIER Yoann
Browse files

Correction fonctionnement des keylistenner pour MacOS, Linux et Windows

parent c052ac84
Branches
No related tags found
No related merge requests found
Pipeline #23886 failed
......@@ -10,6 +10,7 @@ import pong.PongObject;
import pong.PongRacket;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -42,7 +43,7 @@ public class Kernel {
pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap;
this.update();
this.setKeysListeners((JPanel) this.graphicEngine.getContentPane());
this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane());
}
/**
......@@ -78,34 +79,92 @@ public class Kernel {
/**
* Listen to keyboard events.
*/
public void setKeysListeners(JPanel mainPanel) {
mainPanel.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
switch (evt.getKeyCode()) {
case java.awt.event.KeyEvent.VK_UP -> {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, -3)); }
case java.awt.event.KeyEvent.VK_DOWN -> {
pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 3));
}
case java.awt.event.KeyEvent.VK_Z -> {
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, -3)); }
case java.awt.event.KeyEvent.VK_S -> {
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 3));
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));
}
};
public void keyReleased(java.awt.event.KeyEvent evt) {
switch (evt.getKeyCode()) {
case java.awt.event.KeyEvent.VK_UP, java.awt.event.KeyEvent.VK_DOWN -> {
pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 0));
Action sAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 3));
}
case java.awt.event.KeyEvent.VK_Z, java.awt.event.KeyEvent.VK_S -> {
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 0));
};
// 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);
}
......
......@@ -32,15 +32,15 @@ public class PongApp {
String son = "src/main/resources/Sound/son.wav";
Sound.playMusic(son);
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, 100), racketWidth, racketHeight);
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), racketWidth, racketHeight);
components.add(leftRacket);
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, 100), racketWidth, racketHeight);
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), racketWidth, racketHeight);
components.add(rightRacket);
// TODO: Add rackets
//PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
//PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
components.add(pongBall);
Kernel kernel = new Kernel("Pong", width, height, components);
......
......@@ -45,7 +45,7 @@ public class Grid extends JFrame {
try {
kernel.start(); // Start the kernel
kernel.setKeysListeners(playerPanel); // Set keys listeners
kernel.setKeysAndKeyReleasedListeners(playerPanel); // Set keys listeners
revalidate();
} catch (IOException e) {
throw new RuntimeException(e);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment