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..a750963 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 @@ -25,6 +25,7 @@ import pp.battleship.client.gui.EditorAppState; import pp.battleship.client.gui.SeaAppState; import pp.battleship.game.client.BattleshipClient; import pp.battleship.game.client.ClientGameLogic; +import pp.battleship.game.client.GameMusic; import pp.battleship.game.client.ServerConnection; import pp.battleship.game.singlemode.BattleshipClientConfig; import pp.battleship.game.singlemode.ServerConnectionMockup; @@ -265,13 +266,21 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient flyCam.setEnabled(false); stateManager.detach(stateManager.getState(StatsAppState.class)); stateManager.detach(stateManager.getState(DebugKeysAppState.class)); + atttachGameMusic(); attachGameSound(); stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState()); } + private void attachGameMusic() { + final GameMusic gameMusic = new GameMusic(); + gameMusic.setEnabled(GameMusic.enabledInPreferences()); + stateManager.attach(gameMusic); + } + /** * Attaches the game sound state and sets its initial enabled state. + * */ private void attachGameSound() { final GameSound gameSound = new GameSound(); 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..4b64cf2 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 @@ -11,6 +11,7 @@ import com.simsilica.lemur.Button; import com.simsilica.lemur.Checkbox; import com.simsilica.lemur.Label; import com.simsilica.lemur.style.ElementId; +import pp.battleship.game.client.GameMusic; import pp.dialog.Dialog; import pp.dialog.StateCheckboxModel; import pp.dialog.TextInputDialog; @@ -30,6 +31,8 @@ import static pp.util.PreferencesUtils.getPreferences; class Menu extends Dialog { private static final Preferences PREFERENCES = getPreferences(Menu.class); private static final String LAST_PATH = "last.file.path"; + + private final VolumeSlider slider; private final BattleshipApp app; private final Button loadButton = new Button(lookup("menu.map.load")); private final Button saveButton = new Button(lookup("menu.map.save")); @@ -45,6 +48,7 @@ class Menu extends Dialog { addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS addChild(new Checkbox(lookup("menu.sound-enabled"), new StateCheckboxModel(app, GameSound.class))); + slider = new VolumeSlider(app.getStateManager().getState(GameMusic.class)); addChild(loadButton) .addClickCommands(s -> ifTopDialog(this::loadDialog)); addChild(saveButton) @@ -54,6 +58,12 @@ class Menu extends Dialog { addChild(new Button(lookup("menu.quit"))) .addClickCommands(s -> ifTopDialog(app::closeApp)); update(); + 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); + } /** @@ -64,6 +74,10 @@ class Menu extends Dialog { loadButton.setEnabled(app.getGameLogic().mayLoadMap()); saveButton.setEnabled(app.getGameLogic().maySaveMap()); } + @Override + public void update(float delta){ + slider.update(); + } /** * As an escape action, this method closes the menu if it is the top dialog. diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/GameMusic.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/GameMusic.java new file mode 100644 index 0000000..dd89122 --- /dev/null +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/GameMusic.java @@ -0,0 +1,115 @@ +package pp.battleship.game.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); + } +} + diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/VolumeSlider.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/VolumeSlider.java new file mode 100644 index 0000000..7b80471 --- /dev/null +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/VolumeSlider.java @@ -0,0 +1,26 @@ +package pp.battleship.game.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); + } + } + +} + +}