From bc3820c0fabfe0e2d9bd93b978ca0a124194d6a1 Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Sat, 3 Apr 2021 19:47:37 -0400 Subject: [PATCH] Make gameRef late (#742) --- .../lib/stories/components/composability.dart | 2 +- examples/lib/stories/components/debug.dart | 4 ++-- examples/lib/stories/controls/draggables.dart | 2 +- examples/lib/stories/parallax/component.dart | 2 +- .../sprites/spritebatch_auto_load.dart | 4 ++-- .../stories/utils/camera_and_viewport.dart | 4 ++-- packages/flame/CHANGELOG.md | 1 + .../lib/src/components/base_component.dart | 5 +++- .../components/joystick/joystick_action.dart | 4 ++-- .../joystick/joystick_directional.dart | 4 ++-- .../src/components/mixins/has_game_ref.dart | 23 ++++++++++++++++++- .../test/components/has_game_ref_test.dart | 2 +- 12 files changed, 41 insertions(+), 16 deletions(-) diff --git a/examples/lib/stories/components/composability.dart b/examples/lib/stories/components/composability.dart index 11fc40797..1761ccac3 100644 --- a/examples/lib/stories/components/composability.dart +++ b/examples/lib/stories/components/composability.dart @@ -1,7 +1,7 @@ import 'package:flame/components.dart'; import 'package:flame/game.dart'; -class Square extends PositionComponent with HasGameRef { +class Square extends PositionComponent { Square(Vector2 position, Vector2 size, {double angle = 0}) { this.position.setFrom(position); this.size.setFrom(size); diff --git a/examples/lib/stories/components/debug.dart b/examples/lib/stories/components/debug.dart index e9e649c36..9f51e4b01 100644 --- a/examples/lib/stories/components/debug.dart +++ b/examples/lib/stories/components/debug.dart @@ -20,14 +20,14 @@ class LogoCompomnent extends SpriteComponent with HasGameRef { final rect = toRect(); if ((x <= 0 && xDirection == -1) || - (rect.right >= gameRef!.size.x && xDirection == 1)) { + (rect.right >= gameRef.size.x && xDirection == 1)) { xDirection = xDirection * -1; } y += yDirection * speed * dt; if ((y <= 0 && yDirection == -1) || - (rect.bottom >= gameRef!.size.y && yDirection == 1)) { + (rect.bottom >= gameRef.size.y && yDirection == 1)) { yDirection = yDirection * -1; } } diff --git a/examples/lib/stories/controls/draggables.dart b/examples/lib/stories/controls/draggables.dart index be7f14a83..d029b387a 100644 --- a/examples/lib/stories/controls/draggables.dart +++ b/examples/lib/stories/controls/draggables.dart @@ -39,7 +39,7 @@ class DraggableSquare extends PositionComponent return false; } - final localCoords = gameRef!.convertGlobalToLocalCoordinate( + final localCoords = gameRef.convertGlobalToLocalCoordinate( details.globalPosition.toVector2(), ); position.setFrom(localCoords - dragDeltaPosition); diff --git a/examples/lib/stories/parallax/component.dart b/examples/lib/stories/parallax/component.dart index 289b09bbe..a790eba38 100644 --- a/examples/lib/stories/parallax/component.dart +++ b/examples/lib/stories/parallax/component.dart @@ -13,7 +13,7 @@ class MyParallaxComponent extends ParallaxComponent with HasGameRef { @override Future onLoad() async { - parallax = await gameRef!.loadParallax( + parallax = await gameRef.loadParallax( [ 'parallax/bg.png', 'parallax/mountain-far.png', diff --git a/examples/lib/stories/sprites/spritebatch_auto_load.dart b/examples/lib/stories/sprites/spritebatch_auto_load.dart index 29dd029f4..9e8e8f37f 100644 --- a/examples/lib/stories/sprites/spritebatch_auto_load.dart +++ b/examples/lib/stories/sprites/spritebatch_auto_load.dart @@ -9,7 +9,7 @@ class MySpriteBatchComponent extends SpriteBatchComponent with HasGameRef { @override Future onLoad() async { - final spriteBatch = await gameRef!.loadSpriteBatch('boom.png'); + final spriteBatch = await gameRef.loadSpriteBatch('boom.png'); this.spriteBatch = spriteBatch; spriteBatch.add( @@ -26,7 +26,7 @@ class MySpriteBatchComponent extends SpriteBatchComponent color: Colors.redAccent, ); - final size = gameRef!.size; + final size = gameRef.size; const num = 100; final r = Random(); for (var i = 0; i < num; ++i) { diff --git a/examples/lib/stories/utils/camera_and_viewport.dart b/examples/lib/stories/utils/camera_and_viewport.dart index 9ec665792..18b549a63 100644 --- a/examples/lib/stories/utils/camera_and_viewport.dart +++ b/examples/lib/stories/utils/camera_and_viewport.dart @@ -24,7 +24,7 @@ class MovableSquare extends SquareComponent timer = Timer(3.0) ..stop() ..callback = () { - gameRef!.camera.setRelativeOffset(Anchor.center.toVector2()); + gameRef.camera.setRelativeOffset(Anchor.center.toVector2()); }; } @@ -50,7 +50,7 @@ class MovableSquare extends SquareComponent @override void onCollision(Set points, Collidable other) { if (other is Rock) { - gameRef!.camera.setRelativeOffset(Anchor.topCenter.toVector2()); + gameRef.camera.setRelativeOffset(Anchor.topCenter.toVector2()); timer.start(); } } diff --git a/packages/flame/CHANGELOG.md b/packages/flame/CHANGELOG.md index 99dc5044e..26a23793e 100644 --- a/packages/flame/CHANGELOG.md +++ b/packages/flame/CHANGELOG.md @@ -16,6 +16,7 @@ - Revamp all the docs to be up to date with v1.0.0 - Make Assets and Images caches have a configurable prefix - Add `followVector2` method to the `Camera` + - Make `gameRef` late ## 1.0.0-rc8 - Migrate to null safety diff --git a/packages/flame/lib/src/components/base_component.dart b/packages/flame/lib/src/components/base_component.dart index ae8c06b80..3c108f8b4 100644 --- a/packages/flame/lib/src/components/base_component.dart +++ b/packages/flame/lib/src/components/base_component.dart @@ -143,7 +143,10 @@ abstract class BaseComponent extends Component { /// For children that don't need preparation from the game instance can /// disregard both the options given above. Future addChild(Component child, {Game? gameRef}) async { - gameRef ??= (this as HasGameRef).gameRef; + if (this is HasGameRef) { + final c = this as HasGameRef; + gameRef ??= c.hasGameRef ? c.gameRef : null; + } if (gameRef is BaseGame) { gameRef.prepare(child); } diff --git a/packages/flame/lib/src/components/joystick/joystick_action.dart b/packages/flame/lib/src/components/joystick/joystick_action.dart index 49a0ccd29..860149b1c 100644 --- a/packages/flame/lib/src/components/joystick/joystick_action.dart +++ b/packages/flame/lib/src/components/joystick/joystick_action.dart @@ -67,7 +67,7 @@ class JoystickAction extends BaseComponent with Draggable, HasGameRef { @override Future onLoad() async { - initialize(gameRef!.size); + initialize(gameRef.size); } @override @@ -188,7 +188,7 @@ class JoystickAction extends BaseComponent with Draggable, HasGameRef { @override bool onDragUpdate(int pointerId, DragUpdateDetails details) { if (_dragging) { - _dragPosition = gameRef!.convertGlobalToLocalCoordinate( + _dragPosition = gameRef.convertGlobalToLocalCoordinate( details.globalPosition.toVector2(), ); return true; diff --git a/packages/flame/lib/src/components/joystick/joystick_directional.dart b/packages/flame/lib/src/components/joystick/joystick_directional.dart index 23e8c2e2e..bdfceb23e 100644 --- a/packages/flame/lib/src/components/joystick/joystick_directional.dart +++ b/packages/flame/lib/src/components/joystick/joystick_directional.dart @@ -50,7 +50,7 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef { @override Future onLoad() async { - initialize(gameRef!.size); + initialize(gameRef.size); } @override @@ -163,7 +163,7 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef { @override bool onDragUpdate(int pointerId, DragUpdateDetails details) { if (_dragging) { - _dragPosition = gameRef!.convertGlobalToLocalCoordinate( + _dragPosition = gameRef.convertGlobalToLocalCoordinate( details.globalPosition.toVector2(), ); return false; diff --git a/packages/flame/lib/src/components/mixins/has_game_ref.dart b/packages/flame/lib/src/components/mixins/has_game_ref.dart index 4b3afd248..b27fe5466 100644 --- a/packages/flame/lib/src/components/mixins/has_game_ref.dart +++ b/packages/flame/lib/src/components/mixins/has_game_ref.dart @@ -1,5 +1,26 @@ +import '../../../components.dart'; import '../../game/game.dart'; mixin HasGameRef { - T? gameRef; + T? _gameRef; + + T get gameRef { + final ref = _gameRef; + if (ref == null) { + throw 'Accessing gameRef before the component was added to the game!'; + } + return ref; + } + + bool get hasGameRef => _gameRef != null; + + set gameRef(T gameRef) { + _gameRef = gameRef; + if (this is BaseComponent) { + (this as BaseComponent) + .children + .whereType>() + .forEach((e) => e.gameRef = gameRef); + } + } } diff --git a/packages/flame/test/components/has_game_ref_test.dart b/packages/flame/test/components/has_game_ref_test.dart index e1b024ed7..d81066169 100644 --- a/packages/flame/test/components/has_game_ref_test.dart +++ b/packages/flame/test/components/has_game_ref_test.dart @@ -11,7 +11,7 @@ class MyGame extends BaseGame { class MyComponent extends PositionComponent with HasGameRef { void foo() { - gameRef!.foo(); + gameRef.foo(); } }