added JavaDocs commects where they where missing and removed outcommented code

This commit is contained in:
Hanno Fleischer
2024-10-11 01:25:10 +02:00
parent c56767d994
commit ffd3951a78
16 changed files with 248 additions and 43 deletions

View File

@@ -119,6 +119,12 @@ public Spatial visit(Battleship ship) {
return shipNode; return shipNode;
} }
/**
* this method will create a representation of a shell in the map
*
* @param shell the Shell element to visit
* @return the node the representation is attached to
*/
@Override @Override
public Spatial visit(Shell shell) { public Spatial visit(Shell shell) {
LOGGER.log(Logger.Level.DEBUG, "Visiting {0}", shell); LOGGER.log(Logger.Level.DEBUG, "Visiting {0}", shell);

View File

@@ -11,6 +11,9 @@
import java.lang.System.Logger; import java.lang.System.Logger;
/**
* This class controls a 3D representation of a shell
*/
public class ShellControl extends AbstractControl { public class ShellControl extends AbstractControl {
private final Shell shell; private final Shell shell;
private final BattleshipApp app; private final BattleshipApp app;
@@ -19,11 +22,23 @@ public class ShellControl extends AbstractControl {
static final Logger LOGGER = System.getLogger(ShellControl.class.getName()); static final Logger LOGGER = System.getLogger(ShellControl.class.getName());
/**
* Constructor to create a new ShellControl object
*
* @param shell the shell to be displayed
* @param app the main application
*/
public ShellControl(Shell shell, BattleshipApp app) { public ShellControl(Shell shell, BattleshipApp app) {
this.shell = shell; this.shell = shell;
this.app = app; this.app = app;
} }
/**
* this method moves the representation towards it destination
* and deletes it if it reaches its target
*
* @param tpf time per frame (in seconds)
*/
@Override @Override
protected void controlUpdate(float tpf) { protected void controlUpdate(float tpf) {
spatial.move(0, -MOVE_SPEED * tpf, 0); spatial.move(0, -MOVE_SPEED * tpf, 0);
@@ -35,8 +50,14 @@ protected void controlUpdate(float tpf) {
} }
} }
/**
* This method is called during the rendering phase, but it does not perform any
* operations in this implementation as the control only influences the spatial's
* transformation, not its rendering process.
*
* @param rm the RenderManager rendering the controlled Spatial (not null)
* @param vp the ViewPort being rendered (not null)
*/
@Override @Override
protected void controlRender(RenderManager rm, ViewPort vp) { protected void controlRender(RenderManager rm, ViewPort vp) {

View File

@@ -9,12 +9,22 @@
import pp.battleship.model.IntPoint; import pp.battleship.model.IntPoint;
import pp.util.Position; import pp.util.Position;
/**
* This class controls a ShellMap element
*/
public class ShellMapControl extends AbstractControl { public class ShellMapControl extends AbstractControl {
private final Position position; private final Position position;
private final IntPoint pos; private final IntPoint pos;
private static final Vector3f vector = new Vector3f(); private static final Vector3f vector = new Vector3f();
private final BattleshipApp app; private final BattleshipApp app;
/**
* constructs a new ShellMapControl object
*
* @param position the position where the shell should move to on the map
* @param app the main application
* @param pos the position the then to render shot goes to
*/
public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) { public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) {
super(); super();
this.position = position; this.position = position;
@@ -23,6 +33,12 @@ public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) {
vector.set(new Vector3f(position.getX(), position.getY(), 0)); vector.set(new Vector3f(position.getX(), position.getY(), 0));
} }
/**
* this method moves the shell representation to its correct spot and removes it after
* it arrived at its destination
*
* @param tpf time per frame (in seconds)
*/
@Override @Override
protected void controlUpdate(float tpf) { protected void controlUpdate(float tpf) {
spatial.move(vector.mult(tpf)); spatial.move(vector.mult(tpf));
@@ -32,6 +48,14 @@ protected void controlUpdate(float tpf) {
} }
} }
/**
* This method is called during the rendering phase, but it does not perform any
* operations in this implementation as the control only influences the spatial's
* transformation, not its rendering process.
*
* @param rm the RenderManager rendering the controlled Spatial (not null)
* @param vp the ViewPort being rendered (not null)
*/
@Override @Override
protected void controlRender(RenderManager rm, ViewPort vp) { protected void controlRender(RenderManager rm, ViewPort vp) {

View File

@@ -13,9 +13,11 @@ public class AnimatonState extends ClientState {
private boolean myTurn; private boolean myTurn;
/** /**
* Constructs a client state of the specified game logic. * creates an object of AnimationState
* *
* @param logic the game logic * @param logic the client logic
* @param myTurn a boolean containing if it is the clients turn
* @param position the position a shell should be created
*/ */
public AnimatonState(ClientGameLogic logic, boolean myTurn, IntPoint position) { public AnimatonState(ClientGameLogic logic, boolean myTurn, IntPoint position) {
super(logic); super(logic);
@@ -28,6 +30,11 @@ public AnimatonState(ClientGameLogic logic, boolean myTurn, IntPoint position) {
} }
} }
/**
* This method makes sure the client renders the correct view
*
* @return true
*/
@Override @Override
boolean showBattle() { boolean showBattle() {
return true; return true;
@@ -54,6 +61,11 @@ public void receivedEffect(EffectMessage msg) {
} }
} }
/**
* this method is used to change the client to the battle state again
*
* @param msg the message to process
*/
@Override @Override
public void receivedSwitchBattleState(SwitchBattleState msg) { public void receivedSwitchBattleState(SwitchBattleState msg) {
logic.setState(new BattleState(logic, msg.isTurn())); logic.setState(new BattleState(logic, msg.isTurn()));

View File

@@ -35,6 +35,11 @@ public BattleState(ClientGameLogic logic, boolean myTurn) {
this.myTurn = myTurn; this.myTurn = myTurn;
} }
/**
* This method makes sure the client renders the correct view
*
* @return true
*/
@Override @Override
public boolean showBattle() { public boolean showBattle() {
return true; return true;

View File

@@ -225,11 +225,21 @@ public void received(EffectMessage msg) {
state.receivedEffect(msg); state.receivedEffect(msg);
} }
/**
* Reports that the client should start an animation
*
* @param msg the AnimationStartMessage received
*/
@Override @Override
public void received(AnimationStartMessage msg) { public void received(AnimationStartMessage msg) {
state.receivedAnimationStart(msg); state.receivedAnimationStart(msg);
} }
/**
* Reports that the client should move to the battle state
*
* @param msg the SwitchBattleState received
*/
@Override @Override
public void received(SwitchBattleState msg) { public void received(SwitchBattleState msg) {
state.receivedSwitchBattleState(msg); state.receivedSwitchBattleState(msg);

View File

@@ -163,10 +163,20 @@ void receivedEffect(EffectMessage msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); //NON-NLS ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); //NON-NLS
} }
/**
* Reports that the client should start an animation
*
* @param msg the AnimationStartMessage received
*/
void receivedAnimationStart(AnimationStartMessage msg){ void receivedAnimationStart(AnimationStartMessage msg){
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName());
} }
/**
* Reports that the client should move to the battle state
*
* @param msg the SwitchBattleState received
*/
void receivedSwitchBattleState(SwitchBattleState msg){ void receivedSwitchBattleState(SwitchBattleState msg){
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedSwitchBattleState not allowed in {0}", getName()); ClientGameLogic.LOGGER.log(Level.ERROR, "receivedSwitchBattleState not allowed in {0}", getName());
} }

View File

@@ -251,6 +251,12 @@ else if (!checkMapToLoad(dto)) {
selectedInHarbor = null; selectedInHarbor = null;
} }
/**
* This method is used to check if the loaded map is correct
*
* @param dto the data transfer object to check
* @return boolean if map is correct or not
*/
private boolean checkMapToLoad(ShipMapDTO dto) { private boolean checkMapToLoad(ShipMapDTO dto) {
int mapWidth = dto.getWidth(); int mapWidth = dto.getWidth();
int mapHeight = dto.getHeight(); int mapHeight = dto.getHeight();

View File

@@ -205,6 +205,12 @@ public void received(ShootMessage msg, int from) {
} }
} }
/**
* Handles a clients message that it is done with the animation
*
* @param msg the AnimationEndMessage to be processed
* @param from the connection ID from which the message was received
*/
@Override @Override
public void received(AnimationEndMessage msg, int from){ public void received(AnimationEndMessage msg, int from){
if(state != ServerState.ANIMATION_WAIT) if(state != ServerState.ANIMATION_WAIT)
@@ -248,45 +254,13 @@ void playerReady(Player player, List<Battleship> ships) {
} }
} }
/*
/** /**
* Handles the shooting action by the player. * This method decides what effectMessage the client should get based on the shot made
* and switches the active player if a shot was missed
* *
* @param p the player who shot * @param p the player to be sent the message
* @param pos the position of the shot * @param position the position where the shot would hit in the 2d map model
*//* */
void shoot(Player p, IntPoint pos) {
if (p != activePlayer) return;
final Player otherPlayer = getOpponent(activePlayer);
final Battleship selectedShip = otherPlayer.getMap().findShipAt(pos);
if (selectedShip == null) {
// shot missed
send(activePlayer, EffectMessage.miss(true, pos));
send(otherPlayer, EffectMessage.miss(false, pos));
activePlayer = otherPlayer;
}
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));
}
}
}*/
void shoot(Player p, IntPoint position) { void shoot(Player p, IntPoint position) {
final Battleship selectedShip; final Battleship selectedShip;
if(p != activePlayer){ if(p != activePlayer){

View File

@@ -60,6 +60,13 @@ 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 a AnimationEndMessage
* Creates a copy of the AnimationEndMessage
*
* @param msg the AnimationEndMessage to be processed
* @param from the connection ID from which the message was received
*/
@Override @Override
public void received(AnimationEndMessage msg, int from) { public void received(AnimationEndMessage msg, int from) {
copiedMessage = new AnimationEndMessage(msg.getPosition()); copiedMessage = new AnimationEndMessage(msg.getPosition());

View File

@@ -79,11 +79,21 @@ public void received(EffectMessage msg) {
forward(msg); forward(msg);
} }
/**
* Forwards the received AnimationStartMessage to the client's game logic.
*
* @param msg the AnimationStartMessage received from the server
*/
@Override @Override
public void received(AnimationStartMessage msg) { public void received(AnimationStartMessage msg) {
forward(msg); forward(msg);
} }
/**
* Forwards the received SwitchBattleState to the client's game logic.
*
* @param msg the SwitchBattleState received from the server
*/
@Override @Override
public void received(SwitchBattleState msg){ public void received(SwitchBattleState msg){
LOGGER.log(System.Logger.Level.INFO, "Received SwitchBattleState"); LOGGER.log(System.Logger.Level.INFO, "Received SwitchBattleState");

View File

@@ -111,8 +111,7 @@ public void received(StartBattleMessage msg) {
} }
/** /**
* Receives an effect message, logs it, and updates the turn status. * Receives an effect message, logs it.
* If it is RobotClient's turn to shoot, schedules a shot using shoot();
* *
* @param msg The effect message * @param msg The effect message
*/ */
@@ -121,12 +120,22 @@ public void received(EffectMessage msg) {
LOGGER.log(Level.INFO, "Received EffectMessage: {0}", msg); //NON-NLS LOGGER.log(Level.INFO, "Received EffectMessage: {0}", msg); //NON-NLS
} }
/**
* Receives an AnimationStartMessage, and responds instantly with an AnimationEndMessage
*
* @param msg the AnimationStartMessage received
*/
@Override @Override
public void received(AnimationStartMessage msg) { public void received(AnimationStartMessage msg) {
LOGGER.log(Level.INFO, "Received AnimationStartMessage: {0}", msg); LOGGER.log(Level.INFO, "Received AnimationStartMessage: {0}", msg);
connection.sendRobotMessage(new AnimationEndMessage(msg.getPosition())); connection.sendRobotMessage(new AnimationEndMessage(msg.getPosition()));
} }
/**
* Receives a SwitchBattleState, and shots if it is the robots turn
*
* @param msg the SwitchBattleState received
*/
@Override @Override
public void received(SwitchBattleState msg){ public void received(SwitchBattleState msg){
LOGGER.log(Level.INFO, "Received SwitchBattleStateMessage: {0}", msg); LOGGER.log(Level.INFO, "Received SwitchBattleStateMessage: {0}", msg);

View File

@@ -8,16 +8,35 @@ public class AnimationEndMessage extends ClientMessage {
private IntPoint position; private IntPoint position;
/**
* used for serialization
*/
private AnimationEndMessage(){ /* nothing */} private AnimationEndMessage(){ /* nothing */}
/**
* constructs a new AnimationEndMessage
*
* @param position the position to be effected by the server
*/
public AnimationEndMessage(IntPoint position) { public AnimationEndMessage(IntPoint position) {
this.position = position; this.position = position;
} }
/**
* getter for the position
*
* @return IntPoint position
*/
public IntPoint getPosition() { public IntPoint getPosition() {
return position; return position;
} }
/**
* Accepts Visitors to process this message
*
* @param interpreter the visitor to be used for processing
* @param from the connection ID of the sender
*/
@Override @Override
public void accept(ClientInterpreter interpreter, int from) { public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from); interpreter.received(this, from);

View File

@@ -8,26 +8,55 @@ public class AnimationStartMessage extends ServerMessage {
private IntPoint position; private IntPoint position;
private boolean myTurn; private boolean myTurn;
/**
* used for serialization
*/
private AnimationStartMessage(){ /* nothing */} private AnimationStartMessage(){ /* nothing */}
/**
* constructs a new AnimationStartMessage
*
* @param position the Position a shell should affect
* @param isTurn boolean containing if it is the clients turn or not
*/
public AnimationStartMessage(IntPoint position, boolean isTurn) { public AnimationStartMessage(IntPoint position, boolean isTurn) {
this.position = position; this.position = position;
this.myTurn = isTurn; this.myTurn = isTurn;
} }
/**
* getter for the position
*
* @return IntPoint position
*/
public IntPoint getPosition() { public IntPoint getPosition() {
return position; return position;
} }
/**
* getter for myTurn
*
* @return boolean myTurn
*/
public boolean isMyTurn() { public boolean isMyTurn() {
return myTurn; return myTurn;
} }
/**
* Accepts visitors to process this message
*
* @param interpreter the visitor to be used for processing
*/
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {
interpreter.received(this); interpreter.received(this);
} }
/**
* returns a string that gives context to the message
*
* @return String teh context
*/
@Override @Override
public String getInfoTextKey() { public String getInfoTextKey() {
return (position + " to be animated"); return (position + " to be animated");

View File

@@ -6,21 +6,44 @@
public class SwitchBattleState extends ServerMessage { public class SwitchBattleState extends ServerMessage {
private boolean isTurn; private boolean isTurn;
/**
* used for serialization
*/
private SwitchBattleState(){ /* nothing */} private SwitchBattleState(){ /* nothing */}
/**
* constructs a new SwitchBattleState message
*
* @param isTurn boolean containing if it is the clients turn
*/
public SwitchBattleState(boolean isTurn) { public SwitchBattleState(boolean isTurn) {
this.isTurn = isTurn; this.isTurn = isTurn;
} }
/**
* getter for isTurn
*
* @return boolean isTurn
*/
public boolean isTurn() { public boolean isTurn() {
return isTurn; return isTurn;
} }
/**
* accept visitors the process this message
*
* @param interpreter the visitor to be used for processing
*/
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {
interpreter.received(this); interpreter.received(this);
} }
/**
* returns a string containing context for this method
*
* @return String containing context
*/
@Override @Override
public String getInfoTextKey() { public String getInfoTextKey() {
return ""; return "";

View File

@@ -1,35 +1,75 @@
package pp.battleship.model; package pp.battleship.model;
/**
* This class represents a shell
*/
public class Shell implements Item { public class Shell implements Item {
private int x; private int x;
private int y; private int y;
/**
* constructs a new shell object
*
* @param position the end position of the shell
*/
public Shell(IntPoint position) { public Shell(IntPoint position) {
x = position.getX(); x = position.getX();
y = position.getY(); y = position.getY();
} }
/**
* getter for the x coordinate
*
* @return int x coordinate
*/
public int getX() { public int getX() {
return x; return x;
} }
/**
* getter for the y coordinate
*
* @return int y coordinate
*/
public int getY() { public int getY() {
return y; return y;
} }
/**
* setter for x coordinate
*
* @param x the new value of x coordinate
*/
public void setX(int x) { public void setX(int x) {
this.x = x; this.x = x;
} }
/**
* setter for y coordinate
*
* @param y the new value of y coordinate
*/
public void setY(int y) { public void setY(int y) {
this.y = y; this.y = y;
} }
/**
* Accepts a visitor with a return value.
*
* @param visitor the visitor to accept
* @param <T> the type of the return value
* @return the result of the visitor's visit method
*/
@Override @Override
public <T> T accept(Visitor<T> visitor) { public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this); return visitor.visit(this);
} }
/**
* Accepts a visitor without a return value.
*
* @param visitor the visitor to accept
*/
@Override @Override
public void accept(VoidVisitor visitor) { public void accept(VoidVisitor visitor) {
visitor.visit(this); visitor.visit(this);