dungeon/src/Time.cpp
Adrian Hedqvist bb40711f84 Too big of a commit again
* Add and started using a proper state machine class
  * Uses a "StateResult" struct to handle state switches
* Add timers
* Move imgui to its own folder
* Add logger (not used yet)
2018-04-02 22:24:54 +02:00

68 lines
1.2 KiB
C++

#include "Time.h"
#include <vector>
std::vector<Time::Timer> timers;
void Time::Timer::tick(float dt) {
time_current += dt;
if (continuous) {
callback();
}
else if (time_current >= duration) {
callback();
if (loop && !is_finished()) {
count++;
time_current -= duration;
}
}
}
bool Time::Timer::is_finished() {
return (loop && count >= limit) || (!loop && time_current >= duration);
}
void Time::tick_timers(float dt) {
for (int i = timers.size() - 1; i >= 0; i--) {
timers[i].tick(dt);
if (timers[i].is_finished()) {
timers.erase(timers.begin() + i);
}
}
}
void Time::after(float seconds, TimerCallback callback) {
timers.emplace_back(Timer {
false,
false,
0,
0,
0,
seconds,
callback,
});
}
void Time::every(float seconds, int limit, TimerCallback callback) {
timers.emplace_back(Timer{
true,
false,
limit,
0,
0,
seconds,
callback,
});
}
void Time::during(float seconds, TimerCallback callback) {
timers.emplace_back(Timer{
false,
true,
0,
0,
0,
seconds,
callback,
});
}