]> git.localhorst.tv Git - l2e.git/commitdiff
added hit tracking to timers
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 10 Aug 2012 20:43:03 +0000 (22:43 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 10 Aug 2012 20:43:03 +0000 (22:43 +0200)
src/app/Timer.h

index 465ed64e45edc14a13faa7f462b70e35275e83a1..0a085418739be44a53c24e83281de511d201a394 100644 (file)
 
 #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;
 
 };
 
@@ -57,6 +59,25 @@ public:
        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;
@@ -73,6 +94,11 @@ public:
 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);