zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Hotfix/Logic/Model/Game/Entity/Battle/BuffComponent.cs

61 lines
1.8 KiB
C#
Raw Normal View History

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