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

Push finale ?

parent 12e6b9fa
No related branches found
No related tags found
No related merge requests found
Pipeline #25242 passed
...@@ -19,14 +19,10 @@ public class Kernel { ...@@ -19,14 +19,10 @@ public class Kernel {
ArrayList<Engine> engines = new ArrayList<Engine>(); ArrayList<Engine> engines = new ArrayList<Engine>();
PhysicEngine physicEngine; PhysicEngine physicEngine;
GraphicEngine graphicEngine; GraphicEngine graphicEngine;
SoundEngine soundEngine =new SoundEngine(); SoundEngine soundEngine;
public static boolean isCollision = false;
public int isFirstUpdate = 0; public int isFirstUpdate = 0;
//public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>();
public static ArrayList<PongObject> gameObjects; public static ArrayList<PongObject> gameObjects;
public HashMap<PongObject, Entity> pongObjectEntityHashMap; public HashMap<PongObject, Entity> pongObjectEntityHashMap;
...@@ -35,22 +31,22 @@ public class Kernel { ...@@ -35,22 +31,22 @@ public class Kernel {
this.setGameObject(gameObjects); this.setGameObject(gameObjects);
System.out.println("Model created"); System.out.println("Model created");
soundEngine = new SoundEngine();
engines.add(soundEngine);
this.graphicEngine = new GraphicEngine(); // Will create a new grid and a new kernel 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.init(gameName, width, height); // Will start the kernel and set the keys listeners
graphicEngine.drawRect(); graphicEngine.drawRect();
engines.add(this.graphicEngine);// engines.add(this.graphicEngine);//
this.physicEngine = new PhysicEngine(); this.physicEngine = new PhysicEngine(soundEngine);
engines.add(physicEngine); engines.add(physicEngine);
engines.add(soundEngine);
pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap;
this.update(); this.update();
this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane()); this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane());
soundEngine = new SoundEngine();
String music = "src/main/resources/Sound/son.wav";
SoundEngine.playMusic(music);
start(); start();
} }
...@@ -197,5 +193,25 @@ public class Kernel { ...@@ -197,5 +193,25 @@ public class Kernel {
graphicEngine.addButton(button); graphicEngine.addButton(button);
} }
/**
* Add a new sound to the game.
* @param soundPath
* @param isPlaying
*/
public void addSound(String soundPath, boolean isPlaying){
soundEngine.addSound(soundPath, isPlaying);
}
/**
* Play a sound
*/
public void playSound(String soundPath) {
soundEngine.playMusic(soundPath);
}
public boolean isItACollision(){
return physicEngine.isCollision;
}
} }
...@@ -22,6 +22,19 @@ public class Coordinates2D { ...@@ -22,6 +22,19 @@ public class Coordinates2D {
this.y *= scalar; this.y *= scalar;
} }
public void increment(){
if(this.x < 0){
this.x--;
}else if(this.x > 0){
this.x++;
}
if(this.y < 0){
this.y--;
}else if(this.y > 0){
this.y++;
}
}
public int getX() { public int getX() {
return this.x; return this.x;
} }
......
...@@ -2,6 +2,7 @@ package engine.physic; ...@@ -2,6 +2,7 @@ package engine.physic;
import engine.Engine; import engine.Engine;
import engine.Kernel; import engine.Kernel;
import engine.sound.SoundEngine;
import pong.PongObject; import pong.PongObject;
import java.util.HashMap; import java.util.HashMap;
...@@ -12,8 +13,11 @@ public class PhysicEngine implements Engine { ...@@ -12,8 +13,11 @@ public class PhysicEngine implements Engine {
int i = 0; int i = 0;
public HashMap<PongObject, Entity> pongObjectEntityHashMap; // Map of the pong objects and their associated entity public HashMap<PongObject, Entity> pongObjectEntityHashMap; // Map of the pong objects and their associated entity
public boolean isCollision = false;
SoundEngine soundEngine;
//private HashMap<Coordinates2D, PongObject> positions; // Map of the positions and the associated pong object //private HashMap<Coordinates2D, PongObject> positions; // Map of the positions and the associated pong object
public PhysicEngine(){ public PhysicEngine(SoundEngine soundEngine) {
this.soundEngine = soundEngine;
restart(); restart();
} }
@Override @Override
...@@ -29,12 +33,12 @@ public class PhysicEngine implements Engine { ...@@ -29,12 +33,12 @@ public class PhysicEngine implements Engine {
i++; i++;
//String collisionPath = "src/main/resources/Sound/collision.wav"; //String collisionPath = "src/main/resources/Sound/collision.wav";
//Sound.playMusic(collisionPath); //Sound.playMusic(collisionPath);
Kernel.isCollision = true; soundEngine.playMusic("src/main/resources/Sound/hit_racket.wav");
System.out.println("Collision " + i); System.out.println("Collision " + i);
pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition); pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition);
//TODO: add a method to Entity to increment the speed //TODO: add a method to Entity to increment the speed
if(i % 10 == 0){ if(i % 10 == 0){
pongObjectEntityHashMap.get(pongObject).getSpeed().mul(2); pongObjectEntityHashMap.get(pongObject).getSpeed().increment();
} }
pongObject.setSpeed(pongObjectEntityHashMap.get(pongObject).getSpeed()); // Updating of the pong object speed with the latest speed of the entity pongObject.setSpeed(pongObjectEntityHashMap.get(pongObject).getSpeed()); // Updating of the pong object speed with the latest speed of the entity
...@@ -56,16 +60,8 @@ public class PhysicEngine implements Engine { ...@@ -56,16 +60,8 @@ public class PhysicEngine implements Engine {
@Override @Override
public void restart() { public void restart() {
pongObjectEntityHashMap = new HashMap<>(); pongObjectEntityHashMap = new HashMap<>();
//positions = new HashMap<>();
for (PongObject pongObject: Kernel.gameObjects) { for (PongObject pongObject: Kernel.gameObjects) {
Coordinates2D speed = new Coordinates2D(0, 0); pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), pongObject.getInitSpeed(), new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight()));
if(pongObject.getName().equals("Ball")){
//pongObject.setPosition(pongObject.getInitPosition());
speed = new Coordinates2D(2, 2);
}
//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()));
} }
} }
...@@ -78,4 +74,6 @@ public class PhysicEngine implements Engine { ...@@ -78,4 +74,6 @@ public class PhysicEngine implements Engine {
firstObject.getHeight() + pos1.getY() > pos2.getY(); firstObject.getHeight() + pos1.getY() > pos2.getY();
} }
} }
...@@ -4,6 +4,7 @@ import engine.Engine; ...@@ -4,6 +4,7 @@ import engine.Engine;
import engine.Kernel; import engine.Kernel;
import java.io.File; import java.io.File;
import java.util.HashMap;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
...@@ -12,8 +13,12 @@ import javax.swing.*; ...@@ -12,8 +13,12 @@ import javax.swing.*;
public class SoundEngine implements Engine { public class SoundEngine implements Engine {
private static Clip clip; // Ajout de cette variable pour stocker le clip en cours de lecture private static Clip clip; // Ajout de cette variable pour stocker le clip en cours de lecture
public SoundEngine(){} private HashMap<String, Boolean> soundMap = new HashMap<String, Boolean>();
public static void playMusic(String location){
private boolean isMuted = false;
public SoundEngine() {
}
public void playMusic(String location){
try { try {
File musicPath = new File(location); File musicPath = new File(location);
if (musicPath.exists()) { if (musicPath.exists()) {
...@@ -39,14 +44,27 @@ public class SoundEngine implements Engine { ...@@ -39,14 +44,27 @@ public class SoundEngine implements Engine {
@Override @Override
public void update() { public void update() {
if (Kernel.isCollision){ if(isMuted) return;
String collisionPath = "src/main/resources/Sound/collision.wav"; for (String sound : soundMap.keySet()) {
SoundEngine.playMusic(collisionPath); if (soundMap.get(sound)) {
Kernel.isCollision = false; System.out.println(soundMap.get(sound));
playMusic(sound);
}
}
} }
public void addSound(String soundPath, boolean isPlaying) {
System.out.println("SoundEngine addSound " + isPlaying);
soundMap.put(soundPath, isPlaying);
} }
public void muteSoundEngine(){
isMuted = true;
}
public void unmuteSoundEngine(){
isMuted = false;
}
@Override @Override
......
...@@ -38,29 +38,22 @@ public class PongApp { ...@@ -38,29 +38,22 @@ public class PongApp {
int racketHeight = 120; int racketHeight = 120;
int ballWidth = 32; int ballWidth = 32;
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), racketWidth, racketHeight, true); PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), new Coordinates2D(0, 0), racketWidth, racketHeight, true);
components.add(leftRacket); components.add(leftRacket);
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), racketWidth, racketHeight, false); PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), new Coordinates2D(0, 0), racketWidth, racketHeight, false);
components.add(rightRacket); components.add(rightRacket);
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/pong/ball.png", new Coordinates2D(width/2, height/2), new Coordinates2D(2, 2), ballWidth, ballWidth);
components.add(pongBall); components.add(pongBall);
Kernel kernel = new Kernel("Pong", width, height, components); Kernel kernel = new Kernel("Pong", width, height, components);
//kernel.playSound("src/main/resources/Sound/minecraft.wav");
kernel.addSound("src/main/resources/Sound/hit_racket.wav", kernel.isItACollision());
JButton button = new JButton("Restart"); JButton button = new JButton("Restart");
JButton button2 = new JButton("Mute"); JButton button2 = new JButton("Mute");
/*JLabel muteLabel = new JLabel(new ImageIcon("src/main/resources/Sound/mute.png"));
// définir la taille de l'image
muteLabel.setBounds(0, 0, 10, 50);
muteLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Mettez ici le code pour gérer le clic sur l'image (par exemple, pour activer/désactiver le son)
SoundEngine.stopMusic(); // Exemple, vous devrez implémenter cette méthode
}
});*/
button.addActionListener(e -> { button.addActionListener(e -> {
score1 = 0; score1 = 0;
score2 = 0; score2 = 0;
...@@ -91,6 +84,7 @@ public class PongApp { ...@@ -91,6 +84,7 @@ public class PongApp {
restart(); restart();
//kernel.setGameObject(components); //kernel.setGameObject(components);
kernel.restart(); kernel.restart();
kernel.playSound("src/main/resources/Sound/scoring.wav");
} }
kernel.update(); // Updating the kernel and the game state kernel.update(); // Updating the kernel and the game state
......
...@@ -12,16 +12,20 @@ public class PongBall implements PongObject { ...@@ -12,16 +12,20 @@ public class PongBall implements PongObject {
private Coordinates2D position; private Coordinates2D position;
private Coordinates2D speed; private Coordinates2D speed;
private final Coordinates2D initSpeed;
private Coordinates2D initPosition; private Coordinates2D initPosition;
private final Image image; private final Image image;
PongBall(String name, String imagePath, Coordinates2D position, int width, int height){ PongBall(String name, String imagePath, Coordinates2D position, Coordinates2D speed, int width, int height){
this.name = name; this.name = name;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.image = Toolkit.getDefaultToolkit().getImage(imagePath);
this.position = position; this.position = position;
this.initPosition = position; this.initPosition = position;
this.speed = speed;
this.initSpeed = speed;
} }
@Override @Override
...@@ -88,4 +92,9 @@ public class PongBall implements PongObject { ...@@ -88,4 +92,9 @@ public class PongBall implements PongObject {
public void processCollision(PongObject po2) { public void processCollision(PongObject po2) {
} }
@Override
public Coordinates2D getInitSpeed(){
return this.initSpeed;
}
} }
...@@ -16,7 +16,7 @@ public interface PongObject { ...@@ -16,7 +16,7 @@ public interface PongObject {
public void setSpeed(Coordinates2D speed); public void setSpeed(Coordinates2D speed);
public Coordinates2D getInitSpeed();
public String getName(); public String getName();
Coordinates2D getInitPosition(); Coordinates2D getInitPosition();
......
...@@ -14,9 +14,11 @@ public class PongRacket implements PongObject { ...@@ -14,9 +14,11 @@ public class PongRacket implements PongObject {
private final Image image; private final Image image;
private Coordinates2D speed; private Coordinates2D speed;
private final Coordinates2D initSpeed;
private boolean isLeftRacket; private boolean isLeftRacket;
public PongRacket(String name, String imagePath, Coordinates2D position, int width, int height, boolean isLeftRacket) { public PongRacket(String name, String imagePath, Coordinates2D position, Coordinates2D speed, int width, int height, boolean isLeftRacket) {
this.name = name; this.name = name;
this.width = width; this.width = width;
this.height = height; this.height = height;
...@@ -24,6 +26,8 @@ public class PongRacket implements PongObject { ...@@ -24,6 +26,8 @@ public class PongRacket implements PongObject {
this.position = position; this.position = position;
this.initPosition = position; this.initPosition = position;
this.isLeftRacket = isLeftRacket; this.isLeftRacket = isLeftRacket;
this.speed = speed;
this.initSpeed = speed;
} }
@Override @Override
...@@ -121,4 +125,9 @@ public class PongRacket implements PongObject { ...@@ -121,4 +125,9 @@ public class PongRacket implements PongObject {
} }
} }
} }
@Override
public Coordinates2D getInitSpeed() {
return initSpeed;
}
} }
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment