rokojori_action_library/Runtime/Behavior/Movements/MoveOnPath.cs

63 lines
1005 B
C#
Raw Normal View History

using Godot;
using Rokojori.Extensions;
namespace Rokojori;
[Tool]
[GlobalClass]
public partial class MoveOnPath:SequenceAction
{
[Export]
public Node3D target;
[Export]
public Spline spline;
[Export]
public float duration = 1f;
double _elapsed;
int _activeId = -1;
protected override void _OnTrigger()
{
if ( target == null || spline == null )
{
return;
}
_elapsed = 0;
_activeId = DispatchStart();
}
public override void _PhysicsProcess( double delta )
{
if ( _activeId == -1 )
{
return;
}
_elapsed += delta;
var t = duration <= 0 ? 1f : Mathf.Clamp( (float)( _elapsed / duration ), 0f, 1f );
target.GlobalPosition = spline.GetCurve().PositionAt( t );
if ( t >= 1f )
{
var id = _activeId;
_activeId = -1;
DispatchEnd( id );
}
}
public override void CancelAction( int id )
{
if ( id == _activeId )
{
_activeId = -1;
DispatchCancelled( id );
}
}
}