Aufgabe 1-10 bearbeitet. Musik (Piratenmusik.ogg) hinzugefügt. Schiebregler hinzugefügt (VolumeLSider); update Methode überschrieben; Lemur Doc gelesen

This commit is contained in:
Dennis_Malkmus 2024-10-13 23:45:26 +02:00
parent f47cda7dc4
commit f5dc8a0f05
66 changed files with 476090 additions and 4 deletions

View File

@ -9,6 +9,8 @@ dependencies {
implementation project(":battleship:model") implementation project(":battleship:model")
implementation libs.jme3.desktop implementation libs.jme3.desktop
implementation 'com.simsilica:lemur:1.14.0'
implementation 'com.simsilica:lemur-proto:1.10.0'
runtimeOnly libs.jme3.awt.dialogs runtimeOnly libs.jme3.awt.dialogs
runtimeOnly libs.jme3.plugins runtimeOnly libs.jme3.plugins

View File

@ -0,0 +1,127 @@
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 pp.battleship.notification.SoundEvent;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.prefs.Preferences;
import static pp.util.PreferencesUtils.getPreferences;
/**
* An application state that plays music.
*/
public class GameMusic extends AbstractAppState {
private static final Logger LOGGER = System.getLogger(GameMusic.class.getName());
private static final Preferences PREFERENCES = getPreferences(GameMusic.class);
private static final String ENABLED_PREF = "enabled"; //NON-NLS
private static final String VOLUME_PREF = "volume"; //NON-NLS
private AudioNode piratenMusic;
/**
* Checks if sound is enabled in the preferences.
*
* @return {@code true} if sound is enabled, {@code false} otherwise.
*/
//Prüft, ob die Musik eingeschalten ist.
public static boolean enabledInPreferences() {
return PREFERENCES.getBoolean(ENABLED_PREF, true);
}
/**
* Checks enabled volume in preferences.
*
* @return {@code float} if a volumePreference is set, the volume is set to the Value in PREFERENCES,
* {@code 0.25f} if no volumePreference is set in PREFERENCES, the Volume is set to a default of 0.25f
*
*/
//Standardeinstellung für Musik
public static float volumePreference() {
return PREFERENCES.getFloat(VOLUME_PREF, 0.33f);
}
/**
* Toggles the game sound on or off.
*/
public void toggleSound() {
setEnabled(!isEnabled());
}
/**
* Sets the enabled state of this AppState.
* Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)}
*
* @param enabled {@code true} to enable the AppState, {@code false} to disable it.
*/
@Override
public void setEnabled(boolean enabled) {
if (isEnabled() == enabled) return;
else if (!isEnabled() && enabled) {
if (piratenMusic != null) piratenMusic.play();
} else if (isEnabled() && !enabled) {
if (piratenMusic != null) piratenMusic.stop();
}
super.setEnabled(enabled);
LOGGER.log(Level.INFO, "Music enabled: {0}", enabled); //NON-NLS
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
}
/**
* Initializes the music.
* Overrides {@link AbstractAppState#initialize(AppStateManager, Application)}
*
* @param stateManager The state manager
* @param app The application
*/
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
piratenMusic =loadSound(app, "Sound/BackgroundMusic/Piratenmusik.ogg");
setVolume(volumePreference());
piratenMusic.setLooping(true);
if (isEnabled() && piratenMusic != null) {
piratenMusic.play();
}
}
/**
* Loads the music from the specified file.
*
* @param app The application
* @param name The name of the sound file.
* @return The loaded AudioNode.
*/
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;
}
//Einstellung der Lautstärke
public void setVolume(float vol){
piratenMusic.setVolume(vol);
PREFERENCES.putFloat(VOLUME_PREF, vol);
}
}

View File

@ -7,10 +7,14 @@
package pp.battleship.client; package pp.battleship.client;
import com.jme3.math.Vector3f;
import com.simsilica.lemur.Button; import com.simsilica.lemur.Button;
import com.simsilica.lemur.Checkbox; import com.simsilica.lemur.Checkbox;
import com.simsilica.lemur.Label; import com.simsilica.lemur.Label;
import com.simsilica.lemur.RangedValueModel;
import com.simsilica.lemur.style.ElementId; import com.simsilica.lemur.style.ElementId;
import com.simsilica.lemur.Slider;
import pp.battleship.client.gui.VolumeSlider;
import pp.dialog.Dialog; import pp.dialog.Dialog;
import pp.dialog.StateCheckboxModel; import pp.dialog.StateCheckboxModel;
import pp.dialog.TextInputDialog; import pp.dialog.TextInputDialog;
@ -33,6 +37,7 @@ 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 VolumeSlider volumeSlider;
/** /**
* Constructs the Menu dialog for the Battleship application. * Constructs the Menu dialog for the Battleship application.
@ -45,6 +50,26 @@ class Menu extends Dialog {
addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS
addChild(new Checkbox(lookup("menu.sound-enabled"), addChild(new Checkbox(lookup("menu.sound-enabled"),
new StateCheckboxModel(app, GameSound.class))); new StateCheckboxModel(app, GameSound.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.background-sound-enabled"), new StateCheckboxModel(app, GameMusic.class)));
volumeSlider = new VolumeSlider(app.getStateManager().getState(GameMusic.class));
addChild(volumeSlider);
addChild(loadButton)
.addClickCommands(s -> ifTopDialog(this::loadDialog));
addChild(saveButton);
saveButton.setEnabled(app.getGameLogic().maySaveMap());
// Füge den Slider zum GUI hinzu
addChild(volumeSlider);
addChild(loadButton) addChild(loadButton)
.addClickCommands(s -> ifTopDialog(this::loadDialog)); .addClickCommands(s -> ifTopDialog(this::loadDialog));
addChild(saveButton) addChild(saveButton)
@ -140,4 +165,6 @@ class Menu extends Dialog {
private void saveDialog() { private void saveDialog() {
fileDialog(app.getGameLogic()::saveMap, lookup("menu.map.save")); fileDialog(app.getGameLogic()::saveMap, lookup("menu.map.save"));
} }
} }

View File

@ -32,9 +32,13 @@ import static pp.util.FloatMath.PI;
* It extends the {@link ShipMapSynchronizer} to provide specific synchronization * It extends the {@link ShipMapSynchronizer} to provide specific synchronization
* logic for the sea map. * logic for the sea map.
*/ */
//ANPASSEN!!!!
class SeaSynchronizer extends ShipMapSynchronizer { class SeaSynchronizer extends ShipMapSynchronizer {
private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS
private static final String KING_GEORGE_V_MODEL = "Models/KingGeorgeV/KingGeorgeV.j3o"; //NON-NLS private static final String KING_GEORGE_V_MODEL = "Models/KingGeorgeV/KingGeorgeV.j3o"; //NON-NLS
private static final String FLUGZEUGTRAEGER = "Models/Flugzeugträger_5er/Flugzeugträger.j3o";
private static final String KORVETTE = "Models/Korvette_3er/Korvette.j3o";
private static final String ZERSTOERER = "Models/Zerstörer_2er/Zerstörer.j3o";
private static final String COLOR = "Color"; //NON-NLS private static final String COLOR = "Color"; //NON-NLS
private static final String SHIP = "ship"; //NON-NLS private static final String SHIP = "ship"; //NON-NLS
private static final String SHOT = "shot"; //NON-NLS private static final String SHOT = "shot"; //NON-NLS
@ -140,8 +144,14 @@ class SeaSynchronizer extends ShipMapSynchronizer {
* @param ship the battleship to be represented * @param ship the battleship to be represented
* @return the spatial representing the battleship * @return the spatial representing the battleship
*/ */
//ANPASSEN!!!!!!
private Spatial createShip(Battleship ship) { private Spatial createShip(Battleship ship) {
return ship.getLength() == 4 ? createBattleship(ship) : createBox(ship); if( ship.getLength() == 4){ createBox(ship); return createBattleship(ship); }
else if(ship.getLength()==5){createBox(ship);return createFlugzeugtraeger(ship); }
else if(ship.getLength()==3){createBox(ship);return createKorvette(ship); }
else if(ship.getLength()==2){createBox(ship);return createZerstoerer(ship); }
else if(ship.getLength()==1){createBox(ship);return createZerstoerer(ship); }
else throw new IllegalArgumentException("Deine Länge ist nicht definiert!");
} }
/** /**
@ -186,6 +196,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
* @param ship the battleship to be represented * @param ship the battleship to be represented
* @return the spatial representing the "King George V" battleship * @return the spatial representing the "King George V" battleship
*/ */
//ANPASSEN!!!!
private Spatial createBattleship(Battleship ship) { private Spatial createBattleship(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(KING_GEORGE_V_MODEL); final Spatial model = app.getAssetManager().loadModel(KING_GEORGE_V_MODEL);
@ -196,6 +207,36 @@ class SeaSynchronizer extends ShipMapSynchronizer {
return model; return model;
} }
private Spatial createFlugzeugtraeger(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(FLUGZEUGTRAEGER);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(1.48f);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
private Spatial createKorvette(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(KORVETTE);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(1.48f);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
private Spatial createZerstoerer(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(ZERSTOERER);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(1.48f);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
/** /**
* Calculates the rotation angle for the specified rotation. * Calculates the rotation angle for the specified rotation.
* *

View File

@ -0,0 +1,29 @@
package pp.battleship.client.gui;
import com.simsilica.lemur.Slider;
import pp.battleship.client.GameMusic;
public class VolumeSlider extends Slider {
private final GameMusic piratenMusic;
private double vol;
public VolumeSlider(GameMusic music) {
super();
this.piratenMusic = music;
vol = GameMusic.volumePreference();
getModel().setPercent(vol);
}
public void update() {
if (vol != getModel().getPercent()) {
vol = getModel().getPercent();
piratenMusic.setVolume( (float) vol);
}
}
}

View File

@ -42,10 +42,13 @@ public class ModelExporter extends SimpleApplication {
@Override @Override
public void simpleInitApp() { public void simpleInitApp() {
export("Models/KingGeorgeV/King_George_V.obj", "KingGeorgeV.j3o"); //NON-NLS export("Models/KingGeorgeV/King_George_V.obj", "KingGeorgeV.j3o"); //NON-NLS
export("Models/Flugzeugträger_5er/Flugzeugträger.obj", "Flugzeugträger.j3o");
export("Models/Korvette_3er/caravel.obj", "Korvette.j3o");
export("Models/Zerstörer_2er/ShipV_LOW.obj", "Zerstörer.j3o");
stop(); stop();
} }
/** /**
* Exports spatial into a file * Exports spatial into a file
* *

View File

@ -0,0 +1,112 @@
# Blender 4.2.2 LTS MTL File: 'None'
# www.blender.org
newmtl Main
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.490000 0.257495 0.132300
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.340000 0.164333 0.000000
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.1
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.190000 0.077615 0.017100
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.1_1
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.320000 0.151520 0.060800
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.2
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.000000 0.000000 0.000000
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.3
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.160000 0.151680 0.147200
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.4
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.990000 0.990000 0.990000
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.5
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.070000 0.062265 0.058100
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.6
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.266600 0.466860 0.620000
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Mat.7
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.940000 0.561180 0.206800
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Sail
Ns 319.999939
Ka 1.000000 1.000000 1.000000
Kd 0.070000 0.070000 0.070000
Ks 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2

View File

@ -0,0 +1,52 @@
# Blender 4.2.2 LTS MTL File: 'None'
# www.blender.org
newmtl DeckStuff
Ns 20.000006
Ka 1.000000 1.000000 1.000000
Kd 0.184314 0.600000 0.792157
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 1
newmtl Hull
Ns 20.000006
Ka 1.000000 1.000000 1.000000
Kd 0.701961 0.678431 0.019608
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 1
newmtl MastsRigging
Ns 20.000006
Ka 1.000000 1.000000 1.000000
Kd 0.086275 0.768628 0.117647
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 1
newmtl Poopdeck
Ns 20.000006
Ka 1.000000 1.000000 1.000000
Kd 0.976471 0.211765 0.211765
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 1
newmtl Sail
Ns 20.000006
Ka 1.000000 1.000000 1.000000
Kd 0.588000 0.588000 0.588000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 1

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 472 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 727 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1003 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 658 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 472 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

View File

@ -0,0 +1,32 @@
# Blender MTL File: 'ShipV_0002.blend'
# Material Count: 3
newmtl map_ShipV_001
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.144874 0.000708
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
newmtl map_ShipV_002
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.035888 0.004678
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
newmtl map_ShipV_003
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.001405 0.006690 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 589 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 589 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

@ -72,7 +72,7 @@ class BattleState extends ClientState {
* @return the map (either the opponent's or player's own map) that is affected by the shot * @return the map (either the opponent's or player's own map) that is affected by the shot
*/ */
private ShipMap affectedMap(EffectMessage msg) { private ShipMap affectedMap(EffectMessage msg) {
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap(); return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOpponentMap();
} }
/** /**

View File

@ -8,6 +8,7 @@
package pp.battleship.game.server; package pp.battleship.game.server;
import pp.battleship.BattleshipConfig; import pp.battleship.BattleshipConfig;
import pp.battleship.model.Battleship;
import pp.battleship.model.ShipMap; import pp.battleship.model.ShipMap;
/** /**
@ -51,4 +52,9 @@ public class Player {
public ShipMap getMap() { public ShipMap getMap() {
return map; return map;
} }
} }

View File

@ -168,11 +168,19 @@ public class ServerGameLogic implements ClientInterpreter {
* @param ships the list of ships placed by the player * @param ships the list of ships placed by the player
*/ */
void playerReady(Player player, List<Battleship> ships) { void playerReady(Player player, List<Battleship> ships) {
// Überprüfe, ob die Karte des Spielers gültig ist
if (!player.getMap().isValid2(ships)) {
LOGGER.log(Level.ERROR, "Invalid map configuration for player {0}", player); //NON-NLS
return;
}
if (!readyPlayers.add(player)) { if (!readyPlayers.add(player)) {
LOGGER.log(Level.ERROR, "{0} was already ready", player); //NON-NLS LOGGER.log(Level.ERROR, "{0} was already ready", player); //NON-NLS
return; return;
} }
ships.forEach(player.getMap()::add); ships.forEach(player.getMap()::add);
if (readyPlayers.size() == 2) { if (readyPlayers.size() == 2) {
for (Player p : players) for (Player p : players)
send(p, new StartBattleMessage(p == activePlayer)); send(p, new StartBattleMessage(p == activePlayer));
@ -217,4 +225,5 @@ public class ServerGameLogic implements ClientInterpreter {
} }
} }
} }
} }

View File

@ -10,6 +10,7 @@ package pp.battleship.model;
import pp.battleship.notification.GameEvent; import pp.battleship.notification.GameEvent;
import pp.battleship.notification.GameEventBroker; import pp.battleship.notification.GameEventBroker;
import pp.battleship.notification.ItemAddedEvent; import pp.battleship.notification.ItemAddedEvent;
import pp.battleship.notification.ItemRemovedEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -97,7 +98,7 @@ public class ShipMap {
*/ */
public void remove(Item item) { public void remove(Item item) {
items.remove(item); items.remove(item);
notifyListeners(new ItemAddedEvent(item, this)); notifyListeners(new ItemRemovedEvent(item, this));
} }
/** /**
@ -184,6 +185,35 @@ public class ShipMap {
getShips().filter(s -> s != ship).noneMatch(ship::collidesWith); getShips().filter(s -> s != ship).noneMatch(ship::collidesWith);
} }
//ich brauche eine zweite isValid Methode, da in der ServerGameLogic eine Liste von Schiffen und
//kein einzelnes übergeben wird. Da ich keinen Bock habe, dort eine for Schleife zu bauen, mache ich
//das hier.
public boolean isValid2(List<Battleship> ships) {
// Überprüfe, ob jedes Schiff innerhalb der Spielfeldgrenzen liegt
//istWithinBounds kommt 20 Zeilen weiter unten. Minas gab uns schon Code vor, um das ganze zu lösen.
for (Battleship ship : ships) {
if (!isShipWithinBounds(ship)) {
return false; // Ein Schiff liegt außerhalb der Grenzen
}
}
// Überprüfe, ob sich keine Schiffe überlappen
for (int i = 0; i < ships.size(); i++) {
Battleship ship1 = ships.get(i);
for (int j = i + 1; j < ships.size(); j++) {
Battleship ship2 = ships.get(j);
if (ship1.collidesWith(ship2)) {
return false; // Schiffe überlappen sich
}
}
}
return true; // Alle Schiffe sind korrekt platziert
}
private boolean isShipWithinBounds(Battleship ship) {
return isValid(ship.getMinX(), ship.getMinY()) &&
isValid(ship.getMaxX(), ship.getMaxY());
}
/** /**
* Finds a battleship at the specified coordinates. * Finds a battleship at the specified coordinates.
* *