feat: added replay functionality

pull/441/head
alestiago 3 years ago
parent bac790db95
commit 09754760f0

@ -13,7 +13,8 @@ class BallSpawningBehavior extends Component
bool listenWhen(GameState? previousState, GameState newState) {
if (!newState.status.isPlaying) return false;
final startedGame = previousState?.status.isWaiting ?? true;
final startedGame = (previousState?.status.isWaiting ?? true) ||
(previousState?.status.isGameOver ?? true);
final lostRound =
(previousState?.rounds ?? newState.rounds + 1) > newState.rounds;
return startedGame || lostRound;

@ -19,7 +19,7 @@ class GameBloc extends Bloc<GameEvent, GameState> {
static const _maxScore = 9999999999;
void _onGameStarted(GameStarted _, Emitter emit) {
emit(state.copyWith(status: GameStatus.playing));
emit(const GameState.initial().copyWith(status: GameStatus.playing));
}
void _onGameOver(GameOver _, Emitter emit) {

@ -64,3 +64,10 @@ class GameOver extends GameEvent {
@override
List<Object?> get props => [];
}
class Replayed extends GameEvent {
const Replayed();
@override
List<Object?> get props => [];
}

@ -52,7 +52,7 @@ class GameState extends Equatable {
totalScore = 0,
roundScore = 0,
multiplier = 1,
rounds = 3,
rounds = 1,
bonusHistory = const [];
/// The score for the current round of the game.

@ -66,7 +66,7 @@ class GameOverInfoDisplay extends Component with HasGameRef {
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef.overlays.add(PinballGame.playButtonOverlay);
gameRef.overlays.add(PinballGame.replayButtonOverlay);
}
}

@ -27,6 +27,7 @@ class GameBlocStatusListener extends Component
.forEach(_addFlipperKeyControls);
gameRef.overlays.remove(PinballGame.playButtonOverlay);
gameRef.overlays.remove(PinballGame.replayButtonOverlay);
break;
case GameStatus.gameOver:
readProvider<PinballAudioPlayer>().play(PinballAudio.gameOverVoiceOver);

@ -41,6 +41,9 @@ class PinballGame extends PinballForge2DGame
/// Identifier of the play button overlay
static const playButtonOverlay = 'play_button';
/// Identifier of the play button overlay
static const replayButtonOverlay = 'replay_button';
/// Identifier of the mobile controls overlay
static const mobileControlsOverlay = 'mobile_controls';

@ -100,22 +100,25 @@ class PinballGameLoadedView extends StatelessWidget {
focusNode: game.focusNode,
initialActiveOverlays: const [PinballGame.playButtonOverlay],
overlayBuilderMap: {
PinballGame.playButtonOverlay: (context, game) {
return const Positioned(
PinballGame.playButtonOverlay: (_, game) => const Positioned(
bottom: 20,
right: 0,
left: 0,
child: PlayButtonOverlay(),
);
},
PinballGame.mobileControlsOverlay: (context, game) {
return Positioned(
),
PinballGame.mobileControlsOverlay: (_, game) => Positioned(
bottom: 0,
left: 0,
right: 0,
child: MobileControls(game: game),
);
},
),
PinballGame.replayButtonOverlay: (context, game) =>
const Positioned(
bottom: 20,
right: 0,
left: 0,
child: ReplayButtonOverlay(),
)
},
),
),

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart';
import 'package:pinball/start_game/start_game.dart';
import 'package:pinball_ui/pinball_ui.dart';
@ -18,6 +19,7 @@ class ReplayButtonOverlay extends StatelessWidget {
return PinballButton(
text: l10n.replay,
onTap: () {
context.read<GameBloc>().add(const GameStarted());
context.read<StartGameBloc>().add(const ReplayTapped());
},
);

Loading…
Cancel
Save