88 lines
2.5 KiB
C#
Executable File
88 lines
2.5 KiB
C#
Executable File
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(1000, self.Update);
|
|
}
|
|
public static void InitNodes(this AIComponent self,IEnumerable<AINode> list)
|
|
{
|
|
self.aiLists.Clear();
|
|
self.aiLists.AddRange(list);
|
|
}
|
|
public static T GetData<T>(this AIComponent self,string key)
|
|
{
|
|
if (!self.aiData.TryGetValue(key, out object value)) return default;
|
|
if (value == null) return default;
|
|
return (T)value;
|
|
}
|
|
public static void SetData(this AIComponent self, string key,object data)
|
|
{
|
|
self.aiData[key] = data;
|
|
}
|
|
public static void Update(this AIComponent self)
|
|
{
|
|
Unit unit = self.GetParent<Unit>();
|
|
AINode next = null;
|
|
foreach (AINode 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();
|
|
}
|
|
}
|
|
}
|