Solution for exercise 13:
edited another state in the server and client, added the rocket, added the 'Shell.java' and 'ShellControl.java' edited the logic for the states and 3 messages for the server client comunication, edited the 'SeaSynchronizer' and ShipMapSynchronizer', so that the animations will be displayed, added the sound for the rocket
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.server.AnimationStartMessage;
|
||||
import pp.battleship.message.server.BackToBattleStateMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.notification.Sound;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
public class AnimationState extends ClientState {
|
||||
private static final Logger LOGGER = System.getLogger(AnimationState.class.getName());
|
||||
|
||||
/**
|
||||
* Constructs a client state of the specified game logic.
|
||||
*
|
||||
* @param logic the game logic
|
||||
*/
|
||||
AnimationState(ClientGameLogic logic, boolean myTurn, IntPoint pos) {
|
||||
super(logic);
|
||||
if (myTurn) {
|
||||
logic.getOpponentMap().add(new Shell(pos));
|
||||
logic.playSound(Sound.ROCKET);
|
||||
} else {
|
||||
logic.getOwnMap().add(new Shell(pos));
|
||||
logic.playSound(Sound.ROCKET);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean showBattle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reports the effect of a shot based on the server message.
|
||||
*
|
||||
* @param msg the message containing the effect of the shot
|
||||
*/
|
||||
@Override
|
||||
public void receivedEffect(EffectMessage msg) {
|
||||
logic.playSound(Sound.ROCKET_STOP);
|
||||
ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
|
||||
playSound(msg);
|
||||
logic.setInfoText(msg.getInfoTextKey()); //
|
||||
affectedMap(msg).add(msg.getShot());
|
||||
if (destroyedOpponentShip(msg)) {
|
||||
logic.getOpponentMap().add(msg.getDestroyedShip());
|
||||
}
|
||||
if (msg.isGameOver()) {
|
||||
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
|
||||
logic.setState(new GameOverState(logic));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines which map (own or opponent's) should be affected by the shot based on the message.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return the map (either the opponent's or player's own map) that is affected by the shot
|
||||
*/
|
||||
private ShipMap affectedMap(EffectMessage msg) {
|
||||
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the opponent's ship was destroyed by the player's shot.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return true if the shot destroyed an opponent's ship, false otherwise
|
||||
*/
|
||||
private boolean destroyedOpponentShip(EffectMessage msg) {
|
||||
return msg.getDestroyedShip() != null && msg.isOwnShot();
|
||||
}
|
||||
|
||||
/**
|
||||
* receives an AnimationStartMessage and sets the state to AnimationState
|
||||
*
|
||||
* @param msg the message received
|
||||
*/
|
||||
public void receivedAnimationStart(AnimationStartMessage msg) {
|
||||
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
|
||||
* or destruction of a ship.
|
||||
*
|
||||
* @param msg the effect message containing the result of the shot
|
||||
*/
|
||||
private void playSound(EffectMessage msg) {
|
||||
if (!msg.getShot().isHit())
|
||||
logic.playSound(Sound.SPLASH);
|
||||
else if (msg.getDestroyedShip() == null)
|
||||
logic.playSound(Sound.EXPLOSION);
|
||||
else
|
||||
logic.playSound(Sound.DESTROYED_SHIP);
|
||||
}
|
||||
|
||||
/**
|
||||
* receives a BackToBattleStateMessage and sets the state to BattleState
|
||||
*
|
||||
* @param msg the message received
|
||||
*/
|
||||
@Override
|
||||
public void receiveBackToBattleState(BackToBattleStateMessage msg) {
|
||||
logic.setState(new BattleState(logic, msg.isMyTurn()));
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,8 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.AnimationStartMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.notification.Sound;
|
||||
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* Represents the state of the client where players take turns to attack each other's ships.
|
||||
@@ -32,11 +28,21 @@ public BattleState(ClientGameLogic logic, boolean myTurn) {
|
||||
this.myTurn = myTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* this return-statement decides, whether the battle will be shown
|
||||
*
|
||||
* @return true, so the battle will be displayed
|
||||
*/
|
||||
@Override
|
||||
public boolean showBattle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* the logic, responsible for deciding, whether it was a valid input or not
|
||||
*
|
||||
* @param pos the position where the click occurred
|
||||
*/
|
||||
@Override
|
||||
public void clickOpponentMap(IntPoint pos) {
|
||||
if (!myTurn)
|
||||
@@ -46,60 +52,13 @@ else if (logic.getOpponentMap().isValid(pos))
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the effect of a shot based on the server message.
|
||||
* Receives an AnimationStartEvent and changes then the client-state to AnimationState
|
||||
*
|
||||
* @param msg the message containing the effect of the shot
|
||||
* @param msg the message received
|
||||
*/
|
||||
//tODO
|
||||
@Override
|
||||
public void receivedEffect(EffectMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
|
||||
playSound(msg);
|
||||
myTurn = msg.isMyTurn(); //boolean, describes whether it is my turn
|
||||
logic.setInfoText(msg.getInfoTextKey()); //
|
||||
affectedMap(msg).add(msg.getShot());
|
||||
if (destroyedOpponentShip(msg)) {
|
||||
logic.getOpponentMap().add(msg.getDestroyedShip());
|
||||
}
|
||||
if (msg.isGameOver()) {
|
||||
//msg.getRemainingOpponentShips().forEach(logic.getOwnMap()::add);
|
||||
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
|
||||
logic.setState(new GameOverState(logic));
|
||||
}
|
||||
public void receivedAnimationStart(AnimationStartMessage msg) {
|
||||
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines which map (own or opponent's) should be affected by the shot based on the message.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return the map (either the opponent's or player's own map) that is affected by the shot
|
||||
*/
|
||||
private ShipMap affectedMap(EffectMessage msg) {
|
||||
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the opponent's ship was destroyed by the player's shot.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return true if the shot destroyed an opponent's ship, false otherwise
|
||||
*/
|
||||
private boolean destroyedOpponentShip(EffectMessage msg) {
|
||||
return msg.getDestroyedShip() != null && msg.isOwnShot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
|
||||
* or destruction of a ship.
|
||||
*
|
||||
* @param msg the effect message containing the result of the shot
|
||||
*/
|
||||
private void playSound(EffectMessage msg) {
|
||||
if (!msg.getShot().isHit())
|
||||
logic.playSound(Sound.SPLASH);
|
||||
else if (msg.getDestroyedShip() == null)
|
||||
logic.playSound(Sound.EXPLOSION);
|
||||
else
|
||||
logic.playSound(Sound.DESTROYED_SHIP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.model.dto.ShipMapDTO;
|
||||
@@ -226,6 +223,26 @@ public void received(EffectMessage msg) {
|
||||
state.receivedEffect(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the AnimationStartMessage based on the server-state
|
||||
*
|
||||
* @param animationStartMessage the StartMessage received
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationStartMessage animationStartMessage) {
|
||||
state.receivedAnimationStart(animationStartMessage);//TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the BackToBattleStateMessage based on the server-state
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
@Override
|
||||
public void received(BackToBattleStateMessage backToBattleStateMessage) {
|
||||
state.receiveBackToBattleState(backToBattleStateMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the player's own map, opponent's map, and harbor based on the game details.
|
||||
*
|
||||
@@ -304,7 +321,7 @@ public void saveMap(File file) throws IOException {
|
||||
*
|
||||
* @param msg the message to be sent
|
||||
*/
|
||||
void send(ClientMessage msg) {
|
||||
public void send(ClientMessage msg) {
|
||||
if (clientSender == null)
|
||||
LOGGER.log(Level.ERROR, "trying to send {0} with sender==null", msg); //NON-NLS
|
||||
else
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.IntPoint;
|
||||
|
||||
import java.io.File;
|
||||
@@ -165,6 +163,13 @@ void receivedEffect(EffectMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); //NON-NLS
|
||||
}
|
||||
|
||||
/**
|
||||
* @param msg the message received
|
||||
*/
|
||||
void receivedAnimationStart(AnimationStartMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedAnimationStart not allowed in {0}", getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a map from the specified file.
|
||||
*
|
||||
@@ -193,6 +198,15 @@ boolean maySaveMap() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Received a BackToBattleStateMessage and logs it
|
||||
*
|
||||
* @param msg the message received
|
||||
*/
|
||||
public void receiveBackToBattleState(BackToBattleStateMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedBackToBattleState not allowed in {0}", getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Called once per frame by the update loop if this state is active.
|
||||
*
|
||||
|
||||
@@ -8,16 +8,13 @@
|
||||
package pp.battleship.game.server;
|
||||
|
||||
import pp.battleship.BattleshipConfig;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.Rotation;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
@@ -39,6 +36,8 @@ public class ServerGameLogic implements ClientInterpreter {
|
||||
private final ServerSender serverSender;
|
||||
private Player activePlayer;
|
||||
private ServerState state = ServerState.WAIT;
|
||||
private boolean playerOneAnimationReady = false;
|
||||
private boolean playerTwoAnimationReady = false;
|
||||
|
||||
/**
|
||||
* Constructs a ServerGameLogic with the specified sender and configuration.
|
||||
@@ -146,11 +145,34 @@ public void received(MapMessage msg, int from) {
|
||||
else if (!verifyMap(msg, from)) {
|
||||
LOGGER.log(Level.ERROR, "player submitted invalid map", state);
|
||||
send(getPlayerById(from), new GameDetails(config));
|
||||
}
|
||||
else
|
||||
} else
|
||||
playerReady(getPlayerById(from), msg.getShips());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received(AnimationEndMessage msg, int from) {
|
||||
LOGGER.log(Level.INFO, "AnimationEndMessage was received by ServerGameLogic");
|
||||
if (state != ServerState.ANIMATION_WAIT_STATE)
|
||||
LOGGER.log(Level.ERROR, "animation not allowed in {0}", state);
|
||||
else if (getPlayerById(from) == players.get(0)) {
|
||||
LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from));
|
||||
playerOneAnimationReady = true;
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
} else if (getPlayerById(from) == players.get(1)) {
|
||||
LOGGER.log(Level.DEBUG, "{0} set to true {1}", getPlayerById(from), getPlayerById(from).toString());
|
||||
playerTwoAnimationReady = true;
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
}
|
||||
if (playerOneAnimationReady && playerTwoAnimationReady) {
|
||||
setState(ServerState.BATTLE);
|
||||
for (Player player : players)
|
||||
send(player, new BackToBattleStateMessage(player == activePlayer));
|
||||
playerOneAnimationReady = false;
|
||||
playerTwoAnimationReady = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* this method returns true, if the given map is valid (don't overlap or out of bound)
|
||||
*
|
||||
@@ -206,8 +228,13 @@ private boolean verifyOverlap(MapMessage msg) {
|
||||
public void received(ShootMessage msg, int from) {
|
||||
if (state != ServerState.BATTLE)
|
||||
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
|
||||
else
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
else {
|
||||
for (Player player : players) {
|
||||
send(player, new AnimationStartMessage(msg.getPosition(), player == activePlayer));
|
||||
setState(ServerState.ANIMATION_WAIT_STATE);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,38 +260,125 @@ void playerReady(Player player, List<Battleship> ships) {
|
||||
/**
|
||||
* Handles the shooting action by the player.
|
||||
*
|
||||
* @param p the player who shot
|
||||
* @param pos the position of the shot
|
||||
* @param p the player who shot
|
||||
* @param position the position of the shot
|
||||
*/
|
||||
void shoot(Player p, IntPoint pos) {
|
||||
if (p != activePlayer) return;
|
||||
final Player otherPlayer = getOpponent(activePlayer);
|
||||
final Battleship selectedShip = otherPlayer.getMap().findShipAt(pos);
|
||||
void shoot(Player p, IntPoint position) {
|
||||
final Battleship selectedShip;
|
||||
selectedShip = getSelectedShip(p, position);
|
||||
if (selectedShip == null) {
|
||||
// shot missed
|
||||
send(activePlayer, EffectMessage.miss(true, pos));
|
||||
send(otherPlayer, EffectMessage.miss(false, pos));
|
||||
activePlayer = otherPlayer;
|
||||
nullShip(p, position);
|
||||
} else {
|
||||
shootShip(p, position, selectedShip);
|
||||
}
|
||||
else {
|
||||
// shot hit a ship
|
||||
selectedShip.hit(pos);
|
||||
if (otherPlayer.getMap().getRemainingShips().isEmpty()) {
|
||||
// game is over
|
||||
send(activePlayer, EffectMessage.won(pos, selectedShip));
|
||||
send(otherPlayer, EffectMessage.lost(pos, selectedShip, activePlayer.getMap().getRemainingShips()));
|
||||
setState(ServerState.GAME_OVER);
|
||||
}
|
||||
else if (selectedShip.isDestroyed()) {
|
||||
// ship has been destroyed, but game is not yet over
|
||||
send(activePlayer, EffectMessage.shipDestroyed(true, pos, selectedShip));
|
||||
send(otherPlayer, EffectMessage.shipDestroyed(false, pos, selectedShip));
|
||||
}
|
||||
else {
|
||||
// ship has been hit, but it hasn't been destroyed
|
||||
send(activePlayer, EffectMessage.hit(true, pos));
|
||||
send(otherPlayer, EffectMessage.hit(false, pos));
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the selected battleship through the shot
|
||||
*
|
||||
* @param p the current player
|
||||
* @param position the position shot at
|
||||
* @return the battleship, on the position shot at
|
||||
*/
|
||||
private Battleship getSelectedShip(Player p, IntPoint position) {
|
||||
return (p != activePlayer) ? p.getMap().findShipAt(position) : getOpponent(p).getMap().findShipAt(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* this method handles the shot logic, if the shot misses
|
||||
*
|
||||
* @param p the current player
|
||||
* @param position the position shot at
|
||||
*/
|
||||
private void nullShip(Player p, IntPoint position) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.miss(false, position));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.miss(true, position));
|
||||
}
|
||||
//switches the active player, when both have finished their animation
|
||||
if (playerOneAnimationReady && playerTwoAnimationReady) {
|
||||
LOGGER.log(Level.DEBUG, "switched active player");
|
||||
if (p != activePlayer) {
|
||||
activePlayer = p;
|
||||
} else {
|
||||
activePlayer = getOpponent(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* this method handles the shot logic, if the shot misses
|
||||
*
|
||||
* @param p the current player
|
||||
* @param position the position shot at
|
||||
* @param selectedShip the ship shot at
|
||||
*/
|
||||
private void shootShip(Player p, IntPoint position, Battleship selectedShip) {
|
||||
selectedShip.hit(position);
|
||||
if (isGameOver(getOpponent(p))) {
|
||||
gameOver(p, position, selectedShip);
|
||||
} else if (selectedShip.isDestroyed()) {
|
||||
shipDestroys(p, position, selectedShip);
|
||||
} else {
|
||||
shipHit(p, position);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checks, if the given player ends the game, bc he has no ships
|
||||
*
|
||||
* @param p the player
|
||||
* @return true, if the given player has no shi left
|
||||
*/
|
||||
private boolean isGameOver(Player p) {
|
||||
return p.getMap().getRemainingShips().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* handles the shot logic,if the game is over
|
||||
*
|
||||
* @param p the player
|
||||
* @param position the position shot at
|
||||
* @param selectedShip the current ship, shot at
|
||||
*/
|
||||
private void gameOver(Player p, IntPoint position, Battleship selectedShip) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.lost(position, selectedShip, activePlayer.getMap().getRemainingShips()));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.won(position, selectedShip));
|
||||
}
|
||||
if (playerOneAnimationReady && playerTwoAnimationReady) {
|
||||
setState(ServerState.GAME_OVER);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handles the logic,if the ship is destroyed and it's not gameOver
|
||||
*
|
||||
* @param p the player
|
||||
* @param position the position shot at
|
||||
* @param selectedShip the ship shot at
|
||||
*/
|
||||
private void shipDestroys(Player p, IntPoint position, Battleship selectedShip) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.shipDestroyed(false, position, selectedShip));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.shipDestroyed(true, position, selectedShip));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handles the logic,if the ship is hit and it's not gameOver
|
||||
*
|
||||
* @param p the player
|
||||
* @param position the position shot at
|
||||
*/
|
||||
private void shipHit(Player p, IntPoint position) {
|
||||
if (p != activePlayer) {
|
||||
send(p, EffectMessage.hit(false, position));
|
||||
} else {
|
||||
send(activePlayer, EffectMessage.hit(true, position));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,5 +29,10 @@ enum ServerState {
|
||||
/**
|
||||
* The game has ended because all the ships of one player have been destroyed.
|
||||
*/
|
||||
GAME_OVER
|
||||
GAME_OVER,
|
||||
|
||||
/**
|
||||
* The game has paused, cause the clients need time to play their respective animations
|
||||
*/
|
||||
ANIMATION_WAIT_STATE
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package pp.battleship.game.singlemode;
|
||||
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
@@ -63,6 +64,17 @@ public void received(MapMessage msg, int from) {
|
||||
copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives an AnimationEndMessage, copies it
|
||||
*
|
||||
* @param msg the message
|
||||
* @param from the connectionID
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationEndMessage msg, int from) {
|
||||
copiedMessage = new AnimationEndMessage(msg.getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the provided {@link Battleship}.
|
||||
*
|
||||
|
||||
@@ -9,19 +9,19 @@
|
||||
|
||||
import pp.battleship.game.client.BattleshipClient;
|
||||
import pp.battleship.game.client.ClientGameLogic;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* A proxy class that interprets messages from the server and forwards them to the BattleshipClient.
|
||||
* Implements the ServerInterpreter interface to handle specific server messages.
|
||||
*/
|
||||
class InterpreterProxy implements ServerInterpreter {
|
||||
private static final Logger LOGGER = System.getLogger(InterpreterProxy.class.getName());
|
||||
|
||||
private final BattleshipClient playerClient;
|
||||
|
||||
/**
|
||||
@@ -55,8 +55,7 @@ private void loadMap() {
|
||||
final ClientGameLogic clientGameLogic = playerClient.getGameLogic();
|
||||
try {
|
||||
clientGameLogic.loadMap(playerClient.getConfig().getOwnMap());
|
||||
}
|
||||
catch (IOException e) {
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to load PlayerClient map", e);
|
||||
}
|
||||
clientGameLogic.mapFinished();
|
||||
@@ -82,6 +81,27 @@ public void received(EffectMessage msg) {
|
||||
forward(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Received an AnimationStartMessage, logs it and forwards it
|
||||
*
|
||||
* @param animationStartMessage the StartMessage received
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationStartMessage animationStartMessage) {
|
||||
LOGGER.log(Level.INFO, "AnimationStartMessage was received by InterpreterProxy");
|
||||
forward(animationStartMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Received a BackToBattleStateMessage, and forwards it
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
@Override
|
||||
public void received(BackToBattleStateMessage backToBattleStateMessage) {
|
||||
forward(backToBattleStateMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards the specified ServerMessage to the client's game logic by enqueuing the message acceptance.
|
||||
*
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package pp.battleship.game.singlemode;
|
||||
|
||||
import pp.battleship.game.client.BattleshipClient;
|
||||
import pp.battleship.message.client.AnimationEndMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
import pp.battleship.message.server.StartBattleMessage;
|
||||
import pp.battleship.message.server.*;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.dto.ShipMapDTO;
|
||||
import pp.util.RandomPositionIterator;
|
||||
@@ -124,4 +122,27 @@ public void received(EffectMessage msg) {
|
||||
if (msg.isMyTurn())
|
||||
shoot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Received an AnimationStartMessage, logs it, and sends an AnimationEndMessage, bc it can't be displayed
|
||||
*
|
||||
* @param msg the StartMessage received
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationStartMessage msg) {
|
||||
LOGGER.log(Level.INFO, "AnimationStartMessage was received by RobotClient");
|
||||
connection.sendRobotMessage(new AnimationEndMessage(msg.getPosition()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Received an Message, if the BackToBattleState is active, checks, if it's his turn and fires, if it is
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
@Override
|
||||
public void received(BackToBattleStateMessage backToBattleStateMessage) {
|
||||
if (backToBattleStateMessage.isMyTurn()) {
|
||||
shoot();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package pp.battleship.message.client;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
import pp.battleship.model.IntPoint;
|
||||
|
||||
/**
|
||||
* this class represents the AnimationEndMessage, that is sent from the clients to the server, to say, they are finished with the animation
|
||||
*/
|
||||
@Serializable
|
||||
public class AnimationEndMessage extends ClientMessage {
|
||||
|
||||
/**
|
||||
* the position, where was shot at
|
||||
*/
|
||||
private IntPoint position;
|
||||
|
||||
/**
|
||||
* this private constructor is used for the serialization
|
||||
*/
|
||||
private AnimationEndMessage() { /* nothing */}
|
||||
|
||||
/**
|
||||
* this is the constructor for this class
|
||||
*
|
||||
* @param position IntPoint
|
||||
*/
|
||||
public AnimationEndMessage(IntPoint position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* the getter for the position
|
||||
*
|
||||
* @return IntPoint position
|
||||
*/
|
||||
public IntPoint getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* the accept-method for the ClientInterpreter
|
||||
*
|
||||
* @param interpreter the visitor to be used for processing
|
||||
* @param from the connection ID of the sender
|
||||
*/
|
||||
@Override
|
||||
public void accept(ClientInterpreter interpreter, int from) {
|
||||
interpreter.received(this, from);
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,12 @@ public interface ClientInterpreter {
|
||||
* @param from the connection ID from which the message was received
|
||||
*/
|
||||
void received(MapMessage msg, int from);
|
||||
|
||||
/**
|
||||
* Processes a received AnimationEndMessage
|
||||
*
|
||||
* @param animationEndMessage the message processed
|
||||
* @param from the connectionID
|
||||
*/
|
||||
void received(AnimationEndMessage animationEndMessage, int from);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package pp.battleship.message.server;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
import pp.battleship.model.IntPoint;
|
||||
|
||||
/**
|
||||
* This class represents the AnimationStartMessage, which tells the clients, that they should display their animations
|
||||
*/
|
||||
@Serializable
|
||||
public class AnimationStartMessage extends ServerMessage {
|
||||
private IntPoint position;
|
||||
private boolean myTurn;
|
||||
|
||||
/**
|
||||
* this empty constructor is needed for the serialization
|
||||
*/
|
||||
private AnimationStartMessage() { /* nothing */}
|
||||
|
||||
/**
|
||||
* this is the constructor for the AnimationMessage
|
||||
*
|
||||
* @param position the position, where was shot
|
||||
* @param isTurn of the receiving client
|
||||
*/
|
||||
public AnimationStartMessage(IntPoint position, boolean isTurn) {
|
||||
this.position = position;
|
||||
this.myTurn = isTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the position
|
||||
*
|
||||
* @return IntPoint position
|
||||
*/
|
||||
public IntPoint getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for myTurn
|
||||
*
|
||||
* @return boolean myTurn
|
||||
*/
|
||||
public boolean isMyTurn() {
|
||||
return myTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* this method accept ServerInterpreter
|
||||
*
|
||||
* @param interpreter the visitor to be used for processing
|
||||
*/
|
||||
@Override
|
||||
public void accept(ServerInterpreter interpreter) {
|
||||
interpreter.received(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the Info text for each state
|
||||
*
|
||||
* @return String InfoText
|
||||
*/
|
||||
@Override
|
||||
public String getInfoTextKey() {
|
||||
return (position + " to be animated");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package pp.battleship.message.server;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
|
||||
|
||||
/**
|
||||
* this Message tells the clients, that they should return to the BattleState
|
||||
*/
|
||||
@Serializable
|
||||
public class BackToBattleStateMessage extends ServerMessage {
|
||||
|
||||
/**
|
||||
* the boolean describes, whether it is his turn
|
||||
*/
|
||||
private boolean isMyTurn;
|
||||
|
||||
/**
|
||||
* the private constructor is used for the serialization
|
||||
*/
|
||||
private BackToBattleStateMessage() { /* nothing */}
|
||||
|
||||
/**
|
||||
* the constructor for the message
|
||||
*
|
||||
* @param isMyTurn indicates, whether the client, that receives this message, has his turn
|
||||
*/
|
||||
public BackToBattleStateMessage(boolean isMyTurn) {
|
||||
this.isMyTurn = isMyTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the MyTurn
|
||||
*
|
||||
* @return boolean myTurn
|
||||
*/
|
||||
public boolean isMyTurn() {
|
||||
return isMyTurn;
|
||||
}
|
||||
|
||||
/**
|
||||
* accept-method for ServerInterpreter
|
||||
*
|
||||
* @param interpreter the visitor to be used for processing
|
||||
*/
|
||||
@Override
|
||||
public void accept(ServerInterpreter interpreter) {
|
||||
interpreter.received(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns info text for this Message
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Override
|
||||
public String getInfoTextKey() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,18 @@ public interface ServerInterpreter {
|
||||
* @param msg the EffectMessage received
|
||||
*/
|
||||
void received(EffectMessage msg);
|
||||
|
||||
/**
|
||||
* Handles the AnimationStartMessage
|
||||
*
|
||||
* @param animationStartMessage the StartMessage received
|
||||
*/
|
||||
void received(AnimationStartMessage animationStartMessage);
|
||||
|
||||
/**
|
||||
* Handles the Message, that sets the clients back in the BattleState
|
||||
*
|
||||
* @param backToBattleStateMessage the Message received
|
||||
*/
|
||||
void received(BackToBattleStateMessage backToBattleStateMessage);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,88 @@
|
||||
package pp.battleship.model;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.lang.System.Logger.Level;
|
||||
|
||||
/**
|
||||
* this class is the model for the shell, that hits the Battleships
|
||||
*/
|
||||
public class Shell implements Item {
|
||||
|
||||
private IntPoint target;
|
||||
private static final Logger LOGGER = System.getLogger(Shell.class.getName());
|
||||
|
||||
public Shell(IntPoint target){
|
||||
this.target=target;
|
||||
/**
|
||||
* the Target, the Shell will hit, displayed through: x,y
|
||||
*/
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
/**
|
||||
* the constructor for this class
|
||||
*
|
||||
* @param target the target, the shell is fired at
|
||||
*/
|
||||
public Shell(IntPoint target) {
|
||||
this.x = target.getX();
|
||||
this.y = target.getY();
|
||||
LOGGER.log(Level.INFO, "Shell has been initialized");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* the accept-method for a generic visitor
|
||||
*
|
||||
* @param visitor the visitor performing operations on the item
|
||||
* @param <T> cause it is generic
|
||||
* @return T
|
||||
*/
|
||||
@Override
|
||||
public <T> T accept(Visitor<T> visitor) {
|
||||
return visitor.visit(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* the accept-method for the void visitor
|
||||
*
|
||||
* @param visitor the visitor performing operations on the item
|
||||
*/
|
||||
@Override
|
||||
public void accept(VoidVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
public IntPoint getTarget(){
|
||||
return target;
|
||||
|
||||
/**
|
||||
* getter for the x coordinate
|
||||
*
|
||||
* @return int x coordinate
|
||||
*/
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for the y coordinate
|
||||
*
|
||||
* @return int y coordinate
|
||||
*/
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for x coordinate
|
||||
*
|
||||
* @param x the new value of x coordinate
|
||||
*/
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for y coordinate
|
||||
*
|
||||
* @param y the new value of y coordinate
|
||||
*/
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,15 @@ public void add(Shot shot) {
|
||||
addItem(shot);
|
||||
}
|
||||
|
||||
/**
|
||||
* adds the shell to the ShipMap
|
||||
*
|
||||
* @param shell the shell that's added
|
||||
*/
|
||||
public void add(Shell shell) {
|
||||
addItem(shell);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an item from the map and triggers an item removal event.
|
||||
*
|
||||
@@ -181,8 +190,8 @@ public int getHeight() {
|
||||
*/
|
||||
public boolean isValid(Battleship ship) {
|
||||
return isValid(ship.getMinX(), ship.getMinY()) &&
|
||||
isValid(ship.getMaxX(), ship.getMaxY()) &&
|
||||
getShips().filter(s -> s != ship).noneMatch(ship::collidesWith);
|
||||
isValid(ship.getMaxX(), ship.getMaxY()) &&
|
||||
getShips().filter(s -> s != ship).noneMatch(ship::collidesWith);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,8 +203,8 @@ public boolean isValid(Battleship ship) {
|
||||
*/
|
||||
public Battleship findShipAt(int x, int y) {
|
||||
return getShips().filter(ship -> ship.contains(x, y))
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,7 +236,7 @@ public boolean isValid(IntPosition pos) {
|
||||
*/
|
||||
public boolean isValid(int x, int y) {
|
||||
return x >= 0 && x < width &&
|
||||
y >= 0 && y < height;
|
||||
y >= 0 && y < height;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,5 +22,13 @@ public enum Sound {
|
||||
/**
|
||||
* Sound of a ship being destroyed.
|
||||
*/
|
||||
DESTROYED_SHIP
|
||||
DESTROYED_SHIP,
|
||||
/**
|
||||
* Sound of a rocket
|
||||
*/
|
||||
ROCKET,
|
||||
/**
|
||||
* Stops the Sound of the rocket
|
||||
*/
|
||||
ROCKET_STOP
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ public void testRemoveItem() {
|
||||
map.remove(battleship);
|
||||
final List<Item> items = map.getItems();
|
||||
assertFalse(items.contains(battleship));
|
||||
//TODO
|
||||
verify(mockBroker).notifyListeners(any(ItemAddedEvent.class));
|
||||
verify(mockBroker).notifyListeners(any(ItemRemovedEvent.class));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user