GodotComponentTest/components/MovableComponent.cs

36 lines
1.0 KiB
C#

using Godot;
using System;
public class MovableComponent : Node
{
public Vector3 targetPosition = Vector3.Zero;
public Vector3 currentPosition = Vector3.Zero;
public Vector3 currentVelocity = Vector3.Zero;
[Export] public float maxSpeed = 3;
private SpringDamper _springDamper;
[Signal]
delegate void PositionUpdated(Vector3 newPosition);
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_springDamper = new SpringDamper(4, 0.99f, 0.5f);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
if ((targetPosition - currentPosition).LengthSquared() > 0.01)
{
var springDamperResult =
_springDamper.CalcClampedSpeed(currentPosition, currentVelocity, targetPosition, delta, maxSpeed);
currentPosition = springDamperResult.Item1;
currentVelocity = springDamperResult.Item2;
EmitSignal("PositionUpdated", currentPosition);
}
}
}