feat: ball added event to game bloc

pull/213/head
RuiAlonso 3 years ago
parent 3e76ea9535
commit 069ceaa3d6

@ -9,6 +9,7 @@ part 'game_state.dart';
class GameBloc extends Bloc<GameEvent, GameState> { class GameBloc extends Bloc<GameEvent, GameState> {
GameBloc() : super(const GameState.initial()) { GameBloc() : super(const GameState.initial()) {
on<BallAdded>(_onBallAdded);
on<BallLost>(_onBallLost); on<BallLost>(_onBallLost);
on<Scored>(_onScored); on<Scored>(_onScored);
on<MultiplierIncreased>(_onIncreasedMultiplier); on<MultiplierIncreased>(_onIncreasedMultiplier);
@ -16,16 +17,21 @@ class GameBloc extends Bloc<GameEvent, GameState> {
on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated); on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated);
} }
void _onBallAdded(BallAdded event, Emitter emit) {
emit(
state.copyWith(balls: state.balls + 1),
);
}
void _onBallLost(BallLost event, Emitter emit) { void _onBallLost(BallLost event, Emitter emit) {
var score = state.score; var score = state.score;
var multiplier = state.multiplier; var multiplier = state.multiplier;
var ballsLeft = event.ballsLeft; final ballsLeft = math.max(state.balls - 1, 0);
var roundsLeft = state.rounds; var roundsLeft = state.rounds;
if (ballsLeft < 1) { if (ballsLeft < 1) {
score = score * state.multiplier; score = score * state.multiplier;
multiplier = 1; multiplier = 1;
ballsLeft = 1;
roundsLeft = state.rounds - 1; roundsLeft = state.rounds - 1;
} }

@ -7,19 +7,26 @@ abstract class GameEvent extends Equatable {
const GameEvent(); const GameEvent();
} }
/// {@template ball_added_game_event}
/// Event added when a ball is spawn to the board.
/// {@endtemplate}
class BallAdded extends GameEvent {
/// {@macro ball_added_game_event}
const BallAdded();
@override
List<Object?> get props => [];
}
/// {@template ball_lost_game_event} /// {@template ball_lost_game_event}
/// Event added when a user drops a ball off the screen. /// Event added when a user drops a ball off the screen.
/// {@endtemplate} /// {@endtemplate}
class BallLost extends GameEvent { class BallLost extends GameEvent {
/// {@macro ball_lost_game_event} /// {@macro ball_lost_game_event}
const BallLost({ const BallLost();
required this.ballsLeft,
}) : assert(ballsLeft >= 0, "Balls left can't be negative");
final int ballsLeft;
@override @override
List<Object?> get props => [ballsLeft]; List<Object?> get props => [];
} }
/// {@template scored_game_event} /// {@template scored_game_event}

@ -33,7 +33,7 @@ class GameState extends Equatable {
const GameState.initial() const GameState.initial()
: score = 0, : score = 0,
multiplier = 1, multiplier = 1,
balls = 1, balls = 0,
rounds = 3, rounds = 3,
bonusHistory = const []; bonusHistory = const [];
@ -43,7 +43,7 @@ class GameState extends Equatable {
/// The current multiplier for the score. /// The current multiplier for the score.
final int multiplier; final int multiplier;
/// The number of balls left in the game. /// The number of balls left in each round.
/// ///
/// When the number of balls is 0, round is lost. /// When the number of balls is 0, round is lost.
final int balls; final int balls;

@ -75,7 +75,12 @@ class BallController extends ComponentController<Ball>
@override @override
void onRemove() { void onRemove() {
super.onRemove(); super.onRemove();
final remainingBalls = gameRef.children.whereType<Ball>().length; gameRef.read<GameBloc>().add(const BallLost());
gameRef.read<GameBloc>().add(BallLost(ballsLeft: remainingBalls)); }
@override
Future<void>? onLoad() {
gameRef.read<GameBloc>().add(const BallAdded());
return super.onLoad();
} }
} }

@ -99,8 +99,11 @@ class _GameBallsController extends ComponentController<PinballGame>
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
final noBallsLeft = component.descendants().whereType<Ball>().isEmpty; //final noBallsLeft = component.descendants().whereType<Ball>().isEmpty;
final canBallRespawn = newState.balls > 0; //final canBallRespawn = newState.balls > 0;
final noBallsLeft = newState.balls == 0;
final canBallRespawn = newState.rounds > 0;
return noBallsLeft && canBallRespawn; return noBallsLeft && canBallRespawn;
} }

Loading…
Cancel
Save