From: Daniel Karbach Date: Sun, 12 Aug 2012 14:06:23 +0000 (+0200) Subject: added repetition awareness in timers X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=64c314421a700696260d73395776634dc64fc46c;p=l2e.git added repetition awareness in timers --- diff --git a/src/app/Timer.h b/src/app/Timer.h index 70b49b0..750f3ad 100644 --- a/src/app/Timer.h +++ b/src/app/Timer.h @@ -17,13 +17,14 @@ namespace app { template struct TimerData { - TimerData() : time(0), target(0), refCount(0), justHit(false) { } - TimerData(Time target) : time(0), target(target), refCount(0), justHit(false) { } + TimerData() : time(0), target(0), refCount(0), justHit(false), repeat(false) { } + TimerData(Time target, bool repeat) : time(0), target(target), refCount(0), justHit(false), repeat(repeat) { } Time time; Time target; int refCount; bool justHit; + bool repeat; }; @@ -47,11 +48,11 @@ public: } public: - bool Running() const { - return data; - } bool Finished() const { - return data ? data->time >= data->target : false; + return data && data->target != Time() && !data->repeat && data->time >= data->target; + } + bool Running() const { + return data && !Finished(); } Time Elapsed() const { return data ? data->time : Time(); @@ -60,7 +61,7 @@ public: return data ? (data->target - data->time) : Time(); } int Iteration() const { - return (data && data->target > 0) ? std::floor(data->time / data->target) : 0; + return (data && data->target > Time()) ? std::floor(data->time / data->target) : Time(); } bool JustHit() const { return data && data->justHit; @@ -73,14 +74,14 @@ public: } } void Reset() { - if (data) data->time = 0; + if (data) data->time = Time(); } void Restart() { if (data) { - if (data->target > 0 && data->justHit) { + if (data->target > Time() && data->justHit) { data->time -= data->target; } else { - data->time = 0; + data->time = Time(); } } } @@ -102,7 +103,7 @@ public: for (typename std::list >::iterator i(data.begin()), end(data.end()); i != end;) { if (i->target > 0) { Time intervalTime(i->time); - while (intervalTime > i->target) intervalTime -= i->target; + if (i->repeat) while (intervalTime > i->target) intervalTime -= i->target; i->justHit = intervalTime < i->target && intervalTime + delta >= i->target; } i->time += delta; @@ -118,11 +119,11 @@ public: return Timer