refactor: changed BallLost event with RoundLost, and moved part of the logic to controlled_ball

pull/213/head
RuiAlonso 3 years ago
parent 18c12bce7d
commit f5495a2d52

@ -9,37 +9,21 @@ part 'game_state.dart';
class GameBloc extends Bloc<GameEvent, GameState> {
GameBloc() : super(const GameState.initial()) {
on<BallAdded>(_onBallAdded);
on<BallLost>(_onBallLost);
on<RoundLost>(_onRoundLost);
on<Scored>(_onScored);
on<MultiplierIncreased>(_onIncreasedMultiplier);
on<BonusActivated>(_onBonusActivated);
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;
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,
),
);

@ -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<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();
class RoundLost extends GameEvent {
/// {@macro round_lost_game_event}
const RoundLost();
@override
List<Object?> get props => [];

@ -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<GameBonus> 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<Object?> get props => [
score,
multiplier,
balls,
rounds,
bonusHistory,
];

@ -75,12 +75,9 @@ class BallController extends ComponentController<Ball>
@override
void onRemove() {
super.onRemove();
gameRef.read<GameBloc>().add(const BallLost());
final noBallsLeft = gameRef.descendants().whereType<Ball>().isEmpty;
if (noBallsLeft) {
gameRef.read<GameBloc>().add(const RoundLost());
}
@override
void onMount() {
super.onMount();
gameRef.read<GameBloc>().add(const BallAdded());
}
}

@ -41,8 +41,6 @@ class PinballGame extends Forge2DGame
@override
Future<void> onLoad() async {
print("ONLOAD");
_addContactCallbacks();
unawaited(add(gameFlowController = GameFlowController(this)));
@ -101,10 +99,12 @@ class _GameBallsController extends ComponentController<PinballGame>
@override
bool listenWhen(GameState? previousState, GameState newState) {
final noBallsLeft = newState.balls == 0;
final canBallRespawn = newState.rounds > 0;
final noBallsLeft = component.descendants().whereType<Ball>().isEmpty;
final notGameOver = !newState.isGameOver;
return noBallsLeft && canBallRespawn;
print("noBallsLeft $noBallsLeft");
print("notGameOver $notGameOver");
return noBallsLeft && notGameOver;
}
@override

Loading…
Cancel
Save