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..7eef71c2 --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java @@ -0,0 +1,92 @@ +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_PREV = "BackgroundVolume"; + private static final String BACKGROUND_MUSIC_ENABLED_PREV = "BackgroundMusicEnabled"; + private Preferences prefs = Preferences.userNodeForPackage(BackgroundMusic.class); + + private final AudioNode backgroundMusic; + private boolean backgroundMusicEnabled; + + private float backgroundVolume; + + public BackgroundMusic(Application app, String backGroundMusicPath) { + this.backgroundVolume = prefs.getFloat(VOLUME_PREV, 1.0f); + this.backgroundMusicEnabled = prefs.getBoolean(BACKGROUND_MUSIC_ENABLED_PREV, true); + + backgroundMusic = new AudioNode(app.getAssetManager(), backGroundMusicPath, DataType.Stream); + backgroundMusic.setLooping(true); + backgroundMusic.setPositional(false); + backgroundMusic.setVolume(backgroundVolume); + + if (backgroundMusicEnabled) { + play(); + } + } + + /** + * this method plays the background sound + */ + public void play() { + if (backgroundMusicEnabled && (backgroundMusic.getStatus() == Status.Stopped || backgroundMusic.getStatus() == Status.Paused)) { + backgroundMusic.play(); + } + } + + /** + * this method stops the background-music + */ + 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 toggleMusic() { + this.backgroundMusicEnabled = !this.backgroundMusicEnabled; + if (backgroundMusicEnabled) { + play(); + } + else { + stop(); + } + prefs.putBoolean(VOLUME_PREV, backgroundMusicEnabled); + } + + /** + * return, if the background-music is enabled + * + * @return true if the music is enabled + */ + public boolean isBackgroundMusicEnabled() { + return backgroundMusicEnabled; + } + + /** + * this method sets the + * + * @param volume is the volume the music is set to + */ + public void setBackgroundVolume(float volume) { + this.backgroundVolume = volume; + backgroundMusic.setVolume(backgroundVolume); + prefs.putFloat(VOLUME_PREV, volume); + } + + /** + * this method returns the BackgroundVolume + * + * @return backgroundVolume + */ + public float getBackgroundVolume() { + return backgroundVolume; + } +} 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..10a666ab 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 @@ -122,6 +122,8 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient */ private final ActionListener escapeListener = (name, isPressed, tpf) -> escape(isPressed); + private BackgroundMusic backgroundMusic; + static { // Configure logging LogManager manager = LogManager.getLogManager(); @@ -225,6 +227,7 @@ public void simpleInitApp() { setupStates(); setupGui(); serverConnection.connect(); + backgroundMusic = new BackgroundMusic(this, "Sound/Background/backgroundMusic.ogg"); } /** @@ -426,4 +429,12 @@ void errorDialog(String errorMessage) { .build() .open(); } + + /** + * this method returns Background-Music + * @return background-music + */ + public BackgroundMusic getBackgroundMusic(){ + return backgroundMusic; + } } 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 9eca93cc..fea606f2 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 @@ -9,8 +9,10 @@ import com.simsilica.lemur.Button; import com.simsilica.lemur.Checkbox; +import com.simsilica.lemur.DefaultRangedValueModel; import com.simsilica.lemur.Label; import com.simsilica.lemur.Slider; +import com.simsilica.lemur.core.VersionedReference; import com.simsilica.lemur.style.ElementId; import pp.dialog.Dialog; import pp.dialog.StateCheckboxModel; @@ -35,6 +37,8 @@ class Menu extends Dialog { 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. @@ -47,9 +51,19 @@ 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))); - addChild(new Checkbox(lookup("background.music.checkbox"))); - addChild(new Label(lookup("background.music.volume"))); - addChild(new Slider(lookup("background.music.volume"))); + + Checkbox backgroundMusicEnabled = new Checkbox(lookup("background.music.checkbox")); + backgroundMusicEnabled.setChecked(app.getBackgroundMusic().isBackgroundMusicEnabled()); + backgroundMusicEnabled.addClickCommands(s -> app.getBackgroundMusic().toggleMusic()); + addChild(backgroundMusicEnabled); + + Slider volumeSlider = new Slider(lookup("background.music.volume")); + volumeSlider.setModel(new DefaultRangedValueModel(0.0,2.0,app.getBackgroundMusic().getBackgroundVolume())); + volumeSlider.setDelta(0.1); + addChild(volumeSlider); + + volumeRef = volumeSlider.getModel().createReference(); + addChild(loadButton) .addClickCommands(s -> ifTopDialog(this::loadDialog)); addChild(saveButton) @@ -62,6 +76,25 @@ public Menu(BattleshipApp app) { update(); } + /** + * updates the background-volume + * @param tmp + */ + @Override + public void update(float tmp){ + if(volumeRef.update()) { + adjustVolume(volumeRef.get()); + } + } + + private void adjustVolume(double newVolume) { + app.getBackgroundMusic().setBackgroundVolume((float) newVolume); + } + + + + + /** * Updates the state of the load and save buttons based on the game logic. */ 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 0316cb54..e4b1c9d3 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 @@ -30,7 +30,7 @@ import static pp.util.FloatMath.HALF_PI; import static pp.util.FloatMath.PI; -//TODO models + /** * The {@code SeaSynchronizer} class is responsible for synchronizing the graphical * representation of the ships and shots on the sea map with the underlying data model. diff --git a/Projekte/battleship/client/src/main/resources/Sound/Background/backgroundMusic.ogg b/Projekte/battleship/client/src/main/resources/Sound/Background/backgroundMusic.ogg new file mode 100644 index 00000000..a4a55af0 Binary files /dev/null and b/Projekte/battleship/client/src/main/resources/Sound/Background/backgroundMusic.ogg differ diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/notification/Sound.java b/Projekte/battleship/model/src/main/java/pp/battleship/notification/Sound.java index ce0d6e8f..d82ab698 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/notification/Sound.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/notification/Sound.java @@ -22,10 +22,5 @@ public enum Sound { /** * Sound of a ship being destroyed. */ - DESTROYED_SHIP, - - /** - * - */ - BACKGROUND_MUSIC //TODO + DESTROYED_SHIP }