diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index 20aa924e..d1721927 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -53,7 +53,7 @@ class Ball extends PositionBodyComponent { final bloc = gameRef.read()..add(const BallLost()); - final shouldBallRespwan = !bloc.state.isLastBall; + final shouldBallRespwan = !bloc.state.isLastBall && !bloc.state.isGameOver; if (shouldBallRespwan) { gameRef.spawnBall(); } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 5854422c..d18ef592 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -4,15 +4,19 @@ import 'dart:async'; import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_theme/pinball_theme.dart'; class PinballGame extends Forge2DGame - with FlameBloc, HasKeyboardHandlerComponents { - PinballGame({required this.theme}); + with FlameBloc, HasKeyboardHandlerComponents, TapDetector { + PinballGame({required this.theme, bool isDebugMode = kDebugMode}) : _isDebugMode = isDebugMode; final PinballTheme theme; + final bool _isDebugMode; + // TODO(erickzanardo): Change to the plumber position late final ballStartingPosition = screenToWorld( Vector2( @@ -104,4 +108,11 @@ class PinballGame extends Forge2DGame ), ); } + + @override + void onTapUp(TapUpInfo info) { + if (_isDebugMode) { + add(Ball(position: info.eventPosition.game)); + } + } } diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index 3048308b..00507d0b 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -3,6 +3,7 @@ import 'package:flame/components.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; import '../helpers/helpers.dart'; @@ -11,6 +12,9 @@ void main() { group('PinballGame', () { TestWidgetsFlutterBinding.ensureInitialized(); final flameTester = FlameTester(PinballGameTest.create); + final debugModeFlameTester = FlameTester( + () => PinballGame(isDebugMode: true), + ); // TODO(alestiago): test if [PinballGame] registers // [BallScorePointsCallback] once the following issue is resolved: @@ -49,6 +53,24 @@ void main() { ); }, ); + + debugModeFlameTester.test('adds a ball on tap up', (game) async { + await game.ready(); + + final eventPosition = MockEventPosition(); + when(() => eventPosition.game).thenReturn(Vector2.all(10)); + + final tapUpEvent = MockTapUpInfo(); + when(() => tapUpEvent.eventPosition).thenReturn(eventPosition); + + game.onTapUp(tapUpEvent); + await game.ready(); + + expect( + game.children.whereType().length, + equals(1), + ); + }); }); }, ); diff --git a/test/helpers/mocks.dart b/test/helpers/mocks.dart index 44e78afe..46886752 100644 --- a/test/helpers/mocks.dart +++ b/test/helpers/mocks.dart @@ -1,3 +1,4 @@ +import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; @@ -32,3 +33,7 @@ class MockRawKeyUpEvent extends Mock implements RawKeyUpEvent { return super.toString(); } } + +class MockTapUpInfo extends Mock implements TapUpInfo {} + +class MockEventPosition extends Mock implements EventPosition {}