feat: added replay button and game flow

pull/359/head
RuiAlonso 3 years ago
parent ddf2a03be3
commit 3a9cbacb57

@ -37,6 +37,7 @@ class CameraFocusingBehavior extends Component
case GameStatus.waiting: case GameStatus.waiting:
break; break;
case GameStatus.playing: case GameStatus.playing:
case GameStatus.replaying:
_zoom(_foci['game']!); _zoom(_foci['game']!);
break; break;
case GameStatus.gameOver: case GameStatus.gameOver:

@ -16,12 +16,17 @@ class GameBloc extends Bloc<GameEvent, GameState> {
on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated); on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated);
on<GameOver>(_onGameOver); on<GameOver>(_onGameOver);
on<GameStarted>(_onGameStarted); on<GameStarted>(_onGameStarted);
on<GameRestarted>(_onGameRestarted);
} }
void _onGameStarted(GameStarted _, Emitter emit) { void _onGameStarted(GameStarted _, Emitter emit) {
emit(state.copyWith(status: GameStatus.playing)); emit(state.copyWith(status: GameStatus.playing));
} }
void _onGameRestarted(GameRestarted _, Emitter emit) {
emit(const GameState.initial().copyWith(status: GameStatus.replaying));
}
void _onGameOver(GameOver _, Emitter emit) { void _onGameOver(GameOver _, Emitter emit) {
emit(state.copyWith(status: GameStatus.gameOver)); emit(state.copyWith(status: GameStatus.gameOver));
} }

@ -67,6 +67,13 @@ class GameStarted extends GameEvent {
List<Object?> get props => []; List<Object?> get props => [];
} }
class GameRestarted extends GameEvent {
const GameRestarted();
@override
List<Object?> get props => [];
}
class GameOver extends GameEvent { class GameOver extends GameEvent {
const GameOver(); const GameOver();

@ -23,12 +23,14 @@ enum GameBonus {
enum GameStatus { enum GameStatus {
waiting, waiting,
playing, playing,
replaying,
gameOver, gameOver,
} }
extension GameStatusX on GameStatus { extension GameStatusX on GameStatus {
bool get isWaiting => this == GameStatus.waiting; bool get isWaiting => this == GameStatus.waiting;
bool get isPlaying => this == GameStatus.playing; bool get isPlaying =>
this == GameStatus.playing || this == GameStatus.replaying;
bool get isGameOver => this == GameStatus.gameOver; bool get isGameOver => this == GameStatus.gameOver;
} }

@ -22,6 +22,9 @@ class GameBlocStatusListener extends Component
readProvider<PinballPlayer>().play(PinballAudio.backgroundMusic); readProvider<PinballPlayer>().play(PinballAudio.backgroundMusic);
gameRef.overlays.remove(PinballGame.playButtonOverlay); gameRef.overlays.remove(PinballGame.playButtonOverlay);
break; break;
case GameStatus.replaying:
gameRef.overlays.remove(PinballGame.replayButtonOverlay);
break;
case GameStatus.gameOver: case GameStatus.gameOver:
readProvider<PinballPlayer>().play(PinballAudio.gameOverVoiceOver); readProvider<PinballPlayer>().play(PinballAudio.gameOverVoiceOver);
gameRef.descendants().whereType<Backbox>().first.requestInitials( gameRef.descendants().whereType<Backbox>().first.requestInitials(

@ -17,7 +17,7 @@ import 'package:pinball_flame/pinball_flame.dart';
import 'package:pinball_theme/pinball_theme.dart'; import 'package:pinball_theme/pinball_theme.dart';
class PinballGame extends PinballForge2DGame class PinballGame extends PinballForge2DGame
with HasKeyboardHandlerComponents, MultiTouchTapDetector { with HasKeyboardHandlerComponents, MultiTouchTapDetector, HasTappables {
PinballGame({ PinballGame({
required CharacterTheme characterTheme, required CharacterTheme characterTheme,
required this.leaderboardRepository, required this.leaderboardRepository,

@ -18,7 +18,7 @@ class ReplayButtonOverlay extends StatelessWidget {
return PinballButton( return PinballButton(
text: l10n.replay, text: l10n.replay,
onTap: () { onTap: () {
context.read<StartGameBloc>().add(const PlayTapped()); context.read<StartGameBloc>().add(const ReplayTapped());
}, },
); );
} }

@ -11,6 +11,7 @@ class StartGameBloc extends Bloc<StartGameEvent, StartGameState> {
/// {@macro start_game_bloc} /// {@macro start_game_bloc}
StartGameBloc() : super(const StartGameState.initial()) { StartGameBloc() : super(const StartGameState.initial()) {
on<PlayTapped>(_onPlayTapped); on<PlayTapped>(_onPlayTapped);
on<ReplayTapped>(_onReplayTapped);
on<CharacterSelected>(_onCharacterSelected); on<CharacterSelected>(_onCharacterSelected);
on<HowToPlayFinished>(_onHowToPlayFinished); on<HowToPlayFinished>(_onHowToPlayFinished);
} }
@ -26,6 +27,18 @@ class StartGameBloc extends Bloc<StartGameEvent, StartGameState> {
); );
} }
void _onReplayTapped(
ReplayTapped event,
Emitter<StartGameState> emit,
) {
emit(
state.copyWith(
status: StartGameStatus.selectCharacter,
restarted: true,
),
);
}
void _onCharacterSelected( void _onCharacterSelected(
CharacterSelected event, CharacterSelected event,
Emitter<StartGameState> emit, Emitter<StartGameState> emit,

@ -19,6 +19,17 @@ class PlayTapped extends StartGameEvent {
List<Object> get props => []; List<Object> get props => [];
} }
/// {@template replay_tapped}
/// Replay tapped event.
/// {@endtemplate}
class ReplayTapped extends StartGameEvent {
/// {@macro replay_tapped}
const ReplayTapped();
@override
List<Object> get props => [];
}
/// {@template character_selected} /// {@template character_selected}
/// Character selected event. /// Character selected event.
/// {@endtemplate} /// {@endtemplate}

@ -22,23 +22,33 @@ class StartGameState extends Equatable {
/// {@macro start_game_state} /// {@macro start_game_state}
const StartGameState({ const StartGameState({
required this.status, required this.status,
this.restarted = false,
}); });
/// Initial [StartGameState]. /// Initial [StartGameState].
const StartGameState.initial() : this(status: StartGameStatus.initial); const StartGameState.initial()
: this(
status: StartGameStatus.initial,
restarted: false,
);
/// Status of [StartGameState]. /// Status of [StartGameState].
final StartGameStatus status; final StartGameStatus status;
/// Game has been restarted from game over screen.
final bool restarted;
/// Creates a copy of [StartGameState]. /// Creates a copy of [StartGameState].
StartGameState copyWith({ StartGameState copyWith({
StartGameStatus? status, StartGameStatus? status,
bool? restarted,
}) { }) {
return StartGameState( return StartGameState(
status: status ?? this.status, status: status ?? this.status,
restarted: restarted ?? this.restarted,
); );
} }
@override @override
List<Object> get props => [status]; List<Object> get props => [status, restarted];
} }

@ -31,7 +31,11 @@ class StartGameListener extends StatelessWidget {
break; break;
case StartGameStatus.selectCharacter: case StartGameStatus.selectCharacter:
_onSelectCharacter(context); _onSelectCharacter(context);
context.read<GameBloc>().add(const GameStarted()); if (state.restarted) {
context.read<GameBloc>().add(const GameRestarted());
} else {
context.read<GameBloc>().add(const GameStarted());
}
break; break;
case StartGameStatus.howToPlay: case StartGameStatus.howToPlay:
_onHowToPlay(context); _onHowToPlay(context);

Loading…
Cancel
Save