using System; using System.Collections.Generic; using System.Text; using UnityEngine; namespace Thousandto.Core.Base { /// /// 时间计算类, 升级支持非Update调用方式 /// public class Clocks { float _lifeTime; // life time / loop time float _startTime; // OverTime Func -- start time / Time.Now float _elapsedTime = 0.0f; // TimesUp Func -- elapsed Time / life time float _lastInterval = 0.0f; bool _ringOnce = false; bool _isSwitch = true; bool _alarm = false; MyAction _myPreDelegate = null; MyAction _myLaterDelegate = null; public Clocks(float theLifeTime, bool theRingOnce = false, MyAction preDelegate = null, MyAction laterDelegate = null) { _lifeTime = theLifeTime; _ringOnce = theRingOnce; _startTime = Time.time; _myPreDelegate = preDelegate; _myLaterDelegate = laterDelegate; } public float LifeTime { get { return _lifeTime; } } public float LastInterval { get { return _lastInterval; } } /// /// Times Up setting , changed for Time Topologies(2014.09.16) /// /// /// public bool TimesUp(float deltaTime) { if (_isSwitch) { _elapsedTime += deltaTime; if (_elapsedTime > _lifeTime) { _lastInterval = _elapsedTime; _elapsedTime -= (Mathf.Floor(_elapsedTime / _lifeTime) * _lifeTime); if (_ringOnce) { _isSwitch = false; } if (_myLaterDelegate != null) { _myLaterDelegate(); } return true; } else { return false; } } else { return false; } } /// /// set/reset OverTime clock /// public void SetAlarm() { _startTime = Time.time; _isSwitch = true; _alarm = true; _elapsedTime = 0.0f; if (_myPreDelegate != null) { _myPreDelegate(); } } public void StopAlarm() { _alarm = false; _isSwitch = false; } public void SetToEnd() { StopAlarm(); if (_myLaterDelegate != null) { _myLaterDelegate(); } } /// /// mark times over, need to reset manually /// /// public bool OverTime() { bool isOverTime = false; if (_alarm) { if (_isSwitch) { float interval = Time.time - _startTime; if (interval > _lifeTime) { _lastInterval = interval; if (_ringOnce) { _isSwitch = false; } if (_myLaterDelegate != null) { _myLaterDelegate(); } isOverTime = true; } } } return isOverTime; } public void ReCount(float setTime = 0.0f) { _elapsedTime = setTime; } } }