Solution for Nr. 10, added 'BackgroundSound.java' and edited 'battleshipApp' and 'Menu'
the 'BackgroundMusic' class defines the background music and the function used to control it, in the 'BattleshipApp' is an attribute of BackgrounMusic and a return function and in the Menu the Checkbox and the slider for the backgroundMusic and the update function were implemented
This commit is contained in:
		@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Double> 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.
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -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.
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@@ -22,10 +22,5 @@ public enum Sound {
 | 
			
		||||
    /**
 | 
			
		||||
     * Sound of a ship being destroyed.
 | 
			
		||||
     */
 | 
			
		||||
    DESTROYED_SHIP,
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     */
 | 
			
		||||
    BACKGROUND_MUSIC //TODO
 | 
			
		||||
    DESTROYED_SHIP
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user