Files
flame/lib/time.dart
erickzanardo 1301fff443 PR suggestions
2019-06-28 15:47:31 -03:00

50 lines
867 B
Dart

/// 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, { bool repeat = false, void Function() callback }) {
_repeat = repeat;
_callback = callback;
}
double get current => _current;
void update(double dt) {
if (_running) {
_current += dt;
if (isFinished()) {
if (_repeat) {
_current -= _limit;
} else {
_running = false;
}
if (_callback != null) {
_callback();
}
}
}
}
bool isFinished() {
return _current >= _limit;
}
void start() {
_current = 0;
_running = true;
}
void stop() {
_current = 0;
_running = false;
}
}