mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-24 23:59:44 +01:00
added background music
This commit is contained in:
parent
4cf14d02ee
commit
133921cfbb
@ -22,6 +22,7 @@ import com.simsilica.lemur.GuiGlobals;
|
||||
import com.simsilica.lemur.style.BaseStyles;
|
||||
import pp.battleship.client.gui.BattleAppState;
|
||||
import pp.battleship.client.gui.EditorAppState;
|
||||
import pp.battleship.client.gui.GameMusic;
|
||||
import pp.battleship.client.gui.SeaAppState;
|
||||
import pp.battleship.game.client.BattleshipClient;
|
||||
import pp.battleship.game.client.ClientGameLogic;
|
||||
@ -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,15 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
|
||||
stateManager.attach(gameSound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches the game sound state and sets its initial enabled state.
|
||||
*/
|
||||
private void attachGameMusic() {
|
||||
final GameMusic gameSound = new GameMusic();
|
||||
gameSound.setEnabled(GameMusic.enabledInPreferences());
|
||||
stateManager.attach(gameSound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the application state every frame.
|
||||
* This method is called once per frame during the game loop.
|
||||
|
@ -11,6 +11,9 @@ import com.simsilica.lemur.Button;
|
||||
import com.simsilica.lemur.Checkbox;
|
||||
import com.simsilica.lemur.Label;
|
||||
import com.simsilica.lemur.style.ElementId;
|
||||
|
||||
import pp.battleship.client.gui.GameMusic;
|
||||
import pp.battleship.client.gui.VolumeSlider;
|
||||
import pp.dialog.Dialog;
|
||||
import pp.dialog.StateCheckboxModel;
|
||||
import pp.dialog.TextInputDialog;
|
||||
@ -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)
|
||||
@ -65,6 +74,11 @@ class Menu extends Dialog {
|
||||
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.
|
||||
*/
|
||||
|
@ -0,0 +1,111 @@
|
||||
package pp.battleship.client.gui;
|
||||
|
||||
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.
|
||||
*
|
||||
* @return //TODO
|
||||
*/
|
||||
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/background.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;
|
||||
}
|
||||
|
||||
/** TODO
|
||||
* 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package pp.battleship.client.gui;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
@ -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:
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user