From 12ebb53eb017cab286d60c5aac1c10f7d2d8b38a Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 6 Apr 2022 12:39:54 +0100 Subject: [PATCH] refactor: refined logic --- lib/game/pinball_game.dart | 8 ++--- test/game/pinball_game_test.dart | 54 ++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 37fae70b..7ab91477 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -99,12 +99,10 @@ class _GameBallsController extends ComponentController @override bool listenWhen(GameState? previousState, GameState newState) { - final previousBalls = - (previousState?.balls ?? 0) + (previousState?.bonusBalls ?? 0); - final currentBalls = newState.balls + newState.bonusBalls; - final canBallRespawn = newState.balls > 0 && newState.bonusBalls == 0; + final noBallsLeft = component.descendants().whereType().isEmpty; + final canBallRespawn = newState.balls > 0; - return previousBalls != currentBalls && canBallRespawn; + return noBallsLeft && canBallRespawn; } @override diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index 137b4b34..839d58bf 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -1,5 +1,7 @@ // ignore_for_file: cascade_invocations +import 'dart:math'; + import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flame_test/flame_test.dart'; @@ -66,42 +68,48 @@ void main() { // TODO(alestiago): Write test to be controller agnostic. group('listenWhen', () { flameTester.test( - 'listens when a ball is lost and no bonus balls', + 'listens when all balls are gone and there are more than 0 balls', (game) async { - const previousState = GameState( - score: 0, - balls: 3, - bonusBalls: 0, - activatedBonusLetters: [], - bonusHistory: [], - activatedDashNests: {}, - ); - final newState = - previousState.copyWith(balls: previousState.balls - 1); + final newState = MockGameState(); + when(() => newState.balls).thenReturn(2); + game.descendants().whereType().forEach(game.remove); + await game.ready(); expect( - game.controller.listenWhen(previousState, newState), + game.controller.listenWhen(MockGameState(), newState), isTrue, ); }, ); flameTester.test( - "doesn't listen when a ball is lost and has bonus balls", + "doesn't listen when some balls are left", (game) async { - const previousState = GameState( - score: 0, - balls: 3, - bonusBalls: 1, - activatedBonusLetters: [], - bonusHistory: [], - activatedDashNests: {}, + final newState = MockGameState(); + when(() => newState.balls).thenReturn(2); + + expect( + game.descendants().whereType().length, + greaterThan(0), + ); + expect( + game.controller.listenWhen(MockGameState(), newState), + isFalse, ); - final newState = - previousState.copyWith(balls: previousState.balls - 1); + }, + ); + + flameTester.test( + "doesn't listen when no balls left", + (game) async { + final newState = MockGameState(); + when(() => newState.balls).thenReturn(0); + + game.descendants().whereType().forEach(game.remove); + await game.ready(); expect( - game.controller.listenWhen(previousState, newState), + game.controller.listenWhen(MockGameState(), newState), isFalse, ); },