using System;
using System.Collections.Generic;
using AlicizaX.Runtime;
using UnityEngine;
namespace AlicizaX.Timer.Runtime
{
///
/// 计时器组件。
///
[DisallowMultipleComponent]
[AddComponentMenu("Game Framework/Timer")]
[UnityEngine.Scripting.Preserve]
public class TimerComponent : GameFrameworkComponent
{
ITimerManager _timerManager;
protected override void Awake()
{
ImplementationComponentType = Utility.Assembly.GetType(componentType);
InterfaceComponentType = typeof(ITimerManager);
base.Awake();
_timerManager = SysModuleCenter.GetModule();
if (_timerManager == null)
{
Log.Fatal("Timer manager is invalid.");
return;
}
}
///
/// 添加计时器。
///
/// 计时器回调。
/// 计时器间隔。
/// 是否循环。
/// 是否不收时间缩放影响。
/// 传参。(避免闭包)
/// 计时器Id。
public int AddTimer(TimerHandler callback, float time, bool isLoop = false, bool isUnscaled = false, params object[] args)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
return _timerManager.AddTimer(callback, time, isLoop, isUnscaled, args);
}
///
/// 暂停计时器。
///
/// 计时器Id。
public void Stop(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Stop(timerId);
}
///
/// 恢复计时器。
///
/// 计时器Id。
public void Resume(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Resume(timerId);
}
///
/// 计时器是否在运行中。
///
/// 计时器Id。
/// 否在运行中。
public bool IsRunning(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
return _timerManager.IsRunning(timerId);
}
///
/// 获得计时器剩余时间。
///
public float GetLeftTime(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
return _timerManager.GetLeftTime(timerId);
}
///
/// 重置计时器,恢复到开始状态。
///
public void Restart(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Restart(timerId);
}
///
/// 重置计时器。
///
public void Reset(int timerId, TimerHandler callback, float time, bool isLoop = false, bool isUnscaled = false)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Reset(timerId, callback, time, isLoop, isUnscaled);
}
///
/// 重置计时器。
///
public void Reset(int timerId, float time, bool isLoop, bool isUnscaled)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Reset(timerId, time, isLoop, isUnscaled);
}
///
/// 移除计时器。
///
/// 计时器Id。
public void RemoveTimer(int timerId)
{
if (_timerManager == null)
{
Log.Error("TimerMgr is invalid.");
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.RemoveTimer(timerId);
}
///
/// 移除所有计时器。
///
public void RemoveAllTimer()
{
if (_timerManager == null)
{
Log.Error("TimerMgr is invalid.");
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.RemoveAllTimer();
}
}
}