From b29e0a6f117b5efd5780dc6158ef96a1c051ddbf Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Thu, 22 Oct 2020 20:15:08 +0200 Subject: [PATCH 1/5] Refactor timer class --- CHANGELOG.md | 1 + doc/examples/particles/lib/main.dart | 2 +- doc/examples/timer/lib/main.dart | 10 +++--- doc/util.md | 33 ++++++++++------- lib/components/timer_component.dart | 4 +-- lib/particle.dart | 9 ++--- lib/time.dart | 53 --------------------------- lib/timer.dart | 54 ++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 79 deletions(-) delete mode 100644 lib/time.dart create mode 100644 lib/timer.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a306ed0e..d4676a79c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [next] - Improve IsometricTileMap and Spritesheet classes - Export full vector_math library from extension + - Refactor timer class ## 1.0.0-rc1 - Move all box2d related code and examples to the flame_box2d repo diff --git a/doc/examples/particles/lib/main.dart b/doc/examples/particles/lib/main.dart index 7ce44189f..ceb2dfd4d 100644 --- a/doc/examples/particles/lib/main.dart +++ b/doc/examples/particles/lib/main.dart @@ -19,7 +19,7 @@ import 'package:flame/particles/animation_particle.dart'; import 'package:flame/particles/component_particle.dart'; import 'package:flame/flame.dart'; import 'package:flame/game.dart'; -import 'package:flame/time.dart' as flame_time; +import 'package:flame/timer.dart' as flame_time; import 'package:flame/particle.dart'; import 'package:flame/extensions/vector2.dart'; import 'package:flame/sprite.dart'; diff --git a/doc/examples/timer/lib/main.dart b/doc/examples/timer/lib/main.dart index 2724cc803..accbae60c 100644 --- a/doc/examples/timer/lib/main.dart +++ b/doc/examples/timer/lib/main.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flame/game.dart'; -import 'package:flame/time.dart'; +import 'package:flame/timer.dart'; import 'package:flame/text_config.dart'; import 'package:flame/gestures.dart'; import 'package:flame/extensions/vector2.dart'; @@ -68,9 +68,11 @@ class MyGame extends Game with TapDetector { MyGame() { countdown = Timer(2); - interval = Timer(1, repeat: true, callback: () { - elapsedSecs += 1; - }); + interval = Timer( + 1, + callback: () => elapsedSecs += 1, + repeat: true, + ); interval.start(); } diff --git a/doc/util.md b/doc/util.md index 8e339d247..f3c5c8c82 100644 --- a/doc/util.md +++ b/doc/util.md @@ -59,7 +59,7 @@ import 'dart:ui'; import 'package:flame/game.dart'; import 'package:flame/text_config.dart'; -import 'package:flame/time.dart'; +import 'package:flame/timer.dart'; import 'package:flame/vector2.dart'; class MyGame extends Game { @@ -73,15 +73,18 @@ class MyGame extends Game { @override void update(double dt) { countdown.update(dt); - if (countdown.isFinished()) { - // do something ... + if (countdown.finished) { + // Prefer the timer callback, but this is better in some cases } } @override void render(Canvas canvas) { - textConfig.render(canvas, "Countdown: ${countdown.current.toString()}", - Vector2(10, 100)); + textConfig.render( + canvas, + "Countdown: ${countdown.current.toString()}", + Vector2(10, 100), + ); } } @@ -94,7 +97,7 @@ import 'dart:ui'; import 'package:flame/game.dart'; import 'package:flame/text_config.dart'; -import 'package:flame/time.dart'; +import 'package:flame/timer.dart'; import 'package:flame/vector2.dart'; class MyGame extends Game { @@ -104,9 +107,11 @@ class MyGame extends Game { int elapsedSecs = 0; MyGame() { - interval = Timer(1, repeat: true, callback: () { - elapsedSecs += 1; - }); + interval = Timer( + 1, + callback: () => elapsedSecs += 1, + repeat: true, + ); interval.start(); } @@ -128,7 +133,7 @@ Timer instances can also be used inside a `BaseGame` game by using the `TimerCom __Timer Component__ ```dart -import 'package:flame/time.dart'; +import 'package:flame/timer.dart'; import 'package:flame/components/timer_component.dart'; import 'package:flame/game.dart'; @@ -136,9 +141,11 @@ class MyBaseGame extends BaseGame { MyBaseGame() { add( TimerComponent( - Timer(10, repeat: true, callback: () { - print("10 seconds elapsed"); - }) + Timer( + 10, + callback: () => print("10 seconds elapsed"), + repeat: true, + ) ..start() ) ); diff --git a/lib/components/timer_component.dart b/lib/components/timer_component.dart index d485e2fe2..24312c38b 100644 --- a/lib/components/timer_component.dart +++ b/lib/components/timer_component.dart @@ -1,6 +1,6 @@ import 'dart:ui'; -import '../time.dart'; +import '../timer.dart'; import 'component.dart'; /// Simple component which wraps a [Timer] instance allowing it to be easily used inside a [BaseGame] game. @@ -19,5 +19,5 @@ class TimerComponent extends Component { void render(Canvas canvas) {} @override - bool destroy() => timer.isFinished(); + bool destroy() => timer.finished; } diff --git a/lib/particle.dart b/lib/particle.dart index 602e80576..c626dae76 100644 --- a/lib/particle.dart +++ b/lib/particle.dart @@ -12,7 +12,7 @@ import 'particles/moving_particle.dart'; import 'particles/rotating_particle.dart'; import 'particles/scaled_particle.dart'; import 'particles/translated_particle.dart'; -import 'time.dart'; +import 'timer.dart'; /// A function which returns [Particle] when called typedef ParticleGenerator = Particle Function(int); @@ -84,10 +84,6 @@ abstract class Particle { /// Marks [Particle] for destroy when it is over. void update(double dt) { _timer.update(dt); - - if (_timer.progress >= 1) { - _shouldBeDestroyed = true; - } } /// A control method allowing a parent of this [Particle] @@ -98,7 +94,8 @@ abstract class Particle { void setLifespan(double lifespan) { _lifespan = lifespan; _timer?.stop(); - _timer = Timer(lifespan); + final void Function() destroyCallback = () => _shouldBeDestroyed = true; + _timer = Timer(lifespan, callback: destroyCallback); _timer.start(); } diff --git a/lib/time.dart b/lib/time.dart deleted file mode 100644 index 1fa3df0f2..000000000 --- a/lib/time.dart +++ /dev/null @@ -1,53 +0,0 @@ -/// 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; - } - - bool isRunning() => _running; - - void start() { - _current = 0; - _running = true; - } - - void stop() { - _current = 0; - _running = false; - } - - /// A value between 0 and 1 indicating the timer progress - double get progress => _current / _limit; -} diff --git a/lib/timer.dart b/lib/timer.dart new file mode 100644 index 000000000..8d8824744 --- /dev/null +++ b/lib/timer.dart @@ -0,0 +1,54 @@ +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; + } +} From ce056e840a42bc2928a99e0eb185912e26f3abc3 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Thu, 22 Oct 2020 20:15:34 +0200 Subject: [PATCH 2/5] Formatting --- lib/timer.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/timer.dart b/lib/timer.dart index 8d8824744..75834f20f 100644 --- a/lib/timer.dart +++ b/lib/timer.dart @@ -34,7 +34,7 @@ class Timer { // 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) { + while (_current >= limit) { _current -= limit; callback?.call(); } From 3a88ccb48d796c0177dce802ab5fb4091a74c2ec Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Thu, 22 Oct 2020 22:44:41 -0300 Subject: [PATCH 3/5] Updating PR template to the v1 branch --- .github/pull_request_template.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index de1b30883..56364caf1 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -17,8 +17,8 @@ Please delete options that are not relevant. If something is unclear, please submit the PR anyways and ask about what you thought was unclear. -- [ ] This branch is based on `develop` -- [ ] This PR is targeted to merge into `develop` (not `master`) +- [ ] This branch is based on `v1.0.0` +- [ ] This PR is targeted to merge into `v1.0.0` (not `master` or `develop`) - [ ] I have added an entry under `[next]` in `CHANGELOG.md` - [ ] I have formatted my code with `flutter format` - [ ] I have made corresponding changes to the documentation From 181ac5ad17d9b7f969f08730466200fe9fac6057 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sat, 24 Oct 2020 13:35:45 +0200 Subject: [PATCH 5/5] Added from 19dbda53 --- lib/timer.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/timer.dart b/lib/timer.dart index 75834f20f..533737e09 100644 --- a/lib/timer.dart +++ b/lib/timer.dart @@ -51,4 +51,12 @@ class Timer { _current = 0; _running = false; } + + void pause() { + _running = false; + } + + void resume() { + _running = true; + } }