2023-04-28 14:19:48 +02:00
|
|
|
using Godot;
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public class GoldBar : KinematicBody {
|
2023-04-28 14:19:48 +02:00
|
|
|
private Vector3 targetPosition;
|
2023-11-18 22:32:57 +01:00
|
|
|
private bool hasTarget;
|
2023-04-28 14:19:48 +02:00
|
|
|
public Vector3 velocity;
|
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _Ready() {
|
2023-04-28 14:19:48 +02:00
|
|
|
targetPosition = GlobalTransform.origin;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void SetTarget(Vector3 target) {
|
2023-04-28 14:19:48 +02:00
|
|
|
targetPosition = target;
|
|
|
|
hasTarget = true;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void UnsetTarget() {
|
2023-04-28 14:19:48 +02:00
|
|
|
hasTarget = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _PhysicsProcess(float delta) {
|
|
|
|
if (hasTarget) {
|
|
|
|
if (targetPosition.IsEqualApprox(GlobalTransform.origin)) {
|
2023-04-28 14:19:48 +02:00
|
|
|
targetPosition = GlobalTransform.origin;
|
|
|
|
velocity = Vector3.Zero;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
|
2023-04-28 14:19:48 +02:00
|
|
|
Vector3 targetDirection = (targetPosition - GlobalTransform.origin).Normalized();
|
|
|
|
velocity = targetDirection * (velocity.Length() + 10 * delta);
|
2023-11-18 22:32:57 +01:00
|
|
|
Transform = new Transform(Transform.basis.Rotated(Vector3.Up, delta * 2.0f), Transform.origin);
|
|
|
|
} else {
|
2023-04-28 14:19:48 +02:00
|
|
|
velocity.y = velocity.y - 9.81f * delta;
|
|
|
|
}
|
|
|
|
|
2023-06-23 15:39:07 +02:00
|
|
|
velocity = MoveAndSlide(velocity, Vector3.Up);
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (IsOnFloor() || Mathf.Abs(Transform.origin.y) < 0.01) {
|
2023-06-23 15:39:07 +02:00
|
|
|
// apply damping when on ground
|
|
|
|
velocity = velocity - velocity.Normalized() * 0.9f * delta;
|
|
|
|
}
|
2023-11-18 22:32:57 +01:00
|
|
|
|
|
|
|
if (velocity.LengthSquared() < 0.01) {
|
2023-04-28 14:19:48 +02:00
|
|
|
velocity = Vector3.Zero;
|
|
|
|
}
|
|
|
|
}
|
2023-11-18 22:32:57 +01:00
|
|
|
}
|