mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
Implementing Timer and its examples
This commit is contained in:
@ -1,19 +1,36 @@
|
||||
|
||||
/// Simple utility class that helps handling time counting and implementing interval like events.
|
||||
///
|
||||
class Timer {
|
||||
final double _limit;
|
||||
void Function() _callback;
|
||||
double _current;
|
||||
bool _repeat;
|
||||
double _current = 0;
|
||||
bool _running = false;
|
||||
|
||||
Timer(this._limit, this._callback);
|
||||
Timer(this._limit, { repeat = false, callback }) {
|
||||
_repeat = repeat;
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
double get current {
|
||||
return _current;
|
||||
}
|
||||
|
||||
void update(double dt) {
|
||||
if (_running) {
|
||||
_current += dt;
|
||||
|
||||
if (isFinished()) {
|
||||
_running = false;
|
||||
_callback();
|
||||
if (_repeat) {
|
||||
_current -= _limit;
|
||||
} else {
|
||||
_running = false;
|
||||
}
|
||||
|
||||
if (_callback != null) {
|
||||
_callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -26,7 +43,7 @@ class Timer {
|
||||
_current = 0;
|
||||
_running = true;
|
||||
}
|
||||
|
||||
|
||||
void stop() {
|
||||
_current = 0;
|
||||
_running = false;
|
||||
|
||||
Reference in New Issue
Block a user