zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/System/AI/AIComponentSystem.cs

78 lines
2.2 KiB
C#
Raw Normal View History

2021-04-09 00:48:56 +08:00
using System;
using System.Collections.Generic;
namespace ET
{
public class AIComponentAwakeSystem : AwakeSystem<AIComponent>
{
public override void Awake(AIComponent self)
{
self.aiLists.Clear();
self.cancelToken = new ETCancellationToken();
self.Load();
}
}
public class AIComponentLoadSystem : LoadSystem<AIComponent>
{
public override void Load(AIComponent self)
{
self.Load();
}
}
public class AIComponentDestroySystem : DestroySystem<AIComponent>
{
public override void Destroy(AIComponent self)
{
self.aiLists.Clear();
self.cancelToken?.Cancel();
self.cancelToken = null;
self.current = null;
TimerComponent.Instance.Remove(ref self.TimerId);
}
}
public static class AIComponentSystem
{
public static void Load(this AIComponent self)
{
TimerComponent.Instance.Remove(ref self.TimerId);
self.TimerId = TimerComponent.Instance.NewRepeatedTimer(2000, self.Update);
}
public static void InitNodes(this AIComponent self,IEnumerable<AINode> list)
{
self.aiLists.Clear();
self.aiLists.AddRange(list);
}
public static void Update(this AIComponent self)
{
var unit = self.GetParent<Unit>();
AINode next = null;
foreach (var node in self.aiLists)
{
if (node.Check(unit))
{
next = node;
break;
}
}
if (next == null)
{
return;
}
// 如果下一个节点跟当前执行的节点一样,那么就不执行
if (next == self.current)
{
return;
}
self.current = next;
// 停止当前协程
self.cancelToken.Cancel();
// 执行下一个协程
self.cancelToken = new ETCancellationToken();
next.Run(unit, self.cancelToken).Coroutine();
}
}
}