mirror of https://github.com/flutter/pinball.git
refactor: include `GameStatus` on `GameBloc` (#345)
parent
34d0e7d65a
commit
2ad0196e44
@ -0,0 +1,33 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_audio/pinball_audio.dart';
|
||||||
|
|
||||||
|
/// Listens to the [GameBloc] and updates the game accordingly.
|
||||||
|
class GameBlocStatusListener extends Component
|
||||||
|
with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> {
|
||||||
|
@override
|
||||||
|
bool listenWhen(GameState? previousState, GameState newState) {
|
||||||
|
return previousState?.status != newState.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onNewState(GameState state) {
|
||||||
|
switch (state.status) {
|
||||||
|
case GameStatus.waiting:
|
||||||
|
break;
|
||||||
|
case GameStatus.playing:
|
||||||
|
gameRef.player.play(PinballAudio.backgroundMusic);
|
||||||
|
gameRef.firstChild<CameraController>()?.focusOnGame();
|
||||||
|
gameRef.overlays.remove(PinballGame.playButtonOverlay);
|
||||||
|
break;
|
||||||
|
case GameStatus.gameOver:
|
||||||
|
gameRef.descendants().whereType<Backbox>().first.initialsInput(
|
||||||
|
score: state.displayScore,
|
||||||
|
characterIconPath: gameRef.characterTheme.leaderboardIcon.keyName,
|
||||||
|
);
|
||||||
|
gameRef.firstChild<CameraController>()!.focusOnGameOverBackbox();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,46 +0,0 @@
|
|||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:flame_bloc/flame_bloc.dart';
|
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_audio/pinball_audio.dart';
|
|
||||||
import 'package:pinball_flame/pinball_flame.dart';
|
|
||||||
|
|
||||||
/// {@template game_flow_controller}
|
|
||||||
/// A [Component] that controls the game over and game restart logic
|
|
||||||
/// {@endtemplate}
|
|
||||||
class GameFlowController extends ComponentController<PinballGame>
|
|
||||||
with BlocComponent<GameBloc, GameState> {
|
|
||||||
/// {@macro game_flow_controller}
|
|
||||||
GameFlowController(PinballGame component) : super(component);
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool listenWhen(GameState? previousState, GameState newState) {
|
|
||||||
return previousState?.isGameOver != newState.isGameOver;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void onNewState(GameState state) {
|
|
||||||
if (state.isGameOver) {
|
|
||||||
_initialsInput();
|
|
||||||
} else {
|
|
||||||
start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Puts the game in the initials input state.
|
|
||||||
void _initialsInput() {
|
|
||||||
// TODO(erickzanardo): implement score submission and "navigate" to the
|
|
||||||
// next page
|
|
||||||
component.descendants().whereType<Backbox>().first.initialsInput(
|
|
||||||
score: state?.displayScore ?? 0,
|
|
||||||
characterIconPath: component.characterTheme.leaderboardIcon.keyName,
|
|
||||||
);
|
|
||||||
component.firstChild<CameraController>()!.focusOnGameOverBackbox();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Puts the game in the playing state.
|
|
||||||
void start() {
|
|
||||||
component.player.play(PinballAudio.backgroundMusic);
|
|
||||||
component.firstChild<CameraController>()?.focusOnGame();
|
|
||||||
component.overlays.remove(PinballGame.playButtonOverlay);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue