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 index 6483174..c37e260 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameMusic.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameMusic.java @@ -15,49 +15,34 @@ import com.jme3.audio.AudioData; import com.jme3.audio.AudioNode; /** - * Handles the background music being played. Is able to start and stop the music. Set the Volume of the Audio. + * Handles the background and secondary music in the game. + * Allows playing, stopping, and toggling between background music and a secondary track. */ -public class GameMusic extends AbstractAppState{ +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 static final String ENABLED_PREF = "enabled"; // NON-NLS + private static final String VOLUME_PREF = "volume"; // NON-NLS - private AudioNode music; + private AudioNode mainMusic; + private AudioNode secondaryMusic; + private boolean isMainMusicPlaying = false; + private boolean isSecondaryMusicPlaying = false; /** - * 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)} + * Initializes the GameMusic app state and loads the background music. * * @param stateManager The state manager - * @param app The application + * @param app The application instance */ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); - music = loadSound(app, "Sound/background.ogg"); + mainMusic = loadSound(app, "Sound/background.ogg"); + secondaryMusic = loadSound(app, "Sound/ChooseYourCharakter.ogg"); setVolume(volumeInPreferences()); - music.setLooping(true); - if (isEnabled() && music != null) { - music.play(); + if (isEnabled()) { + playMainMusic(); } } @@ -71,7 +56,7 @@ public class GameMusic extends AbstractAppState{ private AudioNode loadSound(Application app, String name) { try { final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer); - sound.setLooping(false); + sound.setLooping(true); sound.setPositional(false); return sound; } @@ -82,41 +67,120 @@ public class GameMusic extends AbstractAppState{ } /** - * Sets the enabled state of this AppState. - * Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)} + * Plays the main music. + */ + public void playMainMusic() { + if (mainMusic != null && !isMainMusicPlaying) { + mainMusic.play(); + isMainMusicPlaying = true; + } + } + + /** + * Stops the main music. + */ + public void stopMainMusic() { + if (mainMusic != null && isMainMusicPlaying) { + mainMusic.stop(); + isMainMusicPlaying = false; + } + } + + /** + * Plays the secondary music and stops the main music. * - * @param enabled {@code true} to enable the AppState, {@code false} to disable it. + * @param app The application instance + * @param secondaryMusicFile The file path of the secondary audio file + */ + public void playSecondaryMusic() { + if (isSecondaryMusicPlaying) { + return; // Secondary music is already playing + } + + stopMainMusic(); + + if (secondaryMusic != null) { + secondaryMusic.setVolume(volumeInPreferences()); + secondaryMusic.play(); + isSecondaryMusicPlaying = true; + } + } + + /** + * Stops the secondary music. + */ + public void stopSecondaryMusic() { + if (secondaryMusic != null && isSecondaryMusicPlaying) { + secondaryMusic.stop(); + isSecondaryMusicPlaying = false; + } + } + + /** + * Toggles between the background music and the secondary track. + * If the secondary track is playing, it stops and resumes the background music. + * If the background music is playing, it pauses and plays the secondary track. + * + * @param app The application instance + * @param secondaryMusicFile The file path of the secondary audio file + */ + public void toggleMusic() { + if (isSecondaryMusicPlaying) { + stopSecondaryMusic(); + playMainMusic(); + } else { + playSecondaryMusic(); + } + } + + /** + * Sets the audio volume for both the main and secondary tracks. + * + * @param vol The volume level (0.0f to 1.0f) + */ + public void setVolume(float vol) { + if (mainMusic != null) mainMusic.setVolume(vol); + if (secondaryMusic != null) secondaryMusic.setVolume(vol); + PREFERENCES.putFloat(VOLUME_PREF, vol); + } + + /** + * Enables or disables the sound system. + * When disabled, all music stops. + * + * @param enabled {@code true} to enable, {@code false} to disable */ @Override public void setEnabled(boolean enabled) { if (isEnabled() == enabled) return; - if (music != null) { - if (enabled) { - music.play(); - } else { - music.stop(); - } + if (enabled) { + playMainMusic(); + } else { + stopMainMusic(); + stopSecondaryMusic(); } - + super.setEnabled(enabled); - LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS + LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); // NON-NLS PREFERENCES.putBoolean(ENABLED_PREF, enabled); } /** - * Toggles the game sound on or off. + * Retrieves the current sound volume preference. + * + * @return The volume level (0.0f to 1.0f) */ - public void toggleSound() { - setEnabled(!isEnabled()); + public static float volumeInPreferences() { + return PREFERENCES.getFloat(VOLUME_PREF, 0.5f); } /** - * Sets the volume of music - * @param vol the volume to which the music should be set + * Checks if sound is enabled in the preferences. + * + * @return {@code true} if sound is enabled, {@code false} otherwise */ - public void setVolume(float vol){ - music.setVolume(vol); - PREFERENCES.putFloat(VOLUME_PREF, vol); + public static boolean enabledInPreferences() { + return PREFERENCES.getBoolean(ENABLED_PREF, true); } } diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/LobbyMenu.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/LobbyMenu.java index 16640fe..174a5b4 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/LobbyMenu.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/LobbyMenu.java @@ -1,7 +1,5 @@ package pp.monopoly.client.gui; -import com.jme3.app.Application; -import com.jme3.app.state.BaseAppState; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; @@ -24,6 +22,7 @@ import com.simsilica.lemur.core.VersionedReference; import com.simsilica.lemur.style.ElementId; import pp.dialog.Dialog; +import pp.monopoly.client.GameMusic; import pp.monopoly.client.MonopolyApp; import pp.monopoly.game.server.Player; import pp.monopoly.message.client.PlayerReady; @@ -79,6 +78,9 @@ public class LobbyMenu extends Dialog { super(app.getDialogManager()); this.app = app; + GameMusic music = app.getStateManager().getState(GameMusic.class); + music.toggleMusic(); + playerInputField = new TextField("Spieler "+(app.getId()+1)); // Hintergrundbild laden und hinzufügen addBackgroundImage(); @@ -194,6 +196,7 @@ public class LobbyMenu extends Dialog { readyButton.setFontSize(18); // Adjust font size readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.Green)); // Add color to match the style readyButton.addClickCommands(s -> ifTopDialog(() -> { + music.toggleMusic(); toggleReady(); app.getGameLogic().playSound(Sound.BUTTON); readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.DarkGray)); diff --git a/Projekte/monopoly/client/src/main/resources/Sound/ChooseYourCharakter.ogg b/Projekte/monopoly/client/src/main/resources/Sound/ChooseYourCharakter.ogg new file mode 100644 index 0000000..be3ba94 Binary files /dev/null and b/Projekte/monopoly/client/src/main/resources/Sound/ChooseYourCharakter.ogg differ