From 2110555bbc899cebade75ec0983645a5dbef3bc4 Mon Sep 17 00:00:00 2001 From: Benjamin Feyer Date: Sat, 5 Oct 2024 14:12:23 +0200 Subject: [PATCH] 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 --- .../pp/battleship/client/BackgroundMusic.java | 69 ++++++++++++++----- .../pp/battleship/client/BattleshipApp.java | 5 +- .../pp/battleship/client/ClientServer.java | 7 ++ .../main/java/pp/battleship/client/Menu.java | 24 +++---- .../client/gui/SeaSynchronizer.java | 6 ++ .../battleship/game/client/EditorState.java | 18 +---- .../game/server/ServerGameLogic.java | 16 +---- .../src/main/resources/battleship.properties | 1 + .../main/resources/battleship_de.properties | 1 + 9 files changed, 80 insertions(+), 67 deletions(-) create mode 100644 Projekte/battleship/client/src/main/java/pp/battleship/client/ClientServer.java diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java index 7eef71c2..7b20163b 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/BackgroundMusic.java @@ -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; + } } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java index 10a666ab..a4615b64 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java @@ -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 } /** diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/ClientServer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/ClientServer.java new file mode 100644 index 00000000..3fb21586 --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/ClientServer.java @@ -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 { +} diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java index fea606f2..c0167bfb 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java @@ -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 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()); + 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.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()); + public void update(float tmp) { + if (volumeRef.update()) { + 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. */ diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java index e4b1c9d3..396f54f0 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java @@ -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; } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java index 2d4cc1d6..c1f886e9 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java @@ -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 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 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; - */ - } /** diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java index bc9c540a..17fab180 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java @@ -184,29 +184,15 @@ private boolean verifyBounds(MapMessage msg, int playerID) { * @return true, if no ships overlap */ private boolean verifyOverlap(MapMessage msg) { - List 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 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; - - */ } /** diff --git a/Projekte/battleship/model/src/main/resources/battleship.properties b/Projekte/battleship/model/src/main/resources/battleship.properties index 384aabb3..0aee4da3 100644 --- a/Projekte/battleship/model/src/main/resources/battleship.properties +++ b/Projekte/battleship/model/src/main/resources/battleship.properties @@ -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 diff --git a/Projekte/battleship/model/src/main/resources/battleship_de.properties b/Projekte/battleship/model/src/main/resources/battleship_de.properties index 52585c4a..da5e4af6 100644 --- a/Projekte/battleship/model/src/main/resources/battleship_de.properties +++ b/Projekte/battleship/model/src/main/resources/battleship_de.properties @@ -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ärke +player.submitted.invalid.map= Invalide karte gegeben