From cd5035818145be5e0f29a44f0f0d25cb79eb49c6 Mon Sep 17 00:00:00 2001 From: Filip Szepielewicz Date: Fri, 11 Oct 2024 23:39:09 +0000 Subject: [PATCH] Update 2 files - /Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java - /Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java --- .../pp/battleship/client/BattleshipApp.java | 13 +++ .../java/pp/battleship/client/GameSound.java | 98 +++++++++---------- 2 files changed, 61 insertions(+), 50 deletions(-) diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java index 6ac1c72..1f04c63 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java @@ -23,6 +23,7 @@ import com.simsilica.lemur.style.BaseStyles; import pp.battleship.client.gui.BattleAppState; import pp.battleship.client.gui.EditorAppState; import pp.battleship.client.gui.SeaAppState; +import pp.battleship.client.GameMusic; import pp.battleship.game.client.BattleshipClient; import pp.battleship.game.client.ClientGameLogic; import pp.battleship.game.client.ServerConnection; @@ -267,6 +268,8 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient stateManager.detach(stateManager.getState(DebugKeysAppState.class)); attachGameSound(); + attachGameMusic(); + stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState()); } @@ -280,6 +283,15 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient stateManager.attach(gameSound); } + /** + * Attaches the music state and sets its initial enabled state. + */ + private void attachGameMusic() { + final GameMusic gameMusic = new GameMusic(); + gameMusic.setEnabled(GameMusic.enabledInPreferences()); + stateManager.attach(gameMusic); + } + /** * Updates the application state every frame. * This method is called once per frame during the game loop. @@ -427,3 +439,4 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient .open(); } } + diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java index 0fdcef6..574c97a 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java @@ -1,12 +1,6 @@ -//////////////////////////////////////// -// Programming project code -// UniBw M, 2022, 2023, 2024 -// www.unibw.de/inf2 -// (c) Mark Minas (mark.minas@unibw.de) -//////////////////////////////////////// - package pp.battleship.client; + import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; @@ -14,8 +8,7 @@ import com.jme3.asset.AssetLoadException; import com.jme3.asset.AssetNotFoundException; import com.jme3.audio.AudioData; import com.jme3.audio.AudioNode; -import pp.battleship.notification.GameEventListener; -import pp.battleship.notification.SoundEvent; + import java.lang.System.Logger; import java.lang.System.Logger.Level; @@ -24,16 +17,17 @@ import java.util.prefs.Preferences; import static pp.util.PreferencesUtils.getPreferences; /** - * An application state that plays sounds. + * An application state that plays music. */ -public class GameSound extends AbstractAppState implements GameEventListener { - private static final Logger LOGGER = System.getLogger(GameSound.class.getName()); - private static final Preferences PREFERENCES = getPreferences(GameSound.class); +public class GameMusic extends AbstractAppState { + private static final Logger LOGGER = System.getLogger(GameMusic.class.getName()); + private static final Preferences PREFERENCES = getPreferences(GameMusic.class); private static final String ENABLED_PREF = "enabled"; //NON-NLS + private static final String VOLUME_PREF = "volume"; //NON-NLS - private AudioNode splashSound; - private AudioNode shipDestroyedSound; - private AudioNode explosionSound; + private AudioNode battleMusic; + private AudioNode menuMusicModern; + private AudioNode menuMusicTraditional; /** * Checks if sound is enabled in the preferences. @@ -43,6 +37,17 @@ public class GameSound extends AbstractAppState implements GameEventListener { public static boolean enabledInPreferences() { return PREFERENCES.getBoolean(ENABLED_PREF, true); } + /** + * Checks enabled volume in preferences. + * + * @return {@code float} if a volumePreference is set, the volume is set to the Value in PREFERENCES, + * {@code 0.25f} if no volumePreference is set in PREFERENCES, the Volume is set to a default of 0.25f + * + */ + + public static float volumePreference() { + return PREFERENCES.getFloat(VOLUME_PREF, 0.25f); + } /** * Toggles the game sound on or off. @@ -60,13 +65,19 @@ public class GameSound extends AbstractAppState implements GameEventListener { @Override public void setEnabled(boolean enabled) { if (isEnabled() == enabled) return; + else if (!isEnabled() && enabled) { + if (menuMusicModern != null) menuMusicModern.play(); + } else if (isEnabled() && !enabled) { + if (menuMusicModern != null) menuMusicModern.stop(); + } + super.setEnabled(enabled); - LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS + LOGGER.log(Level.INFO, "Music enabled: {0}", enabled); //NON-NLS PREFERENCES.putBoolean(ENABLED_PREF, enabled); } /** - * Initializes the sound effects for the game. + * Initializes the music. * Overrides {@link AbstractAppState#initialize(AppStateManager, Application)} * * @param stateManager The state manager @@ -75,13 +86,17 @@ public class GameSound extends AbstractAppState implements GameEventListener { @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); - shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS - splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS - explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS + menuMusicModern =loadSound(app, "Sound/BackgroundMusic/menu-music-modern.ogg"); + setVolume(volumePreference()); + menuMusicModern.setLooping(true); + if (isEnabled() && menuMusicModern != null) { + menuMusicModern.play(); + } + } /** - * Loads a sound from the specified file. + * Loads the music from the specified file. * * @param app The application * @param name The name of the sound file. @@ -101,35 +116,18 @@ public class GameSound extends AbstractAppState implements GameEventListener { } /** - * Plays the splash sound effect. + * Sets the vol param to the level set in PREFERENCES + * + * @param vol Volume level of the music as indicated by the Volume control Slider + * */ - public void splash() { - if (isEnabled() && splashSound != null) - splashSound.playInstance(); + public void setVolume(float vol){ + menuMusicModern.setVolume(vol); + PREFERENCES.putFloat(VOLUME_PREF, vol); } - /** - * Plays the explosion sound effect. - */ - public void explosion() { - if (isEnabled() && explosionSound != null) - explosionSound.playInstance(); - } - - /** - * Plays sound effect when a ship has been destroyed. - */ - public void shipDestroyed() { - if (isEnabled() && shipDestroyedSound != null) - shipDestroyedSound.playInstance(); - } - - @Override - public void receivedEvent(SoundEvent event) { - switch (event.sound()) { - case EXPLOSION -> explosion(); - case SPLASH -> splash(); - case DESTROYED_SHIP -> shipDestroyed(); - } - } } + + + +