using Entitas; using Entitas.CodeGeneration.Attributes; using System; using System.Collections.Generic; using CoreGame; namespace CoreGame.Render { public class TimerData { public int id; public float delayTimeS; public Object param; public float initTime; public Action callback; } // Contexts.Combat.renderTimer.AddTimer(1, () => { Debug.Log("1s"); }); [Combat, Unique] public sealed class RenderTimerComponent : IComponent { public int idGen; internal readonly List timerList = new(); internal readonly List cachedTimerList = new(); /// /// 添加一个定时器 /// /// 单位为秒 /// /// /// 返回定时器的id public int AddTimer(float time, Object param, Action callback) { var timerData = new TimerData { id = idGen++, delayTimeS = time, callback = callback, param = param, initTime = GameTime.GetRenderTime() }; cachedTimerList.Add(timerData); return timerData.id; } // 防止死循环 public void SyncTimer() { timerList.AddRange(cachedTimerList); cachedTimerList.Clear(); } } }