fix: game controls are only allowed while the game is playing (#459)

Co-authored-by: Tom Arra <tarra3@gmail.com>
pull/463/head
Jochum van der Ploeg 2 years ago committed by GitHub
parent 1ee4557855
commit 937a18a207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -155,12 +155,11 @@ class PinballGame extends PinballForge2DGame
@override
void onTapDown(int pointerId, TapDownInfo info) {
if (info.raw.kind == PointerDeviceKind.touch) {
if (info.raw.kind == PointerDeviceKind.touch &&
_gameBloc.state.status.isPlaying) {
final rocket = descendants().whereType<RocketSpriteComponent>().first;
final bounds = rocket.topLeftPosition & rocket.size;
// NOTE: As long as Flame does not have https://github.com/flame-engine/flame/issues/1586
// we need to check it at the highest level manually.
final tappedRocket = bounds.contains(info.eventPosition.game.toOffset());
if (tappedRocket) {
descendants()

@ -246,9 +246,61 @@ void main() {
});
group('flipper control', () {
flameTester.test('tap control only works if game is playing',
(game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
final eventPosition = _MockEventPosition();
when(() => eventPosition.game).thenReturn(Vector2.zero());
when(() => eventPosition.widget).thenReturn(Vector2.zero());
final raw = _MockTapDownDetails();
when(() => raw.kind).thenReturn(PointerDeviceKind.touch);
final tapDownEvent = _MockTapDownInfo();
when(() => tapDownEvent.eventPosition).thenReturn(eventPosition);
when(() => tapDownEvent.raw).thenReturn(raw);
final flipperBloc = game
.descendants()
.whereType<Flipper>()
.where((flipper) => flipper.side == BoardSide.left)
.single
.descendants()
.whereType<FlameBlocProvider<FlipperCubit, FlipperState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.gameOver));
game.onTapDown(0, tapDownEvent);
await Future<void>.delayed(Duration.zero);
expect(flipperBloc.state, FlipperState.movingDown);
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
game.onTapDown(0, tapDownEvent);
await Future<void>.delayed(Duration.zero);
expect(flipperBloc.state, FlipperState.movingUp);
});
flameTester.test('tap down moves left flipper up', (game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
final eventPosition = _MockEventPosition();
when(() => eventPosition.game).thenReturn(Vector2.zero());
when(() => eventPosition.widget).thenReturn(Vector2.zero());
@ -278,6 +330,14 @@ void main() {
flameTester.test('tap down moves right flipper up', (game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
final eventPosition = _MockEventPosition();
when(() => eventPosition.game).thenReturn(Vector2.zero());
when(() => eventPosition.widget).thenReturn(game.canvasSize);
@ -307,6 +367,14 @@ void main() {
flameTester.test('tap up moves flipper down', (game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
final eventPosition = _MockEventPosition();
when(() => eventPosition.game).thenReturn(Vector2.zero());
when(() => eventPosition.widget).thenReturn(Vector2.zero());
@ -332,6 +400,14 @@ void main() {
flameTester.test('tap cancel moves flipper down', (game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
final eventPosition = _MockEventPosition();
when(() => eventPosition.game).thenReturn(Vector2.zero());
when(() => eventPosition.widget).thenReturn(Vector2.zero());
@ -363,6 +439,14 @@ void main() {
(game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
final raw = _MockTapDownDetails();
when(() => raw.kind).thenReturn(PointerDeviceKind.touch);
@ -416,6 +500,14 @@ void main() {
flameTester.test('plunger control tap down emits plunging', (game) async {
await game.ready();
final gameBloc = game
.descendants()
.whereType<FlameBlocProvider<GameBloc, GameState>>()
.first
.bloc;
gameBloc.emit(gameBloc.state.copyWith(status: GameStatus.playing));
final eventPosition = _MockEventPosition();
when(() => eventPosition.game).thenReturn(Vector2(40, 60));

Loading…
Cancel
Save