From b01444c902e5a1e44d4dd13a1fe31d467dac2a96 Mon Sep 17 00:00:00 2001 From: Rui Miguel Alonso Date: Wed, 4 May 2022 17:59:44 +0200 Subject: [PATCH] feat: debug feature for turbocharging at `DebugPinballGame` (#333) * feat: debug feature for turbocharging at DebugPinballGame * test: ignore start end for debug pinball * test: coverage --- lib/game/pinball_game.dart | 56 ++++++++++++++++++++++++- test/game/pinball_game_test.dart | 72 +++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 3 deletions(-) diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 0cd130ca..5f6fbbdc 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -163,7 +163,7 @@ class _GameBallsController extends ComponentController } } -class DebugPinballGame extends PinballGame with FPSCounter { +class DebugPinballGame extends PinballGame with FPSCounter, PanDetector { DebugPinballGame({ required CharacterTheme characterTheme, required PinballAudio audio, @@ -174,9 +174,14 @@ class DebugPinballGame extends PinballGame with FPSCounter { controller = _GameBallsController(this); } + Vector2? lineStart; + Vector2? lineEnd; + @override Future onLoad() async { await super.onLoad(); + await add(PreviewLine()); + await add(_DebugInformation()); } @@ -190,10 +195,57 @@ class DebugPinballGame extends PinballGame with FPSCounter { firstChild()?.add(ball); } } + + @override + void onPanStart(DragStartInfo info) { + lineStart = info.eventPosition.game; + } + + @override + void onPanUpdate(DragUpdateInfo info) { + lineEnd = info.eventPosition.game; + } + + @override + void onPanEnd(DragEndInfo info) { + if (lineEnd != null) { + final line = lineEnd! - lineStart!; + _turboChargeBall(line); + lineEnd = null; + lineStart = null; + } + } + + void _turboChargeBall(Vector2 line) { + final ball = ControlledBall.debug()..initialPosition = lineStart!; + final impulse = line * -1 * 10; + ball.add(BallTurboChargingBehavior(impulse: impulse)); + firstChild()?.add(ball); + } } -// TODO(wolfenrain): investigate this CI failure. // coverage:ignore-start +class PreviewLine extends PositionComponent with HasGameRef { + static final _previewLinePaint = Paint() + ..color = Colors.pink + ..strokeWidth = 0.4 + ..style = PaintingStyle.stroke; + + @override + void render(Canvas canvas) { + super.render(canvas); + + if (gameRef.lineEnd != null) { + canvas.drawLine( + gameRef.lineStart!.toOffset(), + gameRef.lineEnd!.toOffset(), + _previewLinePaint, + ); + } + } +} + +// TODO(wolfenrain): investigate this CI failure. class _DebugInformation extends Component with HasGameRef { @override PositionType get positionType => PositionType.widget; diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index 98aac670..5fed979f 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -25,6 +25,12 @@ class _MockTapUpDetails extends Mock implements TapUpDetails {} class _MockTapUpInfo extends Mock implements TapUpInfo {} +class _MockDragStartInfo extends Mock implements DragStartInfo {} + +class _MockDragUpdateInfo extends Mock implements DragUpdateInfo {} + +class _MockDragEndInfo extends Mock implements DragEndInfo {} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); final assets = [ @@ -438,8 +444,9 @@ void main() { }); group('DebugPinballGame', () { + final debugAssets = [Assets.images.ball.flameEffect.keyName, ...assets]; final debugModeFlameTester = FlameTester( - () => DebugPinballTestGame(assets: assets), + () => DebugPinballTestGame(assets: debugAssets), ); debugModeFlameTester.test( @@ -468,5 +475,68 @@ void main() { ); }, ); + + debugModeFlameTester.test( + 'set lineStart on pan start', + (game) async { + final startPosition = Vector2.all(10); + final eventPosition = _MockEventPosition(); + when(() => eventPosition.game).thenReturn(startPosition); + + final dragStartInfo = _MockDragStartInfo(); + when(() => dragStartInfo.eventPosition).thenReturn(eventPosition); + + game.onPanStart(dragStartInfo); + await game.ready(); + + expect( + game.lineStart, + equals(startPosition), + ); + }, + ); + + debugModeFlameTester.test( + 'set lineEnd on pan update', + (game) async { + final endPosition = Vector2.all(10); + final eventPosition = _MockEventPosition(); + when(() => eventPosition.game).thenReturn(endPosition); + + final dragUpdateInfo = _MockDragUpdateInfo(); + when(() => dragUpdateInfo.eventPosition).thenReturn(eventPosition); + + game.onPanUpdate(dragUpdateInfo); + await game.ready(); + + expect( + game.lineEnd, + equals(endPosition), + ); + }, + ); + + debugModeFlameTester.test( + 'launch ball on pan end', + (game) async { + final startPosition = Vector2.zero(); + final endPosition = Vector2.all(10); + + game.lineStart = startPosition; + game.lineEnd = endPosition; + + await game.ready(); + final previousBalls = + game.descendants().whereType().toList(); + + game.onPanEnd(_MockDragEndInfo()); + await game.ready(); + + expect( + game.descendants().whereType().length, + equals(previousBalls.length + 1), + ); + }, + ); }); }