refactor: refined logic

pull/151/head
alestiago 4 years ago
parent b5aef4640c
commit 12ebb53eb0

@ -99,12 +99,10 @@ class _GameBallsController extends ComponentController<PinballGame>
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
final previousBalls = final noBallsLeft = component.descendants().whereType<Ball>().isEmpty;
(previousState?.balls ?? 0) + (previousState?.bonusBalls ?? 0); final canBallRespawn = newState.balls > 0;
final currentBalls = newState.balls + newState.bonusBalls;
final canBallRespawn = newState.balls > 0 && newState.bonusBalls == 0;
return previousBalls != currentBalls && canBallRespawn; return noBallsLeft && canBallRespawn;
} }
@override @override

@ -1,5 +1,7 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'dart:math';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
@ -66,42 +68,48 @@ void main() {
// TODO(alestiago): Write test to be controller agnostic. // TODO(alestiago): Write test to be controller agnostic.
group('listenWhen', () { group('listenWhen', () {
flameTester.test( 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 { (game) async {
const previousState = GameState( final newState = MockGameState();
score: 0, when(() => newState.balls).thenReturn(2);
balls: 3, game.descendants().whereType<Ball>().forEach(game.remove);
bonusBalls: 0, await game.ready();
activatedBonusLetters: [],
bonusHistory: [],
activatedDashNests: {},
);
final newState =
previousState.copyWith(balls: previousState.balls - 1);
expect( expect(
game.controller.listenWhen(previousState, newState), game.controller.listenWhen(MockGameState(), newState),
isTrue, isTrue,
); );
}, },
); );
flameTester.test( flameTester.test(
"doesn't listen when a ball is lost and has bonus balls", "doesn't listen when some balls are left",
(game) async { (game) async {
const previousState = GameState( final newState = MockGameState();
score: 0, when(() => newState.balls).thenReturn(2);
balls: 3,
bonusBalls: 1, expect(
activatedBonusLetters: [], game.descendants().whereType<Ball>().length,
bonusHistory: [], greaterThan(0),
activatedDashNests: {}, );
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<Ball>().forEach(game.remove);
await game.ready();
expect( expect(
game.controller.listenWhen(previousState, newState), game.controller.listenWhen(MockGameState(), newState),
isFalse, isFalse,
); );
}, },

Loading…
Cancel
Save