#include <algorithm>
#include <list>
+#include <cmath>
namespace app {
template<class Time>
struct TimerData {
- TimerData() : time(0), target(0), refCount(0) { }
- TimerData(Time target) : time(0), target(target), refCount(0) { }
+ TimerData() : time(0), target(0), refCount(0), justHit(false) { }
+ TimerData(Time target) : time(0), target(target), refCount(0), justHit(false) { }
Time time;
Time target;
int refCount;
+ bool justHit;
};
Time Remaining() const {
return data ? (data->target - data->time) : Time();
}
+ int Iteration() const {
+ return (data && data->target > 0) ? std::floor(data->time / data->target) : 0;
+ }
+ bool JustHit() const {
+ return data && data->justHit;
+ }
+
+ void Reset() {
+ if (data) data->time = 0;
+ }
+ void Restart() {
+ if (data) {
+ if (data->target > 0 && data->justHit) {
+ data->time -= data->target;
+ } else {
+ data->time = 0;
+ }
+ }
+ }
private:
TimerData<Time> *data;
public:
void Update(Time delta) {
for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
+ if (i->target > 0) {
+ Time intervalTime(i->time);
+ while (intervalTime > i->target) intervalTime -= i->target;
+ i->justHit = intervalTime < i->target && intervalTime + delta >= i->target;
+ }
i->time += delta;
if (i->refCount <= 0) {
i = data.erase(i);