mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2025-07-31 06:37:39 +02:00
Reworked gui and client states
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
package pp.monopoly.client;
|
||||
|
||||
import com.jme3.app.Application;
|
||||
import com.jme3.app.state.AppStateManager;
|
||||
import com.jme3.asset.AssetManager;
|
||||
import com.jme3.light.AmbientLight;
|
||||
import com.jme3.light.DirectionalLight;
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
import com.jme3.math.FastMath;
|
||||
import com.jme3.math.Quaternion;
|
||||
import com.jme3.math.Vector2f;
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.renderer.Camera;
|
||||
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
|
||||
import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
import com.jme3.scene.shape.Box;
|
||||
import com.jme3.shadow.DirectionalLightShadowRenderer;
|
||||
import com.jme3.shadow.EdgeFilteringMode;
|
||||
import com.jme3.texture.Texture;
|
||||
import com.jme3.util.SkyFactory;
|
||||
import com.jme3.util.TangentBinormalGenerator;
|
||||
import pp.monopoly.model.Board;
|
||||
import pp.monopoly.client.gui.BobTheBuilder;
|
||||
import pp.monopoly.client.gui.Toolbar;
|
||||
|
||||
import static pp.util.FloatMath.PI;
|
||||
import static pp.util.FloatMath.TWO_PI;
|
||||
import static pp.util.FloatMath.cos;
|
||||
import static pp.util.FloatMath.sin;
|
||||
import static pp.util.FloatMath.sqrt;
|
||||
|
||||
/**
|
||||
* Manages the rendering and visual aspects of the sea and sky in the Battleship game.
|
||||
* This state is responsible for setting up and updating the sea, sky, and lighting
|
||||
* conditions, and controls the camera to create a dynamic view of the game environment.
|
||||
*/
|
||||
public class BoardAppState extends MonopolyAppState {
|
||||
/**
|
||||
* The path to the unshaded texture material.
|
||||
*/
|
||||
private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS
|
||||
|
||||
/**
|
||||
* The path to the sea texture material.
|
||||
*/
|
||||
private static final String BoardTexture = "Pictures/board2.png"; //NON-NLS
|
||||
|
||||
/**
|
||||
* The root node for all visual elements in this state.
|
||||
*/
|
||||
private final Node viewNode = new Node("view"); //NON-NLS
|
||||
|
||||
/**
|
||||
* The node containing the scene elements, such as the sea surface.
|
||||
*/
|
||||
private final Node sceneNode = new Node("scene"); //NON-NLS
|
||||
|
||||
/**
|
||||
* Synchronizes the buildings's visual representation with the game logic.
|
||||
*/
|
||||
private BobTheBuilder bobTheBuilder;
|
||||
|
||||
/**
|
||||
* The pop-up manager for displaying messages and notifications.
|
||||
*/
|
||||
private PopUpManager popUpManager;;
|
||||
|
||||
/**
|
||||
* Initializes the state by setting up the sky, lights, and other visual components.
|
||||
* This method is called when the state is first attached to the state manager.
|
||||
*
|
||||
* @param stateManager the state manager
|
||||
* @param application the application
|
||||
*/
|
||||
@Override
|
||||
public void initialize(AppStateManager stateManager, Application application) {
|
||||
super.initialize(stateManager, application);
|
||||
popUpManager = new PopUpManager(getApp());
|
||||
viewNode.attachChild(sceneNode);
|
||||
setupLights();
|
||||
setupSky();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the sea and sky state, setting up the scene and registering any necessary listeners.
|
||||
* This method is called when the state is set to active.
|
||||
*/
|
||||
@Override
|
||||
protected void enableState() {
|
||||
getApp().getRootNode().detachAllChildren();
|
||||
getApp().getGuiNode().detachAllChildren();
|
||||
|
||||
new Toolbar(getApp()).open();
|
||||
sceneNode.detachAllChildren();
|
||||
setupScene();
|
||||
if (bobTheBuilder == null) {
|
||||
bobTheBuilder = new BobTheBuilder(getApp(), getApp().getRootNode());
|
||||
System.out.println("LISTENER IS REGISTEDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
|
||||
getGameLogic().addListener(bobTheBuilder);
|
||||
}
|
||||
getApp().getRootNode().attachChild(viewNode);
|
||||
}
|
||||
//TODO remove this only for camera testing
|
||||
private static final float ABOVE_SEA_LEVEL = 10f;
|
||||
private static final float INCLINATION = 2.5f;
|
||||
private float cameraAngle;
|
||||
|
||||
/**
|
||||
* Adjusts the camera position and orientation to create a circular motion around
|
||||
* the center of the map. This provides a dynamic view of the sea and surrounding environment.
|
||||
*/
|
||||
private void adjustCamera() {
|
||||
final Board board = getGameLogic().getBoard();
|
||||
final float mx = 0.5f * board.getWidth();
|
||||
final float my = 0.5f * board.getHeight();
|
||||
final float radius = 2f * sqrt(mx * mx + my + my);
|
||||
final float cos = radius * cos(cameraAngle);
|
||||
final float sin = radius * sin(cameraAngle);
|
||||
final float x = mx - cos;
|
||||
final float y = my - sin;
|
||||
final Camera camera = getApp().getCamera();
|
||||
camera.setLocation(new Vector3f(x, ABOVE_SEA_LEVEL, y));
|
||||
camera.lookAt(new Vector3f(0,0, 0),
|
||||
Vector3f.UNIT_Y);
|
||||
camera.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the sea and sky state, removing visual elements from the scene and unregistering listeners.
|
||||
* This method is called when the state is set to inactive.
|
||||
*/
|
||||
@Override
|
||||
protected void disableState() {
|
||||
getApp().getRootNode().detachChild(viewNode);
|
||||
if (bobTheBuilder != null) {
|
||||
getGameLogic().removeListener(bobTheBuilder);
|
||||
bobTheBuilder = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the state each frame, moving the camera to simulate it circling around the map.
|
||||
*
|
||||
* @param tpf the time per frame (seconds)
|
||||
*/
|
||||
@Override
|
||||
public void update(float tpf) {
|
||||
super.update(tpf);
|
||||
//TODO remove this only for camera testing
|
||||
cameraAngle += TWO_PI * 0.05f * tpf;
|
||||
adjustCamera();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the lighting for the scene, including directional and ambient lights.
|
||||
* Also configures shadows to enhance the visual depth of the scene.
|
||||
*/
|
||||
private void setupLights() {
|
||||
final AssetManager assetManager = getApp().getAssetManager();
|
||||
final DirectionalLightShadowRenderer shRend = new DirectionalLightShadowRenderer(assetManager, 2048, 3);
|
||||
shRend.setLambda(0.55f);
|
||||
shRend.setShadowIntensity(0.6f);
|
||||
shRend.setEdgeFilteringMode(EdgeFilteringMode.Bilinear);
|
||||
getApp().getViewPort().addProcessor(shRend);
|
||||
|
||||
final DirectionalLight sun = new DirectionalLight();
|
||||
sun.setDirection(new Vector3f(-1f, -0.7f, -1f).normalizeLocal());
|
||||
viewNode.addLight(sun);
|
||||
shRend.setLight(sun);
|
||||
|
||||
final AmbientLight ambientLight = new AmbientLight(new ColorRGBA(1f, 1f, 1f, 1f));
|
||||
viewNode.addLight(ambientLight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the sky in the scene using a skybox with textures for all six directions.
|
||||
* This creates a realistic and immersive environment for the sea.
|
||||
*/
|
||||
private void setupSky() {
|
||||
final AssetManager assetManager = getApp().getAssetManager();
|
||||
final Texture west = assetManager.loadTexture("Pictures/Backdrop/left.jpg"); //NON-NLS
|
||||
final Texture east = assetManager.loadTexture("Pictures/Backdrop/right.jpg"); //NON-NLS
|
||||
final Texture north = assetManager.loadTexture("Pictures/Backdrop/front.jpg"); //NON-NLS
|
||||
final Texture south = assetManager.loadTexture("Pictures/Backdrop/back.jpg"); //NON-NLS
|
||||
final Texture up = assetManager.loadTexture("Pictures/Backdrop/up.jpg"); //NON-NLS
|
||||
final Texture down = assetManager.loadTexture("Pictures/Backdrop/down.jpg"); //NON-NLS
|
||||
final Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up, down);
|
||||
// sky.rotate(0, PI, 0);
|
||||
viewNode.attachChild(sky);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the sea surface in the scene. This includes creating the sea mesh,
|
||||
* applying textures, and enabling shadows.
|
||||
*/
|
||||
private void setupScene() {
|
||||
final Board board = getGameLogic().getBoard();
|
||||
final float x = board.getWidth();
|
||||
final float y = board.getHeight();
|
||||
final Box seaMesh = new Box(y, 0.1f, x);
|
||||
final Geometry seaGeo = new Geometry("sea", seaMesh); //NONs-NLS
|
||||
seaGeo.setLocalTranslation(new Vector3f(0, -0.1f, 0));
|
||||
Quaternion rotation = new com.jme3.math.Quaternion();
|
||||
rotation.fromAngleAxis(FastMath.HALF_PI, com.jme3.math.Vector3f.UNIT_Y);
|
||||
seaGeo.setLocalRotation(rotation);
|
||||
final Material seaMat = new Material(getApp().getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
|
||||
Texture texture = getApp().getAssetManager().loadTexture("Pictures/board2.png");
|
||||
seaMat.setTexture("DiffuseMap", texture);
|
||||
seaGeo.setMaterial(seaMat);
|
||||
seaGeo.setShadowMode(ShadowMode.CastAndReceive);
|
||||
TangentBinormalGenerator.generate(seaGeo);
|
||||
sceneNode.attachChild(seaGeo);
|
||||
}
|
||||
}
|
@@ -285,7 +285,7 @@ public class MonopolyApp extends SimpleApplication implements MonopolyClient, Ga
|
||||
|
||||
attachGameSound();
|
||||
attachGameMusic();
|
||||
stateManager.attach(new GameAppState());
|
||||
stateManager.attach(new BoardAppState());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -405,7 +405,7 @@ public class MonopolyApp extends SimpleApplication implements MonopolyClient, Ga
|
||||
*/
|
||||
@Override
|
||||
public void receivedEvent(ClientStateEvent event) {
|
||||
stateManager.getState(GameAppState.class).setEnabled(true);
|
||||
stateManager.getState(BoardAppState.class).setEnabled(logic.isTurn());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,97 @@
|
||||
package pp.monopoly.client;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import pp.monopoly.client.gui.popups.AcceptTrade;
|
||||
import pp.monopoly.client.gui.popups.BuildingPropertyCard;
|
||||
import pp.monopoly.client.gui.popups.ConfirmTrade;
|
||||
import pp.monopoly.client.gui.popups.EventCardPopup;
|
||||
import pp.monopoly.client.gui.popups.FoodFieldCard;
|
||||
import pp.monopoly.client.gui.popups.GateFieldCard;
|
||||
import pp.monopoly.client.gui.popups.Gulag;
|
||||
import pp.monopoly.client.gui.popups.GulagInfo;
|
||||
import pp.monopoly.client.gui.popups.LooserPopUp;
|
||||
import pp.monopoly.client.gui.popups.NoMoneyWarning;
|
||||
import pp.monopoly.client.gui.popups.ReceivedRent;
|
||||
import pp.monopoly.client.gui.popups.RejectTrade;
|
||||
import pp.monopoly.client.gui.popups.Rent;
|
||||
import pp.monopoly.client.gui.popups.TimeOut;
|
||||
import pp.monopoly.client.gui.popups.WinnerPopUp;
|
||||
import pp.monopoly.message.server.NotificationMessage;
|
||||
import pp.monopoly.message.server.TradeReply;
|
||||
import pp.monopoly.model.fields.BuildingProperty;
|
||||
import pp.monopoly.model.fields.FoodField;
|
||||
import pp.monopoly.model.fields.GateField;
|
||||
import pp.monopoly.notification.EventCardEvent;
|
||||
import pp.monopoly.notification.GameEventListener;
|
||||
import pp.monopoly.notification.PopUpEvent;
|
||||
|
||||
public class PopUpManager implements GameEventListener {
|
||||
|
||||
private final MonopolyApp app;
|
||||
|
||||
public PopUpManager(MonopolyApp app) {
|
||||
this.app = app;
|
||||
app.getGameLogic().addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedEvent(PopUpEvent event) {
|
||||
if (event.msg().equals("Buy")) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
app.enqueue(() -> {
|
||||
int field = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()).getFieldID();
|
||||
Object fieldObject = app.getGameLogic().getBoardManager().getFieldAtIndex(field);
|
||||
|
||||
if (fieldObject instanceof BuildingProperty) {
|
||||
new BuildingPropertyCard(app).open();
|
||||
} else if (fieldObject instanceof GateField) {
|
||||
new GateFieldCard(app).open();
|
||||
} else if (fieldObject instanceof FoodField) {
|
||||
new FoodFieldCard(app).open();
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 2500);
|
||||
} else if (event.msg().equals("Winner")) {
|
||||
new WinnerPopUp(app).open();
|
||||
} else if (event.msg().equals("Looser")) {
|
||||
new LooserPopUp(app).open();
|
||||
} else if (event.msg().equals("timeout")) {
|
||||
new TimeOut(app).open();
|
||||
} else if (event.msg().equals("tradeRequest")) {
|
||||
new ConfirmTrade(app).open();
|
||||
} else if (event.msg().equals("goingToJail")) {
|
||||
new Gulag(app).open();
|
||||
} else if (event.msg().equals("NoMoneyWarning")) {
|
||||
new NoMoneyWarning(app).open();
|
||||
} else if(event.msg().equals("rent")) {
|
||||
new Rent(app, ( (NotificationMessage) event.message()).getRentOwner(), ( (NotificationMessage) event.message()).getRentAmount() ).open();
|
||||
} else if (event.msg().equals("jailtryagain")) {
|
||||
new GulagInfo(app, 1).open();
|
||||
} else if (event.msg().equals("jailpay")) {
|
||||
new GulagInfo(app, 3).open();
|
||||
} else if (event.msg().equals("tradepos")) {
|
||||
new AcceptTrade(app, (TradeReply) event.message()).open();
|
||||
} else if (event.msg().equals("tradeneg")) {
|
||||
new RejectTrade(app, (TradeReply) event.message()).open();
|
||||
} else if (event.msg().equals("ReceivedRent")) {
|
||||
new ReceivedRent(app, ( (NotificationMessage) event.message()).getRentOwner(), ( (NotificationMessage) event.message()).getRentAmount() ).open();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedEvent(EventCardEvent event) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
app.enqueue(() -> new EventCardPopup(app, event.description()).open());
|
||||
}
|
||||
}, 2500);
|
||||
}
|
||||
}
|
@@ -1,74 +0,0 @@
|
||||
package pp.monopoly.client.gui;
|
||||
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
|
||||
import pp.monopoly.model.Board;
|
||||
import pp.monopoly.model.Item;
|
||||
import pp.monopoly.model.Visitor;
|
||||
import pp.monopoly.notification.GameEventListener;
|
||||
import pp.monopoly.notification.ItemAddedEvent;
|
||||
import pp.monopoly.notification.ItemRemovedEvent;
|
||||
import pp.view.ModelViewSynchronizer;
|
||||
|
||||
/**
|
||||
* Abstract base class for synchronizing the visual representation of a {@link Board} with its model state.
|
||||
* This class handles the addition and removal of items from the map, ensuring that changes in the model
|
||||
* are accurately reflected in the view.
|
||||
*/
|
||||
abstract class BoardSynchronizer extends ModelViewSynchronizer<Item> implements Visitor<Spatial>, GameEventListener {
|
||||
protected final Board board;
|
||||
|
||||
/**
|
||||
* Constructs a new BoardSynchronizer.
|
||||
*
|
||||
* @param board the game board to synchronize
|
||||
* @param root the root node to which the view representations of the board items are attached
|
||||
*/
|
||||
protected BoardSynchronizer(Board board, Node root) {
|
||||
super(root);
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a model item into its corresponding visual representation.
|
||||
*
|
||||
* @param item the item from the model to be translated
|
||||
* @return the visual representation of the item as a {@link Spatial}
|
||||
*/
|
||||
@Override
|
||||
protected Spatial translate(Item item) {
|
||||
return item.accept(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the existing items from the board to the view during initialization.
|
||||
*/
|
||||
protected void addExisting() {
|
||||
board.getItems().forEach(this::add);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the event when an item is removed from the board.
|
||||
*
|
||||
* @param event the event indicating that an item has been removed from the board
|
||||
*/
|
||||
@Override
|
||||
public void receivedEvent(ItemRemovedEvent event) {
|
||||
if (board == event.getBoard()) {
|
||||
delete(event.getItem());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the event when an item is added to the board.
|
||||
*
|
||||
* @param event the event indicating that an item has been added to the board
|
||||
*/
|
||||
@Override
|
||||
public void receivedEvent(ItemAddedEvent event) {
|
||||
if (board == event.getBoard()) {
|
||||
add(event.getItem());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,159 @@
|
||||
package pp.monopoly.client.gui;
|
||||
|
||||
import static com.jme3.material.Materials.LIGHTING;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.material.RenderState.BlendMode;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
|
||||
import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
import com.jme3.scene.shape.Box;
|
||||
|
||||
import pp.monopoly.client.MonopolyApp;
|
||||
import pp.monopoly.game.server.Player;
|
||||
import pp.monopoly.model.Figure;
|
||||
import pp.monopoly.model.Hotel;
|
||||
import pp.monopoly.model.House;
|
||||
import pp.monopoly.model.Item;
|
||||
import pp.monopoly.notification.UpdatePlayerView;
|
||||
|
||||
public class BobTheBuilder extends GameBoardSynchronizer {
|
||||
|
||||
private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS
|
||||
private static final String COLOR = "Color"; //NON-NLS
|
||||
private static final String FIGURE = "figure"; //NON-NLS
|
||||
private static final String HOUSE = "house"; //NON-NLS
|
||||
private static final String HOTEL = "hotel"; //NON-NLS
|
||||
|
||||
private final MonopolyApp app;
|
||||
|
||||
public BobTheBuilder(MonopolyApp app, Node root) {
|
||||
super(app.getGameLogic().getBoard(), root);
|
||||
this.app = app;
|
||||
addExisting();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Spatial visit(Figure figure) {
|
||||
final Node node = new Node(FIGURE);
|
||||
node.attachChild(createFigure(figure));
|
||||
|
||||
// Setze die Position basierend auf der Feld-ID
|
||||
node.setLocalTranslation(figure.getPos());
|
||||
|
||||
// Setze die Rotation basierend auf der Feld-ID
|
||||
node.setLocalRotation(figure.getRot().toQuaternion());
|
||||
// node.addControl(new FigureControl(figure));
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Spatial visit(Hotel hotel) {
|
||||
final Node node = new Node(HOTEL);
|
||||
node.attachChild(createHotel(hotel));
|
||||
|
||||
// Setze die Position basierend auf der Feld-ID
|
||||
node.setLocalTranslation(hotel.getPos());
|
||||
|
||||
// Setze die Rotation basierend auf der Feld-ID
|
||||
node.setLocalRotation(hotel.getRot().toQuaternion());
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Spatial visit(House house) {
|
||||
final Node node = new Node(HOUSE);
|
||||
node.attachChild(createHouse(house));
|
||||
|
||||
// Setze die Position basierend auf der Feld-ID
|
||||
node.setLocalTranslation(house.getPos());
|
||||
|
||||
// Setze die Rotation basierend auf der Feld-ID
|
||||
node.setLocalRotation(house.getAlignment());
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private Spatial createFigure(Figure figure) {
|
||||
// Lade das Modell
|
||||
Spatial model = app.getAssetManager().loadModel("models/" + "Spielfiguren/" + figure.getType() + "/" + figure.getType() + ".j3o");
|
||||
|
||||
// Skaliere und positioniere das Modell
|
||||
model.scale(0.5f);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
private Spatial createHotel(Hotel hotel) {
|
||||
Spatial model = app.getAssetManager().loadModel("models/Hotel/Hotel.j3o");
|
||||
model.scale(0.2f);
|
||||
model.setShadowMode(ShadowMode.CastAndReceive);
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
private Spatial createHouse(House house) {
|
||||
Spatial model = app.getAssetManager().loadModel("models/Haus/"+house.getStage()+"Haus.j3o");
|
||||
model.scale(0.5f);
|
||||
model.setShadowMode(ShadowMode.CastAndReceive);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a simple box to represent a battleship that is not of the "King George V" type.
|
||||
*
|
||||
* @param ship the battleship to be represented
|
||||
* @return the geometry representing the battleship as a box
|
||||
*/
|
||||
private Spatial createBox(Item item) {
|
||||
final Box box = new Box(3,
|
||||
3f,
|
||||
3);
|
||||
final Geometry geometry = new Geometry(FIGURE, box);
|
||||
geometry.setMaterial(createColoredMaterial(ColorRGBA.Blue));
|
||||
geometry.setShadowMode(ShadowMode.CastAndReceive);
|
||||
geometry.setLocalTranslation(0, 2, 0);
|
||||
|
||||
return geometry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Material} with the specified color.
|
||||
* If the color includes transparency (i.e., alpha value less than 1),
|
||||
* the material's render state is set to use alpha blending, allowing for
|
||||
* semi-transparent rendering.
|
||||
*
|
||||
* @param color the {@link ColorRGBA} to be applied to the material. If the alpha value
|
||||
* of the color is less than 1, the material will support transparency.
|
||||
* @return a {@link Material} instance configured with the specified color and,
|
||||
* if necessary, alpha blending enabled.
|
||||
*/
|
||||
private Material createColoredMaterial(ColorRGBA color) {
|
||||
final Material material = new Material(app.getAssetManager(), UNSHADED);
|
||||
if (color.getAlpha() < 1f)
|
||||
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
|
||||
material.setColor(COLOR, color);
|
||||
return material;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedEvent(UpdatePlayerView event) {
|
||||
board.removePlayers();
|
||||
|
||||
//TODO transition animation
|
||||
for (Player player : app.getGameLogic().getPlayerHandler().getPlayers()) {
|
||||
board.add(player.getFigure());
|
||||
}
|
||||
for (Item item : board.getItems().stream().filter(p -> p instanceof Figure).collect(Collectors.toList())) {
|
||||
add(item);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,120 +1,87 @@
|
||||
////////////////////////////////////////
|
||||
// Programming project code
|
||||
// UniBw M, 2022, 2023, 2024
|
||||
// www.unibw.de/inf2
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.monopoly.client.gui;
|
||||
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.material.RenderState.BlendMode;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
|
||||
import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
import com.jme3.scene.shape.Box;
|
||||
|
||||
import pp.monopoly.client.MonopolyApp;
|
||||
import pp.monopoly.game.server.PlayerColor;
|
||||
import pp.monopoly.model.Item;
|
||||
import pp.monopoly.model.Visitor;
|
||||
import pp.monopoly.notification.DiceRollEvent;
|
||||
import pp.monopoly.notification.GameEventListener;
|
||||
import pp.monopoly.notification.ItemAddedEvent;
|
||||
import pp.monopoly.notification.ItemRemovedEvent;
|
||||
import pp.monopoly.notification.UpdatePlayerView;
|
||||
import pp.monopoly.model.Board;
|
||||
import pp.monopoly.model.Figure;
|
||||
import pp.monopoly.model.Rotation;
|
||||
import static pp.util.FloatMath.HALF_PI;
|
||||
import static pp.util.FloatMath.PI;
|
||||
import pp.view.ModelViewSynchronizer;
|
||||
|
||||
/**
|
||||
* The {@code GameBoardSynchronizer} class is responsible for synchronizing the graphical
|
||||
* representation of the Game board and figures with the underlying data model.
|
||||
* It extends the {@link BoardSynchronizer} to provide specific synchronization
|
||||
* logic for the Game board.
|
||||
* Abstract base class for synchronizing the visual representation of a {@link Board} with its model state.
|
||||
* This class handles the addition and removal of items from the board, ensuring that changes in the model
|
||||
* are accurately reflected in the view.
|
||||
* <p>
|
||||
* Subclasses are responsible for providing the specific implementation of how each item in the map
|
||||
* is represented visually by implementing the {@link Visitor} interface.
|
||||
* </p>
|
||||
*/
|
||||
class GameBoardSynchronizer extends BoardSynchronizer {
|
||||
private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS
|
||||
private static final String LIGHTING = "Common/MatDefs/Light/Lighting.j3md";
|
||||
private static final String COLOR = "Color"; //NON-NLS
|
||||
private static final String FIGURE = "figure"; //NON-NLS
|
||||
|
||||
private final MonopolyApp app;
|
||||
abstract class GameBoardSynchronizer extends ModelViewSynchronizer<Item> implements Visitor<Spatial>, GameEventListener {
|
||||
// The board that this synchronizer is responsible for
|
||||
protected final Board board;
|
||||
|
||||
/**
|
||||
* Constructs a {@code GameBoardSynchronizer} object with the specified application, root node, and ship map.
|
||||
* Constructs a new GameBoardSynchronizer.
|
||||
* Initializes the synchronizer with the provided board and the root node for attaching view representations.
|
||||
*
|
||||
* @param app the Monopoly application
|
||||
* @param root the root node to which graphical elements will be attached
|
||||
* @param map the Game Board containing fields and figures
|
||||
* @param map the board to be synchronized
|
||||
* @param root the root node to which the view representations of the board items are attached
|
||||
*/
|
||||
public GameBoardSynchronizer(MonopolyApp app, Node root, Board board) {
|
||||
super(board, root);
|
||||
this.app = app;
|
||||
addExisting();
|
||||
protected GameBoardSynchronizer(Board board, Node root) {
|
||||
super(root);
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits a {@link Figure} and creates a graphical representation of it.
|
||||
* The representation is a 3D model.
|
||||
* Translates a model item into its corresponding visual representation.
|
||||
* The specific visual representation is determined by the concrete implementation of the {@link Visitor} interface.
|
||||
*
|
||||
* @param figure the figure to be represented
|
||||
* @return the node containing the graphical representation of the figure
|
||||
* @param item the item from the model to be translated
|
||||
* @return the visual representation of the item as a {@link Spatial}
|
||||
*/
|
||||
public Spatial visit(Figure figure) {
|
||||
final Node node = new Node(FIGURE);
|
||||
node.attachChild(createBox(figure));
|
||||
final float x = 1;
|
||||
final float z = 1;
|
||||
node.setLocalTranslation(x, 0f, z);
|
||||
return node;
|
||||
@Override
|
||||
protected Spatial translate(Item item) {
|
||||
return item.accept(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a representation of a figure
|
||||
*
|
||||
* @param figure the figure to be represented
|
||||
* @return the geometry representing the figure
|
||||
* Adds the existing items from the board to the view.
|
||||
* This method should be called during initialization to ensure that all current items in the board
|
||||
* are visually represented.
|
||||
*/
|
||||
private Spatial createBox(Figure figure) {
|
||||
final Box box = new Box(0.5f * (figure.getMaxY() - figure.getMinY()) + 0.3f,
|
||||
0.3f,
|
||||
0.5f * (figure.getMaxX() - figure.getMinX()) + 0.3f);
|
||||
final Geometry geometry = new Geometry(FIGURE, box);
|
||||
geometry.setMaterial(createColoredMaterial(PlayerColor.PINK.getColor()));
|
||||
geometry.setShadowMode(ShadowMode.CastAndReceive);
|
||||
protected void addExisting() {
|
||||
board.getItems().forEach(this::add);
|
||||
}
|
||||
|
||||
return geometry;
|
||||
/**
|
||||
* Handles the event when an item is removed from the ship map.
|
||||
* Removes the visual representation of the item from the view if it belongs to the synchronized ship map.
|
||||
*
|
||||
* @param event the event indicating that an item has been removed from the ship map
|
||||
*/
|
||||
@Override
|
||||
public void receivedEvent(ItemRemovedEvent event) {
|
||||
if (board == event.board())
|
||||
delete(event.item());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Material} with the specified color.
|
||||
* If the color includes transparency (i.e., alpha value less than 1),
|
||||
* the material's render state is set to use alpha blending, allowing for
|
||||
* semi-transparent rendering.
|
||||
* Handles the event when an item is added to the ship map.
|
||||
* Adds the visual representation of the new item to the view if it belongs to the synchronized ship map.
|
||||
*
|
||||
* @param color the {@link ColorRGBA} to be applied to the material. If the alpha value
|
||||
* of the color is less than 1, the material will support transparency.
|
||||
* @return a {@link Material} instance configured with the specified color and,
|
||||
* if necessary, alpha blending enabled.
|
||||
* @param event the event indicating that an item has been added to the ship map
|
||||
*/
|
||||
private Material createColoredMaterial(ColorRGBA color) {
|
||||
final Material material = new Material(app.getAssetManager(), UNSHADED);
|
||||
if (color.getAlpha() < 1f)
|
||||
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
|
||||
material.setColor(COLOR, color);
|
||||
return material;
|
||||
@Override
|
||||
public void receivedEvent(ItemAddedEvent event) {
|
||||
if (board == event.board()){
|
||||
add(event.item());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the rotation angle for the specified rotation.
|
||||
*
|
||||
* @param rot the rotation of the battleship
|
||||
* @return the rotation angle in radians
|
||||
*/
|
||||
private static float calculateRotationAngle(Rotation rot) {
|
||||
return switch (rot) {
|
||||
case RIGHT -> HALF_PI;
|
||||
case DOWN -> 0f;
|
||||
case LEFT -> -HALF_PI;
|
||||
case UP -> PI;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user