From 1ebea02742145a20362f7524d6f827b68d7788b4 Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 6 Apr 2022 12:44:52 +0100 Subject: [PATCH] refactor: simplified logic --- lib/game/bloc/game_bloc.dart | 9 ---- lib/game/bloc/game_state.dart | 16 +----- lib/game/components/controlled_ball.dart | 12 +---- lib/game/components/flutter_forest.dart | 6 +-- test/game/bloc/game_bloc_test.dart | 51 ------------------- test/game/bloc/game_event_test.dart | 13 ----- test/game/bloc/game_state_test.dart | 32 ------------ test/game/components/bonus_word_test.dart | 3 -- test/game/components/flutter_forest_test.dart | 1 - test/game/pinball_game_test.dart | 2 - test/game/view/game_hud_test.dart | 1 - test/game/view/pinball_game_page_test.dart | 1 - 12 files changed, 3 insertions(+), 144 deletions(-) diff --git a/lib/game/bloc/game_bloc.dart b/lib/game/bloc/game_bloc.dart index 669d09e4..67e19651 100644 --- a/lib/game/bloc/game_bloc.dart +++ b/lib/game/bloc/game_bloc.dart @@ -10,7 +10,6 @@ part 'game_state.dart'; class GameBloc extends Bloc { GameBloc() : super(const GameState.initial()) { on(_onBallLost); - on(_onBonusBallLost); on(_onScored); on(_onBonusLetterActivated); on(_onDashNestActivated); @@ -23,10 +22,6 @@ class GameBloc extends Bloc { emit(state.copyWith(balls: state.balls - 1)); } - void _onBonusBallLost(BonusBallLost event, Emitter emit) { - emit(state.copyWith(bonusBalls: state.bonusBalls - 1)); - } - void _onScored(Scored event, Emitter emit) { if (!state.isGameOver) { emit(state.copyWith(score: state.score + event.points)); @@ -68,10 +63,6 @@ class GameBloc extends Bloc { if (achievedBonus) { emit( state.copyWith( - // TODO(alestiago): Question if we should have a private event - // _onBonusBallActivated() to avoid the duplication of code and - // split the logic. - bonusBalls: state.bonusBalls + 1, activatedDashNests: {}, bonusHistory: [ ...state.bonusHistory, diff --git a/lib/game/bloc/game_state.dart b/lib/game/bloc/game_state.dart index cc18c2c6..bbaa4cd8 100644 --- a/lib/game/bloc/game_state.dart +++ b/lib/game/bloc/game_state.dart @@ -9,8 +9,6 @@ enum GameBonus { word, /// Bonus achieved when the user activates all dash nest bumpers. - /// - /// Adds a [GameState.bonusBalls] to the game. dashNest, } @@ -22,18 +20,15 @@ class GameState extends Equatable { const GameState({ required this.score, required this.balls, - required this.bonusBalls, required this.activatedBonusLetters, required this.bonusHistory, required this.activatedDashNests, }) : assert(score >= 0, "Score can't be negative"), - assert(balls >= 0, "Number of balls can't be negative"), - assert(bonusBalls >= 0, "Number of bonus balls can't be negative"); + assert(balls >= 0, "Number of balls can't be negative"); const GameState.initial() : score = 0, balls = 3, - bonusBalls = 0, activatedBonusLetters = const [], activatedDashNests = const {}, bonusHistory = const []; @@ -46,12 +41,6 @@ class GameState extends Equatable { /// When the number of balls is 0, the game is over. final int balls; - /// The number of bonus balls in the game. - /// - /// [bonusBalls] are gained during the game. For example, when a - /// [GameBonus.dashNest] is achieved. - final int bonusBalls; - /// Active bonus letters. final List activatedBonusLetters; @@ -72,7 +61,6 @@ class GameState extends Equatable { GameState copyWith({ int? score, int? balls, - int? bonusBalls, List? activatedBonusLetters, Set? activatedDashNests, List? bonusHistory, @@ -85,7 +73,6 @@ class GameState extends Equatable { return GameState( score: score ?? this.score, balls: balls ?? this.balls, - bonusBalls: bonusBalls ?? this.bonusBalls, activatedBonusLetters: activatedBonusLetters ?? this.activatedBonusLetters, activatedDashNests: activatedDashNests ?? this.activatedDashNests, @@ -97,7 +84,6 @@ class GameState extends Equatable { List get props => [ score, balls, - bonusBalls, activatedBonusLetters, activatedDashNests, bonusHistory, diff --git a/lib/game/components/controlled_ball.dart b/lib/game/components/controlled_ball.dart index e387a43a..2a7b3585 100644 --- a/lib/game/components/controlled_ball.dart +++ b/lib/game/components/controlled_ball.dart @@ -33,7 +33,7 @@ class ControlledBall extends Ball with Controls { /// [Ball] used in [DebugPinballGame]. ControlledBall.debug() : super(baseColor: const Color(0xFFFF0000)) { - controller = _DebugBallController(this); + controller = BonusBallController(this); } } @@ -64,12 +64,6 @@ abstract class BallController extends ComponentController { class BonusBallController extends BallController with HasGameRef { /// {@macro bonus_ball_controller} BonusBallController(Ball component) : super(component); - - @override - void lost() { - super.lost(); - gameRef.read().add(const BonusBallLost()); - } } /// {@template launched_ball_controller} @@ -92,7 +86,3 @@ class LaunchedBallController extends BallController gameRef.read().add(const BallLost()); } } - -class _DebugBallController extends BallController { - _DebugBallController(Ball ball) : super(ball); -} diff --git a/lib/game/components/flutter_forest.dart b/lib/game/components/flutter_forest.dart index c54c9a68..966b0a6b 100644 --- a/lib/game/components/flutter_forest.dart +++ b/lib/game/components/flutter_forest.dart @@ -59,13 +59,9 @@ class _FlutterForestController extends ComponentController @override bool listenWhen(GameState? previousState, GameState newState) { - final gainedBonus = (previousState?.bonusHistory.length ?? 0) < + return (previousState?.bonusHistory.length ?? 0) < newState.bonusHistory.length && newState.bonusHistory.last == GameBonus.dashNest; - final gainedBonusBall = - (previousState?.bonusBalls ?? 0) < newState.bonusBalls; - - return gainedBonus && gainedBonusBall; } @override diff --git a/test/game/bloc/game_bloc_test.dart b/test/game/bloc/game_bloc_test.dart index ec950e5d..557d408c 100644 --- a/test/game/bloc/game_bloc_test.dart +++ b/test/game/bloc/game_bloc_test.dart @@ -21,7 +21,6 @@ void main() { const GameState( score: 0, balls: 2, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -30,38 +29,6 @@ void main() { ); }); - group('BonusLostBall', () { - blocTest( - 'decreases number of balls', - build: GameBloc.new, - act: (bloc) { - for (var i = 0; i < 3; i++) { - bloc.add(DashNestActivated('$i')); - } - bloc.add(const BonusBallLost()); - }, - skip: 2, - expect: () => [ - const GameState( - score: 0, - balls: 3, - bonusBalls: 1, - activatedBonusLetters: [], - activatedDashNests: {}, - bonusHistory: [GameBonus.dashNest], - ), - const GameState( - score: 0, - balls: 3, - bonusBalls: 0, - activatedBonusLetters: [], - activatedDashNests: {}, - bonusHistory: [GameBonus.dashNest], - ), - ], - ); - }); - group('Scored', () { blocTest( 'increases score ' @@ -74,7 +41,6 @@ void main() { const GameState( score: 2, balls: 3, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -82,7 +48,6 @@ void main() { const GameState( score: 5, balls: 3, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -104,7 +69,6 @@ void main() { const GameState( score: 0, balls: 2, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -112,7 +76,6 @@ void main() { const GameState( score: 0, balls: 1, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -120,7 +83,6 @@ void main() { const GameState( score: 0, balls: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -141,7 +103,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0], activatedDashNests: {}, bonusHistory: [], @@ -149,7 +110,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0, 1], activatedDashNests: {}, bonusHistory: [], @@ -157,7 +117,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0, 1, 2], activatedDashNests: {}, bonusHistory: [], @@ -179,7 +138,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0], activatedDashNests: {}, bonusHistory: [], @@ -187,7 +145,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0, 1], activatedDashNests: {}, bonusHistory: [], @@ -195,7 +152,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0, 1, 2], activatedDashNests: {}, bonusHistory: [], @@ -203,7 +159,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0, 1, 2, 3], activatedDashNests: {}, bonusHistory: [], @@ -211,7 +166,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [0, 1, 2, 3, 4], activatedDashNests: {}, bonusHistory: [], @@ -219,7 +173,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [GameBonus.word], @@ -227,7 +180,6 @@ void main() { GameState( score: GameBloc.bonusWordScore, balls: 3, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [GameBonus.word], @@ -248,7 +200,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {'0'}, bonusHistory: [], @@ -256,7 +207,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {'0', '1'}, bonusHistory: [], @@ -264,7 +214,6 @@ void main() { GameState( score: 0, balls: 3, - bonusBalls: 1, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [GameBonus.dashNest], diff --git a/test/game/bloc/game_event_test.dart b/test/game/bloc/game_event_test.dart index bff0b240..af9f6148 100644 --- a/test/game/bloc/game_event_test.dart +++ b/test/game/bloc/game_event_test.dart @@ -18,19 +18,6 @@ void main() { }); }); - group('BonusBallLost', () { - test('can be instantiated', () { - expect(const BonusBallLost(), isNotNull); - }); - - test('supports value equality', () { - expect( - BonusBallLost(), - equals(const BonusBallLost()), - ); - }); - }); - group('Scored', () { test('can be instantiated', () { expect(const Scored(points: 1), isNotNull); diff --git a/test/game/bloc/game_state_test.dart b/test/game/bloc/game_state_test.dart index 15dee5ba..ed80d192 100644 --- a/test/game/bloc/game_state_test.dart +++ b/test/game/bloc/game_state_test.dart @@ -10,7 +10,6 @@ void main() { GameState( score: 0, balls: 0, - bonusBalls: 0, activatedBonusLetters: const [], activatedDashNests: const {}, bonusHistory: const [], @@ -19,7 +18,6 @@ void main() { const GameState( score: 0, balls: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -34,7 +32,6 @@ void main() { const GameState( score: 0, balls: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -52,25 +49,6 @@ void main() { () => GameState( balls: -1, score: 0, - bonusBalls: 0, - activatedBonusLetters: const [], - activatedDashNests: const {}, - bonusHistory: const [], - ), - throwsAssertionError, - ); - }, - ); - - test( - 'throws AssertionError ' - 'when bonusBalls are negative', - () { - expect( - () => GameState( - balls: 0, - score: 0, - bonusBalls: -1, activatedBonusLetters: const [], activatedDashNests: const {}, bonusHistory: const [], @@ -87,7 +65,6 @@ void main() { expect( () => GameState( balls: 0, - bonusBalls: 0, score: -1, activatedBonusLetters: const [], activatedDashNests: const {}, @@ -105,7 +82,6 @@ void main() { const gameState = GameState( balls: 0, score: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -119,7 +95,6 @@ void main() { const gameState = GameState( balls: 1, score: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -135,7 +110,6 @@ void main() { const gameState = GameState( balls: 3, score: 0, - bonusBalls: 0, activatedBonusLetters: [1], activatedDashNests: {}, bonusHistory: [], @@ -150,7 +124,6 @@ void main() { const gameState = GameState( balls: 3, score: 0, - bonusBalls: 0, activatedBonusLetters: [1], activatedDashNests: {}, bonusHistory: [], @@ -167,7 +140,6 @@ void main() { () { const gameState = GameState( balls: 0, - bonusBalls: 0, score: 2, activatedBonusLetters: [], activatedDashNests: {}, @@ -186,7 +158,6 @@ void main() { () { const gameState = GameState( balls: 0, - bonusBalls: 0, score: 2, activatedBonusLetters: [], activatedDashNests: {}, @@ -206,7 +177,6 @@ void main() { const gameState = GameState( score: 2, balls: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], @@ -214,7 +184,6 @@ void main() { final otherGameState = GameState( score: gameState.score + 1, balls: gameState.balls + 1, - bonusBalls: gameState.bonusBalls + 1, activatedBonusLetters: const [0], activatedDashNests: const {'1'}, bonusHistory: const [GameBonus.word], @@ -225,7 +194,6 @@ void main() { gameState.copyWith( score: otherGameState.score, balls: otherGameState.balls, - bonusBalls: otherGameState.bonusBalls, activatedBonusLetters: otherGameState.activatedBonusLetters, activatedDashNests: otherGameState.activatedDashNests, bonusHistory: otherGameState.bonusHistory, diff --git a/test/game/components/bonus_word_test.dart b/test/game/components/bonus_word_test.dart index 733c954f..f01fced9 100644 --- a/test/game/components/bonus_word_test.dart +++ b/test/game/components/bonus_word_test.dart @@ -262,7 +262,6 @@ void main() { const state = GameState( score: 0, balls: 2, - bonusBalls: 0, activatedBonusLetters: [0], activatedDashNests: {}, bonusHistory: [], @@ -291,7 +290,6 @@ void main() { const state = GameState( score: 0, balls: 2, - bonusBalls: 0, activatedBonusLetters: [0], activatedDashNests: {}, bonusHistory: [], @@ -335,7 +333,6 @@ void main() { final state = GameState( score: 0, balls: 2, - bonusBalls: 0, activatedBonusLetters: [index], activatedDashNests: const {}, bonusHistory: const [], diff --git a/test/game/components/flutter_forest_test.dart b/test/game/components/flutter_forest_test.dart index ae3c7965..d85fe54f 100644 --- a/test/game/components/flutter_forest_test.dart +++ b/test/game/components/flutter_forest_test.dart @@ -86,7 +86,6 @@ void main() { const state = GameState( score: 0, balls: 3, - bonusBalls: 1, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [GameBonus.dashNest], diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index 839d58bf..4632bd75 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -1,7 +1,5 @@ // 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'; diff --git a/test/game/view/game_hud_test.dart b/test/game/view/game_hud_test.dart index 445355a2..953b89eb 100644 --- a/test/game/view/game_hud_test.dart +++ b/test/game/view/game_hud_test.dart @@ -12,7 +12,6 @@ void main() { const initialState = GameState( score: 10, balls: 2, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [], diff --git a/test/game/view/pinball_game_page_test.dart b/test/game/view/pinball_game_page_test.dart index 5c29842c..f16b8ef1 100644 --- a/test/game/view/pinball_game_page_test.dart +++ b/test/game/view/pinball_game_page_test.dart @@ -87,7 +87,6 @@ void main() { const state = GameState( score: 0, balls: 0, - bonusBalls: 0, activatedBonusLetters: [], activatedDashNests: {}, bonusHistory: [],