diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java new file mode 100644 index 00000000..adf4b576 --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java @@ -0,0 +1,95 @@ +package pp.battleship.client; + +import com.jme3.app.Application; +import com.jme3.audio.AudioData.DataType; +import com.jme3.audio.AudioNode; +import com.jme3.audio.AudioSource.Status; + +import java.util.prefs.Preferences; + +public class BackgroundMusic { + + private static final String VOLUME_PREF = "volume"; + private static final String MUSIC_ENABLED_PREF = "musicEnabled"; + private Preferences prefs = Preferences.userNodeForPackage(BackgroundMusic.class); + + private final AudioNode backgroundMusic; + private boolean musicEnabled; + private float volume; + + /** + * Constuctor for the BackgroundMusic + * @param app + * @param musicFilePath + */ + public BackgroundMusic(Application app, String musicFilePath) { + this.volume = prefs.getFloat(VOLUME_PREF, 1.0f); + this.musicEnabled = prefs.getBoolean(MUSIC_ENABLED_PREF, true); + + backgroundMusic = new AudioNode(app.getAssetManager(), musicFilePath, DataType.Stream); + backgroundMusic.setLooping(true); + backgroundMusic.setPositional(false); + backgroundMusic.setVolume(1.0f); + + if (musicEnabled) { + play(); + } + } + + /** + * Checks if the condition is met to play BackgroundMusic + */ + public void play() { + if (musicEnabled && (backgroundMusic.getStatus() == Status.Stopped || backgroundMusic.getStatus() == Status.Paused)) { + backgroundMusic.play(); + } + } + + /** + * function to stop the BackgroundMusic + */ + public void stop() { + if (backgroundMusic.getStatus() == Status.Playing) { + backgroundMusic.stop(); + } + } + + /** + * function to toggle the Backgroundmusic also sets the volume to the previous session + */ + public void toogleMusic() { + this.musicEnabled = !this.musicEnabled; + if (musicEnabled) { + play(); + } else { + stop(); + } + prefs.putFloat(VOLUME_PREF, volume); + } + + /** + * Setter for the Volume also safes the volume from the previous session + * @param volume + */ + public void setVolume(float volume) { + this.volume = volume; + backgroundMusic.setVolume(volume); + prefs.putFloat(VOLUME_PREF, volume); + } + + /** + * Getter for Volume + * @return a float value of the volume + */ + public float getVolume() { + return volume; + } + + /** + * Getter for musicEnabled + * @return if the music is enabled return true, false otherwise + */ + public boolean isMusicEnabled() { + return musicEnabled; + } +} \ No newline at end of file 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 6ac1c729..5d6559cc 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 @@ -121,6 +121,10 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient * Listener for handling actions triggered by the Escape key. */ private final ActionListener escapeListener = (name, isPressed, tpf) -> escape(isPressed); + /** + * + */ + private BackgroundMusic backgroundMusic; static { // Configure logging @@ -225,6 +229,7 @@ public void simpleInitApp() { setupStates(); setupGui(); serverConnection.connect(); + backgroundMusic = new BackgroundMusic(this, "Sound/Effects/BackgroundMusic/boss_battle_#2_metal_opening.wav"); } /** @@ -426,4 +431,12 @@ void errorDialog(String errorMessage) { .build() .open(); } + + /** + * Getter for the BackgroundMusic + * @return the BackgroundMusic + */ + public BackgroundMusic getBackgroundMusic(){ + return backgroundMusic; + } } 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 0fdcef68..e7215002 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 @@ -124,6 +124,10 @@ public void shipDestroyed() { shipDestroyedSound.playInstance(); } + /** + * Checks the according case for the soundeffect + * @param event the received event + */ @Override public void receivedEvent(SoundEvent event) { switch (event.sound()) { 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 0e100e86..e49b193b 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 @@ -14,6 +14,10 @@ import pp.dialog.Dialog; import pp.dialog.StateCheckboxModel; import pp.dialog.TextInputDialog; +import com.simsilica.lemur.DefaultRangedValueModel; +import com.simsilica.lemur.Slider; +import com.simsilica.lemur.core.VersionedReference; + import java.io.File; import java.io.IOException; @@ -33,6 +37,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 VersionedReference volumeRef; /** * Constructs the Menu dialog for the Battleship application. @@ -45,6 +50,20 @@ public Menu(BattleshipApp app) { addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS addChild(new Checkbox(lookup("menu.sound-enabled"), new StateCheckboxModel(app, GameSound.class))); + + Checkbox musicToggle = new Checkbox(lookup("menu.music.toggle")); + musicToggle.setChecked(app.getBackgroundMusic().isMusicEnabled()); + musicToggle.addClickCommands(s -> toggleMusic()); + + addChild(musicToggle); + + Slider volumeSlider = new Slider(); + volumeSlider.setModel(new DefaultRangedValueModel(0.0 , 2.0, app.getBackgroundMusic().getVolume())); + volumeSlider.setDelta(0.1); + addChild(volumeSlider); + + volumeRef = volumeSlider.getModel().createReference(); + addChild(loadButton) .addClickCommands(s -> ifTopDialog(this::loadDialog)); addChild(saveButton) @@ -56,6 +75,21 @@ public Menu(BattleshipApp app) { update(); } + public void update(float tmp){ + if(volumeRef.update()) { + double newVolume = volumeRef.get(); + adjustVolume(newVolume); + } + } + + private void adjustVolume(double newVolume) { + app.getBackgroundMusic().setVolume((float) newVolume); + } + + private void toggleMusic(){ + app.getBackgroundMusic().toogleMusic(); + } + /** * Updates the state of the load and save buttons based on the game logic. */ diff --git a/Projekte/battleship/model/src/main/resources/battleship.properties b/Projekte/battleship/model/src/main/resources/battleship.properties index 47f1f6cf..fac90826 100644 --- a/Projekte/battleship/model/src/main/resources/battleship.properties +++ b/Projekte/battleship/model/src/main/resources/battleship.properties @@ -17,6 +17,7 @@ wait.for.opponent=Wait for your opponent! confirm.leaving=Would you really like to leave the game? you.lost.the.game=You lost the game! you.won.the.game=You won the game! +menu.music.toggle=Musik button.yes=Yes button.no=No button.ok=Ok diff --git a/Projekte/battleship/model/src/main/resources/battleship_de.properties b/Projekte/battleship/model/src/main/resources/battleship_de.properties index 40394202..51732c83 100644 --- a/Projekte/battleship/model/src/main/resources/battleship_de.properties +++ b/Projekte/battleship/model/src/main/resources/battleship_de.properties @@ -17,6 +17,7 @@ wait.for.opponent=Warte auf Deinen Gegner! confirm.leaving=Willst Du wirklich das Spiel verlassen? you.lost.the.game=Leider verloren! you.won.the.game=Du hast gewonnen! +menu.music.toggle=Hintergrund Musik button.yes=Ja button.no=Nein button.ok=Ok @@ -28,7 +29,7 @@ port.number=Port 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.sound-enabled=Sound menu.map.load=Karte von Datei laden... menu.map.save=Karte in Datei speichern... label.file=Datei: