mirror of https://github.com/flutter/pinball.git
parent
e5f248302f
commit
27c0f322af
@ -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<GameEvent, GameState> {
|
||||
GameBloc() : super(GameInitial()) {
|
||||
on<GameEvent>((event, emit) {
|
||||
// TODO: implement event handler
|
||||
});
|
||||
GameBloc() : super(const GameState(score: 0, ballsLeft: 3)) {
|
||||
on<BallLost>(_onBallLost);
|
||||
on<Scored>(_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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<Object?> get props => [
|
||||
score,
|
||||
ballsLeft,
|
||||
];
|
||||
}
|
||||
|
Loading…
Reference in new issue