From cc8c3795b8ffbfbdc0d82f31260503c53155873a Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Tue, 19 Nov 2024 19:27:52 +0100 Subject: [PATCH] added game sound logic --- .../java/pp/monopoly/client/GameMusic.java | 122 ++++++++++++++++++ .../java/pp/monopoly/client/GameSound.java | 118 ++++++++++++++--- 2 files changed, 223 insertions(+), 17 deletions(-) create mode 100644 Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameMusic.java diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameMusic.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameMusic.java new file mode 100644 index 0000000..791b604 --- /dev/null +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameMusic.java @@ -0,0 +1,122 @@ +package pp.monopoly.client; + +import static pp.util.PreferencesUtils.getPreferences; + +import java.lang.System.Logger; +import java.lang.System.Logger.Level; +import java.util.prefs.Preferences; + +import com.jme3.app.Application; +import com.jme3.app.state.AbstractAppState; +import com.jme3.app.state.AppStateManager; +import com.jme3.asset.AssetLoadException; +import com.jme3.asset.AssetNotFoundException; +import com.jme3.audio.AudioData; +import com.jme3.audio.AudioNode; + +/** + * Handles the background music beeing played. Is able to start and stop the music. Set the Volume of the Audio. + */ +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 music; + + /** + * Checks if sound is enabled in the preferences. + * + * @return {@code true} if sound is enabled, {@code false} otherwise. + */ + public static boolean enabledInPreferences() { + return PREFERENCES.getBoolean(ENABLED_PREF, true); + } + + /** + * Checks if sound is enabled in the preferences. + * + * @return float to which the volume is set + */ + public static float volumeInPreferences() { + return PREFERENCES.getFloat(VOLUME_PREF, 0.5f); + } + + /** + * Initializes the sound effects for the game. + * Overrides {@link AbstractAppState#initialize(AppStateManager, Application)} + * + * @param stateManager The state manager + * @param app The application + */ + @Override + public void initialize(AppStateManager stateManager, Application app) { + super.initialize(stateManager, app); + music = loadSound(app, "Sound/background.ogg"); + setVolume(volumeInPreferences()); + music.setLooping(true); + if (isEnabled() && music != null) { + music.play(); + } + } + + /** + * Loads a sound from the specified file. + * + * @param app The application + * @param name The name of the sound file. + * @return The loaded AudioNode. + */ + private AudioNode loadSound(Application app, String name) { + try { + final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer); + sound.setLooping(false); + sound.setPositional(false); + return sound; + } + catch (AssetLoadException | AssetNotFoundException ex) { + LOGGER.log(Level.ERROR, ex.getMessage(), ex); + } + return null; + } + + /** + * Sets the enabled state of this AppState. + * Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)} + * + * @param enabled {@code true} to enable the AppState, {@code false} to disable it. + */ + @Override + public void setEnabled(boolean enabled) { + if (isEnabled() == enabled) return; + + if (music != null) { + if (enabled) { + music.play(); + } else { + music.stop(); + } + } + + super.setEnabled(enabled); + LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS + PREFERENCES.putBoolean(ENABLED_PREF, enabled); + } + + /** + * Toggles the game sound on or off. + */ + public void toggleSound() { + setEnabled(!isEnabled()); + } + + /** + * Sets the volume of music + * @param vol the volume to which the music should be set + */ + public void setVolume(float vol){ + music.setVolume(vol); + PREFERENCES.putFloat(VOLUME_PREF, vol); + } +} diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java index bf5d649..f2bb02a 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java @@ -31,9 +31,17 @@ public class GameSound extends AbstractAppState implements GameEventListener { private static final Preferences PREFERENCES = getPreferences(GameSound.class); private static final String ENABLED_PREF = "enabled"; //NON-NLS - private AudioNode splashSound; - private AudioNode shipDestroyedSound; - private AudioNode explosionSound; + private AudioNode passStartSound; + private AudioNode eventCardSound; + private AudioNode gulagSound; + private AudioNode diceRollSound; + private AudioNode moneyCollectSound; + private AudioNode moneyLostSound; + private AudioNode tradeAcceptedSound; + private AudioNode tradeRejectedSound; + private AudioNode winnerSound; + private AudioNode looserSound; + private AudioNode buttonSound; /** * Checks if sound is enabled in the preferences. @@ -75,6 +83,17 @@ public class GameSound extends AbstractAppState implements GameEventListener { @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); + passStartSound = loadSound(app, "Sound/Effects/passStart.ogg"); + eventCardSound = loadSound(app, "Sound/Effects/eventCard.ogg"); + gulagSound = loadSound(app, "Sound/Effects/gulag.ogg"); + diceRollSound = loadSound(app, "Sound/Effects/diceRoll.ogg"); + moneyCollectSound = loadSound(app, "Sound/Effects/moneyCollect.ogg"); + moneyLostSound = loadSound(app, "Sound/Effects/moneyLost.ogg"); + tradeAcceptedSound = loadSound(app, "Sound/Effects/tradeAccepted.ogg"); + tradeRejectedSound = loadSound(app, "Sound/Effects/tradeRejected.ogg"); + winnerSound = loadSound(app, "Sound/Effects/winner.ogg"); + looserSound = loadSound(app, "Sound/Effects/looser.ogg"); + buttonSound = loadSound(app, "Sound/Effects/button.ogg"); } /** @@ -98,32 +117,97 @@ public class GameSound extends AbstractAppState implements GameEventListener { } /** - * Plays the splash sound effect. + * Plays the passStart sound effect. */ - public void splash() { - if (isEnabled() && splashSound != null) - splashSound.playInstance(); + public void passStart() { + if (isEnabled() && passStartSound != null) + passStartSound.playInstance(); } - /** - * Plays the explosion sound effect. + * Plays the eventCard sound effect. */ - public void explosion() { - if (isEnabled() && explosionSound != null) - explosionSound.playInstance(); + public void eventCard() { + if (isEnabled() && eventCardSound != null) + eventCardSound.playInstance(); } - /** - * Plays sound effect when a ship has been destroyed. + * Plays the gulag sound effect. */ - public void shipDestroyed() { - if (isEnabled() && shipDestroyedSound != null) - shipDestroyedSound.playInstance(); + public void gulag() { + if (isEnabled() && gulagSound != null) + gulagSound.playInstance(); + } + /** + * Plays the diceRoll sound effect. + */ + public void diceRoll() { + if (isEnabled() && diceRollSound != null) + diceRollSound.playInstance(); + } + /** + * Plays the moneyCollect sound effect. + */ + public void moneyCollect() { + if (isEnabled() && moneyCollectSound != null) + moneyCollectSound.playInstance(); + } + /** + * Plays the moneyLost sound effect. + */ + public void moneyLost() { + if (isEnabled() && moneyLostSound != null) + moneyLostSound.playInstance(); + } + /** + * Plays the tradeAccepted sound effect. + */ + public void tradeAccepted() { + if (isEnabled() && tradeAcceptedSound != null) + tradeAcceptedSound.playInstance(); + } + /** + * Plays the tradeRejected sound effect. + */ + public void tradeRejected() { + if (isEnabled() && tradeRejectedSound != null) + tradeRejectedSound.playInstance(); + } + /** + * Plays the winner sound effect. + */ + public void winner() { + if (isEnabled() && winnerSound != null) + winnerSound.playInstance(); + } + /** + * Plays the looser sound effect. + */ + public void looser() { + if (isEnabled() && looserSound != null) + looserSound.playInstance(); + } + /** + * Plays the button sound effect. + */ + public void button() { + if (isEnabled() && buttonSound != null) + buttonSound.playInstance(); } @Override public void receivedEvent(SoundEvent event) { switch (event.sound()) { + case PASS_START -> passStart(); + case EVENT_CARD -> eventCard(); + case GULAG -> eventCard(); + case DICE_ROLL -> eventCard(); + case MONEY_COLLECTED -> eventCard(); + case MONEY_LOST -> eventCard(); + case TRADE_ACCEPTED -> eventCard(); + case TRADE_REJECTED -> eventCard(); + case WINNER -> eventCard(); + case LOSER -> eventCard(); + case BUTTON -> eventCard(); } } }