Animation Hinzugefügt

This commit is contained in:
Luca Puderbach
2024-12-09 08:17:07 +01:00
parent c9e99ee9ff
commit 96a78ee61b
3 changed files with 163 additions and 49 deletions

View File

@@ -25,6 +25,7 @@ import com.jme3.util.TangentBinormalGenerator;
import pp.monopoly.client.gui.BobTheBuilder;
import pp.monopoly.client.gui.Toolbar;
import pp.monopoly.model.Board;
import pp.monopoly.client.gui.FigureControl;
import static pp.util.FloatMath.TWO_PI;
import static pp.util.FloatMath.cos;
import static pp.util.FloatMath.sin;
@@ -66,6 +67,8 @@ public class BoardAppState extends MonopolyAppState {
*/
private PopUpManager popUpManager;;
private Vector3f currentTarget = new Vector3f(-10f,0,-10f);
/**
* 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.
@@ -120,12 +123,14 @@ public class BoardAppState extends MonopolyAppState {
final float x = mx - cos;
final float y = my - sin;
final Camera camera = getApp().getCamera();
camera.setLocation(new Vector3f(-30,25,-30));
camera.lookAt(new Vector3f(0,0, 0),
camera.setLocation(new Vector3f(0,30,0));
camera.lookAt(new Vector3f(getCurrentTarget()),
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.
@@ -230,4 +235,8 @@ public class BoardAppState extends MonopolyAppState {
cardDeck.attachChild(card2);
return cardDeck;
}
public Vector3f getCurrentTarget(){
return currentTarget;
}
}

View File

@@ -1,5 +1,8 @@
package pp.monopoly.client.gui;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
@@ -10,38 +13,126 @@ import pp.monopoly.model.Figure;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.UpdatePlayerView;
public class FigureControl extends AbstractControl implements GameEventListener{
import java.util.LinkedList;
import java.util.Queue;
public class FigureControl extends AbstractControl implements GameEventListener {
private final Figure figure;
private final Spatial spatial;
private final MonopolyApp app;
private Queue<Vector3f> path; // Path to follow
private Vector3f currentTarget;
private float animationTime = 0f; // Time elapsed for the current movement
private final float durationPerField = 0.5f; // Time to move between fields
private float delayTime = 3f; // Verzögerung in Sekunden
private float delayElapsed = 0f; // Zeit, die seit dem Start der Verzögerung vergangen ist
public FigureControl(Spatial spatial, Figure figure, MonopolyApp app) {
super();
this.figure = figure;
this.app = app;
this.spatial = spatial;
this.app = app;
this.path = new LinkedList<>();
app.getGameLogic().addListener(this);
}
@Override
protected void controlUpdate(float tpf) {
//TODO: animation
if (delayTime > 0) {
delayElapsed += tpf;
if (delayElapsed < delayTime) {
return; // Warte, bis die Verzögerung abgeschlossen ist
}
delayTime = 0; // Verzögerung abgeschlossen
System.out.println("Delay completed. Starting animation...");
}
if (currentTarget == null && !path.isEmpty()) {
// Hole das nächste Ziel aus dem Pfad
currentTarget = path.poll();
animationTime = 0f;
System.out.println("Next target: " + currentTarget);
}
if (currentTarget != null) {
animationTime += tpf;
Vector3f startPosition = spatial.getLocalTranslation();
Vector3f interpolatedPosition = startPosition.interpolateLocal(
currentTarget,
animationTime / durationPerField
);
// Hüpfeffekt hinzufügen
float hopHeight = 0.5f * FastMath.sin(FastMath.PI * (animationTime / durationPerField));
interpolatedPosition.setY(hopHeight + 1);
spatial.setLocalTranslation(interpolatedPosition);
// Ziel erreicht
if (animationTime >= durationPerField) {
spatial.setLocalTranslation(currentTarget);
figure.moveTo(currentTarget); // Synchronisiere die interne Position
currentTarget = null; // Setze Ziel zurück
System.out.println("Target reached.");
}
}
}
// Beispiel: Berechnung des nächsten Feldes
private int nextField() {
int currentField = figure.getCurrentFieldID();
return (currentField + 1) % 40; // Weiter zum nächsten Feld
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
// No rendering required
// No rendering logic required
}
public void setPath(int startField, int endField) {
System.out.println("setPath called with startField: " + startField + ", endField: " + endField);
path.clear();
for (int fieldId = startField; fieldId != endField; fieldId = (fieldId + 1) % 40) {
Vector3f position = figure.calculateFieldPosition(fieldId);
System.out.println("Adding position to path: " + position);
path.add(position);
}
Vector3f finalPosition = figure.calculateFieldPosition(endField);
path.add(finalPosition);
System.out.println("Final position added to path: " + finalPosition);
System.out.println("Path size: " + path.size());
}
@Override
public void receivedEvent(UpdatePlayerView event) {
System.out.println("Event received: " + event);
int newPos = app.getGameLogic().getPlayerHandler().getPlayerById(figure.getId()).getFieldID();
int currentField = figure.getCurrentFieldID();
figure.moveTo(newPos);
if (currentField == newPos) {
System.out.println("Figure is already at the correct position. No path set.");
return;
}
//TODO hier einzelne Punkte der Animation ablkaufen mit figure.setLocalTransLation und jeweils Rotation anpassen
System.out.println("Movement required. Current field: " + currentField + ", New field: " + newPos);
spatial.setLocalTranslation(figure.getPos());
setPath(currentField, newPos);
delayTime = 3f; // Verzögerung zurücksetzen
delayElapsed = 0f; // Timer zurücksetzen
}
}