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 058148f8bf7282f80112c358cecb182afc7d74b4..e41028d4fb56fa9a31eb3b44a56571be94cd4910 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 @@ -10,13 +10,13 @@ import de.hdm_stuttgart.battlearena.Model.Map.TileManager; import de.hdm_stuttgart.battlearena.Model.Sound.SFX; import de.hdm_stuttgart.battlearena.Model.Sound.SfxOnce; import de.hdm_stuttgart.battlearena.Model.Sound.SoundEffects; +import de.hdm_stuttgart.battlearena.Model.Sound.WalkEffects; import javafx.geometry.BoundingBox; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.io.IOException; import java.util.List; import java.util.Objects; @@ -606,13 +606,8 @@ 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); - } + WalkEffects walk = new WalkEffects(); + walk.startWalkSound(gameSceneController); 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 05fa151b7521ce896a08f71a40c0665bd339691a..3e2f33dcd09a69f1afaa1f0f3a078f9cac9f1de1 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 @@ -2,19 +2,12 @@ package de.hdm_stuttgart.battlearena.Model.Sound; import de.hdm_stuttgart.battlearena.Controller.GameSceneController; import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Persistence; -import de.hdm_stuttgart.battlearena.Model.Entity.IEntity; -import de.hdm_stuttgart.battlearena.Model.Map.ITile; -import de.hdm_stuttgart.battlearena.Model.Map.TileManager; -import javafx.scene.media.AudioClip; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; 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; @@ -30,28 +23,7 @@ public class SoundEffects { public void playSoundEffect(GameSceneController gameSceneController) throws IOException, InterruptedException { - TileManager tileManager = gameSceneController.getTileManager(); - IEntity player = gameSceneController.getPlayer(); - ITile[] tileSet = tileManager.getTileSet(); - int[][] map = tileManager.getTileMap(); - int playerPosX = player.getMapPosX(); - int playerPosY = player.getMapPosY(); - int mapTile = map[playerPosY/48][playerPosX/48]; - SFXLoop sfx = tileSet[mapTile].getSoundeffect(); - if(!(sfx == SFXLoop.NONE)) { - Path path = Paths.get(sfx.getPath()); - List<Path> sfxPaths = fileManager.getFilePathsFromResources(path); - String randomPath = fileManager.getRandomFilePath(sfxPaths); - String resource = fileManager.convertPathToResourcePath(randomPath); - Thread sfxLoop = new Thread(() -> { - AudioClip effect = new AudioClip(getClass().getResource(resource).toString()); - effect.setVolume((double) persistence.getSettings().getSfxVolume() /100); - effect.play(); - }); - sfxLoop.setDaemon(true); - sfxLoop.start(); - sfxPaths.clear(); - } + } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/WalkEffects.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/WalkEffects.java new file mode 100644 index 0000000000000000000000000000000000000000..8a7183ac456aaf490e6da256daf4118dcd88f488 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/WalkEffects.java @@ -0,0 +1,56 @@ +package de.hdm_stuttgart.battlearena.Model.Sound; + +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; +import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Persistence; +import de.hdm_stuttgart.battlearena.Model.Entity.IEntity; +import de.hdm_stuttgart.battlearena.Model.Map.ITile; +import de.hdm_stuttgart.battlearena.Model.Map.TileManager; +import javafx.scene.media.AudioClip; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +public class WalkEffects extends Thread{ + private static final Logger log = LogManager.getLogger(WalkEffects.class); + private GameSceneController gameSceneController; + private SoundFileManager fileManager = new SoundFileManager(); + private Persistence persistence = Persistence.getInstance(); + + @Override + public void run() { + TileManager tileManager = gameSceneController.getTileManager(); + IEntity player = gameSceneController.getPlayer(); + ITile[] tileSet = tileManager.getTileSet(); + int[][] map = tileManager.getTileMap(); + int playerPosX = player.getMapPosX(); + int playerPosY = player.getMapPosY(); + int mapTile = map[playerPosY/48][playerPosX/48]; + SFXLoop sfx = tileSet[mapTile].getSoundeffect(); + if(!(sfx == SFXLoop.NONE)) { + Path path = Paths.get(sfx.getPath()); + List<Path> sfxPaths = null; + try { + sfxPaths = fileManager.getFilePathsFromResources(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + String randomPath = fileManager.getRandomFilePath(sfxPaths); + String resource = fileManager.convertPathToResourcePath(randomPath); + + AudioClip effect = new AudioClip(getClass().getResource(resource).toString()); + effect.setVolume((double) persistence.getSettings().getSfxVolume() /100); + effect.play(); + + sfxPaths.clear(); + } + } + + public void startWalkSound(GameSceneController gameSceneController){ + this.gameSceneController = gameSceneController; + start(); + } +}