rokojori_action_library/Runtime/Actions/Sequence/Parallel.cs

113 lines
3.0 KiB
C#
Raw Normal View History

2025-01-21 20:58:56 +00:00
using Godot;
using System.Collections.Generic;
2026-05-22 12:25:02 +00:00
using Rokojori.Extensions;
2025-01-21 20:58:56 +00:00
namespace Rokojori
{
[Tool][GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Parallel.svg") ]
2026-05-02 10:32:42 +00:00
[RokojoriActionCoreExport]
2025-01-21 20:58:56 +00:00
public partial class Parallel : SequenceAction
{
public enum Mode
{
First_Finishes_Sequence,
Wait_For_All_To_Finish
}
[Export]
public Mode mode = Mode.Wait_For_All_To_Finish;
[Export]
public Action[] actions = new Action[ 0 ];
2025-01-21 20:58:56 +00:00
[Export]
public bool triggerDirectChildren = true;
[Export]
public bool ignoreNonSequenceActions = true;
[Export]
public bool errorsCountAsFinished = false;
2026-05-22 12:25:02 +00:00
Dictionary<int,int> numFinished = new Dictionary<int, int>();
Dictionary<int,bool> running = new Dictionary<int, bool>();
2025-01-21 20:58:56 +00:00
protected override void _OnTrigger()
{
2026-02-12 09:48:23 +00:00
2026-05-22 12:25:02 +00:00
var allActions = new List<Action>( this.actions );
2026-02-12 09:48:23 +00:00
2025-01-21 20:58:56 +00:00
if ( triggerDirectChildren )
{
2026-05-22 12:25:02 +00:00
Nodes.ForEachDirectChild<Action>( this, a => allActions.Add( a ) );
2025-01-21 20:58:56 +00:00
}
2026-05-22 12:25:02 +00:00
var sequenceActions = ListExtensions.FilterType<Action,SequenceAction>( allActions);
2025-01-21 20:58:56 +00:00
var sequenceID = DispatchStart();
2026-05-22 12:25:02 +00:00
if ( ! ignoreNonSequenceActions && sequenceActions.Count != allActions.Count && Mode.First_Finishes_Sequence == mode )
2025-01-21 20:58:56 +00:00
{
2026-05-22 12:25:02 +00:00
allActions.ForEach( a => Action.TriggerSafe( a ) );
2025-01-21 20:58:56 +00:00
DispatchEnd( sequenceID );
return;
}
2026-05-22 12:25:02 +00:00
numFinished[ sequenceID ] = 0;
running[ sequenceID ] = true;
// var numFinished = 0;
// var running = true;
2025-01-21 20:58:56 +00:00
sequenceActions.ForEach(
sa =>
{
sa.onSequenceDone.Once(
( fe )=>
{
2026-02-12 09:48:23 +00:00
// this.LogInfo( "Sequence Action finished:", HierarchyName.Of( sa ) );
2026-05-22 12:25:02 +00:00
if ( ! running.ContainsKey( sequenceID ) )
2025-01-21 20:58:56 +00:00
{
return;
}
if ( fe.success || errorsCountAsFinished )
{
2026-05-22 12:25:02 +00:00
numFinished[ sequenceID ] = numFinished[ sequenceID ] + 1;
2025-01-21 20:58:56 +00:00
if ( Mode.First_Finishes_Sequence == mode )
{
2026-05-22 12:25:02 +00:00
running.Remove( sequenceID );
2026-02-12 09:48:23 +00:00
// this.LogInfo( "DispatchEnd for First_Finishes_Sequence" );
2025-01-21 20:58:56 +00:00
DispatchEnd( sequenceID );
}
else if ( Mode.Wait_For_All_To_Finish == mode )
{
2026-05-22 12:25:02 +00:00
if ( numFinished[ sequenceID ] == sequenceActions.Count )
2025-01-21 20:58:56 +00:00
{
2026-05-22 12:25:02 +00:00
running.Remove( sequenceID );
2026-02-12 09:48:23 +00:00
// this.LogInfo( "DispatchEnd for Wait_For_All_To_Finish" );
2025-01-21 20:58:56 +00:00
DispatchEnd( sequenceID );
}
}
}
else
{
2026-05-22 12:25:02 +00:00
running.Remove( sequenceID );
2026-02-12 09:48:23 +00:00
// this.LogInfo( "DispatchCancelled for Wait_For_All_To_Finish" );
2025-01-21 20:58:56 +00:00
DispatchCancelled( sequenceID );
}
}
);
}
);
2026-05-22 12:25:02 +00:00
allActions.ForEach( a => Action.TriggerSafe( a ) );
2025-01-21 20:58:56 +00:00
}
}
}