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

@@ -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.

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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<HostedConnection>, 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<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>();
@@ -63,17 +61,26 @@ public class BattleshipServer implements MessageListener<HostedConnection>, 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)