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> { class GameBloc extends Bloc<GameEvent, GameState> {
GameBloc() : super(const GameState.initial()) { GameBloc() : super(const GameState.initial()) {
on<BallAdded>(_onBallAdded); on<RoundLost>(_onRoundLost);
on<BallLost>(_onBallLost);
on<Scored>(_onScored); on<Scored>(_onScored);
on<MultiplierIncreased>(_onIncreasedMultiplier); on<MultiplierIncreased>(_onIncreasedMultiplier);
on<BonusActivated>(_onBonusActivated); on<BonusActivated>(_onBonusActivated);
on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated); on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated);
} }
void _onBallAdded(BallAdded event, Emitter emit) { void _onRoundLost(RoundLost event, Emitter emit) {
emit( final score = state.score * state.multiplier;
state.copyWith(balls: state.balls + 1), final roundsLeft = math.max(state.rounds - 1, 0);
);
}
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;
}
emit( emit(
state.copyWith( state.copyWith(
score: score, score: score,
multiplier: multiplier, multiplier: 1,
balls: ballsLeft,
rounds: roundsLeft, rounds: roundsLeft,
), ),
); );

@ -7,23 +7,12 @@ abstract class GameEvent extends Equatable {
const GameEvent(); const GameEvent();
} }
/// {@template ball_added_game_event} /// {@template round_lost_game_event}
/// Event added when a ball is spawn to the board. /// Event added when a user drops all balls off the screen and loses a round.
/// {@endtemplate} /// {@endtemplate}
class BallAdded extends GameEvent { class RoundLost extends GameEvent {
/// {@macro ball_added_game_event} /// {@macro round_lost_game_event}
const BallAdded(); const RoundLost();
@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();
@override @override
List<Object?> get props => []; List<Object?> get props => [];

@ -28,18 +28,15 @@ class GameState extends Equatable {
const GameState({ const GameState({
required this.score, required this.score,
required this.multiplier, required this.multiplier,
required this.balls,
required this.rounds, required this.rounds,
required this.bonusHistory, required this.bonusHistory,
}) : assert(score >= 0, "Score can't be negative"), }) : assert(score >= 0, "Score can't be negative"),
assert(multiplier > 0, 'Multiplier must be greater than zero'), 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"); assert(rounds >= 0, "Number of rounds can't be negative");
const GameState.initial() const GameState.initial()
: score = 0, : score = 0,
multiplier = 1, multiplier = 1,
balls = 0,
rounds = 3, rounds = 3,
bonusHistory = const []; bonusHistory = const [];
@ -49,11 +46,6 @@ 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 each round.
///
/// When the number of balls is 0, round is lost.
final int balls;
/// The number of rounds left in the game. /// The number of rounds left in the game.
/// ///
/// When the number of rounds is 0, the game is over. /// When the number of rounds is 0, the game is over.
@ -63,9 +55,6 @@ class GameState extends Equatable {
/// PinballGame. /// PinballGame.
final List<GameBonus> bonusHistory; final List<GameBonus> bonusHistory;
/// Determines when the round is over.
bool get isRoundOver => balls == 0;
/// Determines when the game is over. /// Determines when the game is over.
bool get isGameOver => rounds == 0; bool get isGameOver => rounds == 0;
@ -84,7 +73,6 @@ class GameState extends Equatable {
return GameState( return GameState(
score: score ?? this.score, score: score ?? this.score,
multiplier: multiplier ?? this.multiplier, multiplier: multiplier ?? this.multiplier,
balls: balls ?? this.balls,
rounds: rounds ?? this.rounds, rounds: rounds ?? this.rounds,
bonusHistory: bonusHistory ?? this.bonusHistory, bonusHistory: bonusHistory ?? this.bonusHistory,
); );
@ -94,7 +82,6 @@ class GameState extends Equatable {
List<Object?> get props => [ List<Object?> get props => [
score, score,
multiplier, multiplier,
balls,
rounds, rounds,
bonusHistory, bonusHistory,
]; ];

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

Loading…
Cancel
Save