49 lines
1.2 KiB
C#
Raw Permalink Normal View History

2024-08-17 14:12:46 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BehaviorTreeSlayer
{
public class MagicQueue : CompositeNode
{
TreeNode last;
public override void Enter(object args)
{
}
public override void Exit(object args)
{
}
public override TaskResult Tick(double dt, object args = null)
{
childs.Sort((a, b) => a.x < b.x ? -1 : 1);
if (childs.Count == 0)
{
return TaskResult.Fail;
}
TreeNode curNode = childs[Index];
if (last != curNode)
{
curNode.Enter(args);
}
TaskResult result = curNode.Tick(dt, args);
last = curNode;
curNode.state = result;
if (result == TaskResult.Running)
{
return TaskResult.Running;
}
if (result == TaskResult.OK)
{
curNode.Exit(args);
Index = (Index + 1) % childs.Count;
return TaskResult.OK;
}
return TaskResult.Fail;
}
}
}