时间管理类
using System;
using System.Collections.Generic;
using UnityEngine;
namespace MyFrameWork.Managers
{
public sealed class TimerManager : Singleton<TimerManager> ,IManager
{
private readonly List<Timer> _timers;
public List<Timer> Timers
{
get { return _timers; }
}
public TimerManager()
{
_timers = new List<Timer>();
}
public override void Init()
{
base.Init();
}
public Timer RegisterTimer(float duration, bool useRealTime = true, uint loopTimes = 1, Action onComplete = null,
Action onFinish = null, Action<float> onUpdate = null)
{
Timer timer = new Timer(duration, useRealTime, loopTimes, onComplete, onFinish, onUpdate);
_timers.Add(timer);
return timer;
}
public void UpdateTimes()
{
_timers.ForEach(t => t.Update(t.OnEachDurationElapsedTime()));
}
public void Cancel(Timer timer)
{
_timers.RemoveAll(t => t == timer);
}
}
public class Timer
{
public Timer(float duration, bool usesRealTime, uint loopTimes, Action onComplete, Action onFinish,
Action<float> onUpdate)
{
Duration = duration;
UsesRealTime = usesRealTime;
LoopTimes = loopTimes;
_onComplete = onComplete;
_onFinish = onFinish;
_onUpdate = onUpdate;
StartTime = GetWorldTime();
TotalTime = duration*LoopTimes;
OnEachLoopStartTime = StartTime;
OnStartTime = StartTime;
}
public float Duration { get; private set; }
public bool UsesRealTime { get; private set; }
public float OnEachLoopStartTime { get; private set; }
public uint LoopTimes { get; private set; }
public float StartTime { get; private set; }
public float TotalTime { get; private set; }
public bool IsDone
{
get { return _isDone; }
}
private bool _isPause = false;
public bool IsPause
{
get { return _isPause; }
set
{
if (value && !_isPause && !IsDone)
{
OnPauseTime = GetWorldTime();
}
_isPause = value;
}
}
public float OnPauseTime { get; private set; }
public float OnStartTime { get; private set; }
private bool _isDone = false;
private float _lastUpdateTime;
private readonly Action _onComplete;
private readonly Action _onFinish;
private readonly Action<float> _onUpdate;
public void Update(float elapsedTime)
{
if (_isDone)
{
TimerManager.Instance.Cancel(this);
return;
}
if (CheckIsDone())
{
_lastUpdateTime = this.GetWorldTime();
if (_onFinish != null) _onFinish();
if (_onComplete != null) _onComplete();
if (_onUpdate != null) _onUpdate(elapsedTime);
return;
}
if (_isPause)
{
OnEachLoopStartTime += this.GetDelaTime();
OnStartTime += this.GetDelaTime();
this._lastUpdateTime = this.GetWorldTime();
return;
}
if (_onUpdate != null) _onUpdate(elapsedTime);
_lastUpdateTime = this.GetWorldTime();
if (_lastUpdateTime >= FireTime())
{
if (_onComplete != null) _onComplete();
OnEachLoopStartTime = GetWorldTime();
}
}
private float GetDelaTime()
{
return this.GetWorldTime() - _lastUpdateTime;
}
private bool CheckIsDone()
{
_isDone = (_lastUpdateTime - OnStartTime) >= TotalTime;
return _isDone;
}
private float FireTime()
{
return OnEachLoopStartTime + Duration;
}
public float OnEachDurationElapsedTime()
{
return _lastUpdateTime - OnEachLoopStartTime;
}
public float GetElapsedTime()
{
if (IsDone)
{
return TotalTime;
}
return GetWorldTime() - OnStartTime;
}
public float GetElapsedTimeSinceRegister()
{
return GetWorldTime() - StartTime;
}
public uint GetElapsedLoopTime()
{
if (IsDone)
{
return LoopTimes;
}
else
{
return (uint) (GetElapsedTime()/Duration);
}
}
public float GetRemainTime()
{
if (IsDone)
{
return 0;
}
return TotalTime - GetElapsedTime();
}
private float GetWorldTime()
{
return this.UsesRealTime ? Time.realtimeSinceStartup : Time.time;
}
}
}