From 2cc1c86da2bc4f08b5cc4039db8deb8bfe19074e Mon Sep 17 00:00:00 2001
From: jg175 <jg175@hdm-stuttgart.de>
Date: Thu, 22 Feb 2024 18:05:44 +0100
Subject: [PATCH] UPDATE: player and Bomb sound

---
 .../Model/Entity/GameplayObjects/Bomb.java    |  6 ++---
 .../battlearena/Model/Entity/Player.java      | 19 ++++++++--------
 .../battlearena/Model/Sound/SoundEffects.java | 22 +++++++++----------
 src/main/resources/player/appSettings.json    |  2 +-
 4 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/GameplayObjects/Bomb.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/GameplayObjects/Bomb.java
index f586f7f5..1296989d 100644
--- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/GameplayObjects/Bomb.java
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/GameplayObjects/Bomb.java
@@ -3,15 +3,13 @@ package de.hdm_stuttgart.battlearena.Model.Entity.GameplayObjects;
 import de.hdm_stuttgart.battlearena.Controller.GameSceneController;
 import de.hdm_stuttgart.battlearena.Model.Entity.EntityDirection;
 import de.hdm_stuttgart.battlearena.Model.Entity.IEntity;
-
 import de.hdm_stuttgart.battlearena.Model.Sound.SFX;
 import de.hdm_stuttgart.battlearena.Model.Sound.SoundEffects;
 import javafx.geometry.BoundingBox;
 import javafx.scene.canvas.GraphicsContext;
 import javafx.scene.image.Image;
-
-import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.util.Objects;
 
@@ -28,6 +26,7 @@ public class Bomb implements IEntity {
     private BoundingBox boxCollider;
     private final ObjectType OBJECT_TYPE = ObjectType.BOMB;
     private ObjectStatus OBJECT_STATUS = ObjectStatus.UNUSED;
+    private SoundEffects sfx = new SoundEffects();
     //private SoundEffects soundEffects;
 
     public Bomb(int posX, int posY, GraphicsContext graphicsContext) {
@@ -85,6 +84,7 @@ public class Bomb implements IEntity {
     }
 
     private void updateAnimation() {        //Easily add more frames
+        sfx.playSoundEffectOnce(SFX.BOMB);
         frameCounter++;
 
         if (frameCounter > 17) {
diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java
index 051bd827..20f63337 100644
--- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java
@@ -9,13 +9,11 @@ import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler;
 import de.hdm_stuttgart.battlearena.Model.Map.TileManager;
 import de.hdm_stuttgart.battlearena.Model.Sound.SFX;
 import de.hdm_stuttgart.battlearena.Model.Sound.SoundEffects;
-
 import javafx.geometry.BoundingBox;
 import javafx.scene.canvas.GraphicsContext;
 import javafx.scene.image.Image;
-
-import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.io.IOException;
 import java.util.List;
@@ -42,6 +40,7 @@ class Player implements IEntity {
 
     private SoundEffects sfx = new SoundEffects();
 
+
     private Image directionDownOne,
             directionDownTwo,
             directionUpOne,
@@ -288,13 +287,6 @@ class Player implements IEntity {
     private void performEntityMovement(boolean isWalkableTile) {
         lastMapPosX = mapPosX;
         lastMapPosY = mapPosY;
-        try {
-            sfx.playSoundEffect(gameSceneController);
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        } catch (InterruptedException e) {
-            throw new RuntimeException(e);
-        }
         if (isWalkableTile) {
             switch (playerDirection) {
                 case UP:
@@ -610,6 +602,13 @@ class Player implements IEntity {
 
     @Override
     public void updateEntityWalkAnimation() {
+        try {
+            sfx.playSoundEffect(gameSceneController);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
         spriteCounter++;
         if (spriteCounter > 10) {
             if (spriteNumber == 1) {
diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundEffects.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundEffects.java
index 7908e96f..d01d6bc6 100644
--- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundEffects.java
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundEffects.java
@@ -13,14 +13,14 @@ import java.io.IOException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 
 public class SoundEffects {
     private static final Logger log = LogManager.getLogger(SoundEffects.class);
 
-
-
-
+    private ExecutorService service = Executors.newCachedThreadPool();
     private SoundFileManager fileManager = new SoundFileManager();
     private Persistence persistence = Persistence.getInstance();
     private boolean soundEffectPlayedTest = false;
@@ -41,13 +41,14 @@ public class SoundEffects {
                 List<Path> sfxPaths = fileManager.getFilePathsFromResources(path);
                 String randomPath = fileManager.getRandomFilePath(sfxPaths);
                 String resource = fileManager.convertPathToResourcePath(randomPath);
-                Thread sfxLoop = new Thread(() -> {
+                ///Thread sfxLoop = new Thread
+               service.execute(() -> {
                     AudioClip effect = new AudioClip(getClass().getResource(resource).toString());
                     effect.setVolume((double) persistence.getSettings().getSfxVolume() /100);
                     effect.play();
                 });
-                sfxLoop.setDaemon(true);
-                sfxLoop.start();
+                //sfxLoop.setDaemon(true);
+                //sfxLoop.start();
                 sfxPaths.clear();
             }
     }
@@ -57,13 +58,13 @@ public class SoundEffects {
         String path = soundEffect.getPath();
         log.info(path);
         String resource = path.substring(18); // Begins at Ressource Folder
-        Thread sfx = new Thread(()->{
+        service.execute(()->{
             AudioClip audioClip = new AudioClip(getClass().getResource(resource).toExternalForm());
             audioClip.setVolume((double) persistence.getSettings().getSfxVolume()/100);
             audioClip.play();
         });
-        sfx.setDaemon(true);
-        sfx.start();
+        //sfx.setDaemon(true);
+        //sfx.start();
         soundEffectPlayedTest = true;
     }
 
@@ -72,9 +73,6 @@ public class SoundEffects {
         persistence.getSettings().setSfxVolume(volume);
     }
 
-    public int getVolume(){
-        return Persistence.getInstance().getSettings().getSfxVolume();
-    }
 
     public boolean isSoundEffectPlayedTest() {
         return soundEffectPlayedTest;
diff --git a/src/main/resources/player/appSettings.json b/src/main/resources/player/appSettings.json
index 7c4eba52..c253782d 100644
--- a/src/main/resources/player/appSettings.json
+++ b/src/main/resources/player/appSettings.json
@@ -1,4 +1,4 @@
 {
   "sfxVolume": 16,
-  "musicVolume": 81
+  "musicVolume": 16
 }
\ No newline at end of file
-- 
GitLab