Improved code documentation and clarify comments for better readability and understanding

This commit is contained in:
Lukas Bauer
2024-10-11 14:53:59 +02:00
parent bf16a18b71
commit edff9e1ad9
11 changed files with 115 additions and 95 deletions

View File

@@ -34,11 +34,19 @@ public BattleState(ClientGameLogic logic, boolean myTurn) {
this.myTurn = myTurn;
}
/**
* displays the battle scene.
* @return true to show the battle
*/
@Override
public boolean showBattle() {
return true;
}
/**
* Handles clicking on the opponent's map. If it's the player's turn and the position is valid, a ShootMessage is sent.
* @param pos the clicked position on the opponent's map
*/
@Override
public void clickOpponentMap(IntPoint pos) {
if (!myTurn)
@@ -87,19 +95,4 @@ private ShipMap affectedMap(EffectMessage msg) {
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);
}
}

View File

@@ -8,12 +8,10 @@
package pp.battleship.game.client;
import pp.battleship.message.client.ClientMessage;
import pp.battleship.message.client.MapMessage;
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.model.Battleship;
import pp.battleship.model.IntPoint;
import pp.battleship.model.ShipMap;
import pp.battleship.model.dto.ShipMapDTO;

View File

@@ -6,21 +6,22 @@
import pp.util.FloatMath;
public class Shell implements Item {
private Vector3f startPosition; // Startposition des Geschosses
private Vector3f targetPosition; // Zielposition des Geschosses0
private Vector3f currentPosition; // Aktuelle Position des Geschosses
private float speed; // Geschwindigkeit des Geschosses
private EffectMessage msg;
private ClientGameLogic logic;
private final Vector3f startPosition; // Startposition des Geschosses
private final Vector3f targetPosition; // Zielposition des Geschosses0
private final Vector3f currentPosition; // Aktuelle Position des Geschosses
private final float speed; // Geschwindigkeit des Geschosses
private final EffectMessage msg;
private final ClientGameLogic logic;
private float progress;
/**
* @param startPosition
* @param targetPosition
* @param speed
* @param msg
* @param logic
* Initializes a Shell with start position, target position, speed, message, and game logic.
* @param startPosition initial position of the shell
* @param targetPosition target position of the shell
* @param speed movement speed of the shell
* @param msg effect message related to the shell
* @param logic game logic instance
*/
public Shell(Vector3f startPosition, Vector3f targetPosition, float speed, EffectMessage msg, ClientGameLogic logic) {
this.startPosition = startPosition;
@@ -31,7 +32,10 @@ public Shell(Vector3f startPosition, Vector3f targetPosition, float speed, Effec
this.logic = logic;
}
// Methode, um die Position des Geschosses basierend auf der Zeit zu aktualisieren
/**
* Updates the shell's position based on elapsed time, using eased interpolation between start and target positions.
* @param deltaTime time elapsed since last update
*/
public void updatePosition(float deltaTime) {
progress += deltaTime * speed;
progress = FloatMath.clamp(progress, 0.0f, 1.0f);
@@ -44,20 +48,18 @@ public void updatePosition(float deltaTime) {
currentPosition.z = FloatMath.extrapolateLinear(t, startPosition.z, targetPosition.z);
}
// Aktuelle Position des Geschosses
/**
* getter for Current Position
* @return current position
*/
public Vector3f getCurrentPosition() {
return currentPosition;
}
// Überprüfen, ob das Ziel erreicht ist
public boolean isAtTarget() {
return currentPosition.distance(targetPosition) < 0.0001f; // Toleranz für die Zielgenauigkeit
}
/**
* @param visitor the visitor performing operations on the item
* @param <T>
* @return
* @param <T> generic type
* @return shell
*/
@Override
public <T> T accept(Visitor<T> visitor) {
@@ -65,6 +67,7 @@ public <T> T accept(Visitor<T> visitor) {
}
/**
* Visitor pattern
* @param visitor the visitor to accept
*/
@Override
@@ -73,21 +76,24 @@ public void accept(VoidVisitor visitor) {
}
/**
* @return
* getter for the isFinished boolean
* @return true if progress lesser than 1, false otherwise
*/
public boolean isFinished() {
return progress >= 1;
}
/**
* @return
* getter for EffectMessage
* @return EffectMessage
*/
public EffectMessage getMsg() {
return msg;
}
/**
* @return
* getter for Client logic
* @return Client logic
*/
public ClientGameLogic getLogic() {
return logic;

View File

@@ -29,5 +29,11 @@ public interface Visitor<T> {
*/
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);
}

View File

@@ -26,5 +26,10 @@ public interface VoidVisitor {
*/
void visit(Battleship ship);
/**
* Visits a Shell element
*
* @param shell the Shell element to visit
*/
void visit(Shell shell);
}