Add Shell
This commit is contained in:
@@ -13,13 +13,11 @@
|
|||||||
import pp.battleship.game.server.Player;
|
import pp.battleship.game.server.Player;
|
||||||
import pp.battleship.game.server.ServerGameLogic;
|
import pp.battleship.game.server.ServerGameLogic;
|
||||||
import pp.battleship.game.server.ServerSender;
|
import pp.battleship.game.server.ServerSender;
|
||||||
|
import pp.battleship.message.client.AnimationEndMessage;
|
||||||
import pp.battleship.message.client.ClientMessage;
|
import pp.battleship.message.client.ClientMessage;
|
||||||
import pp.battleship.message.client.MapMessage;
|
import pp.battleship.message.client.MapMessage;
|
||||||
import pp.battleship.message.client.ShootMessage;
|
import pp.battleship.message.client.ShootMessage;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
import pp.battleship.message.server.GameDetails;
|
|
||||||
import pp.battleship.message.server.ServerMessage;
|
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.Battleship;
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
import pp.battleship.model.Shot;
|
import pp.battleship.model.Shot;
|
||||||
@@ -105,6 +103,9 @@ private void processNextMessage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initializeSerializables() {
|
private void initializeSerializables() {
|
||||||
|
Serializer.registerClass(AnimationEndMessage.class);
|
||||||
|
Serializer.registerClass(AnimationStartMessage.class);
|
||||||
|
Serializer.registerClass(SwitchBattleState.class);
|
||||||
Serializer.registerClass(GameDetails.class);
|
Serializer.registerClass(GameDetails.class);
|
||||||
Serializer.registerClass(StartBattleMessage.class);
|
Serializer.registerClass(StartBattleMessage.class);
|
||||||
Serializer.registerClass(MapMessage.class);
|
Serializer.registerClass(MapMessage.class);
|
||||||
@@ -116,6 +117,7 @@ private void initializeSerializables() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void registerListeners() {
|
private void registerListeners() {
|
||||||
|
myServer.addMessageListener(this, AnimationEndMessage.class);
|
||||||
myServer.addMessageListener(this, MapMessage.class);
|
myServer.addMessageListener(this, MapMessage.class);
|
||||||
myServer.addMessageListener(this, ShootMessage.class);
|
myServer.addMessageListener(this, ShootMessage.class);
|
||||||
myServer.addConnectionListener(this);
|
myServer.addConnectionListener(this);
|
||||||
|
|||||||
@@ -8,10 +8,13 @@
|
|||||||
package pp.battleship.client.gui;
|
package pp.battleship.client.gui;
|
||||||
|
|
||||||
import com.jme3.math.ColorRGBA;
|
import com.jme3.math.ColorRGBA;
|
||||||
|
import com.jme3.math.Vector3f;
|
||||||
import com.jme3.scene.Geometry;
|
import com.jme3.scene.Geometry;
|
||||||
import com.jme3.scene.Node;
|
import com.jme3.scene.Node;
|
||||||
import com.jme3.scene.Spatial;
|
import com.jme3.scene.Spatial;
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.Battleship;
|
||||||
|
import pp.battleship.model.IntPoint;
|
||||||
|
import pp.battleship.model.Shell;
|
||||||
import pp.battleship.model.Shot;
|
import pp.battleship.model.Shot;
|
||||||
import pp.util.Position;
|
import pp.util.Position;
|
||||||
|
|
||||||
@@ -26,6 +29,10 @@ class MapViewSynchronizer extends ShipMapSynchronizer {
|
|||||||
private static final float SHOT_DEPTH = -2f;
|
private static final float SHOT_DEPTH = -2f;
|
||||||
private static final float SHIP_DEPTH = 0f;
|
private static final float SHIP_DEPTH = 0f;
|
||||||
private static final float INDENT = 4f;
|
private static final float INDENT = 4f;
|
||||||
|
private static final float SHELL_DEPTH = 8f;
|
||||||
|
private static final float SHELL_SIZE = 0.75f;
|
||||||
|
private static final float SHELL_CENTERED_IN_MAP_GRID = 0.0625f;
|
||||||
|
|
||||||
|
|
||||||
// Colors used for different visual elements
|
// Colors used for different visual elements
|
||||||
private static final ColorRGBA HIT_COLOR = ColorRGBA.Red;
|
private static final ColorRGBA HIT_COLOR = ColorRGBA.Red;
|
||||||
@@ -109,6 +116,29 @@ public Spatial visit(Battleship ship) {
|
|||||||
return shipNode;
|
return shipNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a visual representation of a shell on the map.
|
||||||
|
*
|
||||||
|
* @param shell the Shell object representing the shell in the model
|
||||||
|
* @return a Spatial representing the shell
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Spatial visit(Shell shell) {
|
||||||
|
final Node shellNode = new Node("shell");
|
||||||
|
|
||||||
|
final Position p1 = view.modelToView(shell.getX(), shell.getY());
|
||||||
|
final Position p2 = view.modelToView(shell.getX() + SHELL_SIZE, shell.getY() + SHELL_SIZE);
|
||||||
|
|
||||||
|
final Position startPosition = view.modelToView(SHELL_CENTERED_IN_MAP_GRID, SHELL_CENTERED_IN_MAP_GRID);
|
||||||
|
|
||||||
|
shellNode.attachChild(view.getApp().getDraw().makeRectangle(startPosition.getX(), startPosition.getY(), SHELL_DEPTH, p2.getX() - p1.getX(), p2.getY() - p1.getY(), ColorRGBA.Magenta));
|
||||||
|
shellNode.setLocalTranslation(startPosition.getX(), startPosition.getY(), SHELL_DEPTH);
|
||||||
|
shellNode.addControl(new ShellControlMap(p1, view.getApp(), new IntPoint(shell.getX(), shell.getY())));
|
||||||
|
return shellNode;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a line geometry representing part of the ship's border.
|
* Creates a line geometry representing part of the ship's border.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -82,6 +82,22 @@ public void createHitParticles(Node node, Shot shot) {
|
|||||||
particles3.setLocalTranslation(shot.getY() + 1f, 0 , shot.getX() + 1f);
|
particles3.setLocalTranslation(shot.getY() + 1f, 0 , shot.getX() + 1f);
|
||||||
particles3.emitAllParticles();
|
particles3.emitAllParticles();
|
||||||
|
|
||||||
|
ParticleEmitter flames = new ParticleEmitter("Effect", ParticleMesh.Type.Triangle,300);
|
||||||
|
flames.setMaterial(material);
|
||||||
|
flames.setImagesX(1);
|
||||||
|
flames.setImagesY(1);
|
||||||
|
flames.setStartColor(ColorRGBA.Red);
|
||||||
|
flames.setEndColor(ColorRGBA.Orange);
|
||||||
|
flames.getParticleInfluencer().setInitialVelocity(new Vector3f(0,1.0f,0));
|
||||||
|
flames.setStartSize(0.4f);
|
||||||
|
flames.setEndSize(0.1f);
|
||||||
|
flames.setGravity(0, -0.2f, 0);
|
||||||
|
flames.setLowLife(0.7f);
|
||||||
|
flames.setHighLife(1.5f);
|
||||||
|
flames.setLocalTranslation(shot.getY(), 0 , shot.getX());
|
||||||
|
flames.setLocalTranslation(flames.getLocalTranslation().subtract(node.getLocalTranslation()));
|
||||||
|
flames.setParticlesPerSec(70);
|
||||||
|
|
||||||
node.attachChild(smokeParticles);
|
node.attachChild(smokeParticles);
|
||||||
smokeParticles.addControl((Control) new ParticleControl(smokeParticles, node));
|
smokeParticles.addControl((Control) new ParticleControl(smokeParticles, node));
|
||||||
|
|
||||||
@@ -91,6 +107,9 @@ public void createHitParticles(Node node, Shot shot) {
|
|||||||
node.attachChild(particles3);
|
node.attachChild(particles3);
|
||||||
particles3.addControl((Control) new ParticleControl(particles3, node));
|
particles3.addControl((Control) new ParticleControl(particles3, node));
|
||||||
|
|
||||||
|
//node.attachChild(flames);
|
||||||
|
//flames.addControl((Control) new ParticleControl(flames, node));
|
||||||
|
|
||||||
LOGGER.log(System.Logger.Level.DEBUG, "Hit-particles at {0}", smokeParticles.getLocalTranslation().toString());
|
LOGGER.log(System.Logger.Level.DEBUG, "Hit-particles at {0}", smokeParticles.getLocalTranslation().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,7 @@
|
|||||||
import com.jme3.scene.shape.Box;
|
import com.jme3.scene.shape.Box;
|
||||||
import com.jme3.scene.shape.Cylinder;
|
import com.jme3.scene.shape.Cylinder;
|
||||||
import pp.battleship.client.BattleshipApp;
|
import pp.battleship.client.BattleshipApp;
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.*;
|
||||||
import pp.battleship.model.Rotation;
|
|
||||||
import pp.battleship.model.ShipMap;
|
|
||||||
import pp.battleship.model.Shot;
|
|
||||||
|
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static pp.util.FloatMath.HALF_PI;
|
import static pp.util.FloatMath.HALF_PI;
|
||||||
@@ -44,6 +41,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
|
|||||||
private static final ColorRGBA BOX_COLOR = ColorRGBA.Gray;
|
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 SPLASH_COLOR = new ColorRGBA(0f, 0f, 1f, 0.4f);
|
||||||
private static final ColorRGBA HIT_COLOR = new ColorRGBA(1f, 0f, 0f, 0.4f);
|
private static final ColorRGBA HIT_COLOR = new ColorRGBA(1f, 0f, 0f, 0.4f);
|
||||||
|
private static final String BOMB = "Models/Bomb/Files/Bomb GBU-43B(MOAB).obj";
|
||||||
|
|
||||||
private final ShipMap map;
|
private final ShipMap map;
|
||||||
private final BattleshipApp app;
|
private final BattleshipApp app;
|
||||||
@@ -66,6 +64,35 @@ public SeaSynchronizer(BattleshipApp app, Node root, ShipMap map) {
|
|||||||
addExisting();
|
addExisting();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits a {@link Shell} and creates a graphical representation of it.
|
||||||
|
*
|
||||||
|
* @param shell the shell to be represented
|
||||||
|
* @return the graphical representation of the shell
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Spatial visit(Shell shell) {
|
||||||
|
final Node node = new Node("shell");
|
||||||
|
|
||||||
|
final Spatial model = app.getAssetManager().loadModel(BOMB);
|
||||||
|
|
||||||
|
model.rotate(-PI/2, 0, 0);
|
||||||
|
model.scale(0.09f);
|
||||||
|
model.setShadowMode(ShadowMode.CastAndReceive);
|
||||||
|
model.move(0, 0, 0);
|
||||||
|
|
||||||
|
node.attachChild(model);
|
||||||
|
|
||||||
|
final float x = shell.getY();
|
||||||
|
final float z = shell.getX();
|
||||||
|
|
||||||
|
node.setLocalTranslation(x, 8f, z);
|
||||||
|
ShellControll control = new ShellControll(shell, app);
|
||||||
|
node.addControl(control);
|
||||||
|
return node;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visits a {@link Shot} and creates a graphical representation of it.
|
* Visits a {@link Shot} and creates a graphical representation of it.
|
||||||
* If the shot is a hit, it attaches the representation to the ship node.
|
* If the shot is a hit, it attaches the representation to the ship node.
|
||||||
@@ -84,7 +111,7 @@ public Spatial visit(Shot shot) {
|
|||||||
* contains the ship model as a child so that it moves with the ship.
|
* contains the ship model as a child so that it moves with the ship.
|
||||||
*
|
*
|
||||||
* @param shot a hit
|
* @param shot a hit
|
||||||
* @return always null to prevent the representation from being attached
|
* @return always null to prevent the representation from being sattached
|
||||||
* to the items node as well
|
* to the items node as well
|
||||||
*/
|
*/
|
||||||
private Spatial handleHit(Shot shot) {
|
private Spatial handleHit(Shot shot) {
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
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.util.Position;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controls the movement of a shell in a specified position on the battlefield.
|
||||||
|
* This control updates the shell's position and sends a message when it reaches
|
||||||
|
* its target location.
|
||||||
|
*/
|
||||||
|
public class ShellControlMap extends AbstractControl {
|
||||||
|
private final Position position; // Target position for the shell
|
||||||
|
private final IntPoint pos; // Target coordinates as an IntPoint
|
||||||
|
private static final Vector3f vector = new Vector3f(); // Movement vector
|
||||||
|
private final BattleshipApp app; // Reference to the main application
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a ShellControlMap object with the specified target position,
|
||||||
|
* application instance, and target coordinates.
|
||||||
|
*
|
||||||
|
* @param position The target Position for the shell.
|
||||||
|
* @param app The BattleshipApp instance managing the game logic.
|
||||||
|
* @param pos The IntPoint representing the target coordinates of the shell.
|
||||||
|
*/
|
||||||
|
public ShellControlMap(Position position, BattleshipApp app, IntPoint pos) {
|
||||||
|
super();
|
||||||
|
this.position = position;
|
||||||
|
this.pos = pos;
|
||||||
|
this.app = app;
|
||||||
|
vector.set(new Vector3f(position.getX(), position.getY(), 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the shell's position based on the time per frame.
|
||||||
|
* If the shell reaches or exceeds the target position, it detaches
|
||||||
|
* the shell from the scene and sends an animation end message.
|
||||||
|
*
|
||||||
|
* @param tpf Time per frame, used to calculate movement.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void controlUpdate(float tpf) {
|
||||||
|
spatial.move(vector.mult(tpf));
|
||||||
|
if (spatial.getLocalTranslation().getX() >= position.getX() &&
|
||||||
|
spatial.getLocalTranslation().getY() >= position.getY()) {
|
||||||
|
spatial.getParent().detachChild(spatial);
|
||||||
|
app.getGameLogic().send(new AnimationEndMessage(pos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the shell control. This method can be overridden to implement
|
||||||
|
* custom rendering behavior, but it is currently empty.
|
||||||
|
*
|
||||||
|
* @param rm The RenderManager for rendering the control.
|
||||||
|
* @param vp The ViewPort where the control is rendered.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) {
|
||||||
|
// No rendering logic implemented for ShellControlMap
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package pp.battleship.client.gui;
|
||||||
|
|
||||||
|
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 java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controls the movement and behavior of a shell in the battleship game.
|
||||||
|
* This control updates the position of the shell and sends a message
|
||||||
|
* when the shell reaches the ground.
|
||||||
|
*/
|
||||||
|
public class ShellControll extends AbstractControl {
|
||||||
|
private final Shell shell;
|
||||||
|
private final BattleshipApp app;
|
||||||
|
|
||||||
|
private static final float MOVE_SPEED = 20.0f; // Speed at which the shell moves
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a ShellControll object with the specified shell and application instance.
|
||||||
|
*
|
||||||
|
* @param shell The Shell instance to control.
|
||||||
|
* @param app The BattleshipApp instance managing the game logic.
|
||||||
|
*/
|
||||||
|
public ShellControll(Shell shell, BattleshipApp app) {
|
||||||
|
this.shell = shell;
|
||||||
|
this.app = app;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the shell's position based on the time per frame.
|
||||||
|
* If the shell's position falls below a certain threshold,
|
||||||
|
* it detaches the shell from the scene and sends an animation end message.
|
||||||
|
*
|
||||||
|
* @param tpf Time per frame, used to calculate movement.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void controlUpdate(float tpf) {
|
||||||
|
spatial.move(0, -MOVE_SPEED * tpf, 0);
|
||||||
|
if (spatial.getLocalTranslation().getY() <= 0.1f) {
|
||||||
|
spatial.getParent().detachChild(spatial);
|
||||||
|
app.getGameLogic().send(new AnimationEndMessage(new IntPoint(shell.getX(), shell.getY())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the shell control. This method can be overridden to implement
|
||||||
|
* custom rendering behavior, but it is currently empty.
|
||||||
|
*
|
||||||
|
* @param rm The RenderManager for rendering the control.
|
||||||
|
* @param vp The ViewPort where the control is rendered.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) {
|
||||||
|
// No rendering logic implemented for ShellControll
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
|||||||
|
# Blender MTL File: 'untitletttd.blend'
|
||||||
|
# Material Count: 1
|
||||||
|
|
||||||
|
newmtl None
|
||||||
|
Ns 0
|
||||||
|
Ka 0.000000 0.000000 0.000000
|
||||||
|
Kd 0.8 0.8 0.8
|
||||||
|
Ks 0.8 0.8 0.8
|
||||||
|
d 1
|
||||||
|
illum 2
|
||||||
|
map_Kd C:\Users\Convidado.Cliente-JMF-PC\Desktop\ghftht.png
|
||||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -0,0 +1,108 @@
|
|||||||
|
package pp.battleship.game.client;
|
||||||
|
|
||||||
|
import com.jme3.math.Vector3f;
|
||||||
|
import pp.battleship.message.client.AnimationEndMessage;
|
||||||
|
import pp.battleship.message.server.EffectMessage;
|
||||||
|
import pp.battleship.message.server.SwitchBattleState;
|
||||||
|
import pp.battleship.model.IntPoint;
|
||||||
|
import pp.battleship.model.Shell;
|
||||||
|
import pp.battleship.model.ShipMap;
|
||||||
|
import pp.battleship.notification.Sound;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AnimationState manages the animation and effects of shots in the battleship game.
|
||||||
|
* It updates the game state based on received messages from the server,
|
||||||
|
* determines the outcome of shots, and plays corresponding sounds.
|
||||||
|
*/
|
||||||
|
public class AnimationState extends ClientState {
|
||||||
|
private boolean myTurn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an AnimationState for the specified game logic.
|
||||||
|
*
|
||||||
|
* @param logic The game logic to be used.
|
||||||
|
* @param myTurn Indicates whether it is the player's turn.
|
||||||
|
* @param position The position of the shell to be added to the map.
|
||||||
|
*/
|
||||||
|
public AnimationState(ClientGameLogic logic, boolean myTurn, IntPoint position) {
|
||||||
|
super(logic);
|
||||||
|
this.myTurn = myTurn;
|
||||||
|
Shell shell = new Shell(position);
|
||||||
|
|
||||||
|
if (myTurn) {
|
||||||
|
logic.getOpponentMap().add(shell);
|
||||||
|
} else {
|
||||||
|
logic.getOwnMap().add(shell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean showBattle() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the effect of a shot based on the server's effect message.
|
||||||
|
*
|
||||||
|
* @param msg The message containing the effect of the shot.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void receivedEffect(EffectMessage msg) {
|
||||||
|
ClientGameLogic.LOGGER.log(System.Logger.Level.INFO, "report effect: {0}", msg); //NON-NLS
|
||||||
|
playSound(msg);
|
||||||
|
myTurn = msg.isMyTurn();
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receivedSwitchBattleState(SwitchBattleState msg) {
|
||||||
|
logic.setState(new BattleState(logic, msg.isTurn()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines which map (own or opponent's) should be affected by the shot.
|
||||||
|
*
|
||||||
|
* @param msg The effect message received from the server.
|
||||||
|
* @return The affected map (either the opponent's or player's own map).
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,9 +7,12 @@
|
|||||||
|
|
||||||
package pp.battleship.game.client;
|
package pp.battleship.game.client;
|
||||||
|
|
||||||
|
import com.jme3.math.Vector3f;
|
||||||
import pp.battleship.message.client.ShootMessage;
|
import pp.battleship.message.client.ShootMessage;
|
||||||
|
import pp.battleship.message.server.AnimationStartMessage;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.EffectMessage;
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
|
import pp.battleship.model.Shell;
|
||||||
import pp.battleship.model.ShipMap;
|
import pp.battleship.model.ShipMap;
|
||||||
import pp.battleship.notification.Sound;
|
import pp.battleship.notification.Sound;
|
||||||
|
|
||||||
@@ -46,57 +49,12 @@ else if (logic.getOpponentMap().isValid(pos))
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reports the effect of a shot based on the server message.
|
* Reports the start of an animation based on a server message
|
||||||
*
|
*
|
||||||
* @param msg the message containing the effect of the shot
|
* @param msg the message containing the start of an animation
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void receivedEffect(EffectMessage msg) {
|
public void receivedAnimationStart(AnimationStartMessage msg){
|
||||||
ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
|
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition()));
|
||||||
playSound(msg);
|
|
||||||
myTurn = msg.isMyTurn();
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
package pp.battleship.game.client;
|
||||||
|
|
||||||
import pp.battleship.message.client.ClientMessage;
|
import pp.battleship.message.client.ClientMessage;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
import pp.battleship.message.server.GameDetails;
|
|
||||||
import pp.battleship.message.server.ServerInterpreter;
|
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
import pp.battleship.model.ShipMap;
|
import pp.battleship.model.ShipMap;
|
||||||
import pp.battleship.model.dto.ShipMapDTO;
|
import pp.battleship.model.dto.ShipMapDTO;
|
||||||
@@ -216,6 +213,30 @@ public void received(StartBattleMessage msg) {
|
|||||||
state.receivedStartBattle(msg);
|
state.receivedStartBattle(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the reception of an animation start message.
|
||||||
|
* This method forwards the received message to the current state
|
||||||
|
* for further processing.
|
||||||
|
*
|
||||||
|
* @param msg The AnimationStartMessage containing details about the start of the animation.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void received(AnimationStartMessage msg) {
|
||||||
|
state.receivedAnimationStart(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the reception of a switch battle state message.
|
||||||
|
* This method forwards the received message to the current state
|
||||||
|
* for further processing.
|
||||||
|
*
|
||||||
|
* @param msg The SwitchBattleState message containing information about the state change.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void received(SwitchBattleState msg) {
|
||||||
|
state.receivedSwitchBattleState(msg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reports the effect of a shot based on the server message.
|
* Reports the effect of a shot based on the server message.
|
||||||
*
|
*
|
||||||
@@ -304,7 +325,7 @@ public void saveMap(File file) throws IOException {
|
|||||||
*
|
*
|
||||||
* @param msg the message to be sent
|
* @param msg the message to be sent
|
||||||
*/
|
*/
|
||||||
void send(ClientMessage msg) {
|
public void send(ClientMessage msg) {
|
||||||
if (clientSender == null)
|
if (clientSender == null)
|
||||||
LOGGER.log(Level.ERROR, "trying to send {0} with sender==null", msg); //NON-NLS
|
LOGGER.log(Level.ERROR, "trying to send {0} with sender==null", msg); //NON-NLS
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -7,9 +7,7 @@
|
|||||||
|
|
||||||
package pp.battleship.game.client;
|
package pp.battleship.game.client;
|
||||||
|
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
import pp.battleship.message.server.GameDetails;
|
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -138,6 +136,24 @@ void mapFinished() {
|
|||||||
ClientGameLogic.LOGGER.log(Level.ERROR, "mapFinished not allowed in {0}", getName()); //NON-NLS
|
ClientGameLogic.LOGGER.log(Level.ERROR, "mapFinished not allowed in {0}", getName()); //NON-NLS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives animation start
|
||||||
|
*
|
||||||
|
* @param msg the animation start message
|
||||||
|
*/
|
||||||
|
void receivedAnimationStart(AnimationStartMessage msg){
|
||||||
|
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives battle switch
|
||||||
|
*
|
||||||
|
** @param msg the battle switch message
|
||||||
|
*/
|
||||||
|
void receivedSwitchBattleState(SwitchBattleState msg){
|
||||||
|
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedSwitchBattleState not allowed in {0}", getName());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the game details provided by the server.
|
* Sets the game details provided by the server.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -8,13 +8,11 @@
|
|||||||
package pp.battleship.game.server;
|
package pp.battleship.game.server;
|
||||||
|
|
||||||
import pp.battleship.BattleshipConfig;
|
import pp.battleship.BattleshipConfig;
|
||||||
|
import pp.battleship.message.client.AnimationEndMessage;
|
||||||
import pp.battleship.message.client.ClientInterpreter;
|
import pp.battleship.message.client.ClientInterpreter;
|
||||||
import pp.battleship.message.client.MapMessage;
|
import pp.battleship.message.client.MapMessage;
|
||||||
import pp.battleship.message.client.ShootMessage;
|
import pp.battleship.message.client.ShootMessage;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
import pp.battleship.message.server.GameDetails;
|
|
||||||
import pp.battleship.message.server.ServerMessage;
|
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.Battleship;
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
|
|
||||||
@@ -39,6 +37,9 @@ public class ServerGameLogic implements ClientInterpreter {
|
|||||||
private Player activePlayer;
|
private Player activePlayer;
|
||||||
private ServerState state = ServerState.WAIT;
|
private ServerState state = ServerState.WAIT;
|
||||||
|
|
||||||
|
private boolean player1AnimationReady = false;
|
||||||
|
private boolean player2AnimationReady = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a ServerGameLogic with the specified sender and configuration.
|
* Constructs a ServerGameLogic with the specified sender and configuration.
|
||||||
*
|
*
|
||||||
@@ -167,7 +168,45 @@ public void received(ShootMessage msg, int from) {
|
|||||||
if (state != ServerState.BATTLE)
|
if (state != ServerState.BATTLE)
|
||||||
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
|
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
|
||||||
else
|
else
|
||||||
|
for (Player player : players){
|
||||||
|
send(player, new AnimationStartMessage(msg.getPosition(), player == activePlayer));
|
||||||
|
setState(ServerState.ANIMATION_WAIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the reception of an animation end message from a player.
|
||||||
|
* This method checks if the current state allows for animation to be processed.
|
||||||
|
* If allowed, it marks the corresponding player as ready for the animation
|
||||||
|
* and triggers a shot at the specified position. Once both players are ready,
|
||||||
|
* it transitions to the battle state and notifies all players of the state change.
|
||||||
|
*
|
||||||
|
* @param msg The AnimationEndMessage containing details about the end of the animation.
|
||||||
|
* @param from The ID of the player who sent the message.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void received(AnimationEndMessage msg, int from){
|
||||||
|
if(state != ServerState.ANIMATION_WAIT)
|
||||||
|
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));
|
||||||
|
player1AnimationReady = true;
|
||||||
shoot(getPlayerById(from), msg.getPosition());
|
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());
|
||||||
|
player2AnimationReady = true;
|
||||||
|
shoot(getPlayerById(from), msg.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(player1AnimationReady && player2AnimationReady){
|
||||||
|
setState(ServerState.BATTLE);
|
||||||
|
for (Player player : players)
|
||||||
|
send(player, new SwitchBattleState(player == activePlayer));
|
||||||
|
player1AnimationReady = false;
|
||||||
|
player2AnimationReady = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -29,5 +29,10 @@ enum ServerState {
|
|||||||
/**
|
/**
|
||||||
* The game has ended because all the ships of one player have been destroyed.
|
* The game has ended because all the ships of one player have been destroyed.
|
||||||
*/
|
*/
|
||||||
GAME_OVER
|
GAME_OVER,
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* The server waits for all players to finish the animation
|
||||||
|
*/
|
||||||
|
ANIMATION_WAIT,
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,10 +7,7 @@
|
|||||||
|
|
||||||
package pp.battleship.game.singlemode;
|
package pp.battleship.game.singlemode;
|
||||||
|
|
||||||
import pp.battleship.message.client.ClientInterpreter;
|
import pp.battleship.message.client.*;
|
||||||
import pp.battleship.message.client.ClientMessage;
|
|
||||||
import pp.battleship.message.client.MapMessage;
|
|
||||||
import pp.battleship.message.client.ShootMessage;
|
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.Battleship;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,6 +60,20 @@ public void received(MapMessage msg, int from) {
|
|||||||
copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
|
copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the reception of an animation end message from a player.
|
||||||
|
* This method creates a copy of the received AnimationEndMessage
|
||||||
|
* with the specified position.
|
||||||
|
*
|
||||||
|
* @param msg The AnimationEndMessage containing details about the end of the animation.
|
||||||
|
* @param from The ID of the player who sent the message.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void received(AnimationEndMessage msg, int from) {
|
||||||
|
copiedMessage = new AnimationEndMessage(msg.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a copy of the provided {@link Battleship}.
|
* Creates a copy of the provided {@link Battleship}.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -9,11 +9,7 @@
|
|||||||
|
|
||||||
import pp.battleship.game.client.BattleshipClient;
|
import pp.battleship.game.client.BattleshipClient;
|
||||||
import pp.battleship.game.client.ClientGameLogic;
|
import pp.battleship.game.client.ClientGameLogic;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
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 java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -82,6 +78,16 @@ public void received(EffectMessage msg) {
|
|||||||
forward(msg);
|
forward(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void received(AnimationStartMessage msg) {
|
||||||
|
forward(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void received(SwitchBattleState msg){
|
||||||
|
forward(msg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forwards the specified ServerMessage to the client's game logic by enqueuing the message acceptance.
|
* Forwards the specified ServerMessage to the client's game logic by enqueuing the message acceptance.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package pp.battleship.game.singlemode;
|
package pp.battleship.game.singlemode;
|
||||||
|
|
||||||
import pp.battleship.game.client.BattleshipClient;
|
import pp.battleship.game.client.BattleshipClient;
|
||||||
|
import pp.battleship.message.client.AnimationEndMessage;
|
||||||
import pp.battleship.message.client.MapMessage;
|
import pp.battleship.message.client.MapMessage;
|
||||||
import pp.battleship.message.client.ShootMessage;
|
import pp.battleship.message.client.ShootMessage;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
import pp.battleship.message.server.GameDetails;
|
|
||||||
import pp.battleship.message.server.ServerInterpreter;
|
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
import pp.battleship.model.dto.ShipMapDTO;
|
import pp.battleship.model.dto.ShipMapDTO;
|
||||||
import pp.util.RandomPositionIterator;
|
import pp.util.RandomPositionIterator;
|
||||||
@@ -124,4 +122,18 @@ public void received(EffectMessage msg) {
|
|||||||
if (msg.isMyTurn())
|
if (msg.isMyTurn())
|
||||||
shoot();
|
shoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void received(AnimationStartMessage msg) {
|
||||||
|
LOGGER.log(Level.INFO, "Received AnimationStartMessage: {0}", msg);
|
||||||
|
connection.sendRobotMessage(new AnimationEndMessage(msg.getPosition()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void received(SwitchBattleState msg){
|
||||||
|
LOGGER.log(Level.INFO, "Received SwitchBattleStateMessage: {0}", msg);
|
||||||
|
if (msg.isTurn())
|
||||||
|
shoot();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package pp.battleship.message.client;
|
||||||
|
|
||||||
|
import com.jme3.network.serializing.Serializable;
|
||||||
|
import pp.battleship.model.IntPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a message sent from the client indicating that an animation has ended.
|
||||||
|
* This message includes the position associated with the animation's end.
|
||||||
|
*/
|
||||||
|
@Serializable
|
||||||
|
public class AnimationEndMessage extends ClientMessage {
|
||||||
|
|
||||||
|
private IntPoint position; // The position related to the end of the animation
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor for serialization purposes.
|
||||||
|
* This constructor is not meant to be used directly.
|
||||||
|
*/
|
||||||
|
private AnimationEndMessage() {
|
||||||
|
/* nothing */
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an AnimationEndMessage with the specified position.
|
||||||
|
*
|
||||||
|
* @param position The position where the animation ended.
|
||||||
|
*/
|
||||||
|
public AnimationEndMessage(IntPoint position) {
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the position associated with the end of the animation.
|
||||||
|
*
|
||||||
|
* @return The position as an IntPoint.
|
||||||
|
*/
|
||||||
|
public IntPoint getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a client interpreter that processes this message.
|
||||||
|
*
|
||||||
|
* @param interpreter The interpreter that will handle this message.
|
||||||
|
* @param from The ID of the player who sent the message.
|
||||||
|
*/
|
||||||
|
@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
|
* @param from the connection ID from which the message was received
|
||||||
*/
|
*/
|
||||||
void received(MapMessage msg, int from);
|
void received(MapMessage msg, int from);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes a received AnimationMessage
|
||||||
|
*
|
||||||
|
* @param msg the AnimationEndMessage to be processed
|
||||||
|
* @param from the connection ID from which the message was received
|
||||||
|
*/
|
||||||
|
void received(AnimationEndMessage msg, int from);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package pp.battleship.message.server;
|
||||||
|
|
||||||
|
import com.jme3.network.serializing.Serializable;
|
||||||
|
import pp.battleship.model.IntPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a message sent from the server indicating that an animation is about to start.
|
||||||
|
* This message includes the position associated with the animation and the player's turn status.
|
||||||
|
*/
|
||||||
|
@Serializable
|
||||||
|
public class AnimationStartMessage extends ServerMessage {
|
||||||
|
private IntPoint position; // The position related to the start of the animation
|
||||||
|
private boolean myTurn; // Indicates if it is the player's turn
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor for serialization purposes.
|
||||||
|
* This constructor is not meant to be used directly.
|
||||||
|
*/
|
||||||
|
private AnimationStartMessage() {
|
||||||
|
/* nothing */
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an AnimationStartMessage with the specified position and turn status.
|
||||||
|
*
|
||||||
|
* @param position The position where the animation is set to start.
|
||||||
|
* @param isTurn Indicates if it is the player's turn.
|
||||||
|
*/
|
||||||
|
public AnimationStartMessage(IntPoint position, boolean isTurn) {
|
||||||
|
this.position = position;
|
||||||
|
this.myTurn = isTurn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the position associated with the start of the animation.
|
||||||
|
*
|
||||||
|
* @return The position as an IntPoint.
|
||||||
|
*/
|
||||||
|
public IntPoint getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if it is the player's turn.
|
||||||
|
*
|
||||||
|
* @return True if it is the player's turn; false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isMyTurn() {
|
||||||
|
return myTurn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a server interpreter that processes this message.
|
||||||
|
*
|
||||||
|
* @param interpreter The interpreter that will handle this message.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void accept(ServerInterpreter interpreter) {
|
||||||
|
interpreter.received(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an informational text key related to the animation.
|
||||||
|
*
|
||||||
|
* @return A string indicating the position to be animated.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getInfoTextKey() {
|
||||||
|
return (position + " to be animated");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,4 +33,18 @@ public interface ServerInterpreter {
|
|||||||
* @param msg the EffectMessage received
|
* @param msg the EffectMessage received
|
||||||
*/
|
*/
|
||||||
void received(EffectMessage msg);
|
void received(EffectMessage msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles an AnimationStartMessage received from the server
|
||||||
|
*
|
||||||
|
* @param msg the AnimationStartMessage received
|
||||||
|
*/
|
||||||
|
void received(AnimationStartMessage msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles an SwitchBattleState received from the server
|
||||||
|
*
|
||||||
|
* @param msg the SwitchBattleState received
|
||||||
|
*/
|
||||||
|
void received(SwitchBattleState msg);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package pp.battleship.message.server;
|
||||||
|
|
||||||
|
import com.jme3.network.serializing.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a message sent from the server to switch the battle state.
|
||||||
|
* This message indicates whether it is the player's turn to act.
|
||||||
|
*/
|
||||||
|
@Serializable
|
||||||
|
public class SwitchBattleState extends ServerMessage {
|
||||||
|
private boolean isTurn; // Indicates if it is the player's turn
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor for serialization purposes.
|
||||||
|
* This constructor is not meant to be used directly.
|
||||||
|
*/
|
||||||
|
private SwitchBattleState() {
|
||||||
|
/* nothing */
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a SwitchBattleState message with the specified turn status.
|
||||||
|
*
|
||||||
|
* @param isTurn Indicates if it is the player's turn.
|
||||||
|
*/
|
||||||
|
public SwitchBattleState(boolean isTurn) {
|
||||||
|
this.isTurn = isTurn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if it is the player's turn.
|
||||||
|
*
|
||||||
|
* @return True if it is the player's turn; false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isTurn() {
|
||||||
|
return isTurn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a server interpreter that processes this message.
|
||||||
|
*
|
||||||
|
* @param interpreter The interpreter that will handle this message.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void accept(ServerInterpreter interpreter) {
|
||||||
|
interpreter.received(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an informational text key related to the state change.
|
||||||
|
*
|
||||||
|
* @return An empty string, as no specific info text is associated.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getInfoTextKey() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package pp.battleship.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a shell in the battleship game, characterized by its position
|
||||||
|
* in a 2D space defined by x and y coordinates.
|
||||||
|
*/
|
||||||
|
public class Shell implements Item {
|
||||||
|
private int x;
|
||||||
|
private int y;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a Shell object at the specified position.
|
||||||
|
*
|
||||||
|
* @param position An IntPoint representing the x and y coordinates of the shell.
|
||||||
|
*/
|
||||||
|
public Shell(IntPoint position) {
|
||||||
|
x = position.getX();
|
||||||
|
y = position.getY();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the x coordinate of the shell.
|
||||||
|
*
|
||||||
|
* @return The x coordinate as an integer.
|
||||||
|
*/
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the y coordinate of the shell.
|
||||||
|
*
|
||||||
|
* @return The y coordinate as an integer.
|
||||||
|
*/
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the x coordinate of the shell.
|
||||||
|
*
|
||||||
|
* @param x The new x coordinate as an integer.
|
||||||
|
*/
|
||||||
|
public void setX(int x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the y coordinate of the shell.
|
||||||
|
*
|
||||||
|
* @param y The new y coordinate as an integer.
|
||||||
|
*/
|
||||||
|
public void setY(int y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a visitor that performs an operation on this shell.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor that will operate on this shell.
|
||||||
|
* @param <T> The type of the result returned by the visitor.
|
||||||
|
* @return The result of the visitor's operation.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <T> T accept(Visitor<T> visitor) {
|
||||||
|
return visitor.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a visitor that performs a void operation on this shell.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor that will operate on this shell.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void accept(VoidVisitor visitor) {
|
||||||
|
visitor.visit(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,6 +78,15 @@ public void add(Battleship ship) {
|
|||||||
addItem(ship);
|
addItem(ship);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a shell to the map and triggers an item addition event.
|
||||||
|
*
|
||||||
|
* @param shell the shell to be added to the map
|
||||||
|
*/
|
||||||
|
public void add(Shell shell) {
|
||||||
|
addItem(shell);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a shot on the map, updates the state of the affected ship (if any),
|
* Registers a shot on the map, updates the state of the affected ship (if any),
|
||||||
* and triggers an item addition event.
|
* and triggers an item addition event.
|
||||||
|
|||||||
@@ -28,4 +28,12 @@ public interface Visitor<T> {
|
|||||||
* @return the result of visiting the Battleship element
|
* @return the result of visiting the Battleship element
|
||||||
*/
|
*/
|
||||||
T visit(Battleship ship);
|
T visit(Battleship ship);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits a Shell element.
|
||||||
|
*
|
||||||
|
* @param shell the shell element to visit
|
||||||
|
* @return the result of visiting the shell element
|
||||||
|
*/
|
||||||
|
T visit(Shell shell);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,4 +25,11 @@ public interface VoidVisitor {
|
|||||||
* @param ship the Battleship element to visit
|
* @param ship the Battleship element to visit
|
||||||
*/
|
*/
|
||||||
void visit(Battleship ship);
|
void visit(Battleship ship);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits a Shell element.
|
||||||
|
*
|
||||||
|
* @param shell the Shell element to visit
|
||||||
|
*/
|
||||||
|
void visit(Shell shell);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,11 @@
|
|||||||
import pp.battleship.game.server.Player;
|
import pp.battleship.game.server.Player;
|
||||||
import pp.battleship.game.server.ServerGameLogic;
|
import pp.battleship.game.server.ServerGameLogic;
|
||||||
import pp.battleship.game.server.ServerSender;
|
import pp.battleship.game.server.ServerSender;
|
||||||
|
import pp.battleship.message.client.AnimationEndMessage;
|
||||||
import pp.battleship.message.client.ClientMessage;
|
import pp.battleship.message.client.ClientMessage;
|
||||||
import pp.battleship.message.client.MapMessage;
|
import pp.battleship.message.client.MapMessage;
|
||||||
import pp.battleship.message.client.ShootMessage;
|
import pp.battleship.message.client.ShootMessage;
|
||||||
import pp.battleship.message.server.EffectMessage;
|
import pp.battleship.message.server.*;
|
||||||
import pp.battleship.message.server.GameDetails;
|
|
||||||
import pp.battleship.message.server.ServerMessage;
|
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.Battleship;
|
||||||
import pp.battleship.model.IntPoint;
|
import pp.battleship.model.IntPoint;
|
||||||
import pp.battleship.model.Shot;
|
import pp.battleship.model.Shot;
|
||||||
@@ -110,6 +108,9 @@ private void processNextMessage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initializeSerializables() {
|
private void initializeSerializables() {
|
||||||
|
Serializer.registerClass(AnimationEndMessage.class);
|
||||||
|
Serializer.registerClass(AnimationStartMessage.class);
|
||||||
|
Serializer.registerClass(SwitchBattleState.class);
|
||||||
Serializer.registerClass(GameDetails.class);
|
Serializer.registerClass(GameDetails.class);
|
||||||
Serializer.registerClass(StartBattleMessage.class);
|
Serializer.registerClass(StartBattleMessage.class);
|
||||||
Serializer.registerClass(MapMessage.class);
|
Serializer.registerClass(MapMessage.class);
|
||||||
@@ -121,6 +122,7 @@ private void initializeSerializables() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void registerListeners() {
|
private void registerListeners() {
|
||||||
|
myServer.addMessageListener(this, AnimationEndMessage.class);
|
||||||
myServer.addMessageListener(this, MapMessage.class);
|
myServer.addMessageListener(this, MapMessage.class);
|
||||||
myServer.addMessageListener(this, ShootMessage.class);
|
myServer.addMessageListener(this, ShootMessage.class);
|
||||||
myServer.addConnectionListener(this);
|
myServer.addConnectionListener(this);
|
||||||
|
|||||||
Reference in New Issue
Block a user