Reworked gui and client states

This commit is contained in:
Johannes Schmelz
2024-12-08 01:19:34 +00:00
parent 14c68f55d9
commit 96fe09c6fa
25 changed files with 1210 additions and 582 deletions

View File

@@ -1,5 +1,9 @@
package pp.monopoly.game.client;
import pp.monopoly.game.server.Player;
import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.ViewAssetsResponse;
/**
* Represents the active client state in the Monopoly game.
* Extends {@link ClientState}.
@@ -15,4 +19,22 @@ public class ActiveState extends ClientState {
ActiveState(ClientGameLogic logic) {
super(logic);
}
@Override
boolean isTurn() {
return true;
}
@Override
void recivedNextPlayerTurn(NextPlayerTurn msg) {
logic.getBoard().clear();
for (Player player : logic.getPlayerHandler().getPlayers()) {
logic.getBoard().add(player.getFigure());
}
}
@Override
void recivedViewAssetsResponse(ViewAssetsResponse msg) {
}
}

View File

@@ -5,8 +5,10 @@ import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.List;
import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.message.client.ClientMessage;
import pp.monopoly.message.server.BuildInfo;
import pp.monopoly.message.server.BuyPropertyRequest;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.message.server.EventDrawCard;
@@ -22,9 +24,11 @@ import pp.monopoly.message.server.TradeReply;
import pp.monopoly.message.server.TradeRequest;
import pp.monopoly.message.server.ViewAssetsResponse;
import pp.monopoly.model.Board;
import pp.monopoly.model.IntPoint;
import pp.monopoly.model.Hotel;
import pp.monopoly.model.House;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.notification.ClientStateEvent;
import pp.monopoly.notification.DiceRollEvent;
import pp.monopoly.notification.ButtonStatusEvent;
@@ -33,6 +37,7 @@ import pp.monopoly.notification.GameEvent;
import pp.monopoly.notification.GameEventBroker;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.InfoTextEvent;
import pp.monopoly.notification.ItemAddedEvent;
import pp.monopoly.notification.PopUpEvent;
import pp.monopoly.notification.Sound;
import pp.monopoly.notification.SoundEvent;
@@ -54,7 +59,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
private final List<GameEventListener> listeners = new ArrayList<>();
/** The game board representing the player's current state. */
private Board board;
private Board board = new Board(10, 10, this);
/** The current state of the client game logic. */
private ClientState state = new LobbyState(this);
@@ -121,15 +126,6 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
return tradeHandler;
}
/**
* Moves the preview figure to the specified position.
*
* @param pos the new position for the preview figure
*/
public void movePreview(IntPoint pos) {
state.movePreview(pos);
}
/**
* Sets the informational text to be displayed to the player.
*
@@ -204,32 +200,21 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
state.update(delta);
}
/**
* Handles the result of a dice roll.
*
* @param msg the message containing the dice roll result
*/
public boolean isTurn() {
return state.isTurn();
}
@Override
public void received(DiceResult msg) {
playSound(Sound.DICE_ROLL);
notifyListeners(new DiceRollEvent(msg.getRollResult().get(0), msg.getRollResult().get(1)));
}
/**
* Handles drawing an event card.
*
* @param msg the message containing the drawn card details
*/
@Override
public void received(EventDrawCard msg) {
notifyListeners(new EventCardEvent(msg.getCardDescription()));
}
/**
* Handles the game over message.
*
* @param msg the message containing game over details
*/
@Override
public void received(GameOver msg) {
if (msg.isWinner()) {
@@ -241,23 +226,17 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
}
}
/**
* Handles the start of the game.
*
* @param msg the game start message
*/
@Override
public void received(GameStart msg) {
playerHandler = msg.getPlayerHandler();
setState(new WaitForTurnState(this));
for (Player player : playerHandler.getPlayers()) {
board.add(player.getFigure());
}
notifyListeners(new ButtonStatusEvent(false));
notifyListeners(new UpdatePlayerView());
}
/**
* Handles jail-related events.
*
* @param msg the message containing jail event details
*/
@Override
public void received(JailEvent msg) {
if (msg.isGoingToJail()) {
@@ -266,11 +245,6 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
}
}
/**
* Updates the status of a player.
*
* @param msg the message containing player status update details
*/
@Override
public void received(PlayerStatusUpdate msg) {
playerHandler = msg.getPlayerHandler();
@@ -278,31 +252,16 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
notifyListeners(new UpdatePlayerView());
}
/**
* Handles timeout warnings.
*
* @param msg the message containing timeout warning details
*/
@Override
public void received(TimeOutWarning msg) {
notifyListeners(new PopUpEvent("timeout", msg));
}
/**
* Displays the player's assets in response to a server query.
*
* @param msg the message containing the player's assets
*/
@Override
public void received(ViewAssetsResponse msg) {
boardManager = msg.getboard();
}
/**
* Handles trade replies from other players.
*
* @param msg the message containing the trade reply
*/
@Override
public void received(TradeReply msg) {
if (msg.isAccepted()) {
@@ -314,24 +273,15 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
}
}
/**
* Handles trade requests from other players.
*
* @param msg the message containing the trade request details
*/
@Override
public void received(TradeRequest msg) {
tradeHandler = msg.getTradeHandler();
notifyListeners(new PopUpEvent("tradeRequest", msg));
}
/**
* Handles the transition to the next player's turn.
*
* @param msg the message indicating it's the next player's turn
*/
@Override
public void received(NextPlayerTurn msg) {
setState(new ActiveState(this));
notifyListeners(new ButtonStatusEvent(true));
}
@@ -357,4 +307,18 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
notifyListeners(new PopUpEvent("ReceivedRent", msg));
}
}
@Override
public void received(BuildInfo msg) {
System.out.println("TRIGGER BUILD INFO");
if (msg.isAdded()) {
BuildingProperty property = ((BuildingProperty)boardManager.getFieldAtIndex(msg.getId()));
if (property.getHotel() == 1 ) {
board.add(new Hotel(property.getId()));
} else {
board.add(new House( property.getHouses(), property.getId()));
}
}
}
}

View File

@@ -11,7 +11,11 @@ import java.io.File;
import java.io.IOException;
import java.lang.System.Logger.Level;
import pp.monopoly.model.IntPoint;
import pp.monopoly.message.server.GameStart;
import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.NotificationMessage;
import pp.monopoly.message.server.PlayerStatusUpdate;
import pp.monopoly.message.server.ViewAssetsResponse;
/**
* Defines the behavior and state transitions for the client-side game logic in Monopoly.
@@ -54,17 +58,39 @@ abstract class ClientState {
*
* @return true if the player's turn should be shown, false otherwise
*/
boolean showTurn() {
boolean isTurn() {
return false;
}
/**
* Moves the preview figure to the specified position on the game board.
* Starts the battle based on the server message.
*
* @param pos the new position for the preview figure
* @param msg the message indicating whose turn it is to shoot
*/
void movePreview(IntPoint pos) {
ClientGameLogic.LOGGER.log(Level.DEBUG, "movePreview has no effect in {0}", getName()); //NON-NLS
void receivedGameStart(GameStart msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedGameStart not allowed in {0}", getName()); //NON-NLS
}
/**
* Updateds the view based on the new player status.
*
* @param msg the message containing the new player status
*/
void recivedPlayerStatusUpdate(PlayerStatusUpdate msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "recivedPlayerStatusUpdate not allowed in {0}", getName()); //NON-NLS
}
void recivedNextPlayerTurn(NextPlayerTurn msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "recivedNextPlayerTurn not allowed in {0}", getName()); //NON-NLS
}
void recivedNotificationMessage(NotificationMessage msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "recivedNotificationMessage not allowed in {0}", getName()); //NON-NLS
}
void recivedViewAssetsResponse(ViewAssetsResponse msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "recivedViewAssetsResponse not allowed in {0}", getName()); //NON-NLS
}
/**

View File

@@ -1,5 +1,7 @@
package pp.monopoly.game.client;
import pp.monopoly.message.server.GameStart;
/**
* Represents the lobby state of the client in the Monopoly game.
* Extends {@link ClientState}.
@@ -14,4 +16,9 @@ public class LobbyState extends ClientState {
LobbyState(ClientGameLogic logic) {
super(logic);
}
@Override
void receivedGameStart(GameStart msg) {
logic.setState(new WaitForTurnState(logic));
}
}

View File

@@ -1,5 +1,7 @@
package pp.monopoly.game.client;
import pp.monopoly.message.server.NextPlayerTurn;
/**
* Represents the state where the client is waiting for their turn in the Monopoly game.
* Extends {@link ClientState}.
@@ -14,4 +16,14 @@ public class WaitForTurnState extends ClientState {
WaitForTurnState(ClientGameLogic logic) {
super(logic);
}
@Override
boolean isTurn() {
return true;
}
@Override
void recivedNextPlayerTurn(NextPlayerTurn msg) {
logic.setState(new ActiveState(logic));
}
}

View File

@@ -7,6 +7,8 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.jme3.math.Vector3f;
import pp.monopoly.MonopolyConfig;
import pp.monopoly.message.client.AlterProperty;
import pp.monopoly.message.client.BuyPropertyResponse;
@@ -18,6 +20,7 @@ import pp.monopoly.message.client.RollDice;
import pp.monopoly.message.client.TradeOffer;
import pp.monopoly.message.client.TradeResponse;
import pp.monopoly.message.client.ViewAssetsRequest;
import pp.monopoly.message.server.BuildInfo;
import pp.monopoly.message.server.GameOver;
import pp.monopoly.message.server.GameStart;
import pp.monopoly.message.server.NextPlayerTurn;
@@ -95,6 +98,17 @@ public class ServerGameLogic implements ClientInterpreter {
}
}
/**
* Sends a message to all players in the game.
*
* @param msg the ServerMessage to send
*/
void sendAll(ServerMessage msg) {
for (Player player : playerHandler.getPlayers()) {
send(player, msg);
}
}
/**
* Adds a new player to the game if the game is in the LOBBY state and the maximum
* player limit has not been reached.
@@ -209,7 +223,9 @@ public class ServerGameLogic implements ClientInterpreter {
String name = msg.getName();
String truc = name.length() > 10 ? name.substring(0, 15) : name;
player.setName(truc);
player.setFigure(new Figure(1, -10, -10, Rotation.LEFT, msg.getFigure()));
Figure figure = new Figure(new Vector3f(0, 1, 0), Rotation.NORTH, msg.getFigure());
player.setFigure(figure);
// board.addFigure(figure);
playerHandler.setPlayerReady(player, true);
LOGGER.log(Level.DEBUG, "Player {0} is ready", player.getName());
}
@@ -407,6 +423,8 @@ public class ServerGameLogic implements ClientInterpreter {
for (BuildingProperty field : properties.stream().map(p -> (BuildingProperty) p).collect(Collectors.toList())) {
if (boardManager.canBuild(field) && sender.getAccountBalance() >= field.getHousePrice()) {
field.build();
updateAllPlayers();
sendAll( new BuildInfo(field.getId(), true));
sender.pay(field.getHousePrice());
}
}
@@ -414,11 +432,12 @@ public class ServerGameLogic implements ClientInterpreter {
for (BuildingProperty field : properties.stream().map(p -> (BuildingProperty) p).collect(Collectors.toList())) {
if (boardManager.canSell(field)) {
field.sell();
updateAllPlayers();
sendAll( new BuildInfo(field.getId(), false));
sender.earnMoney(field.getHousePrice() / 2);
}
}
}
updateAllPlayers();
}
@@ -429,9 +448,9 @@ public class ServerGameLogic implements ClientInterpreter {
} else if (msg.getKeyword().equals("PayJail")) {
playerHandler.getPlayerById(from).payBail();
} else if(msg.getKeyword().equals("hack")) {
for (BuildingProperty bp : boardManager.getPropertyFields( List.of(1,3)).stream().filter(p -> p instanceof BuildingProperty).map(p -> (BuildingProperty) p).collect(Collectors.toList())) {
bp.build();
}
// for (BuildingProperty bp : boardManager.getPropertyFields( List.of(1,3)).stream().filter(p -> p instanceof BuildingProperty).map(p -> (BuildingProperty) p).collect(Collectors.toList())) {
// bp.build();
// }
for(PropertyField field : boardManager.getBoard().stream().filter(p -> p instanceof PropertyField).map(p -> (PropertyField) p).collect(Collectors.toList())) {
field.setOwner(playerHandler.getPlayerById(0));
playerHandler.getPlayerById(0).addProperty(field.getId());

View File

@@ -0,0 +1,34 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
@Serializable
public class BuildInfo extends ServerMessage {
private final int id;
private final boolean added;
private BuildInfo() {
this(0, false);
}
public BuildInfo(int id, boolean added) {
this.id = id;
this.added = added;
}
public int getId() {
return id;
}
public boolean isAdded() {
return added;
}
@Override
public void accept(ServerInterpreter interpreter) {
interpreter.received(this);
}
}

View File

@@ -91,16 +91,23 @@ public interface ServerInterpreter {
void received(NextPlayerTurn msg);
/**
* Handles a NextPlayerTurn message received from the server.
* Handles a BuyPropertyRequest message received from the server.
*
* @param msg the NextPlayerTurn message received
* @param msg the BuyPropertyRequest message received
*/
void received(BuyPropertyRequest msg);
/**
* Handles a NextPlayerTurn message received from the server.
* Handles a NotificationMessage message received from the server.
*
* @param msg the NextPlayerTurn message received
* @param msg the NotificationMessage message received
*/
void received(NotificationMessage msg);
/**
* Handles a BuildInfo message received from the server.
*
* @param msg the BuildInfo message received
*/
void received(BuildInfo msg);
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.monopoly.model;
import java.util.ArrayList;
@@ -66,7 +59,34 @@ public class Board {
*/
private void addItem(Item item) {
items.add(item);
notifyListeners((GameEvent) new ItemAddedEvent(item, null));
notifyListeners(new ItemAddedEvent(this, item));
}
/**
* Adds a figure to the map and triggers an item addition event.
*
* @param figure the figure to be added to the map
*/
public void add(Figure figure) {
addItem(figure);
}
/**
* Adds a house to the map and triggers an item addition event.
*
* @param house the house to be added to the map
*/
public void add(House house) {
addItem(house);
}
/**
* Adds a hotel to the map and triggers an item addition event.
*
* @param hotel the hotel to be added to the map
*/
public void add(Hotel hotel) {
addItem(hotel);
}
/**
@@ -76,7 +96,11 @@ public class Board {
*/
public void remove(Item item) {
items.remove(item);
notifyListeners((GameEvent) new ItemRemovedEvent(item, null)); // Falls es ein entsprechendes ItemRemovedEvent gibt
notifyListeners((GameEvent) new ItemRemovedEvent(this, item));
}
public void removePlayers() {
items.removeIf(item -> item instanceof Figure);
}
/**
@@ -96,6 +120,33 @@ public class Board {
private <T extends Item> Stream<T> getItems(Class<T> clazz) {
return items.stream().filter(clazz::isInstance).map(clazz::cast);
}
/**
* Returns a stream of all figures currently on the map.
*
* @return a stream of all figures on the map
*/
public Stream<Figure> getFigures() {
return getItems(Figure.class);
}
/**
* Returns a stream of all houses currently on the map.
*
* @return a stream of all houses on the map
*/
public Stream<House> getHouses() {
return getItems(House.class);
}
/**
* Returns a stream of all hotels currently on the map.
*
* @return a stream of all hotels on the map
*/
public Stream<Hotel> getHotels() {
return getItems(Hotel.class);
}
/**
* Returns an unmodifiable list of all items currently on the map.
@@ -124,28 +175,6 @@ public class Board {
return height;
}
/**
* Validates whether the specified position is within the map boundaries.
*
* @param pos the position to validate
* @return true if the position is within the map, false otherwise
*/
public boolean isValid(IntPosition pos) {
return isValid(pos.getX(), pos.getY());
}
/**
* Checks if the specified coordinates are within the map boundaries.
*
* @param x the x-coordinate to validate
* @param y the y-coordinate to validate
* @return true if the coordinates are valid, false otherwise
*/
public boolean isValid(int x, int y) {
return x >= 0 && x < width &&
y >= 0 && y < height;
}
/**
* Returns a string representation of the map.
*

View File

@@ -1,28 +1,22 @@
package pp.monopoly.model;
import static java.lang.Math.max;
import static java.lang.Math.min;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.Random;
import com.jme3.math.Vector3f;
import com.jme3.network.serializing.Serializable;
@Serializable
public class Figure implements Item{
private final String type;
private final int length; // The length of the Figure
private int x; // The x-coordinate of the Figure's position
private int y; // The y-coordinate of the Figure's position
private Vector3f position;
private Rotation rot; // The rotation of the Figure
private final Set<IntPoint> damaged = new HashSet<>(); // The set of positions that have been hit on this ship
/**
* Default constructor for serialization. Initializes a Figure with length 0,
* at position (0, 0), with a default rotation of RIGHT.
* at position (0, 0), with a default rotation of NORTH.
*/
private Figure() {
this(0, 0, 0, Rotation.RIGHT, "cube");
this(null, Rotation.NORTH, "");
}
/**
@@ -30,14 +24,12 @@ public class Figure implements Item{
*
* @param length the length of the Figure
* @param x the x-coordinate of the Figure's initial position
* @param y the y-coordinate of the Figure's initial position
* @param z the z-coordinate of the Figure's initial position
* @param rot the rotation of the Figure
*/
public Figure(int length, int x, int y, Rotation rot, String type) {
this.x = x;
this.y = y;
public Figure(Vector3f position, Rotation rot, String type) {
this.position = calculateFieldPosition(0);
this.rot = rot;
this.length = length;
this.type = type;
}
@@ -46,8 +38,17 @@ public class Figure implements Item{
*
* @return the x-coordinate of the Figure
*/
public int getX() {
return x;
public float getX() {
return position.getX();
}
/**
* Returns the current z-coordinate of the Figure's position.
*
* @return the z-coordinate of the Figure
*/
public float getZ() {
return position.getZ();
}
/**
@@ -55,19 +56,17 @@ public class Figure implements Item{
*
* @return the y-coordinate of the Figure
*/
public int getY() {
return y;
public float getY() {
return position.getY();
}
/**
* Moves the Figure to the specified coordinates.
* Returns the current position of the Figure.
*
* @param x the new x-coordinate of the Figure's position
* @param y the new y-coordinate of the Figure's position
* @return the position of the Figure
*/
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
public Vector3f getPos() {
return position;
}
/**
@@ -75,8 +74,8 @@ public class Figure implements Item{
*
* @param pos the new position of the Figure
*/
public void moveTo(IntPosition pos) {
moveTo(pos.getX(), pos.getY());
public void moveTo(Vector3f pos) {
position = pos;
}
/**
@@ -85,85 +84,62 @@ public class Figure implements Item{
* @param fieldId the position to move to
*/
public void moveTo(int fieldId) {
moveTo(fieldIdToPosition(fieldId));
moveTo(calculateFieldPosition(fieldId));
}
private IntPoint fieldIdToPosition(int fieldId) {
if (fieldId < 0 || fieldId > 39) {
throw new IllegalArgumentException("Invalid fieldId: " + fieldId);
private Vector3f calculateFieldPosition(int fieldID) {
float baseX = 0.0f;
float baseZ = 0.0f;
switch (fieldID) {
case 0: baseX = -9.1f; baseZ = -9.1f; break;
case 1: baseX = -6.5f; baseZ = -9.1f; break;
case 2: baseX = -4.9f; baseZ = -9.1f; break;
case 3: baseX = -3.3f; baseZ = -9.1f; break;
case 4: baseX = -1.6f; baseZ = -9.1f; break;
case 5: baseX = 0.0f; baseZ = -9.1f; break;
case 6: baseX = 1.6f; baseZ = -9.1f; break;
case 7: baseX = 3.3f; baseZ = -9.1f; break;
case 8: baseX = 4.9f; baseZ = -9.1f; break;
case 9: baseX = 6.5f; baseZ = -9.1f; break;
case 10: baseX = 9.1f; baseZ = -9.1f; break;
case 11: baseX = 9.1f; baseZ = -6.5f; break;
case 12: baseX = 9.1f; baseZ = -4.9f; break;
case 13: baseX = 9.1f; baseZ = -3.3f; break;
case 14: baseX = 9.1f; baseZ = -1.6f; break;
case 15: baseX = 9.1f; baseZ = 0.0f; break;
case 16: baseX = 9.1f; baseZ = 1.6f; break;
case 17: baseX = 9.1f; baseZ = 3.3f; break;
case 18: baseX = 9.1f; baseZ = 4.9f; break;
case 19: baseX = 9.1f; baseZ = 6.5f; break;
case 20: baseX = 9.1f; baseZ = 9.1f; break;
case 21: baseX = 6.5f; baseZ = 9.1f; break;
case 22: baseX = 4.9f; baseZ = 9.1f; break;
case 23: baseX = 3.3f; baseZ = 9.1f; break;
case 24: baseX = 1.6f; baseZ = 9.1f; break;
case 25: baseX = 0.0f; baseZ = 9.1f; break;
case 26: baseX = -1.6f; baseZ = 9.1f; break;
case 27: baseX = -3.3f; baseZ = 9.1f; break;
case 28: baseX = -4.9f; baseZ = 9.1f; break;
case 29: baseX = -6.5f; baseZ = 9.1f; break;
case 30: baseX = -9.1f; baseZ = 9.1f; break;
case 31: baseX = -9.1f; baseZ = 6.5f; break;
case 32: baseX = -9.1f; baseZ = 4.9f; break;
case 33: baseX = -9.1f; baseZ = 3.3f; break;
case 34: baseX = -9.1f; baseZ = 1.6f; break;
case 35: baseX = -9.1f; baseZ = 0.0f; break;
case 36: baseX = -9.1f; baseZ = -1.6f; break;
case 37: baseX = -9.1f; baseZ = -3.3f; break;
case 38: baseX = -9.1f; baseZ = -4.9f; break;
case 39: baseX = -9.1f; baseZ = -6.5f; break;
default: throw new IllegalArgumentException("Ungültige Feld-ID: " + fieldID);
}
// Determine which edge and position along the edge
if (fieldId <= 9) {
// Bottom edge: From (-10, -10) to (10, -10)
int x = -10 + fieldId * 2;
return new IntPoint(x, -10);
} else if (fieldId <= 19) {
// Right edge: From (10, -10) to (10, 10)
int y = -10 + (fieldId - 10) * 2;
return new IntPoint(10, y);
} else if (fieldId <= 29) {
// Top edge: From (10, 10) to (-10, 10)
int x = 10 - (fieldId - 20) * 2;
return new IntPoint(x, 10);
} else {
// Left edge: From (-10, 10) to (-10, -10)
int y = 10 - (fieldId - 30) * 2;
return new IntPoint(-10, y);
}
}
float xOffset = new Random().nextFloat();
float zOffset = new Random().nextFloat();
private Rotation fieldIdToRotation(int fieldId) {
if (fieldId >= 0 && fieldId <= 10) return Rotation.DOWN;
else if (fieldId <= 20) return Rotation.LEFT;
else if (fieldId <= 30) return Rotation.UP;
else if (fieldId <= 39) return Rotation.RIGHT;
else throw new IllegalArgumentException();
}
/**
* Returns the length of the Figure.
*
* @return the length of the Figure
*/
public int getLength() {
return length;
}
/**
* Returns the minimum x-coordinate that the Figure occupies based on its current position and rotation.
*
* @return the minimum x-coordinate of the Figure
*/
public int getMinX() {
return x + min(0, (length - 1) * rot.dx());
}
/**
* Returns the maximum x-coordinate that the Figure occupies based on its current position and rotation.
*
* @return the maximum x-coordinate of the Figure
*/
public int getMaxX() {
return x + max(0, (length - 1) * rot.dx());
}
/**
* Returns the minimum y-coordinate that the Figure occupies based on its current position and rotation.
*
* @return the minimum y-coordinate of the Figure
*/
public int getMinY() {
return y + min(0, (length - 1) * rot.dy());
}
/**
* Returns the maximum y-coordinate that the Figure occupies based on its current position and rotation.
*
* @return the maximum y-coordinate of the Figure
*/
public int getMaxY() {
return y + max(0, (length - 1) * rot.dy());
//TODO adjust y pos
return new Vector3f(baseX + xOffset, 1, baseZ + zOffset);
}
/**
@@ -184,99 +160,6 @@ public class Figure implements Item{
this.rot = rot;
}
/**
* Rotates the Figure by 90 degrees clockwise.
*/
public void rotated() {
setRotation(rot.rotate());
}
/**
* Attempts to hit the Figure at the specified position.
* If the position is part of the Figure, the hit is recorded.
*
* @param x the x-coordinate of the position to hit
* @param y the y-coordinate of the position to hit
* @return true if the position is part of the Figure, false otherwise
* @see #contains(int, int)
*/
public boolean hit(int x, int y) {
if (!contains(x, y))
return false;
damaged.add(new IntPoint(x, y));
return true;
}
/**
* Attempts to hit the Figure at the specified position.
* If the position is part of the Figure, the hit is recorded.
* This is a convenience method for {@linkplain #hit(int, int)}.
*
* @param position the position to hit
* @return true if the position is part of the Figure, false otherwise
*/
public boolean hit(IntPosition position) {
return hit(position.getX(), position.getY());
}
/**
* Returns the positions of this Figure that have been hit.
*
* @return a set of positions that have been hit
* @see #hit(int, int)
*/
public Set<IntPoint> getDamaged() {
return Collections.unmodifiableSet(damaged);
}
/**
* Checks whether the specified position is covered by the Figure. This method does
* not record a hit, only checks coverage.
* This is a convenience method for {@linkplain #contains(int, int)}.
*
* @param pos the position to check
* @return true if the position is covered by the Figure, false otherwise
*/
public boolean contains(IntPosition pos) {
return contains(pos.getX(), pos.getY());
}
/**
* Checks whether the specified position is covered by the Figure. This method does
* not record a hit, only checks coverage.
*
* @param x the x-coordinate of the position to check
* @param y the y-coordinate of the position to check
* @return true if the position is covered by the Figure, false otherwise
*/
public boolean contains(int x, int y) {
return getMinX() <= x && x <= getMaxX() &&
getMinY() <= y && y <= getMaxY();
}
/**
* Determines if the Figure has been completely destroyed. A Figure is considered
* destroyed if all of its positions have been hit.
*
* @return true if the Figure is destroyed, false otherwise
* @see #hit(int, int)
*/
public boolean isDestroyed() {
return damaged.size() == length;
}
/**
* Checks whether this Figure collides with another Figure. Two Figures collide
* if any of their occupied positions overlap.
*
* @param other the other Figure to check collision with
* @return true if the Figures collide, false otherwise
*/
public boolean collidesWith(Figure other) {
return other.getMaxX() >= getMinX() && getMaxX() >= other.getMinX() &&
other.getMaxY() >= getMinY() && getMaxY() >= other.getMinY();
}
/**
* Returns a string representation of the Figure, including its length, position,
* and rotation.
@@ -285,7 +168,7 @@ public class Figure implements Item{
*/
@Override
public String toString() {
return "Ship{length=" + length + ", x=" + x + ", y=" + y + ", rot=" + rot + '}'; //NON-NLS
return "Figur{ x=" + position.getX() + ", z=" + position.getZ() + ", rot=" + rot + '}'; //NON-NLS
}
/**

View File

@@ -0,0 +1,99 @@
package pp.monopoly.model;
import com.jme3.math.Vector3f;
public class Hotel implements Item{
/**
* The ID of the field the hotel is on.
*/
private final int fieldID;
/**
* Creates a new hotel with fieldID 0.
*/
private Hotel() {
this.fieldID = 0;
}
/**
* Creates a new hotel on the given field.
*
* @param fieldID the ID of the field the hotel is on
*/
public Hotel(int fieldID) {
this.fieldID = fieldID;
}
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override
public void accept(VoidVisitor visitor) {
visitor.visit(this);
}
/**
* Returns the position of the building on the field.
*
* @return the position of the building on the field
*/
public Vector3f getPos() {
float baseX = 0.0f;
float baseZ = 0.0f;
switch (fieldID) {
case 0: baseX = -8.4f; baseZ = -7.7f; break;
case 1: baseX = -6.3f; baseZ = -7.7f; break;
case 2: baseX = -4.7f; baseZ = -7.7f; break;
case 3: baseX = -3.1f; baseZ = -7.7f; break;
case 4: baseX = -1.4f; baseZ = -7.7f; break;
case 5: baseX = 0.2f; baseZ = -7.7f; break;
case 6: baseX = 1.8f; baseZ = -7.7f; break;
case 7: baseX = 3.5f; baseZ = -7.7f; break;
case 8: baseX = 5.1f; baseZ = -7.7f; break;
case 9: baseX = 6.7f; baseZ = -7.7f; break;
case 10: baseX = 8.2f; baseZ = -7.7f; break;
case 11: baseX = 8.2f; baseZ = -6.5f; break; //passt
case 12: baseX = 8.2f; baseZ = -4.9f; break; //passt
case 13: baseX = 8.2f; baseZ = -3.3f; break; //passt
case 14: baseX = 8.2f; baseZ = -1.6f; break; //passt
case 15: baseX = 8.2f; baseZ = 0.0f; break; //passt
case 16: baseX = 8.2f; baseZ = 1.6f; break; //passt
case 17: baseX = 8.2f; baseZ = 3.3f; break; //passt
case 18: baseX = 8.2f; baseZ = 4.9f; break; //passt
case 19: baseX = 8.2f; baseZ = 6.5f; break; //passt
case 20: baseX = 8.2f; baseZ = 7.7f; break;
case 21: baseX = 6.5f; baseZ = 7.7f; break;
case 22: baseX = 4.9f; baseZ = 7.7f; break;
case 23: baseX = 3.3f; baseZ = 7.7f; break;
case 24: baseX = 1.6f; baseZ = 7.7f; break;
case 25: baseX = 0.0f; baseZ = 7.7f; break;
case 26: baseX = -1.6f; baseZ = 7.7f; break;
case 27: baseX = -3.3f; baseZ = 7.7f; break;
case 28: baseX = -4.9f; baseZ = 7.7f; break;
case 29: baseX = -6.5f; baseZ = 7.7f; break;
case 30: baseX = -7.2f; baseZ = 7.7f; break;
case 31: baseX = -7.2f; baseZ = 6.5f; break;
case 32: baseX = -7.2f; baseZ = 4.9f; break;
case 33: baseX = -7.2f; baseZ = 3.3f; break;
case 34: baseX = -7.2f; baseZ = 1.6f; break;
case 35: baseX = -7.2f; baseZ = 0.0f; break;
case 36: baseX = -7.2f; baseZ = -1.6f; break;
case 37: baseX = -7.2f; baseZ = -3.3f; break;
case 38: baseX = -7.2f; baseZ = -4.9f; break;
case 39: baseX = -7.2f; baseZ = -6.5f; break;
default: throw new IllegalArgumentException("Ungültige Feld-ID: " + fieldID);
}
return new Vector3f(baseX, 0, baseZ);
}
@Override
public Rotation getRot() {
// TODO
return Rotation.NORTH;
}
}

View File

@@ -0,0 +1,130 @@
package pp.monopoly.model;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.network.serializing.Serializable;
/**
* A class representing a house in the Monopoly game.
*/
@Serializable
public class House implements Item{
/**
* The stage of the house.
*/
private final int stage;
private final int fieldID;
/**
* Creates a new house with stage 0.
*/
private House() {
this.stage = 0;
this.fieldID = 0;
}
/**
* Creates a new house with the given stage.
*
* @param stage the stage of the house
*/
public House(int stage, int fieldID) {
this.stage = stage;
this.fieldID = fieldID;
}
/**
* Returns the stage of the house.
*
* @return the stage of the house
*/
public int getStage() {
return stage;
}
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override
public void accept(VoidVisitor visitor) {
visitor.visit(this);
}
/**
* Returns the position of the building on the field.
*
* @return the position of the building on the field
*/
public Vector3f getPos() {
float baseX = 0.0f;
float baseZ = 0.0f;
switch (fieldID) {
case 0: baseX = -8.4f; baseZ = -7.7f; break;
case 1: baseX = -6.3f; baseZ = -7.7f; break;
case 2: baseX = -4.7f; baseZ = -7.7f; break;
case 3: baseX = -3.1f; baseZ = -7.7f; break;
case 4: baseX = -1.4f; baseZ = -7.7f; break;
case 5: baseX = 0.2f; baseZ = -7.7f; break;
case 6: baseX = 1.8f; baseZ = -7.7f; break;
case 7: baseX = 3.5f; baseZ = -7.7f; break;
case 8: baseX = 5.1f; baseZ = -7.7f; break;
case 9: baseX = 6.7f; baseZ = -7.7f; break;
case 10: baseX = 8.2f; baseZ = -7.7f; break;
case 11: baseX = 8.2f; baseZ = -6.5f; break; //passt
case 12: baseX = 8.2f; baseZ = -4.9f; break; //passt
case 13: baseX = 8.2f; baseZ = -3.3f; break; //passt
case 14: baseX = 8.2f; baseZ = -1.6f; break; //passt
case 15: baseX = 8.2f; baseZ = 0.0f; break; //passt
case 16: baseX = 8.2f; baseZ = 1.6f; break; //passt
case 17: baseX = 8.2f; baseZ = 3.3f; break; //passt
case 18: baseX = 8.2f; baseZ = 4.9f; break; //passt
case 19: baseX = 8.2f; baseZ = 6.5f; break; //passt
case 20: baseX = 8.2f; baseZ = 7.7f; break;
case 21: baseX = 6.5f; baseZ = 7.7f; break;
case 22: baseX = 4.9f; baseZ = 7.7f; break;
case 23: baseX = 3.3f; baseZ = 7.7f; break;
case 24: baseX = 1.6f; baseZ = 7.7f; break;
case 25: baseX = 0.0f; baseZ = 7.7f; break;
case 26: baseX = -1.6f; baseZ = 7.7f; break;
case 27: baseX = -3.3f; baseZ = 7.7f; break;
case 28: baseX = -4.9f; baseZ = 7.7f; break;
case 29: baseX = -6.5f; baseZ = 7.7f; break;
case 30: baseX = -7.2f; baseZ = 7.7f; break;
case 31: baseX = -7.2f; baseZ = 6.5f; break;
case 32: baseX = -7.2f; baseZ = 4.9f; break;
case 33: baseX = -7.2f; baseZ = 3.3f; break;
case 34: baseX = -7.2f; baseZ = 1.6f; break;
case 35: baseX = -7.2f; baseZ = 0.0f; break;
case 36: baseX = -7.2f; baseZ = -1.6f; break;
case 37: baseX = -7.2f; baseZ = -3.3f; break;
case 38: baseX = -7.2f; baseZ = -4.9f; break;
case 39: baseX = -7.2f; baseZ = -6.5f; break;
default: throw new IllegalArgumentException("Ungültige Feld-ID: " + fieldID);
}
return new Vector3f(baseX, 0, baseZ);
}
@Override
public Rotation getRot() {
//TODO
return Rotation.NORTH;
}
public Quaternion getAlignment() {
Quaternion rotation = new Quaternion();
if (fieldID >= 1 && fieldID <= 10) {
rotation.fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_Y);
} else if (fieldID >= 21 && fieldID <= 30) {
rotation.fromAngleAxis(3 * FastMath.HALF_PI, Vector3f.UNIT_Y);
} else if (fieldID >= 31 && fieldID <= 39) {
rotation.fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y);
}
return rotation;
}
}

View File

@@ -7,6 +7,8 @@
package pp.monopoly.model;
import com.jme3.math.Vector3f;
/**
* An interface representing any item on a board
* It extends the IntPosition interface to provide position information.
@@ -28,4 +30,19 @@ public interface Item {
* @param visitor the visitor performing operations on the item
*/
void accept(VoidVisitor visitor);
/**
* Returns the rotation of the item on the board.
*
* @return the rotation of the item on the board
*/
Rotation getRot();
/**
* Returns the position of the item on the board.
*
* @return the position of the item on the board
*/
Vector3f getPos();
}

View File

@@ -1,67 +1,108 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.monopoly.model;
/**
* Represents the rotation of a Item and provides functionality related to rotation.
*/
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import java.io.Serializable;
import static pp.util.FloatMath.PI;
import static pp.util.FloatMath.TWO_PI;
/**
* Represents the rotation of an item in cardinal directions and provides corresponding 3D rotation quaternions.
*/
public enum Rotation implements Serializable {
/**
* Represents the item facing upwards.
* Represents the item facing north (positive Z-axis).
*/
UP,
NORTH(0, new Quaternion().fromAngleAxis(0, Vector3f.UNIT_Y)),
/**
* Represents the item facing rightwards.
* Represents the item facing south (negative Z-axis).
*/
RIGHT,
SOUTH(PI, new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Y)),
/**
* Represents the item facing downwards.
* Represents the item facing west (negative X-axis).
*/
DOWN,
WEST(3 * PI / 2, new Quaternion().fromAngleAxis(3 * PI / 2, Vector3f.UNIT_Y)),
/**
* Represents the item facing leftwards.
* Represents the item facing east (positive X-axis).
*/
LEFT;
EAST(PI / 2, new Quaternion().fromAngleAxis(PI / 2, Vector3f.UNIT_Y));
/**
* Gets the change in x-coordinate corresponding to this rotation.
*
* @return the change in x-coordinate
*/
public int dx() {
return switch (this) {
case UP, DOWN -> 0;
case RIGHT -> 1;
case LEFT -> -1;
};
private final float radians;
private final Quaternion rotation;
Rotation(float radians, Quaternion rotation) {
this.radians = radians;
this.rotation = rotation;
}
/**
* Gets the change in y-coordinate corresponding to this rotation.
* Gets the 3D rotation quaternion corresponding to this rotation.
*
* @return the change in y-coordinate
* @return the rotation as a {@link Quaternion}.
*/
public int dy() {
return switch (this) {
case UP -> 1;
case LEFT, RIGHT -> 0;
case DOWN -> -1;
};
public Quaternion toQuaternion() {
return rotation;
}
/**
* Rotates the orientation clockwise and returns the next rotation.
* Gets the rotation in radians.
*
* @return the next rotation after rotating clockwise
* @return the rotation in radians.
*/
public Rotation rotate() {
public float radians() {
return radians;
}
/**
* Rotates clockwise (90 degrees or PI/2 radians) and returns the next rotation.
*
* @return the next rotation after rotating clockwise.
*/
public Rotation rotateClockwise() {
return values()[(ordinal() + 1) % values().length];
}
/**
* Rotates counterclockwise (270 degrees or 3 * PI / 2 radians) and returns the next rotation.
*
* @return the next rotation after rotating counterclockwise.
*/
public Rotation rotateCounterclockwise() {
return values()[(ordinal() - 1 + values().length) % values().length];
}
/**
* Rotates by the specified radians and returns the corresponding rotation.
* Valid radians are multiples of PI/2.
*
* @param radians the radians to rotate. Must be a multiple of PI/2.
* @return the resulting rotation.
* @throws IllegalArgumentException if the radians are not a multiple of PI/2.
*/
public Rotation rotateByRadians(float radians) {
if ((radians % (PI / 2)) != 0) {
throw new IllegalArgumentException("Radians must be a multiple of PI/2.");
}
int steps = (int) ((radians / (PI / 2)) % values().length);
if (steps < 0) {
steps += values().length; // Normalize negative steps.
}
return values()[(ordinal() + steps) % values().length];
}
/**
* Finds the rotation closest to the specified radians value.
*
* @param radians the radians value.
* @return the nearest rotation.
*/
public static Rotation closestToRadians(float radians) {
float normalized = (radians % TWO_PI + TWO_PI) % TWO_PI; // Normalize to 0-TWO_PI.
if (normalized < PI / 4 || normalized >= 7 * PI / 4) return NORTH;
if (normalized < 3 * PI / 4) return EAST;
if (normalized < 5 * PI / 4) return SOUTH;
return WEST;
}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.monopoly.model;
/**
@@ -22,4 +15,20 @@ public interface Visitor<T> {
*/
T visit(Figure figure);
/**
* Visits a Hotel element.
*
* @param hotel the Hotel element to visit
* @return the result of visiting the hotel element
*/
T visit(Hotel hotel);
/**
* Visits a house element.
*
* @param house the House element to visit
* @return the result of visiting the house element
*/
T visit(House figure);
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.monopoly.model;
/**
@@ -19,4 +12,18 @@ public interface VoidVisitor {
*/
void visit(Figure figure);
/**
* Visits a Hotel element.
*
* @param hotel the Hotel element to visit
*/
void visit(Hotel hotel);
/**
* Visits a House element.
*
* @param house the House element to visit
*/
void visit(House house);
}

View File

@@ -6,36 +6,10 @@ import pp.monopoly.model.Item;
/**
* Event that is triggered when an item is added to a board.
*/
public class ItemAddedEvent {
private final Item item;
private final Board board;
public record ItemAddedEvent(Board board, Item item) implements GameEvent {
/**
* Constructs a new ItemAddedEvent.
*
* @param item the item that was added
* @param board the board to which the item was added
*/
public ItemAddedEvent(Item item, Board board) {
this.item = item;
this.board = board;
}
/**
* Gets the item that was added.
*
* @return the added item
*/
public Item getItem() {
return item;
}
/**
* Gets the board to which the item was added.
*
* @return the board
*/
public Board getBoard() {
return board;
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}

View File

@@ -1,35 +1,15 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.monopoly.notification;
import pp.monopoly.model.Board;
import pp.monopoly.model.Item;
/**
* Event when an item gets removed.
*
* Event that is triggered when an item is removed from a board.
*/
public class ItemRemovedEvent {
private final Item item;
private final Board board;
public record ItemRemovedEvent(Board board, Item item) implements GameEvent {
public ItemRemovedEvent(Item item, Board board) {
this.item = item;
this.board = board;
}
public Item getItem() {
return item;
}
public Board getBoard() {
return board;
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}