diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior.dart index 57895c45..db98a30a 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior.dart @@ -9,7 +9,7 @@ import 'package:pinball_flame/pinball_flame.dart'; /// the [SpaceshipRamp]. /// {@endtemplate} class RampBallAscendingContactBehavior - extends ContactBehavior { + extends ContactBehavior { @override void beginContact(Object other, Contact contact) { super.beginContact(other, contact); 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 ea7ed2e7..07a5e79b 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 @@ -27,11 +27,6 @@ class SpaceshipRamp extends Component { required this.bloc, }) : super( children: [ - RampScoringSensor( - children: [ - RampBallAscendingContactBehavior(), - ], - )..initialPosition = Vector2(1.7, -20.4), _SpaceshipRampOpening( outsideLayer: Layer.spaceship, outsidePriority: ZIndexes.ballOnSpaceship, @@ -40,7 +35,7 @@ class SpaceshipRamp extends Component { ..initialPosition = Vector2(-13.7, -18.6) ..layer = Layer.spaceshipEntranceRamp, _SpaceshipRampBackground(), - _SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5), + SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5), _SpaceshipRampForegroundRailing(), SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5), _SpaceshipRampBackgroundRailingSpriteComponent(), @@ -246,13 +241,14 @@ extension on SpaceshipRampArrowSpriteState { } } -class _SpaceshipRampBoardOpening extends BodyComponent - with Layered, ZIndex, InitialPosition { - _SpaceshipRampBoardOpening() +class SpaceshipRampBoardOpening extends BodyComponent + with Layered, ZIndex, InitialPosition, ParentIsA { + SpaceshipRampBoardOpening() : super( renderBody: false, children: [ _SpaceshipRampBoardOpeningSpriteComponent(), + RampBallAscendingContactBehavior()..applyTo(['inside']), LayerContactBehavior(layer: Layer.spaceshipEntranceRamp) ..applyTo(['inside']), LayerContactBehavior( @@ -271,6 +267,13 @@ class _SpaceshipRampBoardOpening extends BodyComponent layer = Layer.opening; } + /// Creates a [SpaceshipRampBoardOpening] without any children. + /// + /// This can be used for testing [SpaceshipRampBoardOpening]'s behaviors in + /// isolation. + @visibleForTesting + SpaceshipRampBoardOpening.test(); + List _createFixtureDefs() { final topEdge = EdgeShape() ..set( @@ -495,46 +498,3 @@ class _SpaceshipRampOpening extends LayerSensor { ); } } - -/// {@template ramp_scoring_sensor} -/// Small sensor body used to detect when a ball has entered the -/// [SpaceshipRamp]. -/// {@endtemplate} -class RampScoringSensor extends BodyComponent - with ParentIsA, InitialPosition, Layered { - /// {@macro ramp_scoring_sensor} - RampScoringSensor({ - Iterable? children, - }) : super( - children: children, - renderBody: false, - ) { - layer = Layer.spaceshipEntranceRamp; - } - - /// Creates a [RampScoringSensor] without any children. - @visibleForTesting - RampScoringSensor.test(); - - @override - Body createBody() { - final shape = PolygonShape() - ..setAsBox( - 2.6, - .5, - initialPosition, - -5 * math.pi / 180, - ); - - final fixtureDef = FixtureDef( - shape, - isSensor: true, - ); - final bodyDef = BodyDef( - position: initialPosition, - userData: this, - ); - - return world.createBody(bodyDef)..createFixture(fixtureDef); - } -} diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior_test.dart index ea37550a..d1f03ce7 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_ball_ascending_contact_behavior_test.dart @@ -67,16 +67,16 @@ void main() { initialState: const SpaceshipRampState.initial(), ); - final rampSensor = RampScoringSensor.test(); + final parent = SpaceshipRampBoardOpening.test(); final spaceshipRamp = SpaceshipRamp.test( bloc: bloc, ); when(() => body.linearVelocity).thenReturn(Vector2(0, -1)); - await spaceshipRamp.add(rampSensor); + await spaceshipRamp.add(parent); await game.ensureAddAll([spaceshipRamp, ball]); - await rampSensor.add(behavior); + await parent.add(behavior); behavior.beginContact(ball, _MockContact()); @@ -95,16 +95,16 @@ void main() { initialState: const SpaceshipRampState.initial(), ); - final rampSensor = RampScoringSensor.test(); + final parent = SpaceshipRampBoardOpening.test(); final spaceshipRamp = SpaceshipRamp.test( bloc: bloc, ); when(() => body.linearVelocity).thenReturn(Vector2(0, 1)); - await spaceshipRamp.add(rampSensor); + await spaceshipRamp.add(parent); await game.ensureAddAll([spaceshipRamp, ball]); - await rampSensor.add(behavior); + await parent.add(behavior); behavior.beginContact(ball, _MockContact()); 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 7bd18aeb..1c9c968d 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 @@ -7,6 +7,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_components/src/components/spaceship_ramp/behavior/behavior.dart'; import 'package:pinball_flame/pinball_flame.dart'; import '../../../helpers/helpers.dart'; @@ -324,4 +325,29 @@ void main() { }, ); }); + + group('SpaceshipRampBoardOpening', () { + test('can be instantiated', () { + expect(SpaceshipRampBoardOpening(), isA()); + }); + + flameTester.test('can be loaded', (game) async { + final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit()); + final component = SpaceshipRampBoardOpening(); + await game.ensureAdd(parent); + await parent.ensureAdd(component); + expect(parent.children, contains(component)); + }); + + flameTester.test('adds a RampBallAscendingContactBehavior', (game) async { + final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit()); + final component = SpaceshipRampBoardOpening(); + await game.ensureAdd(parent); + await parent.ensureAdd(component); + expect( + component.children.whereType().length, + equals(1), + ); + }); + }); }