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> {
GameBloc() : super(const GameState.initial()) {
on<BallAdded>(_onBallAdded);
on<BallLost>(_onBallLost);
on<Scored>(_onScored);
on<MultiplierIncreased>(_onIncreasedMultiplier);
@ -16,16 +17,21 @@ class GameBloc extends Bloc<GameEvent, GameState> {
on<SparkyTurboChargeActivated>(_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;
var ballsLeft = event.ballsLeft;
final ballsLeft = math.max(state.balls - 1, 0);
var roundsLeft = state.rounds;
if (ballsLeft < 1) {
score = score * state.multiplier;
multiplier = 1;
ballsLeft = 1;
roundsLeft = state.rounds - 1;
}

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

@ -33,7 +33,7 @@ class GameState extends Equatable {
const GameState.initial()
: score = 0,
multiplier = 1,
balls = 1,
balls = 0,
rounds = 3,
bonusHistory = const [];
@ -43,7 +43,7 @@ class GameState extends Equatable {
/// The current multiplier for the score.
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.
final int balls;

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

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

Loading…
Cancel
Save