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:
Benjamin Feyer
2024-10-05 14:12:23 +02:00
parent 34a0fa2304
commit 2110555bbc
9 changed files with 80 additions and 67 deletions

View File

@@ -7,35 +7,66 @@
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
/**
* this class is used to control the background-music in the game
*/
public class BackgroundMusic { public class BackgroundMusic {
/**
* this attribute ist the keyword to save the last volume, that was set
*/
private static final String VOLUME_PREV = "BackgroundVolume"; 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"; 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); private Preferences prefs = Preferences.userNodeForPackage(BackgroundMusic.class);
/**
* this attribute holds the Music-node, which is used to play the music
*/
private final AudioNode backgroundMusic; private final AudioNode backgroundMusic;
/**
* this boolean holds, whether the music is enabled or not at this state
*/
private boolean backgroundMusicEnabled; private boolean backgroundMusicEnabled;
/**
* this attribute holds the current volume
*/
private float backgroundVolume; 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) { public BackgroundMusic(Application app, String backGroundMusicPath) {
this.backgroundVolume = prefs.getFloat(VOLUME_PREV, 1.0f); 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); 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 = new AudioNode(app.getAssetManager(), backGroundMusicPath, DataType.Stream); // initialize the Audio-node
backgroundMusic.setLooping(true); backgroundMusic.setLooping(true); // sets looping true, so it is played always, when enabled
backgroundMusic.setPositional(false); backgroundMusic.setPositional(false);
backgroundMusic.setVolume(backgroundVolume); backgroundMusic.setVolume(backgroundVolume);
if (backgroundMusicEnabled) { if (backgroundMusicEnabled) { //starts playing, when enabled
play(); play();
} }
} }
/** /**
* this method plays the background sound * this method plays the background sound, if it is enabled
*/ */
public void play() { 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(); backgroundMusic.play();
} }
} }
@@ -43,12 +74,12 @@ public void play() {
/** /**
* this method stops the background-music * 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(); 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() { public void toggleMusic() {
this.backgroundMusicEnabled = !this.backgroundMusicEnabled; this.backgroundMusicEnabled = !this.backgroundMusicEnabled;
@@ -58,16 +89,7 @@ public void toggleMusic() {
else { else {
stop(); stop();
} }
prefs.putBoolean(VOLUME_PREV, backgroundMusicEnabled); prefs.putBoolean(BACKGROUND_MUSIC_ENABLED_PREV, backgroundMusicEnabled); //puts in new enabled to the pref
}
/**
* return, if the background-music is enabled
*
* @return true if the music is enabled
*/
public boolean isBackgroundMusicEnabled() {
return backgroundMusicEnabled;
} }
/** /**
@@ -89,4 +111,13 @@ public void setBackgroundVolume(float volume) {
public float getBackgroundVolume() { public float getBackgroundVolume() {
return backgroundVolume; return backgroundVolume;
} }
/**
* return, if the background-music is enabled
*
* @return true if the music is enabled
*/
public boolean isBackgroundMusicEnabled() {
return backgroundMusicEnabled;
}
} }

View File

@@ -122,6 +122,9 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
*/ */
private final ActionListener escapeListener = (name, isPressed, tpf) -> escape(isPressed); 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; private BackgroundMusic backgroundMusic;
static { static {
@@ -227,7 +230,7 @@ public void simpleInitApp() {
setupStates(); setupStates();
setupGui(); setupGui();
serverConnection.connect(); 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
} }
/** /**

View File

@@ -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 {
}

View File

@@ -36,10 +36,8 @@ class Menu extends Dialog {
private final BattleshipApp app; private final BattleshipApp app;
private final Button loadButton = new Button(lookup("menu.map.load")); private final Button loadButton = new Button(lookup("menu.map.load"));
private final Button saveButton = new Button(lookup("menu.map.save")); private final Button saveButton = new Button(lookup("menu.map.save"));
private final VersionedReference<Double> volumeRef; private final VersionedReference<Double> volumeRef;
/** /**
* Constructs the Menu dialog for the Battleship application. * Constructs the Menu dialog for the Battleship application.
* *
@@ -52,16 +50,17 @@ public Menu(BattleshipApp app) {
addChild(new Checkbox(lookup("menu.sound-enabled"), addChild(new Checkbox(lookup("menu.sound-enabled"),
new StateCheckboxModel(app, GameSound.class))); new StateCheckboxModel(app, GameSound.class)));
//this sets the checkbox,to disable/enable the background-music
Checkbox backgroundMusicEnabled = new Checkbox(lookup("background.music.checkbox")); Checkbox backgroundMusicEnabled = new Checkbox(lookup("background.music.checkbox"));
backgroundMusicEnabled.setChecked(app.getBackgroundMusic().isBackgroundMusicEnabled()); backgroundMusicEnabled.setChecked(app.getBackgroundMusic().isBackgroundMusicEnabled());
backgroundMusicEnabled.addClickCommands(s -> app.getBackgroundMusic().toggleMusic()); backgroundMusicEnabled.addClickCommands(s -> app.getBackgroundMusic().toggleMusic());
addChild(backgroundMusicEnabled); addChild(backgroundMusicEnabled);
//this sets the volume-slider for the volume of the background-music
Slider volumeSlider = new Slider(lookup("background.music.volume")); Slider volumeSlider = new Slider(lookup("background.music.volume"));
volumeSlider.setModel(new DefaultRangedValueModel(0.0,2.0,app.getBackgroundMusic().getBackgroundVolume())); volumeSlider.setModel(new DefaultRangedValueModel(0.0, 2.0, app.getBackgroundMusic().getBackgroundVolume()));
volumeSlider.setDelta(0.1); volumeSlider.setDelta(0.1);
addChild(volumeSlider); addChild(volumeSlider);
volumeRef = volumeSlider.getModel().createReference(); volumeRef = volumeSlider.getModel().createReference();
addChild(loadButton) addChild(loadButton)
@@ -78,23 +77,16 @@ public Menu(BattleshipApp app) {
/** /**
* updates the background-volume * updates the background-volume
*
* @param tmp * @param tmp
*/ */
@Override @Override
public void update(float tmp){ public void update(float tmp) {
if(volumeRef.update()) { 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. * Updates the state of the load and save buttons based on the game logic.
*/ */

View File

@@ -182,6 +182,12 @@ private Spatial createDestroyer(Battleship ship){
model.scale(0.0001f); model.scale(0.0001f);
model.move(0,0.3f ,0); model.move(0,0.3f ,0);
model.setShadowMode(ShadowMode.CastAndReceive); 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; return model;
} }

View File

@@ -242,7 +242,7 @@ public void loadMap(File file) throws IOException {
if (!dto.fits(logic.getDetails())) if (!dto.fits(logic.getDetails()))
throw new IOException(lookup("map.doesnt.fit")); throw new IOException(lookup("map.doesnt.fit"));
if (!verifyMap(dto)) { if (!verifyMap(dto)) {
throw new IOException(lookup("map is not valid")); throw new IOException(lookup("player.submitted.invalid.map"));
} }
ownMap().clear(); ownMap().clear();
dto.getShips().forEach(ownMap()::add); dto.getShips().forEach(ownMap()::add);
@@ -284,29 +284,15 @@ private boolean verifyBounds(ShipMapDTO dto) {
* @return true, if no ships overlap * @return true, if no ships overlap
*/ */
private boolean verifyOverlap(ShipMapDTO dto) { 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(); List<Battleship> ships = dto.getShips();
for (Battleship ship : ships) { for (Battleship ship : ships) {
for (Battleship compareShip : ships) { for (Battleship compareShip : ships) {
if (ships != compareShip) { if (!ship.equals(compareShip)) {
if (ship.collidesWith(compareShip)) return false; if (ship.collidesWith(compareShip)) return false;
} }
} }
} }
return true; return true;
*/
} }
/** /**

View File

@@ -184,29 +184,15 @@ private boolean verifyBounds(MapMessage msg, int playerID) {
* @return true, if no ships overlap * @return true, if no ships overlap
*/ */
private boolean verifyOverlap(MapMessage msg) { 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(); List<Battleship> ships = msg.getShips();
for (Battleship ship : ships) { for (Battleship ship : ships) {
for (Battleship compareShip : ships) { for (Battleship compareShip : ships) {
if (ship != compareShip) { if (!ship.equals(compareShip)) {
if (ship.collidesWith(compareShip)) return false; if (ship.collidesWith(compareShip)) return false;
} }
} }
} }
return true; return true;
*/
} }
/** /**

View File

@@ -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 map.doesnt.fit=The map doesn't fit to this game
background.music.checkbox= Background music on/off background.music.checkbox= Background music on/off
background.music.volume= Background volume background.music.volume= Background volume
player.submitted.invalid.map= invalid map submitted

View File

@@ -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 map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
background.music.checkbox= Musik an/aus background.music.checkbox= Musik an/aus
background.music.volume= Musiklautst<EFBFBD>rke background.music.volume= Musiklautst<EFBFBD>rke
player.submitted.invalid.map= Invalide karte gegeben