rj-action-library/Runtime/Actions/Visual/TweenPosition.cs

91 lines
1.8 KiB
C#

using System;
using Godot;
namespace Rokojori
{
[Tool]
[GlobalClass ]
public partial class TweenPosition:SequenceAction
{
[Export]
public Node3D target;
[Export]
public Node3D endPosition;
[Export]
public Vector3 endOffset;
[Export]
public TweenType tweenType = new TweenTimeCurve();
[Export]
public bool cacheEndPositionOnStart = true;
[Export]
public TimeLine timeLine;
protected override void _OnTrigger()
{
if ( target == null )
{
return;
}
var tl = TimeLineManager.Ensure( timeLine );
var start = tl.position;
var fromPosition = target.GlobalPosition;
var toPosition = endOffset;
if ( endPosition != null )
{
toPosition += endPosition.GlobalPosition;
}
var sequenceID = DispatchStart();
var tweenType = this.tweenType;
if ( tweenType == null )
{
tweenType = TweenTimeCurve.defaultCurve;
}
TimeLineManager.ScheduleSpanIn( tl, 0, tweenType.GetTweenDuration(),
( span, type )=>
{
var timeNow = tl.position;
var elapsed = timeNow - start;
var state = tweenType.GetTweenPhaseForPhase( span.phase );
if ( ! cacheEndPositionOnStart )
{
toPosition = endOffset;
if ( endPosition != null )
{
toPosition += endPosition.GlobalPosition;
}
}
var lerpedPosition = fromPosition.Lerp( toPosition, state );
target.GlobalPosition = lerpedPosition;
if ( type == TimeLineSpanUpdateType.End )
{
DispatchEnd( sequenceID );
}
}
);
}
}
}