Solution for exercise 13:
edited another state in the server and client, added the rocket, added the 'Shell.java' and 'ShellControl.java' edited the logic for the states and 3 messages for the server client comunication, edited the 'SeaSynchronizer' and ShipMapSynchronizer', so that the animations will be displayed, added the sound for the rocket
This commit is contained in:
@@ -33,6 +33,7 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
||||
private AudioNode splashSound;
|
||||
private AudioNode shipDestroyedSound;
|
||||
private AudioNode explosionSound;
|
||||
private AudioNode rocketSound;
|
||||
|
||||
/**
|
||||
* Checks if sound is enabled in the preferences.
|
||||
@@ -77,6 +78,7 @@ public void initialize(AppStateManager stateManager, Application app) {
|
||||
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS
|
||||
splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS
|
||||
explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS
|
||||
rocketSound = loadSound(app, "Sound/Effects/rocket-loop-99748.wav");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,8 +94,7 @@ private AudioNode loadSound(Application app, String name) {
|
||||
sound.setLooping(false);
|
||||
sound.setPositional(false);
|
||||
return sound;
|
||||
}
|
||||
catch (AssetLoadException | AssetNotFoundException ex) {
|
||||
} catch (AssetLoadException | AssetNotFoundException ex) {
|
||||
LOGGER.log(Level.ERROR, ex.getMessage(), ex);
|
||||
}
|
||||
return null;
|
||||
@@ -129,6 +130,25 @@ public void receivedEvent(SoundEvent event) {
|
||||
case EXPLOSION -> explosion();
|
||||
case SPLASH -> splash();
|
||||
case DESTROYED_SHIP -> shipDestroyed();
|
||||
case ROCKET -> rocket();
|
||||
case ROCKET_STOP -> rocketStopped();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* this method plays the sound of the rocket
|
||||
*/
|
||||
private void rocket() {
|
||||
if (isEnabled() && splashSound != null)
|
||||
rocketSound.playInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* this method stops the sound of the rocket
|
||||
*/
|
||||
private void rocketStopped() {
|
||||
rocketSound.stop();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,20 +12,26 @@
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.Shot;
|
||||
import pp.util.Position;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
|
||||
/**
|
||||
* Synchronizes the visual representation of the ship map with the game model.
|
||||
* It handles the rendering of ships and shots on the map view, updating the view
|
||||
* whenever changes occur in the model.
|
||||
*/
|
||||
class MapViewSynchronizer extends ShipMapSynchronizer {
|
||||
private static final Logger LOGGER = System.getLogger(MapViewSynchronizer.class.getName());
|
||||
// Constants for rendering properties
|
||||
private static final float SHIP_LINE_WIDTH = 6f;
|
||||
private static final float SHOT_DEPTH = -2f;
|
||||
private static final float SHIP_DEPTH = 0f;
|
||||
private static final float INDENT = 4f;
|
||||
private static final float SHELL_DEPTH = 8f;
|
||||
private static final float SHELL_IN_GRID = 0.1f;
|
||||
|
||||
// Colors used for different visual elements
|
||||
private static final ColorRGBA HIT_COLOR = ColorRGBA.Red;
|
||||
@@ -65,9 +71,9 @@ public Spatial visit(Shot shot) {
|
||||
|
||||
// Create and return a rectangle representing the shot
|
||||
return view.getApp().getDraw().makeRectangle(p1.getX(), p1.getY(),
|
||||
SHOT_DEPTH,
|
||||
p2.getX() - p1.getX(), p2.getY() - p1.getY(),
|
||||
color);
|
||||
SHOT_DEPTH,
|
||||
p2.getX() - p1.getX(), p2.getY() - p1.getY(),
|
||||
color);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,6 +115,35 @@ public Spatial visit(Battleship ship) {
|
||||
return shipNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* this method will create a representation of a shell in the map
|
||||
*
|
||||
* @param shell the Shell element to visit
|
||||
* @return the node the representation is attached to
|
||||
*/
|
||||
@Override
|
||||
public Spatial visit(Shell shell) {
|
||||
LOGGER.log(Logger.Level.DEBUG, "Visiting {0}", shell);
|
||||
final Node shellNode = new Node("shell");
|
||||
final Position target = view.modelToView(shell.getX(), shell.getY());
|
||||
final Position startPosition = view.modelToView(SHELL_IN_GRID, SHELL_IN_GRID);
|
||||
shellNode.attachChild(createShell());
|
||||
shellNode.setLocalTranslation(startPosition.getX(), startPosition.getY(), SHELL_DEPTH);
|
||||
shellNode.scale(18f);
|
||||
shellNode.addControl(new ShellMapControl(view.getApp(), target, shell));
|
||||
return shellNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the red dot for the shell in the MapViewSynchronizer
|
||||
*
|
||||
* @return Spatial
|
||||
*/
|
||||
private Spatial createShell() {
|
||||
return view.getApp().getDraw().makeFilledCircle(ColorRGBA.Red);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a line geometry representing part of the ship's border.
|
||||
*
|
||||
|
||||
@@ -9,9 +9,6 @@
|
||||
|
||||
import com.jme3.effect.ParticleEmitter;
|
||||
import com.jme3.effect.ParticleMesh.Type;
|
||||
import com.jme3.light.AmbientLight;
|
||||
import com.jme3.light.DirectionalLight;
|
||||
import com.jme3.light.Light;
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.material.RenderState.BlendMode;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
@@ -21,18 +18,15 @@
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
import com.jme3.scene.shape.Box;
|
||||
import com.jme3.scene.shape.Cylinder;
|
||||
import com.simsilica.lemur.effect.EffectControl;
|
||||
import pp.battleship.client.BattleshipApp;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.Rotation;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.model.Shot;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
import java.util.Timer;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static pp.util.FloatMath.HALF_PI;
|
||||
@@ -45,6 +39,8 @@
|
||||
* logic for the sea map.
|
||||
*/
|
||||
class SeaSynchronizer extends ShipMapSynchronizer {
|
||||
private static final Logger LOGGER = System.getLogger(SeaSynchronizer.class.getName());
|
||||
|
||||
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 SUBMARINE = "Models/Submarine/submarine.obj";
|
||||
@@ -54,6 +50,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
|
||||
private static final String COLOR = "Color"; //NON-NLS
|
||||
private static final String SHIP = "ship"; //NON-NLS
|
||||
private static final String SHOT = "shot"; //NON-NLS
|
||||
private static final String ROCKET = "Models/Rocket/rocket.j3o";
|
||||
private static final ColorRGBA BOX_COLOR = ColorRGBA.Gray;
|
||||
private static final ColorRGBA SPLASH_COLOR = new ColorRGBA(0f, 0f, 1f, 0.4f);
|
||||
private static final ColorRGBA HIT_COLOR = new ColorRGBA(1f, 0f, 0f, 0.4f);
|
||||
@@ -145,8 +142,7 @@ private ParticleEmitter createFire(Shot shot) {
|
||||
Vector3f firePos = shotWorld.subtract(shipNodePos);
|
||||
if (map.findShipAt(shot.getX(), shot.getY()).getLength() == 2) {
|
||||
hitEffect.setLocalTranslation(firePos.x, 0.25f, firePos.z);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
hitEffect.setLocalTranslation(firePos.x, 0.5f, firePos.z);
|
||||
}
|
||||
return hitEffect;
|
||||
@@ -240,6 +236,38 @@ public Spatial visit(Battleship ship) {
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits a {@link Shell} and creates a graphical representation of it.
|
||||
* The representation is a 3D model
|
||||
*
|
||||
* @param shell the shell to be represented
|
||||
* @return the node containing the graphical representation of the shell
|
||||
*/
|
||||
@Override
|
||||
public Spatial visit(Shell shell) {
|
||||
LOGGER.log(Level.INFO, "was visited by SeaSynchronizer");
|
||||
final Node node = new Node("Shell");
|
||||
node.attachChild(createShell());
|
||||
node.setLocalTranslation(shell.getY() + 0.5f, 10, shell.getX() + 0.5f);
|
||||
node.addControl(new ShellControl(shell, app));
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* this method loads the Model
|
||||
*
|
||||
* @return model
|
||||
*/
|
||||
private Spatial createShell() {
|
||||
LOGGER.log(Level.INFO, "created Shell");
|
||||
final Spatial model = app.getAssetManager().loadModel(ROCKET);
|
||||
model.rotate(PI, PI, 0);
|
||||
model.scale(0.0005f);
|
||||
model.move(0, 0, 0);
|
||||
model.setShadowMode(ShadowMode.CastAndReceive);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the appropriate graphical representation of the specified battleship.
|
||||
* The representation is either a detailed model or a simple box based on the length of the ship.
|
||||
@@ -266,8 +294,8 @@ private Spatial createShip(Battleship ship) {
|
||||
*/
|
||||
private Spatial createBox(Battleship ship) {
|
||||
final Box box = new Box(0.5f * (ship.getMaxY() - ship.getMinY()) + 0.3f,
|
||||
0.3f,
|
||||
0.5f * (ship.getMaxX() - ship.getMinX()) + 0.3f);
|
||||
0.3f,
|
||||
0.5f * (ship.getMaxX() - ship.getMinX()) + 0.3f);
|
||||
final Geometry geometry = new Geometry(SHIP, box);
|
||||
geometry.setMaterial(createColoredMaterial(BOX_COLOR));
|
||||
geometry.setShadowMode(ShadowMode.CastAndReceive);
|
||||
@@ -275,6 +303,12 @@ private Spatial createBox(Battleship ship) {
|
||||
return geometry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Destroyer to represent a battleship that has the length of 3.
|
||||
*
|
||||
* @param ship the battleship to be represented
|
||||
* @return the model representing the battleship
|
||||
*/
|
||||
private Spatial createDestroyer(Battleship ship) {
|
||||
final Spatial model = app.getAssetManager().loadModel(DESTROYER);
|
||||
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()) - HALF_PI, 0f);
|
||||
@@ -284,6 +318,12 @@ private Spatial createDestroyer(Battleship ship) {
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Submarine to represent a battleship that has the length 2.
|
||||
*
|
||||
* @param ship the battleship to be represented
|
||||
* @return the model representing the battleship
|
||||
*/
|
||||
private Spatial createSubmarine(Battleship ship) {
|
||||
final Spatial model = app.getAssetManager().loadModel(SUBMARINE);
|
||||
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
|
||||
@@ -295,6 +335,12 @@ private Spatial createSubmarine(Battleship ship) {
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SmallShip to represent a battleship that has the length y.
|
||||
*
|
||||
* @param ship the battleship to be represented
|
||||
* @return the model representing the battleship
|
||||
*/
|
||||
private Spatial createSmallShip(Battleship ship) {
|
||||
final Spatial model = app.getAssetManager().loadModel(SMALL_SHIP);
|
||||
|
||||
|
||||
@@ -2,40 +2,67 @@
|
||||
|
||||
import com.jme3.renderer.RenderManager;
|
||||
import com.jme3.renderer.ViewPort;
|
||||
import com.jme3.scene.Spatial;
|
||||
import com.jme3.scene.control.AbstractControl;
|
||||
import pp.battleship.client.BattleshipApp;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.ShipMap;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* this class controls the Shell in the view
|
||||
*/
|
||||
public class ShellControl extends AbstractControl {
|
||||
|
||||
private Shell shell;
|
||||
private ShipMap map;
|
||||
private static final Logger LOGGER = System.getLogger(ShellControl.class.getName());
|
||||
|
||||
/**
|
||||
* the shell, that is displayed
|
||||
*/
|
||||
private Shell shell;
|
||||
|
||||
/**
|
||||
* the Height, when the shell will despawn
|
||||
*/
|
||||
private static final Float HEIGHT = 0f;
|
||||
|
||||
public ShellControl(Shell shell, ShipMap map){
|
||||
this.shell=shell;
|
||||
this.map=map;
|
||||
/**
|
||||
* the battleship app
|
||||
*/
|
||||
private BattleshipApp app;
|
||||
|
||||
/**
|
||||
* the constructor for this class
|
||||
*
|
||||
* @param shell the shell it displays
|
||||
* @param app the BattleshipApp
|
||||
*/
|
||||
public ShellControl(Shell shell, BattleshipApp app) {
|
||||
LOGGER.log(Level.INFO, "ShellControl has been initialized");
|
||||
this.shell = shell;
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
/**
|
||||
* this method controls the movement of the shell in dependent on fpt
|
||||
*
|
||||
* @param tpf time per frame (in seconds)
|
||||
*/
|
||||
@Override
|
||||
protected void controlUpdate(float tpf) {
|
||||
if (spatial == null) return;
|
||||
if(spatial.getLocalTranslation().getY()<=HEIGHT){
|
||||
if (spatial.getLocalTranslation().getY() <= HEIGHT) {
|
||||
spatial.getParent().detachChild(spatial);
|
||||
}
|
||||
else{
|
||||
spatial.move(0,-0.5f*tpf,0);
|
||||
app.getGameLogic().send(new AnimationEndMessage(new IntPoint(shell.getX(), shell.getY())));
|
||||
} else {
|
||||
spatial.move(0, -1 * 4f * tpf, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void controlRender(RenderManager rm, ViewPort vp) {
|
||||
|
||||
}
|
||||
|
||||
public Shell getShell(){
|
||||
return shell;
|
||||
//not in use
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package pp.battleship.client.gui;
|
||||
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.renderer.RenderManager;
|
||||
import com.jme3.renderer.ViewPort;
|
||||
import com.jme3.scene.control.AbstractControl;
|
||||
import pp.battleship.client.BattleshipApp;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.notification.Sound;
|
||||
import pp.util.Position;
|
||||
|
||||
/**
|
||||
* this class controls the behaviour of a shell in a 2d map
|
||||
*/
|
||||
public class ShellMapControl extends AbstractControl {
|
||||
private static final System.Logger LOGGER = System.getLogger(ShellMapControl.class.getName());
|
||||
|
||||
/**
|
||||
* the position in map-coordinates
|
||||
*/
|
||||
private final Position position;
|
||||
|
||||
/**
|
||||
* the vector, the shell is going on the 2d screen
|
||||
*/
|
||||
private static final Vector3f vector = new Vector3f();
|
||||
|
||||
/**
|
||||
* the battleship app
|
||||
*/
|
||||
private final BattleshipApp app;
|
||||
|
||||
/**
|
||||
* the shell displayed
|
||||
*/
|
||||
private final Shell shell;
|
||||
|
||||
/**
|
||||
* the constructor for this class
|
||||
*
|
||||
* @param app the Battleship app
|
||||
* @param position the position shot at in map-coordinates
|
||||
* @param shell the shell shot
|
||||
*/
|
||||
public ShellMapControl(BattleshipApp app, Position position, Shell shell) {
|
||||
super();
|
||||
this.position = position;
|
||||
this.app = app;
|
||||
this.shell = shell;
|
||||
vector.set(new Vector3f(position.getX(), position.getY(), 0));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* the update loop for this shell
|
||||
*
|
||||
* @param tpf time per frame (in seconds)
|
||||
*/
|
||||
protected void controlUpdate(float tpf) {
|
||||
if (spatial.getLocalTranslation().getX() >= position.getX() && spatial.getLocalTranslation().getY() >= position.getY()) {
|
||||
app.getGameLogic().playSound(Sound.ROCKET_STOP);
|
||||
spatial.getParent().detachChild(spatial);
|
||||
app.getGameLogic().send(new AnimationEndMessage(new IntPoint(shell.getX(), shell.getY())));
|
||||
LOGGER.log(System.Logger.Level.DEBUG, "shell has been deleted", spatial.getLocalTranslation());
|
||||
} else {
|
||||
spatial.move(vector.mult(tpf));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void controlRender(RenderManager rm, ViewPort vp) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -18,13 +18,11 @@
|
||||
import pp.battleship.game.server.Player;
|
||||
import pp.battleship.game.server.ServerGameLogic;
|
||||
import pp.battleship.game.server.ServerSender;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Shot;
|
||||
@@ -117,11 +115,15 @@ private void initializeSerializables() {
|
||||
Serializer.registerClass(Battleship.class);
|
||||
Serializer.registerClass(IntPoint.class);
|
||||
Serializer.registerClass(Shot.class);
|
||||
Serializer.registerClass(AnimationEndMessage.class);
|
||||
Serializer.registerClass(AnimationStartMessage.class);
|
||||
Serializer.registerClass(BackToBattleStateMessage.class);
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
myServer.addMessageListener(this, MapMessage.class);
|
||||
myServer.addMessageListener(this, ShootMessage.class);
|
||||
myServer.addMessageListener(this, AnimationEndMessage.class);
|
||||
myServer.addConnectionListener(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
Rocket origin:
|
||||
https://free3d.com/3d-model/proton-rocket-31617.html
|
||||
Licence: free for personal use
|
||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
RocketSound origin:
|
||||
https://pixabay.com/sound-effects/rocket-loop-99748/
|
||||
Licence: free to use
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,116 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.server.AnimationStartMessage;
|
||||
import pp.battleship.message.server.BackToBattleStateMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.notification.Sound;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
public class AnimationState extends ClientState {
|
||||
private static final Logger LOGGER = System.getLogger(AnimationState.class.getName());
|
||||
|
||||
/**
|
||||
* Constructs a client state of the specified game logic.
|
||||
*
|
||||
* @param logic the game logic
|
||||
*/
|
||||
AnimationState(ClientGameLogic logic, boolean myTurn, IntPoint pos) {
|
||||
super(logic);
|
||||
if (myTurn) {
|
||||
logic.getOpponentMap().add(new Shell(pos));
|
||||
logic.playSound(Sound.ROCKET);
|
||||
} else {
|
||||
logic.getOwnMap().add(new Shell(pos));
|
||||
logic.playSound(Sound.ROCKET);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean showBattle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reports the effect of a shot based on the server message.
|
||||
*
|
||||
* @param msg the message containing the effect of the shot
|
||||
*/
|
||||
@Override
|
||||
public void receivedEffect(EffectMessage msg) {
|
||||
logic.playSound(Sound.ROCKET_STOP);
|
||||
ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
|
||||
playSound(msg);
|
||||
logic.setInfoText(msg.getInfoTextKey()); //
|
||||
affectedMap(msg).add(msg.getShot());
|
||||
if (destroyedOpponentShip(msg)) {
|
||||
logic.getOpponentMap().add(msg.getDestroyedShip());
|
||||
}
|
||||
if (msg.isGameOver()) {
|
||||
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
|
||||
logic.setState(new GameOverState(logic));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines which map (own or opponent's) should be affected by the shot based on the message.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return the map (either the opponent's or player's own map) that is affected by the shot
|
||||
*/
|
||||
private ShipMap affectedMap(EffectMessage msg) {
|
||||
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the opponent's ship was destroyed by the player's shot.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return true if the shot destroyed an opponent's ship, false otherwise
|
||||
*/
|
||||
private boolean destroyedOpponentShip(EffectMessage msg) {
|
||||
return msg.getDestroyedShip() != null && msg.isOwnShot();
|
||||
}
|
||||
|
||||
/**
|
||||
* receives an AnimationStartMessage and sets the state to AnimationState
|
||||
*
|
||||
* @param msg the message received
|
||||
*/
|
||||
public void receivedAnimationStart(AnimationStartMessage msg) {
|
||||
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
|
||||
* or destruction of a ship.
|
||||
*
|
||||
* @param msg the effect message containing the result of the shot
|
||||
*/
|
||||
private void playSound(EffectMessage msg) {
|
||||
if (!msg.getShot().isHit())
|
||||
logic.playSound(Sound.SPLASH);
|
||||
else if (msg.getDestroyedShip() == null)
|
||||
logic.playSound(Sound.EXPLOSION);
|
||||
else
|
||||
logic.playSound(Sound.DESTROYED_SHIP);
|
||||
}
|
||||
|
||||
/**
|
||||
* receives a BackToBattleStateMessage and sets the state to BattleState
|
||||
*
|
||||
* @param msg the message received
|
||||
*/
|
||||
@Override
|
||||
public void receiveBackToBattleState(BackToBattleStateMessage msg) {
|
||||
logic.setState(new BattleState(logic, msg.isMyTurn()));
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,8 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.AnimationStartMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.notification.Sound;
|
||||
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* Represents the state of the client where players take turns to attack each other's ships.
|
||||
@@ -32,11 +28,21 @@ public BattleState(ClientGameLogic logic, boolean myTurn) {
|
||||
this.myTurn = myTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* this return-statement decides, whether the battle will be shown
|
||||
*
|
||||
* @return true, so the battle will be displayed
|
||||
*/
|
||||
@Override
|
||||
public boolean showBattle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* the logic, responsible for deciding, whether it was a valid input or not
|
||||
*
|
||||
* @param pos the position where the click occurred
|
||||
*/
|
||||
@Override
|
||||
public void clickOpponentMap(IntPoint pos) {
|
||||
if (!myTurn)
|
||||
@@ -46,60 +52,13 @@ else if (logic.getOpponentMap().isValid(pos))
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the effect of a shot based on the server message.
|
||||
* Receives an AnimationStartEvent and changes then the client-state to AnimationState
|
||||
*
|
||||
* @param msg the message containing the effect of the shot
|
||||
* @param msg the message received
|
||||
*/
|
||||
//tODO
|
||||
@Override
|
||||
public void receivedEffect(EffectMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
|
||||
playSound(msg);
|
||||
myTurn = msg.isMyTurn(); //boolean, describes whether it is my turn
|
||||
logic.setInfoText(msg.getInfoTextKey()); //
|
||||
affectedMap(msg).add(msg.getShot());
|
||||
if (destroyedOpponentShip(msg)) {
|
||||
logic.getOpponentMap().add(msg.getDestroyedShip());
|
||||
}
|
||||
if (msg.isGameOver()) {
|
||||
//msg.getRemainingOpponentShips().forEach(logic.getOwnMap()::add);
|
||||
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
|
||||
logic.setState(new GameOverState(logic));
|
||||
}
|
||||
public void receivedAnimationStart(AnimationStartMessage msg) {
|
||||
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines which map (own or opponent's) should be affected by the shot based on the message.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return the map (either the opponent's or player's own map) that is affected by the shot
|
||||
*/
|
||||
private ShipMap affectedMap(EffectMessage msg) {
|
||||
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the opponent's ship was destroyed by the player's shot.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return true if the shot destroyed an opponent's ship, false otherwise
|
||||
*/
|
||||
private boolean destroyedOpponentShip(EffectMessage msg) {
|
||||
return msg.getDestroyedShip() != null && msg.isOwnShot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
|
||||
* or destruction of a ship.
|
||||
*
|
||||
* @param msg the effect message containing the result of the shot
|
||||
*/
|
||||
private void playSound(EffectMessage msg) {
|
||||
if (!msg.getShot().isHit())
|
||||
logic.playSound(Sound.SPLASH);
|
||||
else if (msg.getDestroyedShip() == null)
|
||||
logic.playSound(Sound.EXPLOSION);
|
||||
else
|
||||
logic.playSound(Sound.DESTROYED_SHIP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.model.dto.ShipMapDTO;
|
||||
@@ -226,6 +223,26 @@ public void received(EffectMessage msg) {
|
||||
state.receivedEffect(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the AnimationStartMessage based on the server-state
|
||||
*
|
||||
* @param animationStartMessage the StartMessage received
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationStartMessage animationStartMessage) {
|
||||
state.receivedAnimationStart(animationStartMessage);//TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the BackToBattleStateMessage based on the server-state
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
@Override
|
||||
public void received(BackToBattleStateMessage backToBattleStateMessage) {
|
||||
state.receiveBackToBattleState(backToBattleStateMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the player's own map, opponent's map, and harbor based on the game details.
|
||||
*
|
||||
@@ -304,7 +321,7 @@ public void saveMap(File file) throws IOException {
|
||||
*
|
||||
* @param msg the message to be sent
|
||||
*/
|
||||
void send(ClientMessage msg) {
|
||||
public void send(ClientMessage msg) {
|
||||
if (clientSender == null)
|
||||
LOGGER.log(Level.ERROR, "trying to send {0} with sender==null", msg); //NON-NLS
|
||||
else
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.IntPoint;
|
||||
|
||||
import java.io.File;
|
||||
@@ -165,6 +163,13 @@ void receivedEffect(EffectMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); //NON-NLS
|
||||
}
|
||||
|
||||
/**
|
||||
* @param msg the message received
|
||||
*/
|
||||
void receivedAnimationStart(AnimationStartMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedAnimationStart not allowed in {0}", getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a map from the specified file.
|
||||
*
|
||||
@@ -193,6 +198,15 @@ boolean maySaveMap() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Received a BackToBattleStateMessage and logs it
|
||||
*
|
||||
* @param msg the message received
|
||||
*/
|
||||
public void receiveBackToBattleState(BackToBattleStateMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedBackToBattleState not allowed in {0}", getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Called once per frame by the update loop if this state is active.
|
||||
*
|
||||
|
||||
@@ -8,16 +8,13 @@
|
||||
package pp.battleship.game.server;
|
||||
|
||||
import pp.battleship.BattleshipConfig;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Rotation;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
@@ -39,6 +36,8 @@ public class ServerGameLogic implements ClientInterpreter {
|
||||
private final ServerSender serverSender;
|
||||
private Player activePlayer;
|
||||
private ServerState state = ServerState.WAIT;
|
||||
private boolean playerOneAnimationReady = false;
|
||||
private boolean playerTwoAnimationReady = false;
|
||||
|
||||
/**
|
||||
* Constructs a ServerGameLogic with the specified sender and configuration.
|
||||
@@ -146,11 +145,34 @@ public void received(MapMessage msg, int from) {
|
||||
else if (!verifyMap(msg, from)) {
|
||||
LOGGER.log(Level.ERROR, "player submitted invalid map", state);
|
||||
send(getPlayerById(from), new GameDetails(config));
|
||||
}
|
||||
else
|
||||
} else
|
||||
playerReady(getPlayerById(from), msg.getShips());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received(AnimationEndMessage msg, int from) {
|
||||
LOGGER.log(Level.INFO, "AnimationEndMessage was received by ServerGameLogic");
|
||||
if (state != ServerState.ANIMATION_WAIT_STATE)
|
||||
LOGGER.log(Level.ERROR, "animation not allowed in {0}", state);
|
||||
else if (getPlayerById(from) == players.get(0)) {
|
||||
LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from));
|
||||
playerOneAnimationReady = true;
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
} else if (getPlayerById(from) == players.get(1)) {
|
||||
LOGGER.log(Level.DEBUG, "{0} set to true {1}", getPlayerById(from), getPlayerById(from).toString());
|
||||
playerTwoAnimationReady = true;
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
}
|
||||
if (playerOneAnimationReady && playerTwoAnimationReady) {
|
||||
setState(ServerState.BATTLE);
|
||||
for (Player player : players)
|
||||
send(player, new BackToBattleStateMessage(player == activePlayer));
|
||||
playerOneAnimationReady = false;
|
||||
playerTwoAnimationReady = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* this method returns true, if the given map is valid (don't overlap or out of bound)
|
||||
*
|
||||
@@ -206,8 +228,13 @@ private boolean verifyOverlap(MapMessage msg) {
|
||||
public void received(ShootMessage msg, int from) {
|
||||
if (state != ServerState.BATTLE)
|
||||
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
|
||||
else
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
else {
|
||||
for (Player player : players) {
|
||||
send(player, new AnimationStartMessage(msg.getPosition(), player == activePlayer));
|
||||
setState(ServerState.ANIMATION_WAIT_STATE);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,38 +260,125 @@ void playerReady(Player player, List<Battleship> ships) {
|
||||
/**
|
||||
* Handles the shooting action by the player.
|
||||
*
|
||||
* @param p the player who shot
|
||||
* @param pos the position of the shot
|
||||
* @param p the player who shot
|
||||
* @param position the position of the shot
|
||||
*/
|
||||
void shoot(Player p, IntPoint pos) {
|
||||
if (p != activePlayer) return;
|
||||
final Player otherPlayer = getOpponent(activePlayer);
|
||||
final Battleship selectedShip = otherPlayer.getMap().findShipAt(pos);
|
||||
void shoot(Player p, IntPoint position) {
|
||||
final Battleship selectedShip;
|
||||
selectedShip = getSelectedShip(p, position);
|
||||
if (selectedShip == null) {
|
||||
// shot missed
|
||||
send(activePlayer, EffectMessage.miss(true, pos));
|
||||
send(otherPlayer, EffectMessage.miss(false, pos));
|
||||
activePlayer = otherPlayer;
|
||||
nullShip(p, position);
|
||||
} else {
|
||||
shootShip(p, position, selectedShip);
|
||||
}
|
||||
else {
|
||||
// shot hit a ship
|
||||
selectedShip.hit(pos);
|
||||
if (otherPlayer.getMap().getRemainingShips().isEmpty()) {
|
||||
// game is over
|
||||
send(activePlayer, EffectMessage.won(pos, selectedShip));
|
||||
send(otherPlayer, EffectMessage.lost(pos, selectedShip, activePlayer.getMap().getRemainingShips()));
|
||||
setState(ServerState.GAME_OVER);
|
||||
}
|
||||
else if (selectedShip.isDestroyed()) {
|
||||
// ship has been destroyed, but game is not yet over
|
||||
send(activePlayer, EffectMessage.shipDestroyed(true, pos, selectedShip));
|
||||
send(otherPlayer, EffectMessage.shipDestroyed(false, pos, selectedShip));
|
||||
}
|
||||
else {
|
||||
// ship has been hit, but it hasn't been destroyed
|
||||
send(activePlayer, EffectMessage.hit(true, pos));
|
||||
send(otherPlayer, EffectMessage.hit(false, pos));
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the selected battleship through the shot
|
||||
*
|
||||
* @param p the current player
|
||||
* @param position the position shot at
|
||||
* @return the battleship, on the position shot at
|
||||
*/
|
||||
private Battleship getSelectedShip(Player p, IntPoint position) {
|
||||
return (p != activePlayer) ? p.getMap().findShipAt(position) : getOpponent(p).getMap().findShipAt(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* this method handles the shot logic, if the shot misses
|
||||
*
|
||||
* @param p the current player
|
||||
* @param position the position shot at
|
||||
*/
|
||||
private void nullShip(Player p, IntPoint position) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.miss(false, position));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.miss(true, position));
|
||||
}
|
||||
//switches the active player, when both have finished their animation
|
||||
if (playerOneAnimationReady && playerTwoAnimationReady) {
|
||||
LOGGER.log(Level.DEBUG, "switched active player");
|
||||
if (p != activePlayer) {
|
||||
activePlayer = p;
|
||||
} else {
|
||||
activePlayer = getOpponent(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* this method handles the shot logic, if the shot misses
|
||||
*
|
||||
* @param p the current player
|
||||
* @param position the position shot at
|
||||
* @param selectedShip the ship shot at
|
||||
*/
|
||||
private void shootShip(Player p, IntPoint position, Battleship selectedShip) {
|
||||
selectedShip.hit(position);
|
||||
if (isGameOver(getOpponent(p))) {
|
||||
gameOver(p, position, selectedShip);
|
||||
} else if (selectedShip.isDestroyed()) {
|
||||
shipDestroys(p, position, selectedShip);
|
||||
} else {
|
||||
shipHit(p, position);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checks, if the given player ends the game, bc he has no ships
|
||||
*
|
||||
* @param p the player
|
||||
* @return true, if the given player has no shi left
|
||||
*/
|
||||
private boolean isGameOver(Player p) {
|
||||
return p.getMap().getRemainingShips().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* handles the shot logic,if the game is over
|
||||
*
|
||||
* @param p the player
|
||||
* @param position the position shot at
|
||||
* @param selectedShip the current ship, shot at
|
||||
*/
|
||||
private void gameOver(Player p, IntPoint position, Battleship selectedShip) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.lost(position, selectedShip, activePlayer.getMap().getRemainingShips()));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.won(position, selectedShip));
|
||||
}
|
||||
if (playerOneAnimationReady && playerTwoAnimationReady) {
|
||||
setState(ServerState.GAME_OVER);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handles the logic,if the ship is destroyed and it's not gameOver
|
||||
*
|
||||
* @param p the player
|
||||
* @param position the position shot at
|
||||
* @param selectedShip the ship shot at
|
||||
*/
|
||||
private void shipDestroys(Player p, IntPoint position, Battleship selectedShip) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.shipDestroyed(false, position, selectedShip));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.shipDestroyed(true, position, selectedShip));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handles the logic,if the ship is hit and it's not gameOver
|
||||
*
|
||||
* @param p the player
|
||||
* @param position the position shot at
|
||||
*/
|
||||
private void shipHit(Player p, IntPoint position) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.hit(false, position));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.hit(true, position));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,5 +29,10 @@ enum ServerState {
|
||||
/**
|
||||
* The game has ended because all the ships of one player have been destroyed.
|
||||
*/
|
||||
GAME_OVER
|
||||
GAME_OVER,
|
||||
|
||||
/**
|
||||
* The game has paused, cause the clients need time to play their respective animations
|
||||
*/
|
||||
ANIMATION_WAIT_STATE
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package pp.battleship.game.singlemode;
|
||||
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
@@ -63,6 +64,17 @@ public void received(MapMessage msg, int from) {
|
||||
copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives an AnimationEndMessage, copies it
|
||||
*
|
||||
* @param msg the message
|
||||
* @param from the connectionID
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationEndMessage msg, int from) {
|
||||
copiedMessage = new AnimationEndMessage(msg.getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the provided {@link Battleship}.
|
||||
*
|
||||
|
||||
@@ -9,19 +9,19 @@
|
||||
|
||||
import pp.battleship.game.client.BattleshipClient;
|
||||
import pp.battleship.game.client.ClientGameLogic;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* A proxy class that interprets messages from the server and forwards them to the BattleshipClient.
|
||||
* Implements the ServerInterpreter interface to handle specific server messages.
|
||||
*/
|
||||
class InterpreterProxy implements ServerInterpreter {
|
||||
private static final Logger LOGGER = System.getLogger(InterpreterProxy.class.getName());
|
||||
|
||||
private final BattleshipClient playerClient;
|
||||
|
||||
/**
|
||||
@@ -55,8 +55,7 @@ private void loadMap() {
|
||||
final ClientGameLogic clientGameLogic = playerClient.getGameLogic();
|
||||
try {
|
||||
clientGameLogic.loadMap(playerClient.getConfig().getOwnMap());
|
||||
}
|
||||
catch (IOException e) {
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to load PlayerClient map", e);
|
||||
}
|
||||
clientGameLogic.mapFinished();
|
||||
@@ -82,6 +81,27 @@ public void received(EffectMessage msg) {
|
||||
forward(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Received an AnimationStartMessage, logs it and forwards it
|
||||
*
|
||||
* @param animationStartMessage the StartMessage received
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationStartMessage animationStartMessage) {
|
||||
LOGGER.log(Level.INFO, "AnimationStartMessage was received by InterpreterProxy");
|
||||
forward(animationStartMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Received a BackToBattleStateMessage, and forwards it
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
@Override
|
||||
public void received(BackToBattleStateMessage backToBattleStateMessage) {
|
||||
forward(backToBattleStateMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards the specified ServerMessage to the client's game logic by enqueuing the message acceptance.
|
||||
*
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package pp.battleship.game.singlemode;
|
||||
|
||||
import pp.battleship.game.client.BattleshipClient;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.dto.ShipMapDTO;
|
||||
import pp.util.RandomPositionIterator;
|
||||
@@ -124,4 +122,27 @@ public void received(EffectMessage msg) {
|
||||
if (msg.isMyTurn())
|
||||
shoot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Received an AnimationStartMessage, logs it, and sends an AnimationEndMessage, bc it can't be displayed
|
||||
*
|
||||
* @param msg the StartMessage received
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationStartMessage msg) {
|
||||
LOGGER.log(Level.INFO, "AnimationStartMessage was received by RobotClient");
|
||||
connection.sendRobotMessage(new AnimationEndMessage(msg.getPosition()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Received an Message, if the BackToBattleState is active, checks, if it's his turn and fires, if it is
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
@Override
|
||||
public void received(BackToBattleStateMessage backToBattleStateMessage) {
|
||||
if (backToBattleStateMessage.isMyTurn()) {
|
||||
shoot();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package pp.battleship.message.client;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
import pp.battleship.model.IntPoint;
|
||||
|
||||
/**
|
||||
* this class represents the AnimationEndMessage, that is sent from the clients to the server, to say, they are finished with the animation
|
||||
*/
|
||||
@Serializable
|
||||
public class AnimationEndMessage extends ClientMessage {
|
||||
|
||||
/**
|
||||
* the position, where was shot at
|
||||
*/
|
||||
private IntPoint position;
|
||||
|
||||
/**
|
||||
* this private constructor is used for the serialization
|
||||
*/
|
||||
private AnimationEndMessage() { /* nothing */}
|
||||
|
||||
/**
|
||||
* this is the constructor for this class
|
||||
*
|
||||
* @param position IntPoint
|
||||
*/
|
||||
public AnimationEndMessage(IntPoint position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* the getter for the position
|
||||
*
|
||||
* @return IntPoint position
|
||||
*/
|
||||
public IntPoint getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* the accept-method for the ClientInterpreter
|
||||
*
|
||||
* @param interpreter the visitor to be used for processing
|
||||
* @param from the connection ID of the sender
|
||||
*/
|
||||
@Override
|
||||
public void accept(ClientInterpreter interpreter, int from) {
|
||||
interpreter.received(this, from);
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,12 @@ public interface ClientInterpreter {
|
||||
* @param from the connection ID from which the message was received
|
||||
*/
|
||||
void received(MapMessage msg, int from);
|
||||
|
||||
/**
|
||||
* Processes a received AnimationEndMessage
|
||||
*
|
||||
* @param animationEndMessage the message processed
|
||||
* @param from the connectionID
|
||||
*/
|
||||
void received(AnimationEndMessage animationEndMessage, int from);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package pp.battleship.message.server;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
import pp.battleship.model.IntPoint;
|
||||
|
||||
/**
|
||||
* This class represents the AnimationStartMessage, which tells the clients, that they should display their animations
|
||||
*/
|
||||
@Serializable
|
||||
public class AnimationStartMessage extends ServerMessage {
|
||||
private IntPoint position;
|
||||
private boolean myTurn;
|
||||
|
||||
/**
|
||||
* this empty constructor is needed for the serialization
|
||||
*/
|
||||
private AnimationStartMessage() { /* nothing */}
|
||||
|
||||
/**
|
||||
* this is the constructor for the AnimationMessage
|
||||
*
|
||||
* @param position the position, where was shot
|
||||
* @param isTurn of the receiving client
|
||||
*/
|
||||
public AnimationStartMessage(IntPoint position, boolean isTurn) {
|
||||
this.position = position;
|
||||
this.myTurn = isTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the position
|
||||
*
|
||||
* @return IntPoint position
|
||||
*/
|
||||
public IntPoint getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for myTurn
|
||||
*
|
||||
* @return boolean myTurn
|
||||
*/
|
||||
public boolean isMyTurn() {
|
||||
return myTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* this method accept ServerInterpreter
|
||||
*
|
||||
* @param interpreter the visitor to be used for processing
|
||||
*/
|
||||
@Override
|
||||
public void accept(ServerInterpreter interpreter) {
|
||||
interpreter.received(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the Info text for each state
|
||||
*
|
||||
* @return String InfoText
|
||||
*/
|
||||
@Override
|
||||
public String getInfoTextKey() {
|
||||
return (position + " to be animated");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package pp.battleship.message.server;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
|
||||
|
||||
/**
|
||||
* this Message tells the clients, that they should return to the BattleState
|
||||
*/
|
||||
@Serializable
|
||||
public class BackToBattleStateMessage extends ServerMessage {
|
||||
|
||||
/**
|
||||
* the boolean describes, whether it is his turn
|
||||
*/
|
||||
private boolean isMyTurn;
|
||||
|
||||
/**
|
||||
* the private constructor is used for the serialization
|
||||
*/
|
||||
private BackToBattleStateMessage() { /* nothing */}
|
||||
|
||||
/**
|
||||
* the constructor for the message
|
||||
*
|
||||
* @param isMyTurn indicates, whether the client, that receives this message, has his turn
|
||||
*/
|
||||
public BackToBattleStateMessage(boolean isMyTurn) {
|
||||
this.isMyTurn = isMyTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the MyTurn
|
||||
*
|
||||
* @return boolean myTurn
|
||||
*/
|
||||
public boolean isMyTurn() {
|
||||
return isMyTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* accept-method for ServerInterpreter
|
||||
*
|
||||
* @param interpreter the visitor to be used for processing
|
||||
*/
|
||||
@Override
|
||||
public void accept(ServerInterpreter interpreter) {
|
||||
interpreter.received(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns info text for this Message
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Override
|
||||
public String getInfoTextKey() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,18 @@ public interface ServerInterpreter {
|
||||
* @param msg the EffectMessage received
|
||||
*/
|
||||
void received(EffectMessage msg);
|
||||
|
||||
/**
|
||||
* Handles the AnimationStartMessage
|
||||
*
|
||||
* @param animationStartMessage the StartMessage received
|
||||
*/
|
||||
void received(AnimationStartMessage animationStartMessage);
|
||||
|
||||
/**
|
||||
* Handles the Message, that sets the clients back in the BattleState
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
void received(BackToBattleStateMessage backToBattleStateMessage);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,88 @@
|
||||
package pp.battleship.model;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* this class is the model for the shell, that hits the Battleships
|
||||
*/
|
||||
public class Shell implements Item {
|
||||
|
||||
private IntPoint target;
|
||||
private static final Logger LOGGER = System.getLogger(Shell.class.getName());
|
||||
|
||||
public Shell(IntPoint target){
|
||||
this.target=target;
|
||||
/**
|
||||
* the Target, the Shell will hit, displayed through: x,y
|
||||
*/
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
/**
|
||||
* the constructor for this class
|
||||
*
|
||||
* @param target the target, the shell is fired at
|
||||
*/
|
||||
public Shell(IntPoint target) {
|
||||
this.x = target.getX();
|
||||
this.y = target.getY();
|
||||
LOGGER.log(Level.INFO, "Shell has been initialized");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* the accept-method for a generic visitor
|
||||
*
|
||||
* @param visitor the visitor performing operations on the item
|
||||
* @param <T> cause it is generic
|
||||
* @return T
|
||||
*/
|
||||
@Override
|
||||
public <T> T accept(Visitor<T> visitor) {
|
||||
return visitor.visit(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* the accept-method for the void visitor
|
||||
*
|
||||
* @param visitor the visitor performing operations on the item
|
||||
*/
|
||||
@Override
|
||||
public void accept(VoidVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
public IntPoint getTarget(){
|
||||
return target;
|
||||
|
||||
/**
|
||||
* getter for the x coordinate
|
||||
*
|
||||
* @return int x coordinate
|
||||
*/
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the y coordinate
|
||||
*
|
||||
* @return int y coordinate
|
||||
*/
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for x coordinate
|
||||
*
|
||||
* @param x the new value of x coordinate
|
||||
*/
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for y coordinate
|
||||
*
|
||||
* @param y the new value of y coordinate
|
||||
*/
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,15 @@ public void add(Shot shot) {
|
||||
addItem(shot);
|
||||
}
|
||||
|
||||
/**
|
||||
* adds the shell to the ShipMap
|
||||
*
|
||||
* @param shell the shell that's added
|
||||
*/
|
||||
public void add(Shell shell) {
|
||||
addItem(shell);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an item from the map and triggers an item removal event.
|
||||
*
|
||||
@@ -181,8 +190,8 @@ public int getHeight() {
|
||||
*/
|
||||
public boolean isValid(Battleship ship) {
|
||||
return isValid(ship.getMinX(), ship.getMinY()) &&
|
||||
isValid(ship.getMaxX(), ship.getMaxY()) &&
|
||||
getShips().filter(s -> s != ship).noneMatch(ship::collidesWith);
|
||||
isValid(ship.getMaxX(), ship.getMaxY()) &&
|
||||
getShips().filter(s -> s != ship).noneMatch(ship::collidesWith);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,8 +203,8 @@ public boolean isValid(Battleship ship) {
|
||||
*/
|
||||
public Battleship findShipAt(int x, int y) {
|
||||
return getShips().filter(ship -> ship.contains(x, y))
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,7 +236,7 @@ public boolean isValid(IntPosition pos) {
|
||||
*/
|
||||
public boolean isValid(int x, int y) {
|
||||
return x >= 0 && x < width &&
|
||||
y >= 0 && y < height;
|
||||
y >= 0 && y < height;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,5 +22,13 @@ public enum Sound {
|
||||
/**
|
||||
* Sound of a ship being destroyed.
|
||||
*/
|
||||
DESTROYED_SHIP
|
||||
DESTROYED_SHIP,
|
||||
/**
|
||||
* Sound of a rocket
|
||||
*/
|
||||
ROCKET,
|
||||
/**
|
||||
* Stops the Sound of the rocket
|
||||
*/
|
||||
ROCKET_STOP
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ public void testRemoveItem() {
|
||||
map.remove(battleship);
|
||||
final List<Item> items = map.getItems();
|
||||
assertFalse(items.contains(battleship));
|
||||
//TODO
|
||||
verify(mockBroker).notifyListeners(any(ItemAddedEvent.class));
|
||||
verify(mockBroker).notifyListeners(any(ItemRemovedEvent.class));
|
||||
}
|
||||
|
||||
@@ -18,13 +18,11 @@
|
||||
import pp.battleship.game.server.Player;
|
||||
import pp.battleship.game.server.ServerGameLogic;
|
||||
import pp.battleship.game.server.ServerSender;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Shot;
|
||||
@@ -118,11 +116,15 @@ private void initializeSerializables() {
|
||||
Serializer.registerClass(Battleship.class);
|
||||
Serializer.registerClass(IntPoint.class);
|
||||
Serializer.registerClass(Shot.class);
|
||||
Serializer.registerClass(AnimationEndMessage.class);
|
||||
Serializer.registerClass(AnimationStartMessage.class);
|
||||
Serializer.registerClass(BackToBattleStateMessage.class);
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
myServer.addMessageListener(this, MapMessage.class);
|
||||
myServer.addMessageListener(this, ShootMessage.class);
|
||||
myServer.addMessageListener(this, AnimationEndMessage.class);
|
||||
myServer.addConnectionListener(this);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user