110 lines
1.8 KiB
C#
110 lines
1.8 KiB
C#
using Godot;
|
|
|
|
using Rokojori.Extensions;
|
|
namespace Rokojori;
|
|
|
|
[Tool]
|
|
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Action.svg")]
|
|
public partial class Brain:Action
|
|
{
|
|
[Export]
|
|
public Receptor[] receptors;
|
|
|
|
[Export]
|
|
public Node behaviorRoot;
|
|
|
|
[Export]
|
|
public BehaviorSequence activeSequence;
|
|
|
|
bool _paused = false;
|
|
|
|
public bool paused => _paused;
|
|
|
|
public void SetPaused( bool value )
|
|
{
|
|
_paused = value;
|
|
}
|
|
|
|
protected override void _OnTrigger()
|
|
{
|
|
if ( _paused )
|
|
{
|
|
return;
|
|
}
|
|
|
|
if ( activeSequence != null && activeSequence.isExclusive && activeSequence.isRunning )
|
|
{
|
|
return;
|
|
}
|
|
|
|
var resolved = Resolve();
|
|
|
|
if ( resolved == null )
|
|
{
|
|
return;
|
|
}
|
|
|
|
if ( resolved == activeSequence )
|
|
{
|
|
if ( activeSequence.reinitialize )
|
|
{
|
|
activeSequence.CancelAllRunning();
|
|
activeSequence.Trigger();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
var previous = activeSequence;
|
|
activeSequence = resolved;
|
|
|
|
if ( previous != null )
|
|
{
|
|
previous.CancelAllRunning();
|
|
}
|
|
|
|
activeSequence.Trigger();
|
|
}
|
|
|
|
BehaviorSequence Resolve()
|
|
{
|
|
if ( behaviorRoot == null )
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return ResolveIn( behaviorRoot );
|
|
}
|
|
|
|
BehaviorSequence ResolveIn( Node parent )
|
|
{
|
|
var walker = NodesWalker.Get();
|
|
var numChildren = walker.NumChildren( parent );
|
|
|
|
for ( int i = 0; i < numChildren; i++ )
|
|
{
|
|
var child = walker.ChildAt( parent, i );
|
|
|
|
if ( child is BehaviorSequence sequence )
|
|
{
|
|
return sequence;
|
|
}
|
|
|
|
if ( child is BehaviorBranch branch )
|
|
{
|
|
if ( branch.IsBranchActive( this ) )
|
|
{
|
|
var found = ResolveIn( branch );
|
|
|
|
if ( found != null )
|
|
{
|
|
return found;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|