diff --git a/lib/game/components/bonus_word.dart b/lib/game/components/bonus_word.dart index cc6391e8..b7c01074 100644 --- a/lib/game/components/bonus_word.dart +++ b/lib/game/components/bonus_word.dart @@ -20,13 +20,9 @@ class BonusWord extends Component with BlocComponent { @override bool listenWhen(GameState? previousState, GameState newState) { - if ((previousState?.bonusHistory.length ?? 0) < + return (previousState?.bonusHistory.length ?? 0) < newState.bonusHistory.length && - newState.bonusHistory.last == GameBonus.word) { - return true; - } - - return false; + newState.bonusHistory.last == GameBonus.word; } @override diff --git a/lib/game/components/flutter_forest.dart b/lib/game/components/flutter_forest.dart index fb5672ef..6095f3f2 100644 --- a/lib/game/components/flutter_forest.dart +++ b/lib/game/components/flutter_forest.dart @@ -13,7 +13,8 @@ import 'package:pinball/game/game.dart'; /// is awarded, and the [_BigDashNestBumper] releases a new [Ball]. /// {@endtemplate} // TODO(alestiago): Make a [Blueprint] once nesting [Blueprint] is implemented. -class FlutterForest extends Component with HasGameRef { +class FlutterForest extends Component + with HasGameRef, BlocComponent { /// {@macro flutter_forest} FlutterForest({ required this.position, @@ -22,6 +23,19 @@ class FlutterForest extends Component with HasGameRef { /// The position of the [FlutterForest] on the [Board]. final Vector2 position; + @override + bool listenWhen(GameState? previousState, GameState newState) { + return (previousState?.bonusHistory.length ?? 0) < + newState.bonusHistory.length && + newState.bonusHistory.last == GameBonus.dashNest; + } + + @override + void onNewState(GameState state) { + super.onNewState(state); + gameRef.add(Ball()..initialPosition = position); + } + @override Future onLoad() async { gameRef.addContactCallback(_DashNestBumperBallContactCallback()); @@ -48,7 +62,7 @@ class FlutterForest extends Component with HasGameRef { /// When hit, the [GameState.score] is increased. /// {@endtemplate} abstract class _DashNestBumper extends BodyComponent - with BlocComponent, ScorePoints, InitialPosition { + with ScorePoints, InitialPosition { /// {@template dash_nest_bumper} _DashNestBumper({required this.id}); diff --git a/test/game/components/flutter_forest_test.dart b/test/game/components/flutter_forest_test.dart index 7d7dcc09..005f853e 100644 --- a/test/game/components/flutter_forest_test.dart +++ b/test/game/components/flutter_forest_test.dart @@ -5,11 +5,13 @@ import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball/game/game.dart'; +import '../../helpers/helpers.dart'; + void main() { TestWidgetsFlutterBinding.ensureInitialized(); group('FlutterForest', () { - final flameTester = FlameTester(Forge2DGame.new); + final flameTester = FlameTester(PinballGameTest.create); flameTester.test( 'loads correctly', @@ -21,5 +23,23 @@ void main() { expect(game.contains(flutterForest), isTrue); }, ); + + flameTester.test( + 'adds a new ball onNewState', + (game) async { + await game.ready(); + final flutterForest = FlutterForest(position: Vector2(0, 0)); + await game.ensureAdd(flutterForest); + + final previousBalls = game.descendants().whereType().length; + flutterForest.onNewState(MockGameState()); + await game.ready(); + + expect( + game.descendants().whereType().length, + greaterThan(previousBalls), + ); + }, + ); }); }