docs: Add javadoc
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package cz.jzitnik.common;
|
||||
|
||||
/**
|
||||
* Configuration constants for the common module.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public class Config {
|
||||
public static final int WORLD_PASSWORD_LENGTH = 5;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents coordinates in a room.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
@Slf4j
|
||||
@ToString
|
||||
@Getter
|
||||
@@ -17,6 +22,12 @@ public class RoomCords implements Cloneable, Serializable {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
/**
|
||||
* Calculates the center coordinate based on a texture's dimensions.
|
||||
*
|
||||
* @param texture The texture to calculate the center for
|
||||
* @return A new RoomCords instance representing the center
|
||||
*/
|
||||
public RoomCords calculateCenter(BufferedImage texture) {
|
||||
return new RoomCords(
|
||||
x + texture.getWidth() / 2,
|
||||
@@ -24,6 +35,12 @@ public class RoomCords implements Cloneable, Serializable {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs RoomCords with specified x and y values.
|
||||
*
|
||||
* @param x The x coordinate
|
||||
* @param y The y coordinate
|
||||
*/
|
||||
@JsonCreator
|
||||
public RoomCords(
|
||||
@JsonProperty("x") int x,
|
||||
@@ -32,15 +49,34 @@ public class RoomCords implements Cloneable, Serializable {
|
||||
updateCords(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the coordinates with new x and y values.
|
||||
*
|
||||
* @param x The new x coordinate
|
||||
* @param y The new y coordinate
|
||||
*/
|
||||
public void updateCords(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the coordinates using another RoomCords instance.
|
||||
*
|
||||
* @param roomCords The RoomCords instance to copy values from
|
||||
*/
|
||||
public void updateCords(RoomCords roomCords) {
|
||||
updateCords(roomCords.getX(), roomCords.getY());
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the coordinates only if the new position does not collide with specified colliders.
|
||||
*
|
||||
* @param colliders The list of colliders in the room
|
||||
* @param x The target x coordinate
|
||||
* @param y The target y coordinate
|
||||
* @param playerCollider The collider of the player
|
||||
*/
|
||||
public void updateCordsWithColliders(List<RoomPart> colliders, int x, int y, RoomPart playerCollider) {
|
||||
var normalizedPlayerCollider = new RoomPart(
|
||||
new RoomCords(playerCollider.getStart().getX() + x, playerCollider.getStart().getY() + y),
|
||||
@@ -53,6 +89,11 @@ public class RoomCords implements Cloneable, Serializable {
|
||||
updateCords(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a copy of this object.
|
||||
*
|
||||
* @return A clone of this instance
|
||||
*/
|
||||
@Override
|
||||
public RoomCords clone() {
|
||||
try {
|
||||
|
||||
@@ -7,12 +7,23 @@ import lombok.ToString;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Represents a rectangular part of a room, defined by start and end coordinates.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
public class RoomPart implements Serializable {
|
||||
private RoomCords start;
|
||||
private RoomCords end;
|
||||
|
||||
/**
|
||||
* Constructs a RoomPart with specified start and end coordinates.
|
||||
*
|
||||
* @param start The start coordinates (top-left)
|
||||
* @param end The end coordinates (bottom-right)
|
||||
*/
|
||||
@JsonCreator
|
||||
public RoomPart(
|
||||
@JsonProperty("start") RoomCords start,
|
||||
@@ -22,6 +33,12 @@ public class RoomPart implements Serializable {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given coordinates are within this room part.
|
||||
*
|
||||
* @param cords The coordinates to check
|
||||
* @return true if the coordinates are within this part, false otherwise
|
||||
*/
|
||||
public boolean isWithin(RoomCords cords) {
|
||||
return cords.getX() >= start.getX() &&
|
||||
cords.getX() <= end.getX() &&
|
||||
@@ -30,7 +47,10 @@ public class RoomPart implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this GameRoomPart overlaps with another.
|
||||
* Checks if this RoomPart overlaps with another.
|
||||
*
|
||||
* @param other The other RoomPart to check for overlap
|
||||
* @return true if it overlaps, false otherwise
|
||||
*/
|
||||
public boolean isOverlapping(RoomPart other) {
|
||||
return start.getX() <= other.getEnd().getX() &&
|
||||
|
||||
@@ -9,6 +9,11 @@ import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Data required for player creation.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
@Getter
|
||||
public final class PlayerCreation implements Serializable {
|
||||
@Setter
|
||||
@@ -16,6 +21,12 @@ public final class PlayerCreation implements Serializable {
|
||||
private final RoomCords playerCords;
|
||||
private final RoomPart collider;
|
||||
|
||||
/**
|
||||
* Constructs a PlayerCreation instance.
|
||||
*
|
||||
* @param playerCords The player's initial coordinates
|
||||
* @param collider The player's collider
|
||||
*/
|
||||
@JsonCreator
|
||||
public PlayerCreation(
|
||||
@JsonProperty("playerCords") RoomCords playerCords,
|
||||
|
||||
@@ -2,5 +2,10 @@ package cz.jzitnik.common.socket;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Base interface for all messages sent over the socket.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public interface SocketMessage extends Serializable {
|
||||
}
|
||||
|
||||
@@ -2,5 +2,10 @@ package cz.jzitnik.common.socket.messages;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Test message for socket communication.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public class Test implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -2,5 +2,10 @@ package cz.jzitnik.common.socket.messages.game;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message indicating that the game has been won.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record GameWin() implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -2,5 +2,11 @@ package cz.jzitnik.common.socket.messages.game;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message indicating that a player has died.
|
||||
*
|
||||
* @param playerId The ID of the player who died.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record PlayerDeath(int playerId) implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -2,5 +2,11 @@ package cz.jzitnik.common.socket.messages.game.connection;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message sent to request connection to a game.
|
||||
*
|
||||
* @param gamePass The password of the game to connect to.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record ConnectToAGame(String gamePass) implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -5,20 +5,45 @@ import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Response message sent after a connection request to a game.
|
||||
*
|
||||
* @param responseType The type of response (SUCCESS or GAME_DOES_NOT_EXIST).
|
||||
* @param playerCreation Information about the player being created.
|
||||
* @param existingPlayers List of existing players in the game.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record ConnectToAGameResponse(ResponseType responseType, PlayerCreation playerCreation, List<PlayerCreation> existingPlayers) implements SocketMessage {
|
||||
/**
|
||||
* Enum representing the possible response types for game connection.
|
||||
*/
|
||||
private enum ResponseType {
|
||||
GAME_DOES_NOT_EXIST,
|
||||
SUCCESS
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor representing a failed connection (game does not exist).
|
||||
*/
|
||||
public ConnectToAGameResponse() {
|
||||
this(ResponseType.GAME_DOES_NOT_EXIST, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for a successful connection.
|
||||
*
|
||||
* @param playerCreation Information about the player being created.
|
||||
* @param existingPlayers List of existing players in the game.
|
||||
*/
|
||||
public ConnectToAGameResponse(PlayerCreation playerCreation, List<PlayerCreation> existingPlayers) {
|
||||
this(ResponseType.SUCCESS, playerCreation, existingPlayers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connection was successful.
|
||||
*
|
||||
* @return true if successful, false otherwise.
|
||||
*/
|
||||
public boolean success() {
|
||||
return responseType == ResponseType.SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,5 +2,10 @@ package cz.jzitnik.common.socket.messages.game.creation;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message sent to request game creation.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public class CreateGame implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ import cz.jzitnik.common.socket.SocketMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Response message sent after a game is created.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class CreateGameResponse implements SocketMessage {
|
||||
|
||||
@@ -2,6 +2,13 @@ package cz.jzitnik.common.socket.messages.items;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message indicating that an item was taken from a chest.
|
||||
*
|
||||
* @param roomId The ID of the room where the chest is located.
|
||||
* @param id The ID of the item taken.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record ItemTookFromChest(
|
||||
String roomId, // For faster lookup i guess
|
||||
int id
|
||||
|
||||
@@ -3,6 +3,16 @@ package cz.jzitnik.common.socket.messages.player;
|
||||
import cz.jzitnik.common.models.coordinates.RoomCords;
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message sent when a player's arrival status in a room changes.
|
||||
*
|
||||
* @param id The ID of the player.
|
||||
* @param playerCords The current coordinates of the player.
|
||||
* @param playerRotation The current rotation of the player.
|
||||
* @param arrived Whether the player has arrived or departed.
|
||||
* @param rerender Whether a rerender is requested.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record PlayerArrivalChange(int id, RoomCords playerCords, PlayerRotation playerRotation,
|
||||
boolean arrived, boolean rerender) implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -2,5 +2,11 @@ package cz.jzitnik.common.socket.messages.player;
|
||||
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message indicating that a player has disconnected.
|
||||
*
|
||||
* @param playerId The ID of the player who disconnected.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record PlayerDisconnected(int playerId) implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -3,5 +3,11 @@ package cz.jzitnik.common.socket.messages.player;
|
||||
import cz.jzitnik.common.models.player.PlayerCreation;
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message indicating that a new player has joined the game.
|
||||
*
|
||||
* @param playerCreation Information about the player who joined.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record PlayerJoined(PlayerCreation playerCreation) implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -3,5 +3,12 @@ package cz.jzitnik.common.socket.messages.player;
|
||||
import cz.jzitnik.common.models.coordinates.RoomCords;
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message sent when a player moves.
|
||||
*
|
||||
* @param newCords The new coordinates of the player.
|
||||
* @param playerRotation The rotation of the player.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record PlayerMove(RoomCords newCords, PlayerRotation playerRotation) implements SocketMessage {
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ import cz.jzitnik.common.socket.SocketMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Message sent when another player moves within the same room.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class PlayerMovedInUrRoom implements SocketMessage {
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package cz.jzitnik.common.socket.messages.player;
|
||||
|
||||
/**
|
||||
* Enum representing the possible rotations of a player.
|
||||
*
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public enum PlayerRotation {
|
||||
FRONT, BACK, LEFT, RIGHT
|
||||
}
|
||||
|
||||
@@ -3,6 +3,14 @@ package cz.jzitnik.common.socket.messages.room;
|
||||
import cz.jzitnik.common.models.coordinates.RoomCords;
|
||||
import cz.jzitnik.common.socket.SocketMessage;
|
||||
|
||||
/**
|
||||
* Message sent to request moving a player to a new room.
|
||||
*
|
||||
* @param newRoomId The ID of the room to move to.
|
||||
* @param oldCords The coordinates in the old room.
|
||||
* @param newCords The initial coordinates in the new room.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record MovePlayerRoom(String newRoomId, RoomCords oldCords, RoomCords newCords) implements SocketMessage {
|
||||
|
||||
}
|
||||
|
||||
@@ -9,13 +9,37 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Response message sent after a player moves to a different room.
|
||||
*
|
||||
* @param players Set of players currently in the new room.
|
||||
* @author Jakub Žitník (jzitnik)
|
||||
*/
|
||||
public record MovePlayerRoomResponse(Set<Registry> players) implements SocketMessage {
|
||||
/**
|
||||
* Represents a registry of a player in a room.
|
||||
*
|
||||
* @param id The ID of the player.
|
||||
* @param cords The coordinates of the player.
|
||||
* @param playerRotation The rotation of the player.
|
||||
*/
|
||||
public record Registry(int id, RoomCords cords, PlayerRotation playerRotation) implements Serializable {}
|
||||
|
||||
/**
|
||||
* Gets a player's registry by their ID.
|
||||
*
|
||||
* @param id The ID of the player to find.
|
||||
* @return An Optional containing the registry if found, otherwise empty.
|
||||
*/
|
||||
public Optional<Registry> getById(int id) {
|
||||
return players.stream().filter(registry -> registry.id == id).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the set of IDs of all players in the response.
|
||||
*
|
||||
* @return A Set of player IDs.
|
||||
*/
|
||||
public Set<Integer> getIds() {
|
||||
return players.stream().map(Registry::id).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user