| 61 | } |
| 62 | |
| 63 | void Timer::update(float dt) |
| 64 | { |
| 65 | if (_elapsed == -1) |
| 66 | { |
| 67 | _elapsed = 0; |
| 68 | _timesExecuted = 0; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // accumulate elapsed time |
| 73 | _elapsed += dt; |
| 74 | |
| 75 | // deal with delay |
| 76 | if (_useDelay) |
| 77 | { |
| 78 | if (_elapsed < _delay) |
| 79 | { |
| 80 | return; |
| 81 | } |
| 82 | _timesExecuted += 1; // important to increment before call trigger |
| 83 | trigger(_delay); |
| 84 | _elapsed = _elapsed - _delay; |
| 85 | _useDelay = false; |
| 86 | // after delay, the rest time should compare with interval |
| 87 | if (isExhausted()) |
| 88 | { // unschedule timer |
| 89 | cancel(); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // if _interval == 0, should trigger once every frame |
| 95 | float interval = (_interval > 0) ? _interval : _elapsed; |
| 96 | while ((_elapsed >= interval) && !_aborted) |
| 97 | { |
| 98 | _timesExecuted += 1; // important to increment before call trigger |
| 99 | trigger(interval); |
| 100 | _elapsed -= interval; |
| 101 | |
| 102 | if (isExhausted()) |
| 103 | { |
| 104 | cancel(); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | if (_elapsed <= 0.f) |
| 109 | { |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool Timer::isExhausted() const |
| 116 | { |