From cd5ba3b906cfbad9b3e5fc29063a16c1b9bc7b7b Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Sun, 21 Jun 2020 10:18:04 -0300 Subject: [PATCH 1/7] Adding method to remove components from the list --- CHANGELOG.md | 1 + doc/game.md | 2 ++ example/.gitignore | 2 ++ example/lib/generated_plugin_registrant.dart | 15 -------- example/lib/main.dart | 37 +++++++++++++++----- lib/game/base_game.dart | 11 ++++++ 6 files changed, 44 insertions(+), 24 deletions(-) delete mode 100644 example/lib/generated_plugin_registrant.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 047aa4823..8f894b695 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ## [next] + - Adding BaseGame#remove ## 0.22.1 - Fix Box2DComponent render priority diff --git a/doc/game.md b/doc/game.md index 5d78cbdcc..86ce970fe 100644 --- a/doc/game.md +++ b/doc/game.md @@ -45,6 +45,8 @@ A very simple `BaseGame` implementation example can be seen below: } ``` +To remove components from the list on a `BaseGame` the `remove` can be used. + ## Flutter Widgets and Game instances Since a Flame game is a widget itself, it is quite easy to use Flutter widgets and Flame game together. But to make it even easier, Flame provides a `mixin` called `HasWidgetsOverlay` which will enable any Flutter widget to be show on top of your game instance, this makes it very easy to create things like a pause menu, or an inventory screen for example. diff --git a/example/.gitignore b/example/.gitignore index 9fd4653bf..60b415cb5 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -74,3 +74,5 @@ build/ macos test +web +lib/generated_plugin_registrant.dart diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart deleted file mode 100644 index 2fe34b2c2..000000000 --- a/example/lib/generated_plugin_registrant.dart +++ /dev/null @@ -1,15 +0,0 @@ -// -// Generated file. Do not edit. -// - -// ignore: unused_import -import 'dart:ui'; - -import 'package:audioplayers/audioplayers_web.dart'; - -import 'package:flutter_web_plugins/flutter_web_plugins.dart'; - -void registerPlugins(PluginRegistry registry) { - AudioplayersPlugin.registerWith(registry.registrarFor(AudioplayersPlugin)); - registry.registerMessageHandler(); -} diff --git a/example/lib/main.dart b/example/lib/main.dart index 185e9e6c5..ec4a7a710 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -24,12 +24,6 @@ class Palette { class Square extends PositionComponent with HasGameRef { static const SPEED = 0.25; - @override - void resize(Size size) { - x = size.width / 2; - y = size.height / 2; - } - @override void render(Canvas c) { prepareCanvas(c); @@ -53,16 +47,41 @@ class Square extends PositionComponent with HasGameRef { } } -class MyGame extends BaseGame with TapDetector { +class MyGame extends BaseGame with DoubleTapDetector, TapDetector { final double squareSize = 128; bool running = true; MyGame() { - add(Square()); + add(Square() + ..x = 100 + ..y = 100); } @override - void onTap() { + void onTapUp(details) { + final touchArea = Rect.fromCenter( + center: details.localPosition, + width: 20, + height: 20, + ); + + bool handled = false; + components.forEach((c) { + if (c is PositionComponent && c.toRect().overlaps(touchArea)) { + handled = true; + remove(c); + } + }); + + if (!handled) { + addLater(Square() + ..x = touchArea.left + ..y = touchArea.top); + } + } + + @override + void onDoubleTap() { if (running) { pauseEngine(); } else { diff --git a/lib/game/base_game.dart b/lib/game/base_game.dart index 0e22a95dd..86e020a73 100644 --- a/lib/game/base_game.dart +++ b/lib/game/base_game.dart @@ -27,6 +27,9 @@ class BaseGame extends Game { /// Components added by the [addLater] method final List _addLater = []; + /// Components to be removed on the next update + final List _removeLater = []; + /// Current screen size, updated every resize via the [resize] method hook Size size; @@ -86,6 +89,11 @@ class BaseGame extends Game { _addLater.add(c); } + /// Removes a component from the components list + void remove(Component c) { + _removeLater.add(c); + } + /// This implementation of render basically calls [renderComponent] for every component, making sure the canvas is reset for each one. /// /// You can override it further to add more custom behaviour. @@ -119,6 +127,9 @@ class BaseGame extends Game { /// You can override it further to add more custom behaviour. @override void update(double t) { + _removeLater.forEach((c) => components.remove(c)); + _removeLater.clear(); + components.addAll(_addLater); _addLater.clear(); From 3511e8dfd2c075068b562810c67bb63f4352ed57 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Sun, 21 Jun 2020 13:33:09 -0300 Subject: [PATCH 2/7] Moving to flutter analyze instead of dartanalayzer, which was causing mixed results --- scripts/lint.sh | 4 ++-- test/has_game_ref_test.dart | 3 --- test/resizable_test.dart | 3 --- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/scripts/lint.sh b/scripts/lint.sh index 516918f4d..d520584aa 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -7,7 +7,7 @@ if [[ $(flutter format -n .) ]]; then fi flutter pub get -result=$(dartanalyzer lib/) +result=$(flutter analyze .) if ! echo "$result" | grep -q "No issues found!"; then echo "$result" echo "dartanalyzer issue: lib" @@ -17,7 +17,7 @@ fi analyzer() { cd $1 flutter pub get - result=$(dartanalyzer .) + result=$(flutter analyze .) if ! echo "$result" | grep -q "No issues found!"; then echo "$result" echo "dartanalyzer issue: $1" diff --git a/test/has_game_ref_test.dart b/test/has_game_ref_test.dart index d9f241d5c..8e68f8921 100644 --- a/test/has_game_ref_test.dart +++ b/test/has_game_ref_test.dart @@ -14,9 +14,6 @@ class MyGame extends BaseGame { } class MyComponent extends PositionComponent with HasGameRef { - @override - void update(double dt) {} - @override void render(Canvas c) {} diff --git a/test/resizable_test.dart b/test/resizable_test.dart index c4d427db1..660e24fda 100644 --- a/test/resizable_test.dart +++ b/test/resizable_test.dart @@ -15,9 +15,6 @@ class MyComponent extends PositionComponent with Resizable { @override Iterable resizableChildren() => myChildren; - @override - void update(double dt) {} - @override void render(Canvas c) {} } From 7707e89fbc5b16635509f18d05e6b6f192462aee Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Sun, 21 Jun 2020 13:55:42 -0300 Subject: [PATCH 3/7] Simplifying linter --- scripts/lint.sh | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/scripts/lint.sh b/scripts/lint.sh index d520584aa..13a4ea9be 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -7,12 +7,14 @@ if [[ $(flutter format -n .) ]]; then fi flutter pub get -result=$(flutter analyze .) -if ! echo "$result" | grep -q "No issues found!"; then - echo "$result" - echo "dartanalyzer issue: lib" - exit 1 -fi + +# We need to run pubget on all the examples +for f in doc/examples/**/pubspec.yaml; do + d=$(dirname $f) + cd $d + flutter pub get + cd - > /dev/null +done analyzer() { cd $1 @@ -26,31 +28,7 @@ analyzer() { cd - > /dev/null } -analyzer "example" - -# Examples that are changed -changed=$(git diff --name-only origin/develop doc/examples \ - | xargs -I {} dirname {} | sed 's/\/lib$//' | uniq \ - | xargs -I {} find {} -name pubspec.yaml | xargs -I {} dirname {}) - -# Examples that are affected by changed code -affected=$(git diff --name-only origin/develop lib/ \ - | xargs -I {} basename {} | xargs -I {} grep -r -l --include \*.dart {} doc/examples/ \ - | xargs -I {} dirname {} | sed 's/\/lib$//' | uniq \ - | xargs -I {} find {} -name pubspec.yaml | xargs -I {} dirname {}) - -both=("${changed[@]}" "${affected[@]}") -lint_examples=$(printf "%s\n" "${both[@]}" | sort -u) -for d in $lint_examples; do - analyzer $d -done - -for f in doc/examples/**/pubspec.yaml; do - d=$(dirname $f) - if [[ ! " ${lint_examples[@]} " =~ " ${d} " ]]; then - analyzer $d - fi -done +analyzer . echo "success" exit 0 From 72ba4784120e8e1e0c9338a8845266c3f4ec59d7 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 23 Jun 2020 18:40:21 -0300 Subject: [PATCH 4/7] Adding remove to composed component --- lib/components/composed_component.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/components/composed_component.dart b/lib/components/composed_component.dart index ee7c3656d..74109923f 100644 --- a/lib/components/composed_component.dart +++ b/lib/components/composed_component.dart @@ -41,6 +41,8 @@ mixin ComposedComponent on Component, HasGameRef, Tapable { OrderedSet components = OrderedSet(Comparing.on((c) => c.priority())); + final List _removeLater = []; + @override void render(Canvas canvas) { canvas.save(); @@ -59,6 +61,9 @@ mixin ComposedComponent on Component, HasGameRef, Tapable { @override void update(double t) { + _removeLater.forEach((c) => components.remove(c)); + _removeLater.clear(); + components.forEach((c) => c.update(t)); components.removeWhere((c) => c.destroy()); } @@ -70,6 +75,10 @@ mixin ComposedComponent on Component, HasGameRef, Tapable { components.add(c); } + void remove(Component component) { + _removeLater.add(component); + } + // this is an important override for the Tapable mixin @override Iterable tapableChildren() => _findT(); From 48856815db0b36a94160dde53a860d96cced8992 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 23 Jun 2020 18:42:27 -0300 Subject: [PATCH 5/7] Fixing output message --- scripts/lint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint.sh b/scripts/lint.sh index 13a4ea9be..f273745d0 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -22,7 +22,7 @@ analyzer() { result=$(flutter analyze .) if ! echo "$result" | grep -q "No issues found!"; then echo "$result" - echo "dartanalyzer issue: $1" + echo "flutter analyze issue: $1" exit 1 fi cd - > /dev/null From 339ed06c3d0e598776895f6e31dcc8b8d46c43f9 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 23 Jun 2020 18:52:52 -0300 Subject: [PATCH 6/7] Renaming remove to markToRemove --- CHANGELOG.md | 2 +- doc/game.md | 2 +- example/lib/main.dart | 2 +- lib/components/composed_component.dart | 2 +- lib/game/base_game.dart | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9705abfb4..a9cb2d0aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # CHANGELOG ## [next] - - Adding BaseGame#remove + - Adding BaseGame#markToRemove - Upgrade tiled and flutter_svg dependencies ## 0.22.1 diff --git a/doc/game.md b/doc/game.md index 86ce970fe..a1097bca4 100644 --- a/doc/game.md +++ b/doc/game.md @@ -45,7 +45,7 @@ A very simple `BaseGame` implementation example can be seen below: } ``` -To remove components from the list on a `BaseGame` the `remove` can be used. +To remove components from the list on a `BaseGame` the `markToRemove` method can be used. ## Flutter Widgets and Game instances diff --git a/example/lib/main.dart b/example/lib/main.dart index ec4a7a710..fa70e444b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -69,7 +69,7 @@ class MyGame extends BaseGame with DoubleTapDetector, TapDetector { components.forEach((c) { if (c is PositionComponent && c.toRect().overlaps(touchArea)) { handled = true; - remove(c); + markToRemove(c); } }); diff --git a/lib/components/composed_component.dart b/lib/components/composed_component.dart index 74109923f..e536c2c0f 100644 --- a/lib/components/composed_component.dart +++ b/lib/components/composed_component.dart @@ -75,7 +75,7 @@ mixin ComposedComponent on Component, HasGameRef, Tapable { components.add(c); } - void remove(Component component) { + void markToRemove(Component component) { _removeLater.add(component); } diff --git a/lib/game/base_game.dart b/lib/game/base_game.dart index 86e020a73..c8246a7e9 100644 --- a/lib/game/base_game.dart +++ b/lib/game/base_game.dart @@ -89,8 +89,8 @@ class BaseGame extends Game { _addLater.add(c); } - /// Removes a component from the components list - void remove(Component c) { + /// Marks a component to be removed from the components list on the next game loop cycle + void markToRemove(Component c) { _removeLater.add(c); } From e43e6817fcb8eeeae67c7be60bb1c8e04dc92d1c Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 23 Jun 2020 19:38:24 -0300 Subject: [PATCH 7/7] PR nits --- scripts/lint.sh | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/scripts/lint.sh b/scripts/lint.sh index f273745d0..ad561415e 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -16,19 +16,14 @@ for f in doc/examples/**/pubspec.yaml; do cd - > /dev/null done -analyzer() { - cd $1 - flutter pub get - result=$(flutter analyze .) - if ! echo "$result" | grep -q "No issues found!"; then - echo "$result" - echo "flutter analyze issue: $1" - exit 1 - fi - cd - > /dev/null -} - -analyzer . +cd . +flutter pub get +result=$(flutter analyze .) +if ! echo "$result" | grep -q "No issues found!"; then + echo "$result" + echo "flutter analyze issue: $1" + exit 1 +fi echo "success" exit 0