diff --git a/src/main/java/engine/Kernel.java b/src/main/java/engine/Kernel.java index cedb9345288f8d3ba4194a3ebefd8132a5d7870e..ac5221d4f6fedd2f2f3bdce58f546cc7bb053ca5 100644 --- a/src/main/java/engine/Kernel.java +++ b/src/main/java/engine/Kernel.java @@ -19,14 +19,10 @@ public class Kernel { ArrayList<Engine> engines = new ArrayList<Engine>(); PhysicEngine physicEngine; GraphicEngine graphicEngine; - SoundEngine soundEngine =new SoundEngine(); - public static boolean isCollision = false; + SoundEngine soundEngine; public int isFirstUpdate = 0; - - //public HashMap<PongObject, ImageComponent> pongObjectImageComponentHashMap = new HashMap<>(); - public static ArrayList<PongObject> gameObjects; public HashMap<PongObject, Entity> pongObjectEntityHashMap; @@ -35,22 +31,22 @@ public class Kernel { this.setGameObject(gameObjects); System.out.println("Model created"); + soundEngine = new SoundEngine(); + engines.add(soundEngine); + 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(); + this.physicEngine = new PhysicEngine(soundEngine); engines.add(physicEngine); - engines.add(soundEngine); pongObjectEntityHashMap = physicEngine.pongObjectEntityHashMap; this.update(); this.setKeysAndKeyReleasedListeners((JPanel) this.graphicEngine.getContentPane()); - soundEngine = new SoundEngine(); - String music = "src/main/resources/Sound/son.wav"; - SoundEngine.playMusic(music); + start(); } @@ -197,5 +193,25 @@ public class Kernel { 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; + } + } diff --git a/src/main/java/engine/physic/Coordinates2D.java b/src/main/java/engine/physic/Coordinates2D.java index d9870e70a3954fd3b9e0f178c9831f5034d4ce53..c2c3f9f3b8483860183ecaf8f84a19949134cb70 100644 --- a/src/main/java/engine/physic/Coordinates2D.java +++ b/src/main/java/engine/physic/Coordinates2D.java @@ -22,6 +22,19 @@ public class Coordinates2D { 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() { return this.x; } diff --git a/src/main/java/engine/physic/PhysicEngine.java b/src/main/java/engine/physic/PhysicEngine.java index 72ccd4632b358bc44c412958b0858bd733cb2eb3..fca35f13d21a30285dc9d732928a8faedcb9445e 100644 --- a/src/main/java/engine/physic/PhysicEngine.java +++ b/src/main/java/engine/physic/PhysicEngine.java @@ -2,6 +2,7 @@ package engine.physic; import engine.Engine; import engine.Kernel; +import engine.sound.SoundEngine; import pong.PongObject; import java.util.HashMap; @@ -12,8 +13,11 @@ public class PhysicEngine implements Engine { int i = 0; 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 - public PhysicEngine(){ + public PhysicEngine(SoundEngine soundEngine) { + this.soundEngine = soundEngine; restart(); } @Override @@ -29,12 +33,12 @@ public class PhysicEngine implements Engine { i++; //String collisionPath = "src/main/resources/Sound/collision.wav"; //Sound.playMusic(collisionPath); - Kernel.isCollision = true; + soundEngine.playMusic("src/main/resources/Sound/hit_racket.wav"); System.out.println("Collision " + i); pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition); //TODO: add a method to Entity to increment the speed 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 @@ -56,16 +60,8 @@ public class PhysicEngine implements Engine { @Override public void restart() { pongObjectEntityHashMap = new HashMap<>(); - //positions = new HashMap<>(); for (PongObject pongObject: Kernel.gameObjects) { - Coordinates2D speed = new Coordinates2D(0, 0); - 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())); - + pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), pongObject.getInitSpeed(), new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight())); } } @@ -78,4 +74,6 @@ public class PhysicEngine implements Engine { firstObject.getHeight() + pos1.getY() > pos2.getY(); } + + } diff --git a/src/main/java/engine/sound/SoundEngine.java b/src/main/java/engine/sound/SoundEngine.java index 7e25d1f912a46ac1216f49b0f60c7d042fb86bcc..4c4dfee06d20bd7828e37b5ed12662d264c00e97 100644 --- a/src/main/java/engine/sound/SoundEngine.java +++ b/src/main/java/engine/sound/SoundEngine.java @@ -4,6 +4,7 @@ import engine.Engine; import engine.Kernel; import java.io.File; +import java.util.HashMap; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; @@ -12,8 +13,12 @@ import javax.swing.*; public class SoundEngine implements Engine { private static Clip clip; // Ajout de cette variable pour stocker le clip en cours de lecture - public SoundEngine(){} - public static void playMusic(String location){ + private HashMap<String, Boolean> soundMap = new HashMap<String, Boolean>(); + + private boolean isMuted = false; + public SoundEngine() { + } + public void playMusic(String location){ try { File musicPath = new File(location); if (musicPath.exists()) { @@ -39,14 +44,27 @@ public class SoundEngine implements Engine { @Override public void update() { - if (Kernel.isCollision){ - String collisionPath = "src/main/resources/Sound/collision.wav"; - SoundEngine.playMusic(collisionPath); - Kernel.isCollision = false; + if(isMuted) return; + for (String sound : soundMap.keySet()) { + if (soundMap.get(sound)) { + 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 diff --git a/src/main/java/pong/PongApp.java b/src/main/java/pong/PongApp.java index 6576366c58b56c53fed6530370387daa654c63ef..8a42458b74dc8ec7ccf5997c35dce32615382d92 100644 --- a/src/main/java/pong/PongApp.java +++ b/src/main/java/pong/PongApp.java @@ -38,29 +38,22 @@ public class PongApp { int racketHeight = 120; 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); - 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); - 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); 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 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 -> { score1 = 0; score2 = 0; @@ -91,6 +84,7 @@ public class PongApp { restart(); //kernel.setGameObject(components); kernel.restart(); + kernel.playSound("src/main/resources/Sound/scoring.wav"); } kernel.update(); // Updating the kernel and the game state diff --git a/src/main/java/pong/PongBall.java b/src/main/java/pong/PongBall.java index e0a2639b404561eaf550dabd7c232c187be31071..f1a087a7c83e1acfcc81d1b924418a6e039bab10 100644 --- a/src/main/java/pong/PongBall.java +++ b/src/main/java/pong/PongBall.java @@ -12,16 +12,20 @@ public class PongBall implements PongObject { private Coordinates2D position; private Coordinates2D speed; + + private final Coordinates2D initSpeed; private Coordinates2D initPosition; 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.width = width; this.height = height; this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.position = position; this.initPosition = position; + this.speed = speed; + this.initSpeed = speed; } @Override @@ -88,4 +92,9 @@ public class PongBall implements PongObject { public void processCollision(PongObject po2) { } + + @Override + public Coordinates2D getInitSpeed(){ + return this.initSpeed; + } } diff --git a/src/main/java/pong/PongObject.java b/src/main/java/pong/PongObject.java index a927fd9140be5cccd3729469692f68c3a74a5772..63cfdab020d0f1514935e63c327a692ccf612168 100644 --- a/src/main/java/pong/PongObject.java +++ b/src/main/java/pong/PongObject.java @@ -16,7 +16,7 @@ public interface PongObject { public void setSpeed(Coordinates2D speed); - + public Coordinates2D getInitSpeed(); public String getName(); Coordinates2D getInitPosition(); diff --git a/src/main/java/pong/PongRacket.java b/src/main/java/pong/PongRacket.java index d8d5329632b24b674104401202d22c2bf73b5a01..9d49986f6c0a29c3a7beb103ff988435be63c170 100644 --- a/src/main/java/pong/PongRacket.java +++ b/src/main/java/pong/PongRacket.java @@ -14,9 +14,11 @@ public class PongRacket implements PongObject { private final Image image; private Coordinates2D speed; + private final Coordinates2D initSpeed; + 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.width = width; this.height = height; @@ -24,6 +26,8 @@ public class PongRacket implements PongObject { this.position = position; this.initPosition = position; this.isLeftRacket = isLeftRacket; + this.speed = speed; + this.initSpeed = speed; } @Override @@ -121,4 +125,9 @@ public class PongRacket implements PongObject { } } } + + @Override + public Coordinates2D getInitSpeed() { + return initSpeed; + } } diff --git a/src/main/resources/Sound/hit_racket.wav b/src/main/resources/Sound/hit_racket.wav new file mode 100644 index 0000000000000000000000000000000000000000..6fed415aff6d4797d44b6f6f69f9315b41a1e90c Binary files /dev/null and b/src/main/resources/Sound/hit_racket.wav differ diff --git a/src/main/resources/Sound/minecraft.wav b/src/main/resources/Sound/minecraft.wav new file mode 100644 index 0000000000000000000000000000000000000000..636bc7fab5e47b555d7904486881b11551b14338 Binary files /dev/null and b/src/main/resources/Sound/minecraft.wav differ diff --git a/src/main/resources/Sound/scoring.wav b/src/main/resources/Sound/scoring.wav new file mode 100644 index 0000000000000000000000000000000000000000..02688d633f3b297e7914ec3c2aefdc7cb8fdfa40 Binary files /dev/null and b/src/main/resources/Sound/scoring.wav differ