Adrian Hedqvist
bb40711f84
* 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)
68 lines
1.2 KiB
C++
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,
|
|
});
|
|
}
|