diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Controller/Enum/GameState.java b/src/main/java/de/hdm_stuttgart/battlearena/Controller/Enum/GameState.java index 1ea713b41a90fa593348760d6a6be8c9f0ed1a62..946a857ea66ab876a298a55957c74b24e5557560 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Controller/Enum/GameState.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Controller/Enum/GameState.java @@ -1,9 +1,18 @@ package de.hdm_stuttgart.battlearena.Controller.Enum; public enum GameState { - MENU, - PAUSE, - PLAYING, - LOST, - WON + MENU("src/main/resources/sound/music/menu"), + PAUSE("src/main/resources/sound/music/game"), + PLAYING("src/main/resources/sound/music/game"), + LOST(""), + WON(""); + + private String path; + GameState(String path){ + this.path = path; + } + + public String getPath(){ + return path; + } } \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java b/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java index dc9b88e85c746bab7c651e5a6baae246eb4b94c4..18984aff8b34492b1058b3a5f51c53c58d0a5dd0 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java @@ -11,6 +11,7 @@ import de.hdm_stuttgart.battlearena.Model.Entity.IEntity; import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; import de.hdm_stuttgart.battlearena.Model.Map.TileManager; +import de.hdm_stuttgart.battlearena.Model.Sound.SoundEffects; import javafx.animation.AnimationTimer; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -20,6 +21,7 @@ import javafx.scene.canvas.GraphicsContext; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; @@ -36,6 +38,8 @@ public class GameSceneController implements Initializable { RuntimeInfo runtimeInfo = RuntimeInfo.getInstance(); + + GameMode gameMode = runtimeInfo.getGameMode(); IEntity player; @@ -88,6 +92,15 @@ public class GameSceneController implements Initializable { tileManager = new TileManager(graphicsContext2D, diffTileCount, horizontalTileCount, verticalTileCount, mapString); + + SoundEffects soundEffects = new SoundEffects(); + try { + soundEffects.playSoundEffect(); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } runtimeInfo.setGameState(GameState.PLAYING); AnimationTimer gameLoop = new AnimationTimer() { diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java index 14d1a3995c5cc6e831b18324cd91f745b58bdc1b..700868280f57d86a35458287c37af2e5bb432f1e 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java @@ -16,6 +16,8 @@ public class RuntimeInfo { private String mapDataGame; private MapData mapGame; private boolean offlineMode; //if Account-Type is online but no SQL connection: start game without stats tracking + private int musicVolume = 50; + private int sfxVolume = 50; private GameState gameState = GameState.MENU; //Default value: MENU private GameMode gameMode = GameMode.LOCAL; //Default value: LOCAL @@ -72,6 +74,22 @@ public class RuntimeInfo { this.gameMode = gameMode; } + public int getMusicVolume() { + return musicVolume; + } + + public void setMusicVolume(int musicVolume) { + this.musicVolume = musicVolume; + } + + public int getSfxVolume() { + return sfxVolume; + } + + public void setSfxVolume(int sfxVolume) { + this.sfxVolume = sfxVolume; + } + public EntityClass getPlayerOneClass() { return playerOneClass; } 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 0eaf4b2ca5c23f540e284e5ef6e80bf1be36037f..c56d432e399e9203e9288c8d11589b2cb59ce129 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 @@ -101,22 +101,22 @@ class Player implements IEntity { maxPlayerHealth = 50; health = 50; damage = 5; - playerSpeed = 5; + playerSpeed = 5; //5 } else if (entityClass == EntityClass.HIGH_BORN) { maxPlayerHealth = 100; health = 75; damage = 2; - playerSpeed = 3; + playerSpeed = 3; //3 } else if (entityClass == EntityClass.LOW_BORN) { maxPlayerHealth = 10; health = 10; damage = 10; - playerSpeed = 7; + playerSpeed = 7; //7 } else { maxPlayerHealth = 40; health = 15; damage = 7; - playerSpeed = 5; + playerSpeed = 5; //5 } } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java index a66f47e852aac620010f786bbff62a0700292535..7954688cdd580766c6b2d46209e8762d78d0ab76 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java @@ -1,5 +1,6 @@ package de.hdm_stuttgart.battlearena.Model.Inputs; +import de.hdm_stuttgart.battlearena.Model.Sound.SoundEffects; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java index 4e7ebadc958ea35d62bb71f3a0b92032c9f3d8b2..27e558d93ab99c6b8354abb6cd7983c8b279cc0f 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java @@ -40,11 +40,11 @@ public class TileManager { tileSet[1] = TileFactory.createTile(TileType.WALKABLE, TileType.NON_DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass02.png"))),SFXLoop.GRASS); tileSet[2] = TileFactory.createTile(TileType.WALKABLE, TileType.NON_DESTRUCTIBLE, - new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass04.png"))), SFXLoop.STONE); + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass04.png"))), SFXLoop.GRASS); tileSet[3] = TileFactory.createTile(TileType.NON_WALKABLE, TileType.DESTRUCTIBLE, - new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone01.png"))), SFXLoop.STONE); + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone01.png"))), SFXLoop.NONE); tileSet[4] = TileFactory.createTile(TileType.NON_WALKABLE, TileType.DESTRUCTIBLE, - new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone02.png"))), SFXLoop.STONE); + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone02.png"))), SFXLoop.NONE); tileSet[5] = TileFactory.createTile(TileType.WALKABLE,TileType.NON_DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/finalheart.png"))), SFXLoop.GRASS); } catch (Exception e) { diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/MusicPlayer.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/MusicPlayer.java index 54ce76864432c489906aaf080116f7582a13901a..63896675b7dee4da7d05b96ece8e73e06be7f60d 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/MusicPlayer.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/MusicPlayer.java @@ -1,6 +1,8 @@ package de.hdm_stuttgart.battlearena.Model.Sound; +import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.RuntimeInfo; +import javafx.fxml.Initializable; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import org.apache.logging.log4j.Logger; @@ -8,19 +10,24 @@ import org.apache.logging.log4j.LogManager; import java.io.IOException; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Random; +import java.util.ResourceBundle; import java.util.stream.Collectors; import java.util.stream.Stream; -public class MusicPlayer { +public class MusicPlayer implements Initializable { private static final Logger log = LogManager.getLogger(MusicPlayer.class); - - private MediaPlayer mediaPlayer; + private RuntimeInfo runtimeInfo; + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + setVolume(runtimeInfo.getMusicVolume()); + } //Todo Make music play @@ -81,4 +88,5 @@ public class MusicPlayer { return resource; } + } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SFXLoop.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SFXLoop.java index 248e94bea7fd3bf17d314db6ba38ce7f58d98924..4ba107d82289306dac34ec6d9ffe1824c61c7124 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SFXLoop.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SFXLoop.java @@ -2,8 +2,8 @@ package de.hdm_stuttgart.battlearena.Model.Sound; public enum SFXLoop { NONE(""), - GRASS("/sound/sfx/steps/grass"), - STONE("/sound/sfx/steps/stone"); + GRASS("src/main/resources/sound/sfx/steps/grass"), + STONE("src/main/resources/sound/sfx/steps/stone"); 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 20860c61a0d37a8970ba966e19208c49b2ad109f..cef4794d3fd12b5669e86dab84e164a4e1ced3de 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 @@ -1,18 +1,73 @@ package de.hdm_stuttgart.battlearena.Model.Sound; +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; +import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.RuntimeInfo; +import de.hdm_stuttgart.battlearena.Model.Entity.IEntity; +import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; +import de.hdm_stuttgart.battlearena.Model.Map.ITile; +import de.hdm_stuttgart.battlearena.Model.Map.TileManager; +import javafx.fxml.Initializable; import javafx.scene.media.AudioClip; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -public class SoundEffects { +import java.io.IOException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.ResourceBundle; + +public class SoundEffects implements Initializable { private static final Logger log = LogManager.getLogger(SoundEffects.class); - boolean stopSoundeffect = false; + private int sfxVolume; + + private RuntimeInfo runtimeInfo; + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + sfxVolume = runtimeInfo.getSfxVolume(); + } + public void playSoundEffect() throws IOException, InterruptedException { + GameSceneController gameSceneController = new GameSceneController(); + MusicPlayer musicPlayer = new MusicPlayer(); + InputHandler inputhandler = InputHandler.getInstance(); + TileManager tileManager = gameSceneController.getTileManager(); + IEntity player = gameSceneController.getPlayer(); + ITile[] tileSet = tileManager.getTileSet(); + int[][] map = tileManager.getTileMap(); + int playerPosX; + int playerPosY; + int mapTile; - public void playSoundEffect(SFXLoop soundeffect){ + while(inputhandler.isMoveDown() || inputhandler.isMoveUp() || inputhandler.isMoveLeft() || inputhandler.isMoveRight()){ + playerPosX = player.getMapPosX(); + playerPosY = player.getMapPosY(); + mapTile = map[playerPosX][playerPosY]; + System.out.println(mapTile); + SFXLoop sfx = tileSet[mapTile].getSoundeffect(); + if(!(sfx == SFXLoop.NONE)) { + Path path = Paths.get(sfx.getPath()); + List<Path> sfxPaths = musicPlayer.getFilePathsFromResources(path); + String randomPath = musicPlayer.getRandomFilePath(sfxPaths); + String resource = musicPlayer.convertPathToResourcePath(randomPath); + Thread sfxLoop = new Thread(() -> { + AudioClip effect = new AudioClip(resource); + effect.setVolume((double) sfxVolume / 100); //TODO Replace with own Method + effect.play(); + }); + sfxLoop.setDaemon(true); + sfxLoop.start(); + sfxLoop.sleep(300); + sfxPaths.clear(); + } + } } + + public void playSoundEffectOnce(SFX soundEffect){ String path = soundEffect.getPath(); String resource = path.substring(18); // Begins at Ressource Folder @@ -28,4 +83,6 @@ public class SoundEffects { private void playSFXFile(String soundEffect){ } + + } diff --git a/src/main/resources/player/appSettings.json b/src/main/resources/player/appSettings.json index ec5bc62a909f2065d81be4cfd6c494b9e10715fb..d505d3363587cdf48cd7f98904e0247ffa321598 100644 --- a/src/main/resources/player/appSettings.json +++ b/src/main/resources/player/appSettings.json @@ -1,4 +1,4 @@ { - "sfxVolume": 50, + "sfxVolume": 52, "musicVolume": 0 } \ No newline at end of file