basic camera movement

This commit is contained in:
Johannes Schmelz 2024-12-10 11:22:21 +01:00
parent e1190cd4a2
commit c6ad605021
5 changed files with 125 additions and 71 deletions

View File

@ -69,9 +69,8 @@ public class BoardAppState extends MonopolyAppState {
*/ */
private PopUpManager popUpManager; private PopUpManager popUpManager;
private Vector3f currentTarget = new Vector3f(-10f,0,-10f);
private CameraController cameraController; private CameraController cameraController;
private CameraInputHandler cameraInputHandler; private CameraInputHandler cameraInputHandler;
@ -87,7 +86,7 @@ public class BoardAppState extends MonopolyAppState {
super.initialize(stateManager, application); super.initialize(stateManager, application);
// Initialisiere den CameraController zuerst // Initialisiere den CameraController zuerst
cameraController = new CameraController(getApp().getCamera(), 25, 40); cameraController = new CameraController(getApp().getCamera(), getApp());
// Danach den CameraInputHandler mit dem initialisierten CameraController // Danach den CameraInputHandler mit dem initialisierten CameraController
cameraInputHandler = new CameraInputHandler(cameraController, getApp().getInputManager()); cameraInputHandler = new CameraInputHandler(cameraController, getApp().getInputManager());
@ -283,10 +282,6 @@ private void addCylinderCaps() {
return cardDeck; return cardDeck;
} }
public Vector3f getCurrentTarget(){
return currentTarget;
}
private void addSnowEffect(Node parentNode) { private void addSnowEffect(Node parentNode) {
// ParticleEmitter für Schnee // ParticleEmitter für Schnee
ParticleEmitter snowEmitter = new ParticleEmitter("Snow", ParticleMesh.Type.Triangle, 5000); ParticleEmitter snowEmitter = new ParticleEmitter("Snow", ParticleMesh.Type.Triangle, 5000);
@ -316,4 +311,10 @@ private void addCylinderCaps() {
parentNode.attachChild(snowEmitter); parentNode.attachChild(snowEmitter);
} }
@Override
public void update(float tpf) {
super.update(tpf);
cameraController.update(tpf);
}
} }

View File

@ -292,7 +292,7 @@ public class MonopolyApp extends SimpleApplication implements MonopolyClient, Ga
final StatsAppState stats = new StatsAppState(guiNode, normalFont); final StatsAppState stats = new StatsAppState(guiNode, normalFont);
stateManager.attach(stats); stateManager.attach(stats);
} }
flyCam.setEnabled(true); flyCam.setEnabled(false);
stateManager.detach(stateManager.getState(StatsAppState.class)); stateManager.detach(stateManager.getState(StatsAppState.class));
stateManager.detach(stateManager.getState(DebugKeysAppState.class)); stateManager.detach(stateManager.getState(DebugKeysAppState.class));

View File

@ -3,7 +3,13 @@ package pp.monopoly.client.gui;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera; import com.jme3.renderer.Camera;
public class CameraController { import pp.monopoly.client.MonopolyApp; // Import MonopolyApp
import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.UpdatePlayerView;
public class CameraController implements GameEventListener{
public enum CameraMode { public enum CameraMode {
FOCUS_CURRENT_PLAYER, FOCUS_CURRENT_PLAYER,
FOCUS_SELF, FOCUS_SELF,
@ -11,49 +17,92 @@ public class CameraController {
} }
private final Camera camera; private final Camera camera;
private CameraMode currentMode = CameraMode.FOCUS_CURRENT_PLAYER; private CameraMode currentMode;
private final float height; // Höhe der Kamera private PlayerHandler playerHandler; // Reference to PlayerHandler for player data
private final float offset; // Versatz zur Spielfeldseite private final MonopolyApp app; // Reference to MonopolyApp for self ID
public CameraController(Camera camera, float height, float offset) { public CameraController(Camera camera, MonopolyApp app) {
this.camera = camera; this.camera = camera;
this.height = height; this.playerHandler = app.getGameLogic().getPlayerHandler();
this.offset = offset; this.app = app;
setMode(currentMode); app.getGameLogic().addListener(this);
setMode(CameraMode.FOCUS_SELF); // Initialize the camera mode
} }
public void setMode(CameraMode mode) { public void setMode(CameraMode mode) {
this.currentMode = mode; this.currentMode = mode;
updatePosition(0); // Standardmäßig das Startfeld fokussieren
} }
public void update(float tpf) { public void update(float tpf) {
if (currentMode != CameraMode.FREECAM) {
camera.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
}
}
public void updatePosition(int fieldID) {
Vector3f newPosition = fieldIdToVector(fieldID);
camera.setLocation(newPosition);
if (currentMode != CameraMode.FREECAM) {
camera.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
}
}
private Vector3f fieldIdToVector(int fieldID) {
switch (currentMode) { switch (currentMode) {
case FOCUS_CURRENT_PLAYER: case FOCUS_CURRENT_PLAYER:
if (fieldID <= 10) return new Vector3f(offset, height, 0); updatePosition();
if (fieldID <= 20) return new Vector3f(0, height, offset); break;
if (fieldID <= 30) return new Vector3f(-offset, height, 0);
return new Vector3f(0, height, -offset);
case FOCUS_SELF: case FOCUS_SELF:
return new Vector3f(0, height, fieldID <= 20 ? offset : -offset); updatePosition();
break;
case FREECAM: case FREECAM:
break;
default: default:
return new Vector3f(0, height, 0); break;
} }
} }
public void updatePosition() {
Vector3f newPosition = getPos();
camera.setLocation(newPosition);
camera.lookAt(app.getGameLogic().getBoard().getFigure(app.getId()).getPos(), Vector3f.UNIT_Y);
camera.update();
}
private Vector3f getPos() {
Vector3f pos = new Vector3f();
switch (currentMode) {
case FOCUS_CURRENT_PLAYER:
pos = app.getGameLogic().getBoard().getFigure(playerHandler.getPlayerById(0).getId()).getPos();
case FOCUS_SELF:
pos = app.getGameLogic().getBoard().getFigure(app.getId()).getPos();
case FREECAM:
break;
default:
break;
}
Vector3f offset = getOffset();
pos = new Vector3f(pos.getX() + offset.getX(), pos.getY() + offset.getY(), pos.getZ() + offset.getZ());
return pos;
}
private Vector3f getOffset() {
Vector3f offset = new Vector3f();
int fieldId = playerHandler.getPlayerById( (currentMode == CameraMode.FOCUS_SELF ? app.getId() : playerHandler.getPlayerAtIndex(0).getId()) ).getFieldID();
// System.out.println();
if(fieldId < 10) {
offset = new Vector3f(0, 15, -20);
} else if(fieldId < 20) {
offset = new Vector3f(20 , 15, 0);
} else if(fieldId < 30) {
offset = new Vector3f(0, 15, 20 );
} else {
offset = new Vector3f(-20, 15, 0);
}
return offset;
}
@Override
public void receivedEvent(UpdatePlayerView event) {
playerHandler = app.getGameLogic().getPlayerHandler();
}
} }

View File

@ -120,7 +120,7 @@ public class PlayerHandler {
* @param index the index of the queue * @param index the index of the queue
* @return the Player at the required index * @return the Player at the required index
*/ */
Player getPlayerAtIndex(int index) { public Player getPlayerAtIndex(int index) {
return players.get(index); return players.get(index);
} }

View File

@ -147,6 +147,10 @@ public class Board {
return getHotels().filter(hotel -> hotel.getFieldID() == fieldId).findFirst().orElse(null); return getHotels().filter(hotel -> hotel.getFieldID() == fieldId).findFirst().orElse(null);
} }
public Figure getFigure(int playerId) {
return getFigures().filter(figure -> figure.getId() == playerId).findFirst().orElse(null);
}
/** /**
* Returns a stream of all hotels currently on the map. * Returns a stream of all hotels currently on the map.
* *