Added functionality for preferences in 'GameMusic'

This commit is contained in:
Cedric Beck
2024-10-05 11:28:15 +02:00
parent 1d114c8d24
commit 8275778d66
5 changed files with 29 additions and 88 deletions

View File

@@ -275,10 +275,9 @@ private void setupStates() {
*/ */
private void attachGameMusic() { private void attachGameMusic() {
//TODO: start volume //TODO: start volume
final GameMusic gameMusic = new GameMusic(0.5f); final GameMusic gameMusic = new GameMusic();
logic.addListener(gameMusic); logic.addListener(gameMusic);
gameMusic.setEnabled(true); gameMusic.setEnabled(GameMusic.enabledInPreferences());
// gameMusic.setEnabled(GameSound.enabledInPreferences());
stateManager.attach(gameMusic); stateManager.attach(gameMusic);
} }

View File

@@ -11,22 +11,36 @@
import java.lang.System.Logger; import java.lang.System.Logger;
import java.lang.System.Logger.Level; import java.lang.System.Logger.Level;
import java.util.prefs.Preferences;
import static pp.util.PreferencesUtils.getPreferences;
public class GameMusic extends AbstractAppState implements GameEventListener { public class GameMusic extends AbstractAppState implements GameEventListener {
private static final Logger LOGGER = System.getLogger(GameMusic.class.getName()); private static final Logger LOGGER = System.getLogger(GameMusic.class.getName());
private AudioNode backgroundMusic; private static final Preferences PREFERENCES = getPreferences(GameMusic.class);
private float startVolume; private static final String ENABLED_PREF = "enabled";
private static final String VOLUME_PREF = "volume";
public GameMusic(float startVolume){ private static final String MUSIC_PATH = "Sound/background.wav";
this. startVolume = startVolume;
private AudioNode backgroundMusic;
public static boolean enabledInPreferences(){
return PREFERENCES.getBoolean(ENABLED_PREF, true);
} }
public static float volumeInPreferences(){
return PREFERENCES.getFloat(VOLUME_PREF, 0.5f);
}
@Override @Override
public void initialize(AppStateManager stateManager, Application app) { public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app); super.initialize(stateManager, app);
backgroundMusic = loadSound(app, "Sound/background.wav"); backgroundMusic = loadSound(app, MUSIC_PATH);
setMusicVolume(startVolume); setMusicVolume(volumeInPreferences());
playMusic(); if (isEnabled()) playMusic();
} }
private AudioNode loadSound(Application app, String name) { private AudioNode loadSound(Application app, String name) {
@@ -51,6 +65,7 @@ else if(!enabled && isEnabled()){
stopMusic(); stopMusic();
} }
super.setEnabled(enabled); super.setEnabled(enabled);
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
} }
public void playMusic(){ public void playMusic(){
@@ -71,6 +86,7 @@ public void stopMusic(){
public void setMusicVolume(float volume){ public void setMusicVolume(float volume){
if(backgroundMusic != null){ if(backgroundMusic != null){
backgroundMusic.setVolume(volume); backgroundMusic.setVolume(volume);
PREFERENCES.putFloat(VOLUME_PREF, volume);
} }
} }
} }

View File

@@ -45,7 +45,7 @@ class Menu extends Dialog {
public Menu(BattleshipApp app) { public Menu(BattleshipApp app) {
super(app.getDialogManager()); super(app.getDialogManager());
this.app = app; this.app = app;
volumeSlider = new VolumeSlider(0.5f, app.getStateManager().getState(GameMusic.class)); volumeSlider = new VolumeSlider(app.getStateManager().getState(GameMusic.class));
addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS
addChild(new Checkbox(lookup("menu.sound-enabled"), addChild(new Checkbox(lookup("menu.sound-enabled"),

View File

@@ -1,71 +0,0 @@
package pp.battleship.client;
import com.simsilica.lemur.RangedValueModel;
import com.simsilica.lemur.core.VersionedReference;
public class SliderRangedValueModel implements RangedValueModel {
SliderRangedValueModel(){
}
@Override
public void setValue( double val ){
System.out.println(val);
}
@Override
public double getValue(){
return 0;
}
@Override
public void setPercent( double val ){
System.out.println(val);
}
@Override
public double getPercent(){
return 0;
}
@Override
public void setMaximum( double max ){
}
@Override
public double getMaximum(){
return 0;
}
@Override
public void setMinimum( double min ){
}
@Override
public double getMinimum(){
return 0;
}
@Override
public long getVersion() {
return 0;
}
@Override
public Double getObject() {
return 0.0;
}
@Override
public VersionedReference<Double> createReference() {
return null;
}
}

View File

@@ -6,18 +6,15 @@ public class VolumeSlider extends Slider {
private GameMusic gameMusic; private GameMusic gameMusic;
private float volume; private float volume;
public VolumeSlider(float startVolume, GameMusic gameMusic){ public VolumeSlider(GameMusic gameMusic){
super(); super();
getModel().setPercent(startVolume);
this.gameMusic = gameMusic; this.gameMusic = gameMusic;
this.volume = (float) getModel().getPercent(); volume = GameMusic.volumeInPreferences();
gameMusic.setMusicVolume(volume); getModel().setPercent(volume);
System.out.println("init");
} }
public void update(){ public void update(){
if(getModel().getPercent() != volume){ if(getModel().getPercent() != volume){
System.out.println(volume);
this.volume = (float) getModel().getPercent(); this.volume = (float) getModel().getPercent();
gameMusic.setMusicVolume(volume); gameMusic.setMusicVolume(volume);
} }