rokojori_action_library/Runtime/Actions/Sequence/SequenceAction.cs

113 lines
2.2 KiB
C#
Raw Normal View History

2025-01-08 18:46:17 +00:00
using Godot;
2026-02-12 09:48:23 +00:00
using System.Collections.Generic;
2026-02-26 14:06:27 +00:00
using System.Linq;
2025-01-08 18:46:17 +00:00
2026-05-22 12:25:02 +00:00
using Rokojori.Extensions;
2025-01-08 18:46:17 +00:00
namespace Rokojori
2026-05-02 10:32:42 +00:00
{
2025-01-08 18:46:17 +00:00
2025-09-26 12:00:59 +00:00
[Tool]
2025-01-21 20:58:56 +00:00
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/SequenceAction.svg")]
2026-05-02 10:32:42 +00:00
[RokojoriActionCoreExport]
2025-09-26 12:00:59 +00:00
public abstract partial class SequenceAction : Action
2026-05-22 12:25:02 +00:00
{
2026-02-12 09:48:23 +00:00
[ExportGroup("Debugging")]
[ExportToolButton("Clear Listeners")]
public Callable clearListeners => Callable.From(
()=>
{
onSequenceDone.ResetAndClearAll();
}
);
[Export]
public bool showRunningSequences = false;
[Export]
public int[] runningSequences = [];
2026-02-26 14:06:27 +00:00
HashSet<int> _running = new HashSet<int>();
2026-02-12 09:48:23 +00:00
2025-01-08 18:46:17 +00:00
int _dispatchCounter = 0;
public int GetLastSequenceActionID()
{
return _dispatchCounter;
}
2026-05-22 12:25:02 +00:00
public readonly EventSlot<SequenceActionFinishedEvent> onSequenceDone = new EventSlot<SequenceActionFinishedEvent>();
2025-01-08 18:46:17 +00:00
public int DispatchStart()
{
2026-05-22 12:25:02 +00:00
2025-01-08 18:46:17 +00:00
_dispatchCounter ++;
2026-02-12 09:48:23 +00:00
if ( showRunningSequences )
{
runningSequences = runningSequences.Add( _dispatchCounter );
}
2026-02-26 14:06:27 +00:00
_running.Add( _dispatchCounter );
2025-01-08 18:46:17 +00:00
return _dispatchCounter;
}
public void DispatchEnd( int id )
{
2026-02-26 14:06:27 +00:00
_running.Remove( id );
2026-05-22 12:25:02 +00:00
var ev = new SequenceActionFinishedEvent{ id = id, success = true };
2026-02-12 09:48:23 +00:00
onSequenceDone.DispatchEvent( ev );
if ( showRunningSequences )
{
runningSequences = runningSequences.Remove( _dispatchCounter );
}
2026-02-26 14:06:27 +00:00
2025-01-08 18:46:17 +00:00
}
public void DispatchCancelled( int id )
{
2026-02-26 14:06:27 +00:00
_running.Remove( id );
2026-05-22 12:25:02 +00:00
var ev = new SequenceActionFinishedEvent{ id = id, success = true };
2026-02-12 09:48:23 +00:00
onSequenceDone.DispatchEvent( ev );
if ( showRunningSequences )
{
runningSequences = runningSequences.Remove( _dispatchCounter );
}
2026-02-26 14:06:27 +00:00
}
public virtual void CancelAllRunning()
{
2026-05-22 12:25:02 +00:00
foreach ( var r in _running )
{
CancelAction( r );
}
2025-01-08 18:46:17 +00:00
}
public virtual void CancelAction( int id )
{
2026-02-12 09:48:23 +00:00
2025-01-08 18:46:17 +00:00
}
2025-12-18 10:29:54 +00:00
public int TriggerSequenceAndGetID()
{
var nextID = GetLastSequenceActionID() + 1;
Trigger();
return GetLastSequenceActionID() < nextID ? -1 : nextID;
}
2025-01-08 18:46:17 +00:00
}
}