From f5495a2d523f810f54eca642a364fcfd434e1441 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Wed, 27 Apr 2022 19:53:19 +0200 Subject: [PATCH] refactor: changed BallLost event with RoundLost, and moved part of the logic to controlled_ball --- lib/game/bloc/game_bloc.dart | 26 +++++------------------- lib/game/bloc/game_event.dart | 21 +++++-------------- lib/game/bloc/game_state.dart | 13 ------------ lib/game/components/controlled_ball.dart | 11 ++++------ lib/game/pinball_game.dart | 10 ++++----- 5 files changed, 19 insertions(+), 62 deletions(-) diff --git a/lib/game/bloc/game_bloc.dart b/lib/game/bloc/game_bloc.dart index 80ceb429..49f40d1f 100644 --- a/lib/game/bloc/game_bloc.dart +++ b/lib/game/bloc/game_bloc.dart @@ -9,37 +9,21 @@ part 'game_state.dart'; class GameBloc extends Bloc { GameBloc() : super(const GameState.initial()) { - on(_onBallAdded); - on(_onBallLost); + on(_onRoundLost); on(_onScored); on(_onIncreasedMultiplier); on(_onBonusActivated); on(_onSparkyTurboChargeActivated); } - void _onBallAdded(BallAdded event, Emitter emit) { - emit( - state.copyWith(balls: state.balls + 1), - ); - } - - void _onBallLost(BallLost event, Emitter emit) { - var score = state.score; - var multiplier = state.multiplier; - final ballsLeft = math.max(state.balls - 1, 0); - var roundsLeft = state.rounds; - - if (ballsLeft < 1) { - score = score * state.multiplier; - multiplier = 1; - roundsLeft = state.rounds - 1; - } + void _onRoundLost(RoundLost event, Emitter emit) { + final score = state.score * state.multiplier; + final roundsLeft = math.max(state.rounds - 1, 0); emit( state.copyWith( score: score, - multiplier: multiplier, - balls: ballsLeft, + multiplier: 1, rounds: roundsLeft, ), ); diff --git a/lib/game/bloc/game_event.dart b/lib/game/bloc/game_event.dart index dd688a90..c81ce526 100644 --- a/lib/game/bloc/game_event.dart +++ b/lib/game/bloc/game_event.dart @@ -7,23 +7,12 @@ abstract class GameEvent extends Equatable { const GameEvent(); } -/// {@template ball_added_game_event} -/// Event added when a ball is spawn to the board. +/// {@template round_lost_game_event} +/// Event added when a user drops all balls off the screen and loses a round. /// {@endtemplate} -class BallAdded extends GameEvent { - /// {@macro ball_added_game_event} - const BallAdded(); - - @override - List get props => []; -} - -/// {@template ball_lost_game_event} -/// Event added when a user drops a ball off the screen. -/// {@endtemplate} -class BallLost extends GameEvent { - /// {@macro ball_lost_game_event} - const BallLost(); +class RoundLost extends GameEvent { + /// {@macro round_lost_game_event} + const RoundLost(); @override List get props => []; diff --git a/lib/game/bloc/game_state.dart b/lib/game/bloc/game_state.dart index e9dc67fb..4ce9042d 100644 --- a/lib/game/bloc/game_state.dart +++ b/lib/game/bloc/game_state.dart @@ -28,18 +28,15 @@ class GameState extends Equatable { const GameState({ required this.score, required this.multiplier, - required this.balls, required this.rounds, required this.bonusHistory, }) : assert(score >= 0, "Score can't be negative"), assert(multiplier > 0, 'Multiplier must be greater than zero'), - assert(balls >= 0, "Number of balls can't be negative"), assert(rounds >= 0, "Number of rounds can't be negative"); const GameState.initial() : score = 0, multiplier = 1, - balls = 0, rounds = 3, bonusHistory = const []; @@ -49,11 +46,6 @@ class GameState extends Equatable { /// The current multiplier for the score. final int multiplier; - /// The number of balls left in each round. - /// - /// When the number of balls is 0, round is lost. - final int balls; - /// The number of rounds left in the game. /// /// When the number of rounds is 0, the game is over. @@ -63,9 +55,6 @@ class GameState extends Equatable { /// PinballGame. final List bonusHistory; - /// Determines when the round is over. - bool get isRoundOver => balls == 0; - /// Determines when the game is over. bool get isGameOver => rounds == 0; @@ -84,7 +73,6 @@ class GameState extends Equatable { return GameState( score: score ?? this.score, multiplier: multiplier ?? this.multiplier, - balls: balls ?? this.balls, rounds: rounds ?? this.rounds, bonusHistory: bonusHistory ?? this.bonusHistory, ); @@ -94,7 +82,6 @@ class GameState extends Equatable { List get props => [ score, multiplier, - balls, rounds, bonusHistory, ]; diff --git a/lib/game/components/controlled_ball.dart b/lib/game/components/controlled_ball.dart index 88f8d115..5ea28bff 100644 --- a/lib/game/components/controlled_ball.dart +++ b/lib/game/components/controlled_ball.dart @@ -75,12 +75,9 @@ class BallController extends ComponentController @override void onRemove() { super.onRemove(); - gameRef.read().add(const BallLost()); - } - - @override - void onMount() { - super.onMount(); - gameRef.read().add(const BallAdded()); + final noBallsLeft = gameRef.descendants().whereType().isEmpty; + if (noBallsLeft) { + gameRef.read().add(const RoundLost()); + } } } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index e7786189..30bd80e9 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -41,8 +41,6 @@ class PinballGame extends Forge2DGame @override Future onLoad() async { - print("ONLOAD"); - _addContactCallbacks(); unawaited(add(gameFlowController = GameFlowController(this))); @@ -101,10 +99,12 @@ class _GameBallsController extends ComponentController @override bool listenWhen(GameState? previousState, GameState newState) { - final noBallsLeft = newState.balls == 0; - final canBallRespawn = newState.rounds > 0; + final noBallsLeft = component.descendants().whereType().isEmpty; + final notGameOver = !newState.isGameOver; - return noBallsLeft && canBallRespawn; + print("noBallsLeft $noBallsLeft"); + print("notGameOver $notGameOver"); + return noBallsLeft && notGameOver; } @override