/** * @file dl_timer.h * @brief 计时器 * * * @version 1.0 * @author lveyou * @date 24-01-03 */ #pragma once namespace dl { //! 计时器,t秒触发一次 class TimerT { public: Timer() : _cur(0), _max(1.0) {}; Timer(FloatTime max) : _cur(0), _max(max) {}; ~Timer() {}; /** * @brief 计时,如果到了时间则返回true,且重新计时。 */ bool Run(FloatTime dt) { _cur += dt; if (_cur >= _max) { _cur -= _max; return true; } return false; } void Set(FloatTime max) { _max = max; } void Set(FloatTime max, FloatTime cur) { _max = max; _cur = cur; } void SetRemain(FloatTime remain) { _cur = _max - remain; } void SetCur(FloatTime cur) { _cur = cur; } private: FloatTime _cur; FloatTime _max; }; //! 互逆计时器,t秒反转一次 class TimerReverse { public: TimerReverse(float t, bool yes = false) { _t = t; Set(yes); } bool Run(real dt) { _cur += dt; if (_cur < _t) return false; if (_cur < _t * 2.0) return true; _cur -= _t * 2.0; return false; } void Set(bool yes = false) { _cur = yes ? _t : 0; } private: FloatTime _cur; FloatTime _t; }; }