46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Godot;
|
|
|
|
public class CollectibleComponent : Component {
|
|
private Vector3 targetPosition;
|
|
private bool hasTarget;
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready() { }
|
|
|
|
public void SetTarget(Vector3 target) {
|
|
targetPosition = target;
|
|
hasTarget = true;
|
|
}
|
|
|
|
public void UnsetTarget() {
|
|
hasTarget = false;
|
|
}
|
|
|
|
public void PhysicsProcess(float delta, Entity entity) {
|
|
if (hasTarget) {
|
|
if (targetPosition.IsEqualApprox(entity.GlobalTransform.origin)) {
|
|
targetPosition = entity.GlobalTransform.origin;
|
|
entity.Velocity = Vector3.Zero;
|
|
}
|
|
|
|
|
|
Vector3 targetDirection = (targetPosition - entity.GlobalTransform.origin).Normalized();
|
|
entity.Velocity = targetDirection * (entity.Velocity.Length() + 10 * delta);
|
|
entity.Transform = new Transform(entity.Transform.basis.Rotated(Vector3.Up, delta * 2.0f),
|
|
entity.Transform.origin);
|
|
} else {
|
|
entity.Velocity = entity.Velocity - 9.81f * delta * Vector3.Up;
|
|
}
|
|
|
|
entity.Velocity = entity.MoveAndSlide(entity.Velocity, Vector3.Up);
|
|
|
|
if (entity.IsOnFloor() || Mathf.Abs(entity.Transform.origin.y) < 0.01) {
|
|
// apply damping when on ground
|
|
entity.Velocity = entity.Velocity - entity.Velocity.Normalized() * 0.9f * delta;
|
|
}
|
|
|
|
if (entity.Velocity.LengthSquared() < 0.01) {
|
|
entity.Velocity = Vector3.Zero;
|
|
}
|
|
}
|
|
} |