You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

55 lines
1.5 KiB

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<Object> callback;
}
// Contexts.Combat.renderTimer.AddTimer(1, () => { Debug.Log("1s"); });
[Combat, Unique]
public sealed class RenderTimerComponent : IComponent
{
public int idGen;
internal readonly List<TimerData> timerList = new();
internal readonly List<TimerData> cachedTimerList = new();
/// <summary>
/// 添加一个定时器
/// </summary>
/// <param name="time">单位为秒</param>
/// <param name="param"></param>
/// <param name="callback"></param>
/// <returns>返回定时器的id</returns>
public int AddTimer(float time, Object param, Action<Object> 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();
}
}
}