diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp.dart index 3229075b..a10afd67 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp.dart @@ -34,14 +34,17 @@ class SpaceshipRamp extends Blueprint { _SpaceshipRampForegroundRailing(), _SpaceshipRampBase()..initialPosition = Vector2(1.7, -20), _SpaceshipRampBackgroundRailingSpriteComponent(), - SpaceshipRampArrowSpriteComponent(), + _SpaceshipRampArrowSpriteComponent(), ], ); /// Forwards the sprite to the next [SpaceshipRampArrowSpriteState]. /// /// If the current state is the last one it cycles back to the initial state. - void progress() => children.firstChild()?.progress(); + void progress() => components + .whereType<_SpaceshipRampArrowSpriteComponent>() + .first + .progress(); } /// Indicates the state of the arrow on the [SpaceshipRamp]. @@ -170,7 +173,7 @@ class _SpaceshipRampBackgroundRailingSpriteComponent extends SpriteComponent position: Vector2(-11.7, -54.3), priority: RenderPriority.spaceshipRampBackgroundRailing, ); - + @override Future onLoad() async { await super.onLoad(); @@ -204,13 +207,13 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent /// {@template spaceship_ramp_arrow_sprite_component} /// An arrow inside [SpaceshipRamp]. /// -/// Lights up a each dash whenever a [Ball] gets into [SpaceshipRamp]. +/// Lights progressively whenever a [Ball] gets into [SpaceshipRamp]. /// {@endtemplate} -class SpaceshipRampArrowSpriteComponent +class _SpaceshipRampArrowSpriteComponent extends SpriteGroupComponent with HasGameRef { /// {@macro spaceship_ramp_arrow_sprite_component} - SpaceshipRampArrowSpriteComponent() + _SpaceshipRampArrowSpriteComponent() : super( anchor: Anchor.center, position: Vector2(-3.9, -56.5), @@ -238,6 +241,8 @@ class SpaceshipRampArrowSpriteComponent class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent with HasGameRef { + _SpaceshipRampBoardOpeningSpriteComponent() : super(anchor: Anchor.center); + @override Future onLoad() async { await super.onLoad(); @@ -248,7 +253,6 @@ class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent ); this.sprite = sprite; size = sprite.originalSize / 10; - anchor = Anchor.center; } } @@ -264,8 +268,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent } List _createFixtureDefs() { - final fixturesDef = []; - final innerLeftCurveShape = BezierCurveShape( controlPoints: [ Vector2(-24.5, -38), @@ -273,10 +275,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent Vector2(-13.8, -64.5), ], ); - - final innerLeftCurveFixtureDef = FixtureDef(innerLeftCurveShape); - fixturesDef.add(innerLeftCurveFixtureDef); - final innerRightCurveShape = BezierCurveShape( controlPoints: [ innerLeftCurveShape.vertices.last, @@ -284,28 +282,22 @@ class _SpaceshipRampForegroundRailing extends BodyComponent Vector2(0, -44.5), ], ); - - final innerRightCurveFixtureDef = FixtureDef(innerRightCurveShape); - fixturesDef.add(innerRightCurveFixtureDef); - final boardOpeningEdgeShape = EdgeShape() ..set( innerRightCurveShape.vertices.last, Vector2(-0.85, -40.8), ); - final boardOpeningEdgeShapeFixtureDef = FixtureDef(boardOpeningEdgeShape); - fixturesDef.add(boardOpeningEdgeShapeFixtureDef); - return fixturesDef; + return [ + FixtureDef(innerLeftCurveShape), + FixtureDef(innerRightCurveShape), + FixtureDef(boardOpeningEdgeShape), + ]; } @override Body createBody() { - final bodyDef = BodyDef( - position: initialPosition, - userData: this, - ); - + final bodyDef = BodyDef(position: initialPosition); final body = world.createBody(bodyDef); _createFixtureDefs().forEach(body.createFixture); @@ -354,10 +346,7 @@ class _SpaceshipRampBase extends BodyComponent with InitialPosition, Layered { ], ); final fixtureDef = FixtureDef(baseShape); - final bodyDef = BodyDef( - position: initialPosition, - userData: this, - ); + final bodyDef = BodyDef(position: initialPosition); return world.createBody(bodyDef)..createFixture(fixtureDef); } diff --git a/packages/pinball_components/test/src/components/spaceship_ramp_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp_test.dart index 4e5d149b..064800bf 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp_test.dart @@ -1,7 +1,6 @@ // ignore_for_file: cascade_invocations import 'package:flame/components.dart'; -import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball_components/pinball_components.dart'; @@ -46,10 +45,9 @@ void main() { final spaceshipRamp = SpaceshipRamp(); await game.addFromBlueprint(spaceshipRamp); await game.ready(); - await tester.pump(); expect( - spaceshipRamp.spaceshipRampArrow.current, + spaceshipRamp.firstChild()!.current, SpaceshipRampArrowSpriteState.inactive, ); @@ -70,11 +68,9 @@ void main() { final spaceshipRamp = SpaceshipRamp(); await game.addFromBlueprint(spaceshipRamp); await game.ready(); - spaceshipRamp.progress(); - await tester.pump(); expect( - spaceshipRamp.spaceshipRampArrow.current, + spaceshipRamp.firstChild()!.current, SpaceshipRampArrowSpriteState.active1, ); @@ -94,14 +90,13 @@ void main() { await game.images.loadAll(assets); final spaceshipRamp = SpaceshipRamp(); await game.addFromBlueprint(spaceshipRamp); - await game.ready(); spaceshipRamp ..progress() ..progress(); - await tester.pump(); + await game.ready(); expect( - spaceshipRamp.spaceshipRampArrow.current, + spaceshipRamp.firstChild()!.current, SpaceshipRampArrowSpriteState.active2, ); @@ -126,10 +121,10 @@ void main() { ..progress() ..progress() ..progress(); - await tester.pump(); + await game.ready(); expect( - spaceshipRamp.spaceshipRampArrow.current, + spaceshipRamp.firstChild()!.current, SpaceshipRampArrowSpriteState.active3, ); @@ -149,16 +144,15 @@ void main() { await game.images.loadAll(assets); final spaceshipRamp = SpaceshipRamp(); await game.addFromBlueprint(spaceshipRamp); - await game.ready(); spaceshipRamp ..progress() ..progress() ..progress() ..progress(); - await tester.pump(); + await game.ready(); expect( - spaceshipRamp.spaceshipRampArrow.current, + spaceshipRamp.firstChild()!.current, SpaceshipRampArrowSpriteState.active4, ); @@ -178,17 +172,16 @@ void main() { await game.images.loadAll(assets); final spaceshipRamp = SpaceshipRamp(); await game.addFromBlueprint(spaceshipRamp); - await game.ready(); spaceshipRamp ..progress() ..progress() ..progress() ..progress() ..progress(); - await tester.pump(); + await game.ready(); expect( - spaceshipRamp.spaceshipRampArrow.current, + spaceshipRamp.firstChild()!.current, SpaceshipRampArrowSpriteState.active5, );