mirror of
				https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
				synced 2025-10-26 16:28:25 +01:00 
			
		
		
		
	Grundlage Toolbar
This commit is contained in:
		| @@ -0,0 +1,168 @@ | ||||
| package pp.monopoly.client.gui; | ||||
|  | ||||
| import java.util.Random; | ||||
|  | ||||
| import com.jme3.font.BitmapText; | ||||
| import com.jme3.math.Vector3f; | ||||
| import com.jme3.scene.Geometry; | ||||
| import com.simsilica.lemur.Axis; | ||||
| import com.simsilica.lemur.Button; | ||||
| import com.simsilica.lemur.Container; | ||||
| import com.simsilica.lemur.component.SpringGridLayout; | ||||
|  | ||||
| import pp.monopoly.client.MonopolyApp; | ||||
|  | ||||
| /** | ||||
|  * Toolbar Klasse, die am unteren Rand der Szene angezeigt wird. | ||||
|  * Die Buttons bewegen den Würfel auf dem Spielfeld. | ||||
|  */ | ||||
| public class Toolbar2 { | ||||
|  | ||||
|     private final MonopolyApp app; | ||||
|     private final Container toolbarContainer; | ||||
|     private final Geometry cube; // Referenz auf den Würfel | ||||
|     private final BitmapText positionText; // Anzeige für die aktuelle Position | ||||
|     private final float boardLimit = 0.95f; // Grenzen des Bretts | ||||
|     private final float stepSize = 0.18f; // Schrittgröße pro Bewegung | ||||
|     private int currentPosition = 0; // Aktuelle Position auf dem Spielfeld | ||||
|     private final int positionsPerSide = 10; // Anzahl der Positionen pro Seite | ||||
|     private final Random random = new Random(); // Zufallsgenerator für den Würfelwurf | ||||
|  | ||||
|     /** | ||||
|      * Konstruktor für die Toolbar. | ||||
|      * | ||||
|      * @param app  Die Hauptanwendung (MonopolyApp) | ||||
|      * @param cube Der Würfel, der bewegt werden soll | ||||
|      */ | ||||
|     public Toolbar2(MonopolyApp app, Geometry cube) { | ||||
|         this.app = app; | ||||
|         this.cube = cube; | ||||
|  | ||||
|         // Erstelle die Toolbar | ||||
|         toolbarContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y)); | ||||
|  | ||||
|         // Setze die Position am unteren Rand und die Breite | ||||
|         toolbarContainer.setLocalTranslation( | ||||
|                 0,                                 // Links bündig | ||||
|                 100,                               // Höhe über dem unteren Rand | ||||
|                 0                                  // Z-Ebene | ||||
|         ); | ||||
|         toolbarContainer.setPreferredSize(new Vector3f(app.getCamera().getWidth(), 100, 0)); // Volle Breite | ||||
|  | ||||
|         // Füge Buttons zur Toolbar hinzu | ||||
|         initializeButtons(); | ||||
|  | ||||
|         // Füge die Toolbar zur GUI hinzu | ||||
|         app.getGuiNode().attachChild(toolbarContainer); | ||||
|  | ||||
|         // Erstelle die Position-Anzeige | ||||
|         positionText = createPositionDisplay(); | ||||
|         updatePositionDisplay(); // Initialisiere die Anzeige mit der Startposition | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Initialisiert die Buttons in der Toolbar. | ||||
|      */ | ||||
|     private void initializeButtons() { | ||||
|         addButton("Vorwärts", 1);  // Bewegung nach vorne | ||||
|         addButton("Rückwärts", -1); // Bewegung nach hinten | ||||
|         addDiceRollButton();       // Würfel-Button | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Fügt einen Button mit einer Bewegung hinzu. | ||||
|      * | ||||
|      * @param label   Der Text des Buttons | ||||
|      * @param step    Schrittweite (+1 für vorwärts, -1 für rückwärts) | ||||
|      */ | ||||
|     private void addButton(String label, int step) { | ||||
|         Button button = new Button(label); | ||||
|         button.setPreferredSize(new Vector3f(150, 50, 0)); // Größe der Buttons | ||||
|         button.addClickCommands(source -> moveCube(step)); | ||||
|         toolbarContainer.addChild(button); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Fügt den Würfel-Button hinzu, der die Figur entsprechend der gewürfelten Zahl bewegt. | ||||
|      */ | ||||
|     private void addDiceRollButton() { | ||||
|         Button diceButton = new Button("Würfeln"); | ||||
|         diceButton.setPreferredSize(new Vector3f(150, 50, 0)); // Größe des Buttons | ||||
|         diceButton.addClickCommands(source -> rollDice()); | ||||
|         toolbarContainer.addChild(diceButton); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Simuliert einen Würfelwurf und bewegt die Figur entsprechend. | ||||
|      */ | ||||
|     private void rollDice() { | ||||
|         int diceRoll = random.nextInt(6) + 1; // Zahl zwischen 1 und 6 | ||||
|         System.out.println("Gewürfelt: " + diceRoll); | ||||
|         moveCube(diceRoll); // Bewege die Figur um die gewürfelte Zahl | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Bewegt den Würfel basierend auf der aktuellen Position auf dem Brett. | ||||
|      * | ||||
|      * @param step Schrittweite (+1 für vorwärts, -1 für rückwärts oder andere Werte) | ||||
|      */ | ||||
|     private void moveCube(int step) { | ||||
|         currentPosition = (currentPosition + step + 4 * positionsPerSide) % (4 * positionsPerSide); | ||||
|         Vector3f newPosition = calculatePosition(currentPosition); | ||||
|         cube.setLocalTranslation(newPosition); | ||||
|         updatePositionDisplay(); // Aktualisiere die Positionsanzeige | ||||
|         System.out.println("Würfelposition: " + newPosition + " (Feld-ID: " + currentPosition + ")"); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Berechnet die neue Position des Würfels basierend auf der aktuellen Brettseite und Position. | ||||
|      * | ||||
|      * @param position Aktuelle Position auf dem Spielfeld | ||||
|      * @return Die berechnete Position als Vector3f | ||||
|      */ | ||||
|     private Vector3f calculatePosition(int position) { | ||||
|         int side = position / positionsPerSide; // Seite des Bretts (0 = unten, 1 = rechts, 2 = oben, 3 = links) | ||||
|         int offset = position % positionsPerSide; // Position auf der aktuellen Seite | ||||
|  | ||||
|         switch (side) { | ||||
|             case 0: // Unten (positive x-Achse) | ||||
|                 return new Vector3f(-boardLimit + offset * stepSize, 0.1f, -boardLimit + 0.05f); | ||||
|             case 1: // Rechts (positive z-Achse) | ||||
|                 return new Vector3f(boardLimit - 0.05f, 0.1f, -boardLimit + offset * stepSize); | ||||
|             case 2: // Oben (negative x-Achse) | ||||
|                 return new Vector3f(boardLimit - offset * stepSize, 0.1f, boardLimit - 0.05f); | ||||
|             case 3: // Links (negative z-Achse) | ||||
|                 return new Vector3f(-boardLimit + 0.05f, 0.1f, boardLimit - offset * stepSize); | ||||
|             default: | ||||
|                 throw new IllegalArgumentException("Ungültige Position: " + position); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Erstellt die Anzeige für die aktuelle Position. | ||||
|      * | ||||
|      * @return Das BitmapText-Objekt für die Anzeige | ||||
|      */ | ||||
|     private BitmapText createPositionDisplay() { | ||||
|         BitmapText text = new BitmapText(app.getAssetManager().loadFont("Interface/Fonts/Default.fnt"), false); | ||||
|         text.setSize(20); // Schriftgröße | ||||
|         text.setLocalTranslation(10, app.getCamera().getHeight() - 10, 0); // Oben links | ||||
|         app.getGuiNode().attachChild(text); | ||||
|         return text; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Aktualisiert die Anzeige für die aktuelle Position. | ||||
|      */ | ||||
|     private void updatePositionDisplay() { | ||||
|         positionText.setText("Feld-ID: " + currentPosition); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Entfernt die Toolbar. | ||||
|      */ | ||||
|     public void remove() { | ||||
|         app.getGuiNode().detachChild(toolbarContainer); | ||||
|         app.getGuiNode().detachChild(positionText); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user