diff --git a/lib/game/bloc/game_bloc.dart b/lib/game/bloc/game_bloc.dart index a1933667..001d9f25 100644 --- a/lib/game/bloc/game_bloc.dart +++ b/lib/game/bloc/game_bloc.dart @@ -1,13 +1,25 @@ import 'package:bloc/bloc.dart'; +import 'package:equatable/equatable.dart'; import 'package:meta/meta.dart'; part 'game_event.dart'; part 'game_state.dart'; class GameBloc extends Bloc { - GameBloc() : super(GameInitial()) { - on((event, emit) { - // TODO: implement event handler - }); + GameBloc() : super(const GameState(score: 0, ballsLeft: 3)) { + on(_onBallLost); + on(_onScored); + } + + void _onBallLost(BallLost event, Emitter emit) { + if (state.ballsLeft > 0) { + emit(state.copyWith(ballsLeft: state.ballsLeft - 1)); + } + } + + void _onScored(Scored event, Emitter emit) { + if (!state.isGameOver) { + emit(state.copyWith(score: state.score + event.points)); + } } } diff --git a/lib/game/bloc/game_event.dart b/lib/game/bloc/game_event.dart index 5a4efbe3..807da83b 100644 --- a/lib/game/bloc/game_event.dart +++ b/lib/game/bloc/game_event.dart @@ -1,4 +1,16 @@ part of 'game_bloc.dart'; @immutable -abstract class GameEvent {} +abstract class GameEvent { + const GameEvent(); +} + +class BallLost extends GameEvent { + const BallLost(); +} + +class Scored extends GameEvent { + const Scored(this.points); + + final int points; +} diff --git a/lib/game/bloc/game_state.dart b/lib/game/bloc/game_state.dart index 68fca14f..0874bb9d 100644 --- a/lib/game/bloc/game_state.dart +++ b/lib/game/bloc/game_state.dart @@ -1,6 +1,29 @@ part of 'game_bloc.dart'; -@immutable -abstract class GameState {} +class GameState extends Equatable { + const GameState({ + required this.score, + required this.ballsLeft, + }); -class GameInitial extends GameState {} + final int score; + final int ballsLeft; + + bool get isGameOver => ballsLeft == 0; + + GameState copyWith({ + int? score, + int? ballsLeft, + }) { + return GameState( + score: score ?? this.score, + ballsLeft: ballsLeft ?? this.ballsLeft, + ); + } + + @override + List get props => [ + score, + ballsLeft, + ]; +}