128 lines
3.7 KiB
C#
128 lines
3.7 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace ET
|
|||
|
{
|
|||
|
public class BattleEventComponentAwakSystem : AwakeSystem<BattleEventComponent>
|
|||
|
{
|
|||
|
public override void Awake(BattleEventComponent self)
|
|||
|
{
|
|||
|
self.Awake();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class BattleEventComponent : Entity
|
|||
|
{
|
|||
|
private static BattleEventComponent instance;
|
|||
|
|
|||
|
|
|||
|
private readonly Dictionary<string, LinkedList<IEvent>> allEvents = new Dictionary<string, LinkedList<IEvent>>();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 缓存的结点字典
|
|||
|
/// </summary>
|
|||
|
private readonly Dictionary<string, LinkedListNode<IEvent>> cachedNodes = new Dictionary<string, LinkedListNode<IEvent>>();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 临时结点字典
|
|||
|
/// </summary>
|
|||
|
private readonly Dictionary<string, LinkedListNode<IEvent>> tempNodes = new Dictionary<string, LinkedListNode<IEvent>>();
|
|||
|
|
|||
|
|
|||
|
public void Awake()
|
|||
|
{
|
|||
|
instance = this;
|
|||
|
}
|
|||
|
public void RegisterEvent(string eventId, IEvent e)
|
|||
|
{
|
|||
|
if (!this.allEvents.ContainsKey(eventId))
|
|||
|
{
|
|||
|
this.allEvents.Add(eventId, new LinkedList<IEvent>());
|
|||
|
}
|
|||
|
|
|||
|
this.allEvents[eventId].AddLast(e);
|
|||
|
}
|
|||
|
|
|||
|
public void UnRegisterEvent(string eventId, IEvent e)
|
|||
|
{
|
|||
|
if (cachedNodes.Count > 0)
|
|||
|
{
|
|||
|
foreach (KeyValuePair<string, LinkedListNode<IEvent>> cachedNode in cachedNodes)
|
|||
|
{
|
|||
|
//预防极端情况,比如两个不同的事件id订阅了同一个事件处理者
|
|||
|
if (cachedNode.Value != null && cachedNode.Key == eventId && cachedNode.Value.Value == e)
|
|||
|
{
|
|||
|
//注意这里添加的Handler是下一个
|
|||
|
tempNodes.Add(cachedNode.Key, cachedNode.Value.Next);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//把临时结点字典中的目标元素值更新到缓存结点字典
|
|||
|
if (tempNodes.Count > 0)
|
|||
|
{
|
|||
|
foreach (KeyValuePair<string, LinkedListNode<IEvent>> cachedNode in tempNodes)
|
|||
|
{
|
|||
|
cachedNodes[cachedNode.Key] = cachedNode.Value;
|
|||
|
}
|
|||
|
|
|||
|
//清除临时结点
|
|||
|
tempNodes.Clear();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (this.allEvents.ContainsKey(eventId))
|
|||
|
{
|
|||
|
this.allEvents[eventId].Remove(e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void Init()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
public static void Publish<T>(string key,T a)where T : struct
|
|||
|
{
|
|||
|
instance.PublishInternel<T>(key,a);
|
|||
|
}
|
|||
|
private void PublishInternel<T>(string type,T a) where T : struct
|
|||
|
{
|
|||
|
if (!this.allEvents.TryGetValue(type, out var iEvents))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
LinkedListNode<IEvent> temp = iEvents.First;
|
|||
|
|
|||
|
while (temp != null)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
this.cachedNodes[type] = temp.Next;
|
|||
|
if (!(temp.Value is AEvent_Sync<T> aEvent))
|
|||
|
{
|
|||
|
Log.Error($"event error: {temp.Value.GetType().Name}");
|
|||
|
continue;
|
|||
|
}
|
|||
|
aEvent.Run(a);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
Log.Error(e);
|
|||
|
}
|
|||
|
|
|||
|
temp = this.cachedNodes[type];
|
|||
|
}
|
|||
|
|
|||
|
this.cachedNodes.Remove(type);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
namespace EventType
|
|||
|
{
|
|||
|
public struct BuffAdd
|
|||
|
{
|
|||
|
public Unit unit;
|
|||
|
public Unit targetUnit;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|