From edff9e1ad968c8a0b6cded86c29da08896a6bb8e Mon Sep 17 00:00:00 2001 From: Lukas Bauer <157071544+Heady045@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:53:59 +0200 Subject: [PATCH] Improved code documentation and clarify comments for better readability and understanding --- Projekte/battleship/client/client.properties | 2 +- .../java/pp/battleship/client/GameSound.java | 11 ++-- .../main/java/pp/battleship/client/Menu.java | 12 +++++ .../client/gui/MapViewSynchronizer.java | 9 ++-- .../battleship/client/gui/ShellControl.java | 49 ++++------------- .../client/server/BattleshipServer.java | 39 +++++++++++++- .../battleship/game/client/BattleState.java | 23 +++----- .../game/client/ClientGameLogic.java | 2 - .../main/java/pp/battleship/model/Shell.java | 52 +++++++++++-------- .../java/pp/battleship/model/Visitor.java | 6 +++ .../java/pp/battleship/model/VoidVisitor.java | 5 ++ 11 files changed, 115 insertions(+), 95 deletions(-) diff --git a/Projekte/battleship/client/client.properties b/Projekte/battleship/client/client.properties index 89d59df4..4403fa3b 100644 --- a/Projekte/battleship/client/client.properties +++ b/Projekte/battleship/client/client.properties @@ -9,7 +9,7 @@ # # Specifies the map used by the opponent in single mode. # Single mode is activated if this property is set. -map.opponent=maps/map2.json +#map.opponent=maps/map2.json # # Specifies the map used by the player in single mode. # The player must define their own map if this property is not set. diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java index e915a995..04d4470a 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java @@ -45,13 +45,6 @@ public static boolean enabledInPreferences() { return PREFERENCES.getBoolean(ENABLED_PREF, true); } - /** - * Toggles the game sound on or off. - */ - public void toggleSound() { - setEnabled(!isEnabled()); - } - /** * Sets the enabled state of this AppState. * Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)} @@ -101,6 +94,9 @@ private AudioNode loadSound(Application app, String name) { return null; } + /** + * Plays the missile launch sound effect. + */ public void missileLaunch() { missileLaunch.playInstance(); } @@ -131,7 +127,6 @@ public void shipDestroyed() { /** * Checks the according case for the soundeffect - * * @param event the received event */ @Override diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java index e49b193b..a3ac088e 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java @@ -75,6 +75,10 @@ public Menu(BattleshipApp app) { update(); } + /** + * Updates the volume if there is a change and adjusts it accordingly. + * @param tmp unused time parameter + */ public void update(float tmp){ if(volumeRef.update()) { double newVolume = volumeRef.get(); @@ -82,10 +86,18 @@ public void update(float tmp){ } } + /** + * Adjusts the background music volume to the specified value. + * @param newVolume the new volume level + */ + private void adjustVolume(double newVolume) { app.getBackgroundMusic().setVolume((float) newVolume); } + /** + * Toggles the background music on or off. + */ private void toggleMusic(){ app.getBackgroundMusic().toogleMusic(); } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java index 7e7f0c48..18031009 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java @@ -12,11 +12,8 @@ import com.jme3.scene.Node; import com.jme3.scene.Spatial; import pp.battleship.model.Battleship; -import pp.battleship.model.IntPoint; import pp.battleship.model.Shell; import pp.battleship.model.Shot; -import pp.util.FloatMath; -import pp.util.FloatPoint; import pp.util.Position; import java.lang.System.Logger; @@ -117,9 +114,9 @@ public Spatial visit(Battleship ship) { } /** - * - * @param shell - * @return + * Creates a visual representation (Spatial) for the given shell, attaching a control for its behavior. + * @param shell the shell to visit and visualize + * @return the constructed shell node (Spatial) */ @Override public Spatial visit(Shell shell) { diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java index f448f1ad..a99ec588 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java @@ -7,51 +7,26 @@ package pp.battleship.client.gui; -import com.jme3.math.Quaternion; -import com.jme3.math.Vector3f; import com.jme3.renderer.RenderManager; import com.jme3.renderer.ViewPort; import com.jme3.scene.control.AbstractControl; import pp.battleship.game.client.ClientGameLogic; import pp.battleship.message.server.EffectMessage; -import pp.battleship.model.Battleship; import pp.battleship.model.Shell; import pp.battleship.notification.Sound; -import pp.util.FloatPoint; import pp.util.Position; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; - - -import static pp.util.FloatMath.DEG_TO_RAD; -import static pp.util.FloatMath.TWO_PI; -import static pp.util.FloatMath.sin; - /** * Controls the oscillating pitch motion of a battleship model in the game. * The ship oscillates to simulate a realistic movement on water, based on its orientation and length. */ class ShellControl extends AbstractControl { - /** - * A quaternion representing the ship's current pitch rotation. - */ - private final Quaternion pitch = new Quaternion(); - /** - * The current time within the oscillation cycle, used to calculate the ship's pitch angle. - */ - private float time; - - private Shell shell; + private final Shell shell; private MapView view; - private ClientGameLogic logic; - private EffectMessage msg; + private final ClientGameLogic logic; + private final EffectMessage msg; private boolean hasPlayedSound = false; - /** - * Logger for logging messages related to ShipControl operations. - */ - static final Logger LOGGER = System.getLogger(ShipControl.class.getName()); /** * Constructs a new ShipControl instance for the specified Battleship. @@ -67,9 +42,10 @@ public ShellControl(Shell shell, ClientGameLogic clientGameLogic) { } /** - * @param shell - * @param view - * @param clientGameLogic + * Initializes ShellControl with a shell, map view, and game logic. + * @param shell the shell to be controlled + * @param view the map view to display the shell + * @param clientGameLogic the game logic instance */ public ShellControl(Shell shell, MapView view, ClientGameLogic clientGameLogic) { this.shell = shell; @@ -86,7 +62,7 @@ public ShellControl(Shell shell, MapView view, ClientGameLogic clientGameLogic) */ @Override protected void controlUpdate(float tpf) { - // If spatial is null, do nothing + if (spatial == null) return; if (shell.isFinished() && !hasPlayedSound) { @@ -109,12 +85,9 @@ else if (msg.getDestroyedShip() == null) } /** - * 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) + * Called during rendering, but no operations are needed as this control only affects spatial transformation. + * @param rm the RenderManager rendering the spatial + * @param vp the ViewPort being rendered */ @Override protected void controlRender(RenderManager rm, ViewPort vp) { diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipServer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipServer.java index a206cf8a..7c5211f7 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipServer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipServer.java @@ -10,7 +10,6 @@ import com.jme3.network.*; import com.jme3.network.serializing.Serializer; import pp.battleship.BattleshipConfig; -import pp.battleship.client.server.ReceivedMessage; import pp.battleship.game.server.Player; import pp.battleship.game.server.ServerGameLogic; import pp.battleship.game.server.ServerSender; @@ -41,7 +40,6 @@ public class BattleshipServer implements MessageListener, Conn private static final Logger LOGGER = System.getLogger(BattleshipServer.class.getName()); private static final File CONFIG_FILE = new File("server.properties"); - private final BattleshipConfig config = new BattleshipConfig(); private Server myServer; private final ServerGameLogic logic; private final BlockingQueue pendingMessages = new LinkedBlockingQueue<>(); @@ -63,17 +61,26 @@ public class BattleshipServer implements MessageListener, Conn * Creates the server. */ public BattleshipServer() { + BattleshipConfig config = new BattleshipConfig(); config.readFromIfExists(CONFIG_FILE); LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS logic = new ServerGameLogic(this, config); } + /** + * Starts the server on the given port and continuously processes incoming messages. + * @param port the port to start the server on + */ public void run(int port) { startServer(port); while (true) processNextMessage(); } + /** + * Initializes and starts the server on the specified port, handling exceptions if the server fails to start. + * @param port the port to start the server on + */ private void startServer(int port) { try { LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS @@ -89,6 +96,9 @@ private void startServer(int port) { } } + /** + * + */ private void processNextMessage() { try { pendingMessages.take().process(logic); @@ -99,6 +109,9 @@ private void processNextMessage() { } } + /** + * Processes the next message from the queue, handling interruptions during message retrieval. + */ private void initializeSerializables() { Serializer.registerClass(GameDetails.class); Serializer.registerClass(StartBattleMessage.class); @@ -110,12 +123,20 @@ private void initializeSerializables() { Serializer.registerClass(Shot.class); } + /** + * Registers message and connection listeners for the server. + */ private void registerListeners() { myServer.addMessageListener(this, MapMessage.class); myServer.addMessageListener(this, ShootMessage.class); myServer.addConnectionListener(this); } + /** + * Handles received messages, logging the source and adding client messages to the pending queue. + * @param source the connection the message was received from + * @param message the received message + */ @Override public void messageReceived(HostedConnection source, Message message) { LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS @@ -123,12 +144,22 @@ public void messageReceived(HostedConnection source, Message message) { pendingMessages.add(new ReceivedMessage(clientMessage, source.getId())); } + /** + * Handles a new connection by logging it and adding a new player to the game logic. + * @param server the server receiving the connection + * @param hostedConnection the newly added connection + */ @Override public void connectionAdded(Server server, HostedConnection hostedConnection) { LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS logic.addPlayer(hostedConnection.getId()); } + /** + * Handles the removal of a connection by logging it, checking if it belongs to an active player, and exiting if necessary. + * @param server the server losing the connection + * @param hostedConnection the removed connection + */ @Override public void connectionRemoved(Server server, HostedConnection hostedConnection) { LOGGER.log(Level.INFO, "connection closed: {0}", hostedConnection); //NON-NLS @@ -141,6 +172,10 @@ public void connectionRemoved(Server server, HostedConnection hostedConnection) } } + /** + * Closes all client connections and terminates the server with the given exit value. + * @param exitValue the exit code to terminate the application with + */ private void exit(int exitValue) { //NON-NLS LOGGER.log(Level.INFO, "close request"); //NON-NLS if (myServer != null) diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java index 675ba3f2..5cc8669a 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java @@ -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); - } } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ClientGameLogic.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ClientGameLogic.java index 83e21f31..3170e8be 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ClientGameLogic.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ClientGameLogic.java @@ -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; diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java index 48762264..b638e6e6 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java @@ -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 - * @return + * @param generic type + * @return shell */ @Override public T accept(Visitor visitor) { @@ -65,6 +67,7 @@ public T accept(Visitor 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; diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/Visitor.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/Visitor.java index 1046142b..b1847fa1 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/model/Visitor.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/Visitor.java @@ -29,5 +29,11 @@ public interface Visitor { */ 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); } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/VoidVisitor.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/VoidVisitor.java index 3bde11a1..d460cd37 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/model/VoidVisitor.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/VoidVisitor.java @@ -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); }