diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart index c1db4b0b..19ab2d4b 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart @@ -1 +1 @@ -export 'ramp_contact_behavior.dart'; +export 'ramp_ball_contact_behavior.dart'; diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_contact_behavior.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_contact_behavior.dart similarity index 78% rename from packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_contact_behavior.dart rename to packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_contact_behavior.dart index cc8a492a..329a83ac 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_contact_behavior.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_contact_behavior.dart @@ -4,21 +4,20 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; -/// {@template ramp_contact_behavior} +/// {@template ramp_ball_contact_behavior} /// Detects a [Ball]that enters in the [SpaceshipRamp]. /// /// The [Ball] can hit with sensor to recognize if [Ball] goes in or out the /// [SpaceshipRamp]. /// {@endtemplate} -class RampContactBehavior extends ContactBehavior { +class RampBallContactBehavior extends ContactBehavior { @override void beginContact(Object other, Contact contact) { super.beginContact(other, contact); - if (other is! Ball) return; if (other.body.linearVelocity.y < 0) { - parent.parent.bloc.onInside(); + parent.parent.bloc.onBallInside(); } } } diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart index 93ce24b0..53b6d710 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart @@ -8,7 +8,7 @@ part 'spaceship_ramp_state.dart'; class SpaceshipRampCubit extends Cubit { SpaceshipRampCubit() : super(const SpaceshipRampState.initial()); - void onInside() { + void onBallInside() { emit( state.copyWith( hits: state.hits + 1, diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart index bb6f39a5..99d1b621 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart @@ -24,7 +24,7 @@ class SpaceshipRamp extends Component { // only one sensor. RampScoringSensor( children: [ - RampContactBehavior(), + RampBallContactBehavior(), ], )..initialPosition = Vector2(1.7, -20.4), _SpaceshipRampOpening( @@ -57,8 +57,7 @@ class SpaceshipRamp extends Component { @visibleForTesting SpaceshipRamp.test({ required this.bloc, - Iterable? children, - }) : super(children: children); + }) : super(); // TODO(alestiago): Consider refactoring once the following is merged: // https://github.com/flame-engine/flame/pull/1538 diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_contact_behavior_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_contact_behavior_test.dart similarity index 87% rename from packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_contact_behavior_test.dart rename to packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_contact_behavior_test.dart index cf78b8c0..a78455ab 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_contact_behavior_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_contact_behavior_test.dart @@ -36,12 +36,12 @@ void main() { final flameTester = FlameTester(() => TestGame(assets)); group( - 'RampContactBehavior', + 'RampBallContactBehavior', () { test('can be instantiated', () { expect( - RampContactBehavior(), - isA(), + RampBallContactBehavior(), + isA(), ); }); @@ -57,9 +57,9 @@ void main() { }); flameTester.test( - "calls 'onInside' when a ball enters into the ramp", + "calls 'onBallInside' when a ball enters into the ramp", (game) async { - final behavior = RampContactBehavior(); + final behavior = RampBallContactBehavior(); final bloc = _MockSpaceshipRampCubit(); whenListen( bloc, @@ -80,14 +80,14 @@ void main() { behavior.beginContact(ball, _MockContact()); - verify(bloc.onInside).called(1); + verify(bloc.onBallInside).called(1); }, ); flameTester.test( - "doesn't call 'onInside' when a ball goes out the ramp", + "doesn't call 'onBallInside' when a ball goes out the ramp", (game) async { - final behavior = RampContactBehavior(); + final behavior = RampBallContactBehavior(); final bloc = _MockSpaceshipRampCubit(); whenListen( bloc, @@ -108,7 +108,7 @@ void main() { behavior.beginContact(ball, _MockContact()); - verifyNever(bloc.onInside); + verifyNever(bloc.onBallInside); }, ); }); diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart index 97857e9f..4b05d428 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart @@ -6,14 +6,14 @@ import 'package:pinball_components/pinball_components.dart'; void main() { group('SpaceshipRampCubit', () { - group('onInside', () { + group('onBallInside', () { blocTest( 'emits hits incremented', build: SpaceshipRampCubit.new, act: (bloc) => bloc - ..onInside() - ..onInside() - ..onInside(), + ..onBallInside() + ..onBallInside() + ..onBallInside(), expect: () => [ SpaceshipRampState(hits: 1), SpaceshipRampState(hits: 2), diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart index 8eecae69..765bc055 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart @@ -6,6 +6,7 @@ import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_flame/pinball_flame.dart'; import '../../../helpers/helpers.dart'; @@ -33,10 +34,184 @@ void main() { (game) async { final spaceshipRamp = SpaceshipRamp(); await game.ensureAdd(spaceshipRamp); - expect(game.descendants(), contains(spaceshipRamp)); + expect(game.children, contains(spaceshipRamp)); }, ); + group('renders correctly', () { + const goldenFilePath = '../golden/spaceship_ramp/'; + final centerForSpaceshipRamp = Vector2(-13, -55); + + flameTester.testGameWidget( + 'inactive sprite', + setUp: (game, tester) async { + await game.images.loadAll(assets); + final component = SpaceshipRamp(); + final canvas = ZCanvasComponent(children: [component]); + await game.ensureAdd(canvas); + + await tester.pump(); + + expect( + component.children.whereType().first.current, + SpaceshipRampArrowSpriteState.inactive, + ); + + game.camera.followVector2(centerForSpaceshipRamp); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('${goldenFilePath}inactive.png'), + ); + }, + ); + + flameTester.testGameWidget( + 'active1 sprite', + setUp: (game, tester) async { + await game.images.loadAll(assets); + final component = SpaceshipRamp(); + final canvas = ZCanvasComponent(children: [component]); + await game.ensureAdd(canvas); + + component.progress(); + await tester.pump(); + + expect( + component.children.whereType().first.current, + SpaceshipRampArrowSpriteState.active1, + ); + + game.camera.followVector2(centerForSpaceshipRamp); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('${goldenFilePath}active1.png'), + ); + }, + ); + + flameTester.testGameWidget( + 'active2 sprite', + setUp: (game, tester) async { + await game.images.loadAll(assets); + final component = SpaceshipRamp(); + final canvas = ZCanvasComponent(children: [component]); + await game.ensureAdd(canvas); + + component + ..progress() + ..progress(); + await tester.pump(); + + expect( + component.children.whereType().first.current, + SpaceshipRampArrowSpriteState.active2, + ); + + game.camera.followVector2(centerForSpaceshipRamp); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('${goldenFilePath}active2.png'), + ); + }, + ); + + flameTester.testGameWidget( + 'active3 sprite', + setUp: (game, tester) async { + await game.images.loadAll(assets); + final component = SpaceshipRamp(); + final canvas = ZCanvasComponent(children: [component]); + await game.ensureAdd(canvas); + + component + ..progress() + ..progress() + ..progress(); + await tester.pump(); + + expect( + component.children.whereType().first.current, + SpaceshipRampArrowSpriteState.active3, + ); + + game.camera.followVector2(centerForSpaceshipRamp); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('${goldenFilePath}active3.png'), + ); + }, + ); + + flameTester.testGameWidget( + 'active4 sprite', + setUp: (game, tester) async { + await game.images.loadAll(assets); + final component = SpaceshipRamp(); + final canvas = ZCanvasComponent(children: [component]); + await game.ensureAdd(canvas); + + component + ..progress() + ..progress() + ..progress() + ..progress(); + await tester.pump(); + + expect( + component.children.whereType().first.current, + SpaceshipRampArrowSpriteState.active4, + ); + + game.camera.followVector2(centerForSpaceshipRamp); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('${goldenFilePath}active4.png'), + ); + }, + ); + + flameTester.testGameWidget( + 'active5 sprite', + setUp: (game, tester) async { + await game.images.loadAll(assets); + final component = SpaceshipRamp(); + final canvas = ZCanvasComponent(children: [component]); + await game.ensureAdd(canvas); + + component + ..progress() + ..progress() + ..progress() + ..progress() + ..progress(); + await tester.pump(); + + expect( + component.children.whereType().first.current, + SpaceshipRampArrowSpriteState.active5, + ); + + game.camera.followVector2(centerForSpaceshipRamp); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('${goldenFilePath}active5.png'), + ); + }, + ); + }); + flameTester.test('closes bloc when removed', (game) async { final bloc = _MockSpaceshipRampCubit(); whenListen( @@ -60,11 +235,12 @@ void main() { group('adds', () { flameTester.test('new children', (game) async { final component = Component(); - final spaceshipRamp = SpaceshipRamp( - children: [component], + final ramp = SpaceshipRamp.test( + bloc: _MockSpaceshipRampCubit(), ); - await game.ensureAdd(spaceshipRamp); - expect(spaceshipRamp.children, contains(component)); + await ramp.addAll([component]); + await game.ensureAdd(ramp); + expect(ramp.children, contains(component)); }); }); }); diff --git a/packages/pinball_components/test/src/components/spaceship_ramp_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp_test.dart deleted file mode 100644 index 7fd8923f..00000000 --- a/packages/pinball_components/test/src/components/spaceship_ramp_test.dart +++ /dev/null @@ -1,244 +0,0 @@ -// ignore_for_file: cascade_invocations - -import 'package:bloc_test/bloc_test.dart'; -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_components/pinball_components.dart'; -import 'package:pinball_flame/pinball_flame.dart'; - -import '../../helpers/helpers.dart'; - -class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {} - -void main() { - TestWidgetsFlutterBinding.ensureInitialized(); - final assets = [ - Assets.images.android.ramp.boardOpening.keyName, - Assets.images.android.ramp.railingForeground.keyName, - Assets.images.android.ramp.railingBackground.keyName, - Assets.images.android.ramp.main.keyName, - Assets.images.android.ramp.arrow.inactive.keyName, - Assets.images.android.ramp.arrow.active1.keyName, - Assets.images.android.ramp.arrow.active2.keyName, - Assets.images.android.ramp.arrow.active3.keyName, - Assets.images.android.ramp.arrow.active4.keyName, - Assets.images.android.ramp.arrow.active5.keyName, - ]; - final flameTester = FlameTester(() => TestGame(assets)); - - group('SpaceshipRamp', () { - flameTester.test('loads correctly', (game) async { - final component = SpaceshipRamp(); - await game.ensureAdd(component); - expect(game.contains(component), isTrue); - }); - - group('renders correctly', () { - const goldenFilePath = 'golden/spaceship_ramp/'; - final centerForSpaceshipRamp = Vector2(-13, -55); - - flameTester.testGameWidget( - 'inactive sprite', - setUp: (game, tester) async { - await game.images.loadAll(assets); - final component = SpaceshipRamp(); - final canvas = ZCanvasComponent(children: [component]); - await game.ensureAdd(canvas); - - await tester.pump(); - - expect( - component.children.whereType().first.current, - SpaceshipRampArrowSpriteState.inactive, - ); - - game.camera.followVector2(centerForSpaceshipRamp); - }, - verify: (game, tester) async { - await expectLater( - find.byGame(), - matchesGoldenFile('${goldenFilePath}inactive.png'), - ); - }, - ); - - flameTester.testGameWidget( - 'active1 sprite', - setUp: (game, tester) async { - await game.images.loadAll(assets); - final component = SpaceshipRamp(); - final canvas = ZCanvasComponent(children: [component]); - await game.ensureAdd(canvas); - - component.progress(); - await tester.pump(); - - expect( - component.children.whereType().first.current, - SpaceshipRampArrowSpriteState.active1, - ); - - game.camera.followVector2(centerForSpaceshipRamp); - }, - verify: (game, tester) async { - await expectLater( - find.byGame(), - matchesGoldenFile('${goldenFilePath}active1.png'), - ); - }, - ); - - flameTester.testGameWidget( - 'active2 sprite', - setUp: (game, tester) async { - await game.images.loadAll(assets); - final component = SpaceshipRamp(); - final canvas = ZCanvasComponent(children: [component]); - await game.ensureAdd(canvas); - - component - ..progress() - ..progress(); - await tester.pump(); - - expect( - component.children.whereType().first.current, - SpaceshipRampArrowSpriteState.active2, - ); - - game.camera.followVector2(centerForSpaceshipRamp); - }, - verify: (game, tester) async { - await expectLater( - find.byGame(), - matchesGoldenFile('${goldenFilePath}active2.png'), - ); - }, - ); - - flameTester.testGameWidget( - 'active3 sprite', - setUp: (game, tester) async { - await game.images.loadAll(assets); - final component = SpaceshipRamp(); - final canvas = ZCanvasComponent(children: [component]); - await game.ensureAdd(canvas); - - component - ..progress() - ..progress() - ..progress(); - await tester.pump(); - - expect( - component.children.whereType().first.current, - SpaceshipRampArrowSpriteState.active3, - ); - - game.camera.followVector2(centerForSpaceshipRamp); - }, - verify: (game, tester) async { - await expectLater( - find.byGame(), - matchesGoldenFile('${goldenFilePath}active3.png'), - ); - }, - ); - - flameTester.testGameWidget( - 'active4 sprite', - setUp: (game, tester) async { - await game.images.loadAll(assets); - final component = SpaceshipRamp(); - final canvas = ZCanvasComponent(children: [component]); - await game.ensureAdd(canvas); - - component - ..progress() - ..progress() - ..progress() - ..progress(); - await tester.pump(); - - expect( - component.children.whereType().first.current, - SpaceshipRampArrowSpriteState.active4, - ); - - game.camera.followVector2(centerForSpaceshipRamp); - }, - verify: (game, tester) async { - await expectLater( - find.byGame(), - matchesGoldenFile('${goldenFilePath}active4.png'), - ); - }, - ); - - flameTester.testGameWidget( - 'active5 sprite', - setUp: (game, tester) async { - await game.images.loadAll(assets); - final component = SpaceshipRamp(); - final canvas = ZCanvasComponent(children: [component]); - await game.ensureAdd(canvas); - - component - ..progress() - ..progress() - ..progress() - ..progress() - ..progress(); - await tester.pump(); - - expect( - component.children.whereType().first.current, - SpaceshipRampArrowSpriteState.active5, - ); - - game.camera.followVector2(centerForSpaceshipRamp); - }, - verify: (game, tester) async { - await expectLater( - find.byGame(), - matchesGoldenFile('${goldenFilePath}active5.png'), - ); - }, - ); - }); - - flameTester.test('closes bloc when removed', (game) async { - final bloc = _MockSpaceshipRampCubit(); - whenListen( - bloc, - const Stream.empty(), - initialState: const SpaceshipRampState.initial(), - ); - when(bloc.close).thenAnswer((_) async {}); - - final ramp = SpaceshipRamp.test( - bloc: bloc, - ); - - await game.ensureAdd(ramp); - game.remove(ramp); - await game.ready(); - - verify(bloc.close).called(1); - }); - - group('adds', () { - flameTester.test('new children', (game) async { - final component = Component(); - final ramp = SpaceshipRamp.test( - bloc: _MockSpaceshipRampCubit(), - children: [component], - ); - await game.ensureAdd(ramp); - expect(ramp.children, contains(component)); - }); - }); - }); -}