2022-12-04 21:54:44 +01:00
|
|
|
using Godot;
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public class MovableComponent : Component {
|
2022-12-04 21:54:44 +01:00
|
|
|
public Vector3 targetPosition = Vector3.Zero;
|
|
|
|
public Vector3 currentPosition = Vector3.Zero;
|
|
|
|
public Vector3 currentVelocity = Vector3.Zero;
|
2022-12-06 21:06:25 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public float targetAngle;
|
|
|
|
public float currentAngle;
|
|
|
|
public float currentAngularVelocity;
|
2022-12-28 16:22:53 +01:00
|
|
|
|
2022-12-04 21:54:44 +01:00
|
|
|
[Export] public float maxSpeed = 3;
|
|
|
|
|
2022-12-06 21:06:25 +01:00
|
|
|
private SpringDamper _positionSpringDamper;
|
|
|
|
private SpringDamper _angleSpringDamper;
|
2022-12-04 21:54:44 +01:00
|
|
|
|
|
|
|
[Signal]
|
2023-11-18 22:32:57 +01:00
|
|
|
private delegate void PositionUpdated(Vector3 newPosition);
|
2022-12-04 21:54:44 +01:00
|
|
|
|
2022-12-06 21:06:25 +01:00
|
|
|
[Signal]
|
2023-11-18 22:32:57 +01:00
|
|
|
private delegate void OrientationUpdated(float newAngle);
|
2022-12-06 21:06:25 +01:00
|
|
|
|
2022-12-04 21:54:44 +01:00
|
|
|
// Called when the node enters the scene tree for the first time.
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _Ready() {
|
2022-12-06 21:06:25 +01:00
|
|
|
_positionSpringDamper = new SpringDamper(4, 0.99f, 0.5f);
|
|
|
|
_angleSpringDamper = new SpringDamper(4, 0.99f, 0.5f);
|
2022-12-04 21:54:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _Process(float delta) {
|
2022-12-28 16:22:53 +01:00
|
|
|
Vector3 targetError = targetPosition - currentPosition;
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (targetError.LengthSquared() > 0.01) {
|
2023-05-01 18:37:35 +02:00
|
|
|
Vector3 targetDir = targetError.Normalized();
|
2022-12-28 16:22:53 +01:00
|
|
|
targetAngle = new Vector3(0, 0, 1).SignedAngleTo(targetDir, Vector3.Up);
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (targetAngle - currentAngle > Mathf.Pi) {
|
2022-12-28 16:22:53 +01:00
|
|
|
currentAngle += 2 * Mathf.Pi;
|
2023-11-18 22:32:57 +01:00
|
|
|
} else if (targetAngle - currentAngle < -Mathf.Pi) {
|
2022-12-28 16:22:53 +01:00
|
|
|
currentAngle -= 2 * Mathf.Pi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (Mathf.Abs(currentAngularVelocity) > 0.1 || Mathf.Abs(targetAngle - currentAngle) > 0.01) {
|
|
|
|
(float, float) springDamperResult =
|
2022-12-28 16:22:53 +01:00
|
|
|
_angleSpringDamper.Calc(currentAngle, currentAngularVelocity, targetAngle, delta);
|
|
|
|
|
|
|
|
currentAngle = springDamperResult.Item1;
|
|
|
|
currentAngularVelocity = springDamperResult.Item2;
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
EmitSignal("OrientationUpdated", currentAngle);
|
2022-12-28 16:22:53 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (Mathf.Abs(currentAngularVelocity) < 5 && Mathf.Abs(targetAngle - currentAngle) < 0.3
|
|
|
|
&& (targetPosition - currentPosition).LengthSquared() > 0.01) {
|
|
|
|
(Vector3, Vector3) springDamperResult =
|
2022-12-28 21:57:34 +01:00
|
|
|
_positionSpringDamper.CalcClampedSpeedXZ(currentPosition, currentVelocity, targetPosition, delta,
|
2022-12-28 16:22:53 +01:00
|
|
|
maxSpeed);
|
|
|
|
|
|
|
|
currentPosition = springDamperResult.Item1;
|
|
|
|
currentVelocity = springDamperResult.Item2;
|
|
|
|
}
|
2023-11-18 22:32:57 +01:00
|
|
|
|
2022-12-28 21:57:34 +01:00
|
|
|
currentVelocity.y = currentVelocity.y - 9.81f * delta;
|
|
|
|
currentPosition.y = currentPosition.y + currentVelocity.y * delta;
|
2023-11-18 22:32:57 +01:00
|
|
|
|
2022-12-28 21:57:34 +01:00
|
|
|
EmitSignal("PositionUpdated", currentPosition);
|
2022-12-04 21:54:44 +01:00
|
|
|
}
|
|
|
|
}
|