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..8a6a91e 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,7 @@ 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 +282,16 @@ 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. diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameMusic.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameMusic.java new file mode 100644 index 0000000..672e030 --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameMusic.java @@ -0,0 +1,111 @@ +package pp.battleship.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; + +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. + * + * + */ + 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/DasBootMenu.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; + else if(!isEnabled() && enabled) { + if (music != null) music.play(); + } else if (isEnabled() && !enabled) { + if (music != null) 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()); + } + + public void setVolume(float vol){ + music.setVolume(vol); + PREFERENCES.putFloat(VOLUME_PREF, vol); + } +} \ No newline at end of file diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java index 0e100e8..82c1055 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java @@ -15,6 +15,9 @@ import pp.dialog.Dialog; import pp.dialog.StateCheckboxModel; import pp.dialog.TextInputDialog; +import pp.battleship.client.GameMusic; +import pp.battleship.client.VolumeSlider; + import java.io.File; import java.io.IOException; import java.util.prefs.Preferences; @@ -33,6 +36,7 @@ class Menu extends Dialog { private final BattleshipApp app; private final Button loadButton = new Button(lookup("menu.map.load")); private final Button saveButton = new Button(lookup("menu.map.save")); + private final VolumeSlider slider; /** * Constructs the Menu dialog for the Battleship application. @@ -42,9 +46,14 @@ class Menu extends Dialog { public Menu(BattleshipApp app) { super(app.getDialogManager()); this.app = app; + slider = new VolumeSlider(app.getStateManager().getState(GameMusic.class)); addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS - addChild(new Checkbox(lookup("menu.sound-enabled"), - new StateCheckboxModel(app, GameSound.class))); + addChild(new Checkbox(lookup("menu.sound-enabled"), new StateCheckboxModel(app, GameSound.class))); + + addChild(new Checkbox(lookup("menu.background-sound-enabled"), new StateCheckboxModel(app, GameMusic.class))); + + addChild(slider); + addChild(loadButton) .addClickCommands(s -> ifTopDialog(this::loadDialog)); addChild(saveButton) @@ -140,4 +149,13 @@ class Menu extends Dialog { private void saveDialog() { fileDialog(app.getGameLogic()::saveMap, lookup("menu.map.save")); } + + /** + * Updates the position of the volume control slider. + */ + + @Override + public void update(float delta) { + slider.update(); + } } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/VolumeSlider.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/VolumeSlider.java new file mode 100644 index 0000000..2938b65 --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/VolumeSlider.java @@ -0,0 +1,24 @@ +package pp.battleship.client; + +import com.simsilica.lemur.Slider; + +public class VolumeSlider extends Slider { + + private final GameMusic music; + + private double vol; + + public VolumeSlider(GameMusic music) { + super(); + this.music = music; + vol = GameMusic.volumeInPreferences(); + getModel().setPercent(vol); + } + + public void update() { + if (vol != getModel().getPercent()) { + vol = getModel().getPercent(); + music.setVolume( (float) vol); + } + } +} \ No newline at end of file diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java index 3058a6b..51af2ad 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java @@ -152,10 +152,10 @@ class SeaSynchronizer extends ShipMapSynchronizer { return createLargeship(ship); case 2: - return createMediumship(ship); + return createMediumship(ship); case 1: - return createSmallship(ship); + return createSmallship(ship); default: return createBox(ship); diff --git a/Projekte/battleship/client/src/main/resources/Sound/DasBootMenu.ogg b/Projekte/battleship/client/src/main/resources/Sound/DasBootMenu.ogg new file mode 100644 index 0000000..28cccf4 Binary files /dev/null and b/Projekte/battleship/client/src/main/resources/Sound/DasBootMenu.ogg differ diff --git a/Projekte/battleship/model/src/main/resources/battleship.properties b/Projekte/battleship/model/src/main/resources/battleship.properties index 47f1f6c..3cfd79b 100644 --- a/Projekte/battleship/model/src/main/resources/battleship.properties +++ b/Projekte/battleship/model/src/main/resources/battleship.properties @@ -29,6 +29,7 @@ wait.its.not.your.turn=Wait, it's not your turn!! menu.quit=Quit game menu.return-to-game=Return to game menu.sound-enabled=Sound switched on +menu.background-sound-enabled=Background music switched on menu.map.load=Load map from file... menu.map.save=Save map in file... label.file=File: diff --git a/Projekte/battleship/model/src/main/resources/battleship_de.properties b/Projekte/battleship/model/src/main/resources/battleship_de.properties index 4039420..7526773 100644 --- a/Projekte/battleship/model/src/main/resources/battleship_de.properties +++ b/Projekte/battleship/model/src/main/resources/battleship_de.properties @@ -29,6 +29,7 @@ wait.its.not.your.turn=Warte, Du bist nicht dran!! menu.quit=Spiel beenden menu.return-to-game=Zurück zum Spiel menu.sound-enabled=Sound eingeschaltet +menu.background-sound-enabled=Hintergrundmusik eingeschaltet menu.map.load=Karte von Datei laden... menu.map.save=Karte in Datei speichern... label.file=Datei: