Files
2024-03-02 16:12:22 +08:00

87 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file dl_timer.h
* @brief 计时器
*
*
* @version 1.0
* @author lveyou
* @date 24-01-03
*
* @note 默认值:1s,false
*/
#pragma once
namespace dl
{
//! 计时器,t秒触发一次
class TimerRegular
{
public:
TimerRegular() : _cur(0), _max(1.0) {};
TimerRegular(FloatTime max) : _cur(0), _max(max) {};
~TimerRegular() {};
/**
* @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()
{
Set(1.0, false);
}
TimerReverse(float t, bool yes = false)
{
Set(t, yes);
}
bool Run(FloatTime dt)
{
_cur += dt;
if (_cur < _t)
return false;
if (_cur < _t * 2.0_f)
return true;
_cur -= _t * 2.0_f;
return false;
}
void Set(FloatTime t, bool yes = false)
{
_t = t;
_cur = yes ? _t : 0;
}
void Reset(bool yes)
{
Set(_t, yes);
}
private:
FloatTime _cur;
FloatTime _t;
};
}