From 91fea83f26e19613418496289f41a09a75d7a58c Mon Sep 17 00:00:00 2001 From: Luca Puderbach Date: Mon, 2 Dec 2024 04:19:56 +0100 Subject: [PATCH] Modelle Fixes --- .../pp/monopoly/client/gui/TestWorld.java | 154 ++++++++++++++++-- .../monopoly/model/fields/BoardManager.java | 13 +- 2 files changed, 151 insertions(+), 16 deletions(-) diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java index 2dffab0..fce9ae6 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java @@ -5,11 +5,16 @@ import java.util.List; import java.util.Timer; import java.util.TimerTask; +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.Vector3f; import com.jme3.renderer.RenderManager; import com.jme3.renderer.ViewPort; import com.jme3.scene.control.AbstractControl; +import com.jme3.texture.Texture; import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.gui.popups.BuildingPropertyCard; @@ -43,6 +48,7 @@ public class TestWorld implements GameEventListener { private PlayerHandler playerHandler; private CameraController cameraController; private Toolbar toolbar; + private List existingHouses = new ArrayList<>(); /** * Konstruktor für die TestWorld. @@ -70,6 +76,7 @@ public class TestWorld implements GameEventListener { //Füge Inhalte ein setSkyColor(); createBoard(); + addLighting(); createPlayerFigures(); toolbar = new Toolbar(app); toolbar.open(); @@ -90,9 +97,9 @@ public class TestWorld implements GameEventListener { 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); - 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); + Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); + Texture texture = app.getAssetManager().loadTexture("Pictures/board2.png"); + mat.setTexture("DiffuseMap", texture); geom.setMaterial(mat); geom.setLocalTranslation(0, -0.1f, 0); @@ -102,33 +109,49 @@ public class TestWorld implements GameEventListener { geom.setLocalRotation(rotation); app.getRootNode().attachChild(geom); - - System.out.println("Spielbrett erfolgreich erstellt, gedreht und hinzugefügt."); } catch (Exception e) { System.err.println("Fehler beim Erstellen des Spielfelds: " + e.getMessage()); } } + private void addLighting() { + // Direktionales Licht + DirectionalLight sun = new DirectionalLight(); + sun.setColor(ColorRGBA.White); + sun.setDirection(new Vector3f(-0.5f, -0.7f, -1.0f).normalizeLocal()); + app.getRootNode().addLight(sun); + + // Umgebungslicht + AmbientLight ambient = new AmbientLight(); + ambient.setColor(new ColorRGBA(0.6f, 0.6f, 0.6f, 1.0f)); + app.getRootNode().addLight(ambient); + } + /** * Erstellt die Spielfiguren basierend auf der bereits bekannten Spielerliste. */ private void createPlayerFigures() { - for (int i = 0; i < playerHandler.getPlayers().size(); i++) { - Player player = playerHandler.getPlayers().get(i); + for (Player player : playerHandler.getPlayers()) { try { + // Lade das Modell com.jme3.scene.Spatial model = app.getAssetManager().loadModel( - "models/" + player.getFigure().getType() + "/" + player.getFigure().getType() + ".j3o" - ); + "models/" + player.getFigure().getType() + "/" + player.getFigure().getType() + ".j3o"); + + // Setze das Material mit silberner Farbe + Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); + mat.setColor("Diffuse", new com.jme3.math.ColorRGBA(0.45f, 0.45f, 0.45f, 1.0f)); // Silberne Farbe + mat.setColor("Specular", new com.jme3.math.ColorRGBA(0.6f, 0.6f, 0.6f, 1.0f)); // Glanzlicht + mat.setFloat("Shininess", 64f); // Höhere Werte machen das Material glänzender + model.setMaterial(mat); + + // Skaliere und positioniere das Modell model.setLocalScale(0.5f); - - int playerIndexOnField = calculatePlayerIndexOnField(player.getFieldID(), player.getId()); - Vector3f startPosition = calculateFieldPosition(player.getFieldID(), playerIndexOnField); + Vector3f startPosition = calculateFieldPosition(player.getFieldID(), player.getId()); model.setLocalTranslation(startPosition); - model.setName("PlayerFigure_" + player.getId()); + + // Füge das Modell zur Szene hinzu app.getRootNode().attachChild(model); - - 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()); } @@ -384,6 +407,106 @@ public class TestWorld implements GameEventListener { } + private Vector3f calculateBuildingPosition(int fieldID) { + 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); + } + + private void updateHousesOnBoard() { + app.enqueue(() -> { + List propertiesWithBuildings = app.getGameLogic().getBoardManager().getPropertiesWithBuildings(); + + for (BuildingProperty property : propertiesWithBuildings) { + int houseCount = property.getHouses(); + int hotelCount = property.getHotel(); + + String uniqueIdentifier = "Building_" + property.getId() + "_" + (hotelCount > 0 ? "Hotel" : houseCount); + + if (existingHouses.contains(uniqueIdentifier)) continue; + + try { + String modelPath = hotelCount > 0 + ? "models/Hotel/Hotel.j3o" + : "models/Haus/" + houseCount + "Haus.j3o"; + + com.jme3.scene.Spatial buildingModel = app.getAssetManager().loadModel(modelPath); + + Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); + Texture texture = app.getAssetManager().loadTexture("models/Haus/" + "Haus.png"); + mat.setTexture("DiffuseMap", texture); + buildingModel.setMaterial(mat); + + buildingModel.setLocalScale(0.5f); + Vector3f position = calculateBuildingPosition(property.getId()).add(0, 0.5f, 0); + buildingModel.setLocalTranslation(position); + + com.jme3.math.Quaternion rotation = new com.jme3.math.Quaternion(); + if (property.getId() >= 1 && property.getId() <= 10) { + rotation.fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_Y); + } else if (property.getId() >= 21 && property.getId() <= 30) { + rotation.fromAngleAxis(3 * FastMath.HALF_PI, Vector3f.UNIT_Y); + } else if (property.getId() >= 31 && property.getId() <= 39) { + rotation.fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y); + } + + buildingModel.setLocalRotation(rotation); + buildingModel.setName(uniqueIdentifier); + app.getRootNode().attachChild(buildingModel); + existingHouses.add(uniqueIdentifier); + + } catch (Exception e) { + System.err.println("Fehler beim Hinzufügen eines Gebäudes: " + e.getMessage()); + } + } + }); + } + @Override public void receivedEvent(EventCardEvent event) { Timer timer = new Timer(); @@ -401,5 +524,6 @@ public class TestWorld implements GameEventListener { for (Player player : playerHandler.getPlayers()) { movePlayerFigure(player); } + updateHousesOnBoard(); } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java index 7fbbd26..5df9080 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java @@ -3,7 +3,6 @@ package pp.monopoly.model.fields; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; -import java.util.stream.Collector; import java.util.stream.Collectors; import com.jme3.network.serializing.Serializable; @@ -180,5 +179,17 @@ public class BoardManager { return groupProperties.stream() .allMatch(bp -> bp.getHouses() <= currentHouses); } + + /** + * Gibt eine Liste von BuildingProperty-Feldern zurück, auf denen Häuser oder Hotels stehen. + * @return Liste von BuildingProperty-Feldern mit Gebäuden + */ + public List getPropertiesWithBuildings() { + return board.stream() + .filter(f -> f instanceof BuildingProperty) + .map(f -> (BuildingProperty) f) + .filter(bp -> bp.getHouses() > 0 || bp.getHotel() > 0) + .collect(Collectors.toList()); + } }