corrected the code for nr. 8 +10
now the backgroundmusic-settings will be stored correctly and when a invalid map is submitted,the error will be displayed correctly
This commit is contained in:
@@ -7,35 +7,66 @@
|
||||
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
/**
|
||||
* this class is used to control the background-music in the game
|
||||
*/
|
||||
public class BackgroundMusic {
|
||||
|
||||
/**
|
||||
* this attribute ist the keyword to save the last volume, that was set
|
||||
*/
|
||||
private static final String VOLUME_PREV = "BackgroundVolume";
|
||||
|
||||
/**
|
||||
* this attribute ist the keyword to save the state, whether the music was on or off in the last session
|
||||
*/
|
||||
private static final String BACKGROUND_MUSIC_ENABLED_PREV = "BackgroundMusicEnabled";
|
||||
|
||||
/**
|
||||
* this attribute holds the last set values for on/off and the volume, with the keywords "BackgroundVolume" and "BackgroundMusicEnabled"
|
||||
*/
|
||||
private Preferences prefs = Preferences.userNodeForPackage(BackgroundMusic.class);
|
||||
|
||||
/**
|
||||
* this attribute holds the Music-node, which is used to play the music
|
||||
*/
|
||||
private final AudioNode backgroundMusic;
|
||||
|
||||
/**
|
||||
* this boolean holds, whether the music is enabled or not at this state
|
||||
*/
|
||||
private boolean backgroundMusicEnabled;
|
||||
|
||||
/**
|
||||
* this attribute holds the current volume
|
||||
*/
|
||||
private float backgroundVolume;
|
||||
|
||||
/**
|
||||
* this is the constructor of this class, is used to set the current values to the values stored in the preferences
|
||||
*
|
||||
* @param app the application the Background-music uis used in
|
||||
* @param backGroundMusicPath the path, where the Background-music is stored in
|
||||
*/
|
||||
public BackgroundMusic(Application app, String backGroundMusicPath) {
|
||||
this.backgroundVolume = prefs.getFloat(VOLUME_PREV, 1.0f);
|
||||
this.backgroundMusicEnabled = prefs.getBoolean(BACKGROUND_MUSIC_ENABLED_PREV, true);
|
||||
this.backgroundVolume = prefs.getFloat(VOLUME_PREV, 1.0f); // sets the volume to the value stored in pref,or 1.0f if it is not accessible
|
||||
this.backgroundMusicEnabled = prefs.getBoolean(BACKGROUND_MUSIC_ENABLED_PREV, true); // sets the enabled to the value stored in pref,or true if it is not accessible
|
||||
|
||||
backgroundMusic = new AudioNode(app.getAssetManager(), backGroundMusicPath, DataType.Stream);
|
||||
backgroundMusic.setLooping(true);
|
||||
backgroundMusic = new AudioNode(app.getAssetManager(), backGroundMusicPath, DataType.Stream); // initialize the Audio-node
|
||||
backgroundMusic.setLooping(true); // sets looping true, so it is played always, when enabled
|
||||
backgroundMusic.setPositional(false);
|
||||
backgroundMusic.setVolume(backgroundVolume);
|
||||
|
||||
if (backgroundMusicEnabled) {
|
||||
if (backgroundMusicEnabled) { //starts playing, when enabled
|
||||
play();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* this method plays the background sound
|
||||
* this method plays the background sound, if it is enabled
|
||||
*/
|
||||
public void play() {
|
||||
if (backgroundMusicEnabled && (backgroundMusic.getStatus() == Status.Stopped || backgroundMusic.getStatus() == Status.Paused)) {
|
||||
if (backgroundMusicEnabled && (backgroundMusic.getStatus() == Status.Stopped || backgroundMusic.getStatus() == Status.Paused)) { //checks, if it is enabled and stopped or paused
|
||||
backgroundMusic.play();
|
||||
}
|
||||
}
|
||||
@@ -43,12 +74,12 @@ public void play() {
|
||||
/**
|
||||
* this method stops the background-music
|
||||
*/
|
||||
public void stop() {
|
||||
public void stop() { //stops the music, when it is playing
|
||||
if (backgroundMusic.getStatus() == Status.Playing) backgroundMusic.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* function to toggle the Backgroundmusic also sets the volume to the previous session
|
||||
* function to toggle the Background-music ,so if it was disabled it is now enabled and other way around
|
||||
*/
|
||||
public void toggleMusic() {
|
||||
this.backgroundMusicEnabled = !this.backgroundMusicEnabled;
|
||||
@@ -58,16 +89,7 @@ public void toggleMusic() {
|
||||
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;
|
||||
prefs.putBoolean(BACKGROUND_MUSIC_ENABLED_PREV, backgroundMusicEnabled); //puts in new enabled to the pref
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,4 +111,13 @@ public void setBackgroundVolume(float volume) {
|
||||
public float getBackgroundVolume() {
|
||||
return backgroundVolume;
|
||||
}
|
||||
|
||||
/**
|
||||
* return, if the background-music is enabled
|
||||
*
|
||||
* @return true if the music is enabled
|
||||
*/
|
||||
public boolean isBackgroundMusicEnabled() {
|
||||
return backgroundMusicEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +122,9 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
|
||||
*/
|
||||
private final ActionListener escapeListener = (name, isPressed, tpf) -> escape(isPressed);
|
||||
|
||||
/**
|
||||
* this is the object, responsible for the background-music and to control it
|
||||
*/
|
||||
private BackgroundMusic backgroundMusic;
|
||||
|
||||
static {
|
||||
@@ -227,7 +230,7 @@ public void simpleInitApp() {
|
||||
setupStates();
|
||||
setupGui();
|
||||
serverConnection.connect();
|
||||
backgroundMusic = new BackgroundMusic(this, "Sound/Background/backgroundMusic.ogg");
|
||||
backgroundMusic = new BackgroundMusic(this, "Sound/Background/backgroundMusic.ogg"); //initialise backgroundMusic and sets the backgroundMusic-path
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package pp.battleship.client;
|
||||
|
||||
/**
|
||||
*this class is used for the server, when one player decides to host the server locally
|
||||
*/
|
||||
public class ClientServer {
|
||||
}
|
||||
@@ -36,10 +36,8 @@ 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 VersionedReference<Double> volumeRef;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs the Menu dialog for the Battleship application.
|
||||
*
|
||||
@@ -52,16 +50,17 @@ public Menu(BattleshipApp app) {
|
||||
addChild(new Checkbox(lookup("menu.sound-enabled"),
|
||||
new StateCheckboxModel(app, GameSound.class)));
|
||||
|
||||
//this sets the checkbox,to disable/enable the background-music
|
||||
Checkbox backgroundMusicEnabled = new Checkbox(lookup("background.music.checkbox"));
|
||||
backgroundMusicEnabled.setChecked(app.getBackgroundMusic().isBackgroundMusicEnabled());
|
||||
backgroundMusicEnabled.addClickCommands(s -> app.getBackgroundMusic().toggleMusic());
|
||||
addChild(backgroundMusicEnabled);
|
||||
|
||||
//this sets the volume-slider for the volume of the background-music
|
||||
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)
|
||||
@@ -78,23 +77,16 @@ public Menu(BattleshipApp app) {
|
||||
|
||||
/**
|
||||
* updates the background-volume
|
||||
*
|
||||
* @param tmp
|
||||
*/
|
||||
@Override
|
||||
public void update(float tmp) {
|
||||
if (volumeRef.update()) {
|
||||
adjustVolume(volumeRef.get());
|
||||
app.getBackgroundMusic().setBackgroundVolume((float) (double) volumeRef.get()); //sets new background-volume
|
||||
}
|
||||
}
|
||||
|
||||
private void adjustVolume(double newVolume) {
|
||||
app.getBackgroundMusic().setBackgroundVolume((float) newVolume);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Updates the state of the load and save buttons based on the game logic.
|
||||
*/
|
||||
|
||||
@@ -182,6 +182,12 @@ private Spatial createDestroyer(Battleship ship){
|
||||
model.scale(0.0001f);
|
||||
model.move(0,0.3f ,0);
|
||||
model.setShadowMode(ShadowMode.CastAndReceive);
|
||||
model.setMaterial(app.getAssetManager().loadMaterial("Models/Destroyer/10619_Battleship.mtl"));
|
||||
//TODO
|
||||
//Material m = new Material();
|
||||
|
||||
//m.setTexture("textureDestroyer",app.getAssetManager().loadTexture("Models/Destroyer/10619_Battleship.mtl"));
|
||||
//model.setMaterial(m);
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
@@ -242,7 +242,7 @@ public void loadMap(File file) throws IOException {
|
||||
if (!dto.fits(logic.getDetails()))
|
||||
throw new IOException(lookup("map.doesnt.fit"));
|
||||
if (!verifyMap(dto)) {
|
||||
throw new IOException(lookup("map is not valid"));
|
||||
throw new IOException(lookup("player.submitted.invalid.map"));
|
||||
}
|
||||
ownMap().clear();
|
||||
dto.getShips().forEach(ownMap()::add);
|
||||
@@ -284,29 +284,15 @@ private boolean verifyBounds(ShipMapDTO dto) {
|
||||
* @return true, if no ships overlap
|
||||
*/
|
||||
private boolean verifyOverlap(ShipMapDTO dto) {
|
||||
List<Battleship> battleshipList = dto.getShips();
|
||||
for (int i = 0; i < battleshipList.size(); i++) {
|
||||
Battleship ship1 = battleshipList.get(i);
|
||||
for (int j = i + 1; j < battleshipList.size(); j++) {
|
||||
Battleship ship2 = battleshipList.get(j);
|
||||
if (ship1.collidesWith(ship2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
/*
|
||||
List<Battleship> ships = dto.getShips();
|
||||
for (Battleship ship : ships) {
|
||||
for (Battleship compareShip : ships) {
|
||||
if (ships != compareShip) {
|
||||
if (!ship.equals(compareShip)) {
|
||||
if (ship.collidesWith(compareShip)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -184,29 +184,15 @@ private boolean verifyBounds(MapMessage msg, int playerID) {
|
||||
* @return true, if no ships overlap
|
||||
*/
|
||||
private boolean verifyOverlap(MapMessage msg) {
|
||||
List<Battleship> battleshipList = msg.getShips();
|
||||
for (int i = 0; i < battleshipList.size(); i++) {
|
||||
Battleship ship1 = battleshipList.get(i);
|
||||
for (int j = i + 1; j < battleshipList.size(); j++) {
|
||||
Battleship ship2 = battleshipList.get(j);
|
||||
if (ship1.collidesWith(ship2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
/*
|
||||
List<Battleship> ships = msg.getShips();
|
||||
for (Battleship ship : ships) {
|
||||
for (Battleship compareShip : ships) {
|
||||
if (ship != compareShip) {
|
||||
if (!ship.equals(compareShip)) {
|
||||
if (ship.collidesWith(compareShip)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,3 +39,4 @@ port.must.be.integer=Port must be an integer number
|
||||
map.doesnt.fit=The map doesn't fit to this game
|
||||
background.music.checkbox= Background music on/off
|
||||
background.music.volume= Background volume
|
||||
player.submitted.invalid.map= invalid map submitted
|
||||
|
||||
@@ -39,4 +39,5 @@ port.must.be.integer=Der Port muss eine ganze Zahl sein
|
||||
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
|
||||
background.music.checkbox= Musik an/aus
|
||||
background.music.volume= Musiklautst<EFBFBD>rke
|
||||
player.submitted.invalid.map= Invalide karte gegeben
|
||||
|
||||
|
||||
Reference in New Issue
Block a user