1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-09 03:39:14 +00:00
OpenMW/components/misc/timer.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

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
{
public:
explicit DeviatingPeriodicTimer(float period, float deviation, float timeLeft)
: mPeriod(period)
, mDeviation(deviation)
, mTimeLeft(timeLeft)
{
}
2022-09-22 18:26:05 +00:00
TimerStatus update(float duration, Rng::Generator& prng)
2022-09-22 18:26:05 +00:00
{
if (mTimeLeft > 0)
{
mTimeLeft -= duration;
return TimerStatus::Waiting;
}
mTimeLeft = Rng::deviate(mPeriod, mDeviation, prng);
return TimerStatus::Elapsed;
2022-09-22 18:26:05 +00:00
}
void reset(float timeLeft) { mTimeLeft = timeLeft; }
private:
const float mPeriod;
const float mDeviation;
float mTimeLeft;
};
}
#endif