Added Ex. 10
This commit is contained in:
@@ -265,11 +265,23 @@ private void setupStates() {
|
||||
flyCam.setEnabled(false);
|
||||
stateManager.detach(stateManager.getState(StatsAppState.class));
|
||||
stateManager.detach(stateManager.getState(DebugKeysAppState.class));
|
||||
|
||||
attachGameMusic();
|
||||
attachGameSound();
|
||||
stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches the game sound state and sets its initial enabled state.
|
||||
*/
|
||||
private void attachGameMusic() {
|
||||
//TODO: start volume
|
||||
final GameMusic gameMusic = new GameMusic(0.5f);
|
||||
logic.addListener(gameMusic);
|
||||
gameMusic.setEnabled(true);
|
||||
// gameMusic.setEnabled(GameSound.enabledInPreferences());
|
||||
stateManager.attach(gameMusic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches the game sound state and sets its initial enabled state.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package pp.battleship.client;
|
||||
|
||||
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;
|
||||
import pp.battleship.notification.GameEventListener;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
public class GameMusic extends AbstractAppState implements GameEventListener {
|
||||
private static final Logger LOGGER = System.getLogger(GameMusic.class.getName());
|
||||
private AudioNode backgroundMusic;
|
||||
private float startVolume;
|
||||
|
||||
public GameMusic(float startVolume){
|
||||
this. startVolume = startVolume;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(AppStateManager stateManager, Application app) {
|
||||
super.initialize(stateManager, app);
|
||||
backgroundMusic = loadSound(app, "Sound/background.wav");
|
||||
setMusicVolume(startVolume);
|
||||
playMusic();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
if(enabled && !isEnabled()){
|
||||
playMusic();
|
||||
}
|
||||
else if(!enabled && isEnabled()){
|
||||
stopMusic();
|
||||
}
|
||||
super.setEnabled(enabled);
|
||||
}
|
||||
|
||||
public void playMusic(){
|
||||
if (backgroundMusic != null){
|
||||
backgroundMusic.play();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops background music
|
||||
*/
|
||||
public void stopMusic(){
|
||||
if (backgroundMusic != null){
|
||||
backgroundMusic.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void setMusicVolume(float volume){
|
||||
if(backgroundMusic != null){
|
||||
backgroundMusic.setVolume(volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
||||
private AudioNode shipDestroyedSound;
|
||||
private AudioNode explosionSound;
|
||||
|
||||
|
||||
/**
|
||||
* Checks if sound is enabled in the preferences.
|
||||
*
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
import com.simsilica.lemur.Button;
|
||||
import com.simsilica.lemur.Checkbox;
|
||||
import com.simsilica.lemur.Label;
|
||||
import com.simsilica.lemur.RangedValueModel;
|
||||
import com.simsilica.lemur.Slider;
|
||||
import com.simsilica.lemur.style.ElementId;
|
||||
import pp.dialog.Dialog;
|
||||
import pp.dialog.StateCheckboxModel;
|
||||
@@ -33,6 +35,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 volumeSlider;
|
||||
|
||||
/**
|
||||
* Constructs the Menu dialog for the Battleship application.
|
||||
@@ -42,9 +45,15 @@ class Menu extends Dialog {
|
||||
public Menu(BattleshipApp app) {
|
||||
super(app.getDialogManager());
|
||||
this.app = app;
|
||||
volumeSlider = new VolumeSlider(0.5f, 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.music-enabled"), new StateCheckboxModel(app, GameMusic.class)));
|
||||
addChild(volumeSlider);
|
||||
|
||||
addChild(loadButton)
|
||||
.addClickCommands(s -> ifTopDialog(this::loadDialog));
|
||||
addChild(saveButton)
|
||||
@@ -65,6 +74,11 @@ public void update() {
|
||||
saveButton.setEnabled(app.getGameLogic().maySaveMap());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta){
|
||||
volumeSlider.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* As an escape action, this method closes the menu if it is the top dialog.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package pp.battleship.client;
|
||||
|
||||
import com.simsilica.lemur.Slider;
|
||||
|
||||
public class VolumeSlider extends Slider {
|
||||
private GameMusic gameMusic;
|
||||
private float volume;
|
||||
|
||||
public VolumeSlider(float startVolume, GameMusic gameMusic){
|
||||
super();
|
||||
getModel().setPercent(startVolume);
|
||||
this.gameMusic = gameMusic;
|
||||
this.volume = (float) getModel().getPercent();
|
||||
gameMusic.setMusicVolume(volume);
|
||||
System.out.println("init");
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(getModel().getPercent() != volume){
|
||||
System.out.println(volume);
|
||||
this.volume = (float) getModel().getPercent();
|
||||
gameMusic.setMusicVolume(volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -31,6 +31,7 @@ menu.return-to-game=Return to game
|
||||
menu.sound-enabled=Sound switched on
|
||||
menu.map.load=Load map from file...
|
||||
menu.map.save=Save map in file...
|
||||
menu.music-enabled=Music on
|
||||
label.file=File:
|
||||
label.connecting=Connecting...
|
||||
dialog.error=Error
|
||||
@@ -38,3 +39,4 @@ dialog.question=Question
|
||||
port.must.be.integer=Port must be an integer number
|
||||
map.doesnt.fit=The map doesn't fit to this game
|
||||
invalid.map=Invalid Map!
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ menu.return-to-game=Zur
|
||||
menu.sound-enabled=Sound eingeschaltet
|
||||
menu.map.load=Karte von Datei laden...
|
||||
menu.map.save=Karte in Datei speichern...
|
||||
menu.music-enabled=Musik an
|
||||
label.file=Datei:
|
||||
label.connecting=Verbindung wird aufgebaut...
|
||||
dialog.error=Fehler
|
||||
|
||||
Reference in New Issue
Block a user