diff --git a/lib/flame/extensions.dart b/lib/flame/blueprint.dart similarity index 90% rename from lib/flame/extensions.dart rename to lib/flame/blueprint.dart index 0197a237..9f2a68f6 100644 --- a/lib/flame/extensions.dart +++ b/lib/flame/blueprint.dart @@ -23,9 +23,9 @@ abstract class Blueprint { /// Attach the [Component]s built on [build] to the [game] /// instance @mustCallSuper - void attach(FlameGame game) { + Future attach(FlameGame game) async { build(); - game.addAll(_components); + await game.addAll(_components); _isAttached = true; } @@ -63,8 +63,8 @@ abstract class Forge2DBlueprint extends Blueprint { } @override - void attach(FlameGame game) { - super.attach(game); + Future attach(FlameGame game) async { + await super.attach(game); assert(game is Forge2DGame, 'Forge2DBlueprint used outside a Forge2DGame'); @@ -81,7 +81,7 @@ abstract class Forge2DBlueprint extends Blueprint { extension FlameGameBlueprint on FlameGame { /// Shortcut to attach a [Blueprint] instance to this game /// equivalent to `MyBluepinrt().attach(game)` - void addFromBlueprint(Blueprint blueprint) { - blueprint.attach(this); + Future addFromBlueprint(Blueprint blueprint) async { + await blueprint.attach(this); } } diff --git a/lib/game/components/spaceship.dart b/lib/game/components/spaceship.dart index c4a7d0ca..c6b1bb6d 100644 --- a/lib/game/components/spaceship.dart +++ b/lib/game/components/spaceship.dart @@ -5,7 +5,7 @@ import 'dart:math'; import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:pinball/flame/extensions.dart'; +import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/game/game.dart'; // TODO(erickzanardo): change this to use the layer class @@ -20,6 +20,12 @@ class Spaceship extends Forge2DBlueprint { @override void build() { final position = Vector2(20, -24); + + addAllContactCallback([ + SpaceshipHoleBallContactCallback(), + SpaceshipEntranceBallContactCallback(), + ]); + addAll([ SpaceshipSaucer()..initialPosition = position, SpaceshipEntrance()..initialPosition = position, @@ -29,11 +35,6 @@ class Spaceship extends Forge2DBlueprint { SpaceshipHole()..initialPosition = position - Vector2(-5, 4), SpaceshipWall()..initialPosition = position, ]); - - addAllContactCallback([ - SpaceshipHoleBallContactCallback(), - SpaceshipEntranceBallContactCallback(), - ]); } } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 1065172f..ef5cb3b1 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -4,7 +4,7 @@ import 'dart:async'; import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:pinball/flame/extensions.dart'; +import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_theme/pinball_theme.dart'; @@ -30,7 +30,7 @@ class PinballGame extends Forge2DGame unawaited(_addPlunger()); unawaited(_addPaths()); - addFromBlueprint(Spaceship()); + unawaited(addFromBlueprint(Spaceship())); // Corner wall above plunger so the ball deflects into the rest of the // board. diff --git a/test/flame/extensions_test.dart b/test/flame/blueprint_test.dart similarity index 88% rename from test/flame/extensions_test.dart rename to test/flame/blueprint_test.dart index 05fd4e29..e521a83c 100644 --- a/test/flame/extensions_test.dart +++ b/test/flame/blueprint_test.dart @@ -2,7 +2,7 @@ import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:pinball/flame/extensions.dart'; +import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/game/game.dart'; import '../helpers/helpers.dart'; @@ -41,10 +41,11 @@ void main() { test( 'throws assertion error when adding to an already attached blueprint', - () { + () async { final mockGame = MockPinballGame(); when(() => mockGame.addAll(any())).thenAnswer((_) async {}); - final blueprint = MyBlueprint()..attach(mockGame); + final blueprint = MyBlueprint(); + await blueprint.attach(mockGame); expect(() => blueprint.add(Component()), throwsAssertionError); expect(() => blueprint.addAll([Component()]), throwsAssertionError); @@ -63,22 +64,23 @@ void main() { expect(blueprint.callbacks.length, equals(3)); }); - test('adds the callbacks to a game on attach', () { + test('adds the callbacks to a game on attach', () async { final mockGame = MockPinballGame(); when(() => mockGame.addAll(any())).thenAnswer((_) async {}); when(() => mockGame.addContactCallback(any())).thenAnswer((_) async {}); - MyForge2dBlueprint().attach(mockGame); + await MyForge2dBlueprint().attach(mockGame); verify(() => mockGame.addContactCallback(any())).called(3); }); test( 'throws assertion error when adding to an already attached blueprint', - () { + () async { final mockGame = MockPinballGame(); when(() => mockGame.addAll(any())).thenAnswer((_) async {}); when(() => mockGame.addContactCallback(any())).thenAnswer((_) async {}); - final blueprint = MyForge2dBlueprint()..attach(mockGame); + final blueprint = MyForge2dBlueprint(); + await blueprint.attach(mockGame); expect( () => blueprint.addContactCallback(MockContactCallback()),