rj-action-library/Runtime/Actions/Time/TimeLooper.cs

75 lines
1.1 KiB
C#

using Godot;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class TimeLooper : Node
{
bool _active = false;
[Export]
public bool active
{
get => _active;
set
{
SetActive( value );
}
}
[Export]
public float duration;
[Export]
public float offset;
[Export]
public TimeLine timeLine;
[Export]
public Action action;
int _eventID = -1;
void SetActive( bool active )
{
if ( active == _active || Engine.IsEditorHint() )
{
return;
}
_active = active;
if ( _active )
{
if ( _eventID != -1 )
{
TimeLineManager.RemoveEvent( timeLine, _eventID );
return;
}
var tle = TimeLineManager.ScheduleLoopEvent( timeLine, duration, offset, tle => Action.Trigger( action ) );
_eventID = tle.id;
}
else
{
if ( _eventID == -1 )
{
return;
}
TimeLineManager.RemoveEvent( timeLine, _eventID );
_eventID = -1;
}
}
}
}