forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.dart
62 lines (51 loc) · 1.39 KB
/
timer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'dart:math';
/// Simple utility class that helps handling time counting and implementing
/// interval like events.
class Timer {
final double limit;
void Function() callback;
bool repeat;
double _current = 0;
bool _running = false;
Timer(this.limit, {this.callback, this.repeat = false});
/// The current amount of ms that has passed on this iteration
double get current => _current;
/// If the timer is finished, timers that repeat never finish
bool get finished => _current >= limit && !repeat;
/// Whether the timer is running or not
bool isRunning() => _running;
/// A value between 0.0 and 1.0 indicating the timer progress
double get progress => max(_current / limit, 1.0);
void update(double dt) {
if (_running) {
_current += dt;
if (_current >= limit) {
if (!repeat) {
_running = false;
return;
}
// This is used to cover the rare case of _current being more than
// two times the value of limit, so that the callback is called the
// correct number of times
while (_current >= limit) {
_current -= limit;
callback?.call();
}
}
}
}
void start() {
_current = 0;
_running = true;
}
void stop() {
_current = 0;
_running = false;
}
void pause() {
_running = false;
}
void resume() {
_running = true;
}
}