Figuren Bewegung eingefügt

This commit is contained in:
Luca Puderbach 2024-12-01 05:05:11 +01:00
parent 528126aa29
commit 51b879a7c8
2 changed files with 154 additions and 59 deletions

View File

@ -38,7 +38,18 @@ import static pp.monopoly.Resources.lookup;
import pp.monopoly.client.gui.SettingsMenu;
import pp.monopoly.client.gui.StartMenu;
import pp.monopoly.client.gui.TestWorld;
import pp.monopoly.client.gui.popups.*;
import pp.monopoly.client.gui.popups.Bankrupt;
import pp.monopoly.client.gui.popups.BuildingPropertyCard;
import pp.monopoly.client.gui.popups.BuyCard;
import pp.monopoly.client.gui.popups.BuyHouse;
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.LooserPopUp;
import pp.monopoly.client.gui.popups.RepayMortage;
import pp.monopoly.client.gui.popups.SellHouse;
import pp.monopoly.client.gui.popups.TakeMortage;
import pp.monopoly.client.gui.popups.TimeOut;
import pp.monopoly.game.client.ClientGameLogic;
import pp.monopoly.game.client.MonopolyClient;
import pp.monopoly.game.client.ServerConnection;
@ -335,10 +346,10 @@ public class MonopolyApp extends SimpleApplication implements MonopolyClient, Ga
logic.update(tpf);
stateManager.update(tpf);
//TODO testing replace later
if (testWorld != null) {
testWorld.update(tpf);
}
// //TODO testing replace later
// if (testWorld != null) {
// testWorld.update(tpf);
// }
}
/**

View File

@ -1,22 +1,20 @@
package pp.monopoly.client.gui;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.jme3.math.Vector3f;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.client.gui.popups.BuyCard;
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.LooserPopUp;
import pp.monopoly.client.gui.popups.Rent;
import pp.monopoly.client.gui.popups.TimeOut;
import pp.monopoly.client.gui.popups.WinnerPopUp;
import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.message.server.NotificationMessage;
import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.model.fields.FoodField;
import pp.monopoly.model.fields.GateField;
@ -28,10 +26,10 @@ import pp.monopoly.notification.UpdatePlayerView;
/**
* TestWorld zeigt eine einfache Szene mit Spielfeld und Spielfiguren.
*/
public class TestWorld implements GameEventListener{
public class TestWorld implements GameEventListener {
private final MonopolyApp app;
private PlayerHandler playerHandler; // Liste der Spieler, bereits aus GameStart geladen
private PlayerHandler playerHandler;
private CameraController cameraController;
private Toolbar toolbar;
@ -58,10 +56,10 @@ public class TestWorld implements GameEventListener{
System.out.println("Szene initialisiert.");
// Initialisiere Szene
//Füge Inhalte ein
setSkyColor();
createBoard();
createPlayerFigures(); // Lädt Figuren aus der bereits vorhandenen Liste
createPlayerFigures();
toolbar = new Toolbar(app);
toolbar.open();
}
@ -78,21 +76,23 @@ public class TestWorld implements GameEventListener{
*/
private void createBoard() {
try {
// Erstelle das Spielfeld als flaches Rechteck
com.jme3.scene.shape.Box box = new com.jme3.scene.shape.Box(10, 0.1f, 10); // Breite, Höhe, Tiefe
com.jme3.scene.shape.Box box = new com.jme3.scene.shape.Box(10, 0.1f, 10);
com.jme3.scene.Geometry geom = new com.jme3.scene.Geometry("Board", box);
// Lade und setze das Material mit der Textur
com.jme3.material.Material mat = new com.jme3.material.Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
com.jme3.texture.Texture texture = app.getAssetManager().loadTexture("Pictures/board2.png");
mat.setTexture("ColorMap", texture);
geom.setMaterial(mat);
// Positioniere das Spielfeld in der Szene
geom.setLocalTranslation(0, -0.1f, 0); // Direkt auf der Grundebene
geom.setLocalTranslation(0, -0.1f, 0);
com.jme3.math.Quaternion rotation = new com.jme3.math.Quaternion();
rotation.fromAngleAxis(com.jme3.math.FastMath.HALF_PI, com.jme3.math.Vector3f.UNIT_Y);
geom.setLocalRotation(rotation);
app.getRootNode().attachChild(geom);
System.out.println("Spielbrett erfolgreich erstellt und hinzugefügt.");
System.out.println("Spielbrett erfolgreich erstellt, gedreht und hinzugefügt.");
} catch (Exception e) {
System.err.println("Fehler beim Erstellen des Spielfelds: " + e.getMessage());
}
@ -105,70 +105,154 @@ public class TestWorld implements GameEventListener{
for (int i = 0; i < playerHandler.getPlayers().size(); i++) {
Player player = playerHandler.getPlayers().get(i);
try {
// Lade das 3D-Modell der Spielfigur
com.jme3.scene.Spatial model = app.getAssetManager().loadModel("Models/" + player.getFigure().getType() + "/" + player.getFigure().getType() + ".j3o");
model.setLocalScale(0.5f); // Skaliere das Modell
// Positioniere die Figur unten rechts
float startX = 9.1f; // X-Koordinate für die Ecke unten rechts
float startZ = -9.1f; // Z-Koordinate für die Ecke unten rechts
float spacing = 0.4f; // Abstand zwischen den Figuren
model.setLocalTranslation(startX, 0, startZ - (i * spacing)); // Position anpassen
com.jme3.scene.Spatial model = app.getAssetManager().loadModel(
"Models/" + player.getFigure().getType() + "/" + player.getFigure().getType() + ".j3o"
);
model.setLocalScale(0.5f);
Vector3f startPosition = calculateFieldPosition(player.getFieldID(), i);
model.setLocalTranslation(startPosition);
model.setName("PlayerFigure_" + player.getId());
app.getRootNode().attachChild(model);
System.out.println("Figur für Spieler " + player.getId() + " positioniert bei (" + startX + ", 0, " + (startZ - (i * spacing)) + ")");
System.out.println("Figur für Spieler " + player.getId() + " erstellt bei " + startPosition);
} catch (Exception e) {
System.err.println("Fehler beim Laden des Modells für Spieler " + player.getId() + ": " + e.getMessage());
}
}
}
/**
* Wird bei jedem Frame aufgerufen, um die Szene zu aktualisieren.
*
* @param tpf Zeit seit dem letzten Frame in Sekunden
*/
public void update(float tpf) {
if (cameraController != null) {
cameraController.update(tpf); // Aktualisiere die Kameraposition
private Vector3f calculateFieldPosition(int fieldID, int playerIndex) {
float offset = 0.1f;
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);
}
float xOffset = (playerIndex % 2) * offset;
float zOffset = (playerIndex / 2) * offset;
return new Vector3f(baseX + xOffset, 0, baseZ + zOffset);
}
private void movePlayerFigure(Player player) {
int fieldID = player.getFieldID();
int playerIndex = playerHandler.getPlayers().indexOf(player); // Hole den Index des Spielers
Vector3f targetPosition = calculateFieldPosition(fieldID, playerIndex);
String figureName = "PlayerFigure_" + player.getId();
com.jme3.scene.Spatial figure = app.getRootNode().getChild(figureName);
if (figure != null) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
app.enqueue(() -> {
figure.setLocalTranslation(targetPosition);
System.out.println("Spieler " + player.getId() + " bewegt zu Feld " + fieldID + " (" + targetPosition + ")");
});
}
}, 3000); // 3 Sekunden Verzögerung für die Bewegung
} else {
System.err.println("Figur für Spieler " + player.getId() + " nicht gefunden.");
}
}
@Override
public void receivedEvent(PopUpEvent event) {
if(event.msg().equals("Buy")) {
int field = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()).getFieldID();
Object fieldObject = app.getGameLogic().getBoardManager().getFieldAtIndex(field);
if (fieldObject instanceof BuildingProperty) {
new BuyCard(app).open();
} else if (fieldObject instanceof GateField){
new GateFieldCard(app).open();
} else if (fieldObject instanceof FoodField) {
new FoodFieldCard(app).open();
}
} else if(event.msg().equals("Winner")) {
if (event.msg().equals("Buy")) {
// Erstelle einen Timer, um den Delay umzusetzen
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// GUI-Operationen im jMonkey-Thread ausführen
app.enqueue(() -> {
int field = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()).getFieldID();
Object fieldObject = app.getGameLogic().getBoardManager().getFieldAtIndex(field);
if (fieldObject instanceof BuildingProperty) {
new BuyCard(app).open();
} else if (fieldObject instanceof GateField) {
new GateFieldCard(app).open();
} else if (fieldObject instanceof FoodField) {
new FoodFieldCard(app).open();
}
});
}
}, 5000); // Verzögerung in Millisekunden
} else if (event.msg().equals("Winner")) {
new WinnerPopUp(app).open();
} else if(event.msg().equals("Looser")) {
} else if (event.msg().equals("Looser")) {
new LooserPopUp(app).open();
} else if(event.msg().equals("timeout")) {
} 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("rent")) {
new Rent(app, ( (NotificationMessage) event.message()).getRentOwner(), ( (NotificationMessage) event.message()).getRentAmount() ).open();
}
}
@Override
public void receivedEvent(EventCardEvent event) {
new EventCardPopup(app, event.description()).open();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
app.enqueue(() -> new EventCardPopup(app, event.description()).open());
}
}, 5000); // 5 Sekunden Verzögerung für Event-Popups
}
@Override
public void receivedEvent(UpdatePlayerView event) {
this.playerHandler = app.getGameLogic().getPlayerHandler();
cameraController.setPosition(playerHandler.getPlayerById(app.getId()).getFieldID());
cameraController.update(0);
// Aktualisiere die Position aller Spielerfiguren
for (Player player : playerHandler.getPlayers()) {
movePlayerFigure(player);
}
}
}