using System; using System.Collections.Generic; using ET; namespace ET { public class BuffComponentAwakeSystem : AwakeSystem { public override void Awake(BuffComponent self) { self.timerId = TimerComponent.Instance.NewRepeatedTimer(500, self.Update); } } public class BuffComponentDestroySystem : DestroySystem { public override void Destroy(BuffComponent self) { self.buffList.Clear(); TimerComponent.Instance.Remove(ref self.timerId); } } public class BuffComponent : Entity { public long timerId; public readonly Dictionary buffList = new Dictionary(); public void AddBuff(BuffStateInfo buff) { buffList[buff.Id] = buff; Game.EventSystem.Publish(new ET.EventType.RefreshBuff { unitId=this.Id, }).Coroutine(); } public void RemoveBuff(long Id) { if (!buffList.Remove(Id)) { Log.Error($"can't remove buff where id is {Id}"); } Game.EventSystem.Publish(new ET.EventType.RefreshBuff { unitId = this.Id, }).Coroutine(); } internal void Update() { long now = TimeHelper.ServerNow(); using var listComponent = ListComponent.Create(); listComponent.List.AddRange(buffList.Keys); foreach (long key in listComponent.List) { BuffStateInfo buff = buffList[key]; if (now > buff.leastTime) RemoveBuff(buff.Id); } } } }