1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:29:56 +00:00
openmw/components/misc/timer.hpp

46 lines
964 B
C++
Raw Normal View History

#ifndef OPENMW_COMPONENTS_MISC_TIMER_H
#define OPENMW_COMPONENTS_MISC_TIMER_H
#include "rng.hpp"
namespace Misc
{
enum class TimerStatus
{
Waiting,
Elapsed,
};
class DeviatingPeriodicTimer
{
2022-09-22 18:26:05 +00:00
public:
explicit DeviatingPeriodicTimer(float period, float deviation, float timeLeft)
: mPeriod(period)
, mDeviation(deviation)
, mTimeLeft(timeLeft)
{
}
TimerStatus update(float duration, Rng::Generator& prng)
{
if (mTimeLeft > 0)
{
2022-09-22 18:26:05 +00:00
mTimeLeft -= duration;
return TimerStatus::Waiting;
}
2022-09-22 18:26:05 +00:00
mTimeLeft = Rng::deviate(mPeriod, mDeviation, prng);
return TimerStatus::Elapsed;
}
void reset(float timeLeft) { mTimeLeft = timeLeft; }
2022-09-22 18:26:05 +00:00
private:
const float mPeriod;
const float mDeviation;
float mTimeLeft;
};
}
#endif