part solution for exercise 13
added an animation state for the server and client and gave it the functionality to display a 3d model representing the shot of the other person adjusted the server to serialize the new messages for handling the animation states
This commit is contained in:
		
						parent
						
							da2508395c
						
					
				
				
					commit
					f99b91324c
				
			@@ -12,6 +12,7 @@
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
@@ -109,6 +110,21 @@ public Spatial visit(Battleship ship) {
 | 
			
		||||
        return shipNode;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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() + 1, shell.getY() + 1);
 | 
			
		||||
 | 
			
		||||
        final float x1 = p1.getX() + INDENT;
 | 
			
		||||
        final float y1 = p1.getY() + INDENT;
 | 
			
		||||
        final float x2 = p2.getX() - INDENT;
 | 
			
		||||
        final float y2 = p2.getY() - INDENT;
 | 
			
		||||
 | 
			
		||||
        return shellNode;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a line geometry representing part of the ship's border.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -18,10 +18,7 @@
 | 
			
		||||
import com.jme3.scene.shape.Box;
 | 
			
		||||
import com.jme3.scene.shape.Cylinder;
 | 
			
		||||
import pp.battleship.client.BattleshipApp;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.Rotation;
 | 
			
		||||
import pp.battleship.model.ShipMap;
 | 
			
		||||
import pp.battleship.model.Shot;
 | 
			
		||||
import pp.battleship.model.*;
 | 
			
		||||
 | 
			
		||||
import static java.util.Objects.requireNonNull;
 | 
			
		||||
import static pp.util.FloatMath.HALF_PI;
 | 
			
		||||
@@ -39,9 +36,11 @@ class SeaSynchronizer extends ShipMapSynchronizer {
 | 
			
		||||
    private static final String UBOAT = "Models/UBoat/14084_WWII_Ship_German_Type_II_U-boat_v2_L1.obj"; //NON-NLS
 | 
			
		||||
    private static final String BATTLE_SHIP_MODERN = "Models/BattleShipModern/10619_Battleship.obj";
 | 
			
		||||
    private static final String PATROL_BOAT = "Models/PatrolBoat/12219_boat_v2_L2.obj";
 | 
			
		||||
    private static final String SHELL_ROCKET = "Models/Rocket/Rocket.obj";
 | 
			
		||||
    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 SHELL = "shell";
 | 
			
		||||
    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);
 | 
			
		||||
@@ -138,6 +137,42 @@ public Spatial visit(Battleship ship) {
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Visits a shell and creates a graphical representation
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell the Shell element to visit
 | 
			
		||||
     * @return the node containing the graphical representation
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Spatial visit(Shell shell){
 | 
			
		||||
        final Node node = new Node(SHELL);
 | 
			
		||||
        node.attachChild(createRocket());
 | 
			
		||||
 | 
			
		||||
        final float x = shell.getY();
 | 
			
		||||
        final float z = shell.getX();
 | 
			
		||||
 | 
			
		||||
        node.setLocalTranslation(x + 0.5f, 10f, z + 0.5f);
 | 
			
		||||
        ShellControl shellControl = new ShellControl(shell, app);
 | 
			
		||||
        node.addControl(shellControl);
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * creates the spatial representation of a rocket
 | 
			
		||||
     *
 | 
			
		||||
     * @return a spatial the rocket
 | 
			
		||||
     */
 | 
			
		||||
    private Spatial createRocket() {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel(SHELL_ROCKET);
 | 
			
		||||
 | 
			
		||||
        model.rotate(PI, 0f, 0f);
 | 
			
		||||
        model.scale(0.005f);
 | 
			
		||||
        model.setShadowMode(ShadowMode.CastAndReceive);
 | 
			
		||||
        model.move(0, 0, 0);
 | 
			
		||||
 | 
			
		||||
        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.
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,42 @@
 | 
			
		||||
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.lang.System.Logger;
 | 
			
		||||
 | 
			
		||||
public class ShellControl extends AbstractControl {
 | 
			
		||||
    private final Shell shell;
 | 
			
		||||
    private final BattleshipApp app;
 | 
			
		||||
 | 
			
		||||
    private static final float MOVE_SPEED = 8.0f;
 | 
			
		||||
 | 
			
		||||
    static final Logger LOGGER = System.getLogger(ShellControl.class.getName());
 | 
			
		||||
 | 
			
		||||
    public ShellControl(Shell shell, BattleshipApp app) {
 | 
			
		||||
        this.shell = shell;
 | 
			
		||||
        this.app = app;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlUpdate(float tpf) {
 | 
			
		||||
        spatial.move(0, -MOVE_SPEED * tpf, 0);
 | 
			
		||||
        //LOGGER.log(System.Logger.Level.DEBUG, "moved rocket {0}", spatial.getLocalTranslation().getY());
 | 
			
		||||
        if (spatial.getLocalTranslation().getY() <= 1.5){
 | 
			
		||||
            spatial.getParent().detachChild(spatial);
 | 
			
		||||
            app.getGameLogic().send(new AnimationEndMessage(new IntPoint(shell.getX(), shell.getY())));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @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;
 | 
			
		||||
@@ -114,11 +112,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(SwitchBattleState.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,250 @@
 | 
			
		||||
# 
 | 
			
		||||
# Generated by Sweet Home 3D - ven. janv. 02 20:37:08 CET 2015
 | 
			
		||||
# http://www.sweethome3d.com/
 | 
			
		||||
# 
 | 
			
		||||
 | 
			
		||||
newmtl FrontColorNoCulling
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 0.2 0.2 0.2
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl ForegroundColor
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 0.2 0.2 0.2
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl white
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.48235294 0.5019608 0.5803922
 | 
			
		||||
Kd 0.48235294 0.5019608 0.5803922
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl white_Cylinder_5
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.47843137 0.49803922 0.5764706
 | 
			
		||||
Kd 0.47843137 0.49803922 0.5764706
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl white_Cylinder_10
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Kd 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl FrontColorNoCulling_11
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Kd 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl ForegroundColor_12
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Kd 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl white_Mesh_13
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.6 0.6 0.6
 | 
			
		||||
Kd 0.6 0.6 0.6
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_1_1
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.0 0.0 0.0
 | 
			
		||||
Kd 0.0 0.0 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_7_7
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.4 0.4 0.4
 | 
			
		||||
Kd 0.4 0.4 0.4
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_10_10
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8 0.4 0.0
 | 
			
		||||
Kd 0.8 0.4 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_11_11
 | 
			
		||||
illum 2
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 0.2 0.2 0.2
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl 12_12
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 0.2 0.2 0.2
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_1_1_Cube_1_1_1_38
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.6 0.6 0.6
 | 
			
		||||
Kd 0.6 0.6 0.6
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl white_Cylinder_58
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.1882353 0.27058825 0.58431375
 | 
			
		||||
Kd 0.1882353 0.27058825 0.58431375
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl white_Cylinder_59
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.3137255 0.14901961 0.011764706
 | 
			
		||||
Kd 0.3137255 0.14901961 0.011764706
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 0.0
 | 
			
		||||
 | 
			
		||||
newmtl 1_1
 | 
			
		||||
illum 2
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 1.0 1.0 1.0
 | 
			
		||||
Ks 0.5 0.5 0.5
 | 
			
		||||
Ns 64.0
 | 
			
		||||
Ni 1.0
 | 
			
		||||
d 0.48000002
 | 
			
		||||
map_Kd Missile_AIM-120_D_[AMRAAM]_1_1.png
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_2_2
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8 0.4 0.0
 | 
			
		||||
Kd 0.8 0.4 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_4_4
 | 
			
		||||
illum 2
 | 
			
		||||
Ka 0.6 0.6 0.6
 | 
			
		||||
Kd 0.6 0.6 0.6
 | 
			
		||||
Ks 0.5 0.5 0.5
 | 
			
		||||
Ns 64.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_5_5
 | 
			
		||||
illum 2
 | 
			
		||||
Ka 0.8 0.8 0.0
 | 
			
		||||
Kd 0.8 0.8 0.0
 | 
			
		||||
Ks 0.5 0.5 0.5
 | 
			
		||||
Ns 64.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_6_6
 | 
			
		||||
illum 2
 | 
			
		||||
Ka 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Kd 0.8784314 0.8745098 0.8901961
 | 
			
		||||
Ks 0.5 0.5 0.5
 | 
			
		||||
Ns 64.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_10_10_Cylinder_10_10_73
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 0.2 0.2 0.2
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl 11_11
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.6 0.6 0.6
 | 
			
		||||
Kd 0.6 0.6 0.6
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_1_1_Cube_1_1_1_76
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 1.0 1.0 1.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
Ni 1.0
 | 
			
		||||
map_Kd Missile_AIM-120_D_[AMRAAM]_Cube_1_1_1_Cube_1_1_1_76.png
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_2_2
 | 
			
		||||
illum 2
 | 
			
		||||
Ka 0.6 0.6 0.6
 | 
			
		||||
Kd 0.6 0.6 0.6
 | 
			
		||||
Ks 0.5 0.5 0.5
 | 
			
		||||
Ns 64.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_3_3
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.4 0.4 0.0
 | 
			
		||||
Kd 0.4 0.4 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_4_4_Cylinder_4_4_79
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.0 0.0 0.0
 | 
			
		||||
Kd 0.0 0.0 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_5_5
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 1.0 1.0 1.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
map_Kd Missile_AIM-120_D_[AMRAAM]_Cube_1_5_5.png
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_6_6
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 1.0 1.0 1.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
Ni 1.0
 | 
			
		||||
map_Kd Missile_AIM-120_D_[AMRAAM]_Cube_1_6_6.png
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_1_1
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.4 0.4 0.4
 | 
			
		||||
Kd 0.4 0.4 0.4
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_5_5_Cube_1_5_5_86
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.2 0.2 0.2
 | 
			
		||||
Kd 0.2 0.2 0.2
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cube_1_6_6_Cube_1_6_6_87
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8 0.0 0.0
 | 
			
		||||
Kd 0.8 0.0 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_7_7_Cylinder_7_7_88
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.8 0.4 0.0
 | 
			
		||||
Kd 0.8 0.4 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
 | 
			
		||||
newmtl Cylinder_8_8
 | 
			
		||||
illum 1
 | 
			
		||||
Ka 0.4 0.6 0.0
 | 
			
		||||
Kd 0.4 0.6 0.0
 | 
			
		||||
Ks 0.0 0.0 0.0
 | 
			
		||||
Ns 1.0
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 210 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 164 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 289 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 26 KiB  | 
							
								
								
									
										22889
									
								
								Projekte/battleship/client/src/main/resources/Models/Rocket/Rocket.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22889
									
								
								Projekte/battleship/client/src/main/resources/Models/Rocket/Rocket.obj
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											Binary file not shown.
										
									
								
							@@ -0,0 +1,96 @@
 | 
			
		||||
package pp.battleship.game.client;
 | 
			
		||||
 | 
			
		||||
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.Music;
 | 
			
		||||
import pp.battleship.notification.Sound;
 | 
			
		||||
 | 
			
		||||
public class AnimatonState extends ClientState {
 | 
			
		||||
    private boolean myTurn;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a client state of the specified game logic.
 | 
			
		||||
     *
 | 
			
		||||
     * @param logic the game logic
 | 
			
		||||
     */
 | 
			
		||||
    public AnimatonState(ClientGameLogic logic, boolean myTurn, IntPoint position) {
 | 
			
		||||
        super(logic);
 | 
			
		||||
        logic.playMusic(Music.BATTLE_THEME);
 | 
			
		||||
        this.myTurn = myTurn;
 | 
			
		||||
        if(myTurn) {
 | 
			
		||||
            logic.send(new AnimationEndMessage(position));
 | 
			
		||||
        }else {
 | 
			
		||||
            logic.getOwnMap().add(new Shell(position));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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) {
 | 
			
		||||
        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, msg.isGameLost()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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 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,6 +8,7 @@
 | 
			
		||||
package pp.battleship.game.client;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
import pp.battleship.message.server.AnimationStartMessage;
 | 
			
		||||
import pp.battleship.message.server.EffectMessage;
 | 
			
		||||
import pp.battleship.model.IntPoint;
 | 
			
		||||
import pp.battleship.model.ShipMap;
 | 
			
		||||
@@ -47,59 +48,8 @@ else if (logic.getOpponentMap().isValid(pos))
 | 
			
		||||
            logic.send(new ShootMessage(pos));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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) {
 | 
			
		||||
        ClientGameLogic.LOGGER.log(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, msg.isGameLost()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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);
 | 
			
		||||
    public void receivedAnimationStart(AnimationStartMessage msg){
 | 
			
		||||
        logic.setState(new AnimatonState(logic, msg.isMyTurn(), msg.getPosition()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
@@ -228,6 +225,16 @@ public void received(EffectMessage msg) {
 | 
			
		||||
        state.receivedEffect(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(AnimationStartMessage msg) {
 | 
			
		||||
        state.receivedAnimationStart(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(SwitchBattleState msg) {
 | 
			
		||||
        state.receivedSwitchBattleState(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Initializes the player's own map, opponent's map, and harbor based on the game details.
 | 
			
		||||
     *
 | 
			
		||||
@@ -315,7 +322,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,14 @@ void receivedEffect(EffectMessage msg) {
 | 
			
		||||
        ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); //NON-NLS
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void receivedAnimationStart(AnimationStartMessage msg){
 | 
			
		||||
        ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void receivedSwitchBattleState(SwitchBattleState msg){
 | 
			
		||||
        ClientGameLogic.LOGGER.log(Level.ERROR, "receivedSwitchBattleState not allowed in {0}", getName());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Loads a map from the specified file.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -8,13 +8,11 @@
 | 
			
		||||
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;
 | 
			
		||||
@@ -43,6 +41,9 @@ public class ServerGameLogic implements ClientInterpreter {
 | 
			
		||||
    private Player activePlayer;
 | 
			
		||||
    private ServerState state = ServerState.WAIT;
 | 
			
		||||
 | 
			
		||||
    private boolean player1AnimationReady = false;
 | 
			
		||||
    private boolean player2AnimationReady = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a ServerGameLogic with the specified sender and configuration.
 | 
			
		||||
     *
 | 
			
		||||
@@ -198,7 +199,33 @@ 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());
 | 
			
		||||
            for (Player player : players){
 | 
			
		||||
                send(player, new AnimationStartMessage(msg.getPosition(), player == activePlayer));
 | 
			
		||||
                setState(ServerState.ANIMATION_WAIT);
 | 
			
		||||
            }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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());
 | 
			
		||||
            } 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;
 | 
			
		||||
            }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -221,12 +248,13 @@ 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
 | 
			
		||||
     */
 | 
			
		||||
     *//*
 | 
			
		||||
    void shoot(Player p, IntPoint pos) {
 | 
			
		||||
        if (p != activePlayer) return;
 | 
			
		||||
        final Player otherPlayer = getOpponent(activePlayer);
 | 
			
		||||
@@ -257,5 +285,53 @@ else if (selectedShip.isDestroyed()) {
 | 
			
		||||
                send(otherPlayer, EffectMessage.hit(false, pos));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }*/
 | 
			
		||||
 | 
			
		||||
    void shoot(Player p, IntPoint position) {
 | 
			
		||||
        final Battleship selectedShip;
 | 
			
		||||
        if(p != activePlayer){
 | 
			
		||||
            selectedShip = p.getMap().findShipAt(position);
 | 
			
		||||
        } else {
 | 
			
		||||
            selectedShip = getOpponent(p).getMap().findShipAt(position);
 | 
			
		||||
        }
 | 
			
		||||
        if (selectedShip == null) {
 | 
			
		||||
            if (p != activePlayer) {
 | 
			
		||||
                send(p, EffectMessage.miss(false, position));
 | 
			
		||||
            } else {
 | 
			
		||||
                send(activePlayer, EffectMessage.miss(true, position));
 | 
			
		||||
            }
 | 
			
		||||
            if(player1AnimationReady && player2AnimationReady){
 | 
			
		||||
                LOGGER.log(Level.DEBUG, "switched active player");
 | 
			
		||||
                if(p != activePlayer){
 | 
			
		||||
                    activePlayer = p;
 | 
			
		||||
                } else {
 | 
			
		||||
                    activePlayer = getOpponent(p);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            selectedShip.hit(position);
 | 
			
		||||
            if(getOpponent(activePlayer).getMap().getRemainingShips().isEmpty()){
 | 
			
		||||
                if(p != activePlayer){
 | 
			
		||||
                    send(p, EffectMessage.lost(position, selectedShip, activePlayer.getMap().getRemainingShips()));
 | 
			
		||||
                } else {
 | 
			
		||||
                    send(activePlayer, EffectMessage.won(position, selectedShip));
 | 
			
		||||
                }
 | 
			
		||||
                if(player1AnimationReady && player2AnimationReady){
 | 
			
		||||
                    setState(ServerState.GAME_OVER);
 | 
			
		||||
                }
 | 
			
		||||
            } else if (selectedShip.isDestroyed()){
 | 
			
		||||
                if(p != activePlayer){
 | 
			
		||||
                    send(p, EffectMessage.shipDestroyed(false, position, selectedShip));
 | 
			
		||||
                } else {
 | 
			
		||||
                    send(activePlayer, EffectMessage.shipDestroyed(true, position, selectedShip));
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                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 server waits for all players to finish the animation
 | 
			
		||||
     */
 | 
			
		||||
    ANIMATION_WAIT
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,10 +7,7 @@
 | 
			
		||||
 | 
			
		||||
package pp.battleship.game.singlemode;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.message.client.ClientInterpreter;
 | 
			
		||||
import pp.battleship.message.client.ClientMessage;
 | 
			
		||||
import pp.battleship.message.client.MapMessage;
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
import pp.battleship.message.client.*;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -63,6 +60,11 @@ public void received(MapMessage msg, int from) {
 | 
			
		||||
        copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(AnimationEndMessage msg, int from) {
 | 
			
		||||
        copiedMessage = new AnimationEndMessage(msg.getPosition());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a copy of the provided {@link Battleship}.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -9,11 +9,7 @@
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
@@ -24,6 +20,7 @@
 | 
			
		||||
class InterpreterProxy implements ServerInterpreter {
 | 
			
		||||
    private final BattleshipClient playerClient;
 | 
			
		||||
 | 
			
		||||
    static final System.Logger LOGGER = System.getLogger(InterpreterProxy.class.getName());
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs an InterpreterProxy with the specified BattleshipClient.
 | 
			
		||||
     *
 | 
			
		||||
@@ -82,6 +79,17 @@ public void received(EffectMessage msg) {
 | 
			
		||||
        forward(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(AnimationStartMessage msg) {
 | 
			
		||||
        forward(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(SwitchBattleState msg){
 | 
			
		||||
        LOGGER.log(System.Logger.Level.INFO, "Received SwitchBattleState");
 | 
			
		||||
        forward(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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;
 | 
			
		||||
@@ -121,7 +119,18 @@ public void received(StartBattleMessage msg) {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(EffectMessage msg) {
 | 
			
		||||
        LOGGER.log(Level.INFO, "Received EffectMessage: {0}", msg); //NON-NLS
 | 
			
		||||
        if (msg.isMyTurn())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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,25 @@
 | 
			
		||||
package pp.battleship.message.client;
 | 
			
		||||
 | 
			
		||||
import com.jme3.network.serializing.Serializable;
 | 
			
		||||
import pp.battleship.model.IntPoint;
 | 
			
		||||
 | 
			
		||||
@Serializable
 | 
			
		||||
public class AnimationEndMessage extends ClientMessage {
 | 
			
		||||
 | 
			
		||||
    private IntPoint position;
 | 
			
		||||
 | 
			
		||||
    private AnimationEndMessage(){ /* nothing */}
 | 
			
		||||
 | 
			
		||||
    public AnimationEndMessage(IntPoint position) {
 | 
			
		||||
        this.position = position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public IntPoint getPosition() {
 | 
			
		||||
        return position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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 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,35 @@
 | 
			
		||||
package pp.battleship.message.server;
 | 
			
		||||
 | 
			
		||||
import com.jme3.network.serializing.Serializable;
 | 
			
		||||
import pp.battleship.model.IntPoint;
 | 
			
		||||
 | 
			
		||||
@Serializable
 | 
			
		||||
public class AnimationStartMessage extends ServerMessage {
 | 
			
		||||
    private IntPoint position;
 | 
			
		||||
    private boolean myTurn;
 | 
			
		||||
 | 
			
		||||
    private AnimationStartMessage(){ /* nothing */}
 | 
			
		||||
 | 
			
		||||
    public AnimationStartMessage(IntPoint position, boolean isTurn) {
 | 
			
		||||
        this.position = position;
 | 
			
		||||
        this.myTurn = isTurn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public IntPoint getPosition() {
 | 
			
		||||
        return position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isMyTurn() {
 | 
			
		||||
        return myTurn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void accept(ServerInterpreter interpreter) {
 | 
			
		||||
        interpreter.received(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getInfoTextKey() {
 | 
			
		||||
        return (position + " to be animated");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -33,4 +33,18 @@ public interface ServerInterpreter {
 | 
			
		||||
     * @param msg the EffectMessage received
 | 
			
		||||
     */
 | 
			
		||||
    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,28 @@
 | 
			
		||||
package pp.battleship.message.server;
 | 
			
		||||
 | 
			
		||||
import com.jme3.network.serializing.Serializable;
 | 
			
		||||
 | 
			
		||||
@Serializable
 | 
			
		||||
public class SwitchBattleState extends ServerMessage {
 | 
			
		||||
    private boolean isTurn;
 | 
			
		||||
 | 
			
		||||
    private SwitchBattleState(){ /* nothing */}
 | 
			
		||||
 | 
			
		||||
    public SwitchBattleState(boolean isTurn) {
 | 
			
		||||
        this.isTurn = isTurn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isTurn() {
 | 
			
		||||
        return isTurn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void accept(ServerInterpreter interpreter) {
 | 
			
		||||
        interpreter.received(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getInfoTextKey() {
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,37 @@
 | 
			
		||||
package pp.battleship.model;
 | 
			
		||||
 | 
			
		||||
public class Shell implements Item {
 | 
			
		||||
    private int x;
 | 
			
		||||
    private int y;
 | 
			
		||||
 | 
			
		||||
    public Shell(IntPoint position) {
 | 
			
		||||
        x = position.getX();
 | 
			
		||||
        y = position.getY();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getX() {
 | 
			
		||||
        return x;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getY() {
 | 
			
		||||
        return y;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setX(int x) {
 | 
			
		||||
        this.x = x;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setY(int y) {
 | 
			
		||||
        this.y = y;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public <T> T accept(Visitor<T> visitor) {
 | 
			
		||||
        return visitor.visit(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void accept(VoidVisitor visitor) {
 | 
			
		||||
        visitor.visit(this);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -91,6 +91,15 @@ public void add(Shot shot) {
 | 
			
		||||
        addItem(shot);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Registers a shell in the map and updates an item added event
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell the shell to be registered
 | 
			
		||||
     */
 | 
			
		||||
    public void add(Shell shell){
 | 
			
		||||
        addItem(shell);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Removes an item from the map and triggers an item removal event.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -28,4 +28,12 @@ public interface Visitor<T> {
 | 
			
		||||
     * @return the result of visiting the Battleship element
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
     */
 | 
			
		||||
    void visit(Battleship ship);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Visits a Shell element
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell the Shell element to visit
 | 
			
		||||
     */
 | 
			
		||||
    void visit(Shell shell);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -18,15 +18,14 @@
 | 
			
		||||
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.Shell;
 | 
			
		||||
import pp.battleship.model.Shot;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
@@ -118,11 +117,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(SwitchBattleState.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