diff --git a/packages/pinball_components/lib/src/components/ball.dart b/packages/pinball_components/lib/src/components/ball.dart index f0fb2e7c..36059cfd 100644 --- a/packages/pinball_components/lib/src/components/ball.dart +++ b/packages/pinball_components/lib/src/components/ball.dart @@ -14,13 +14,18 @@ class Ball extends BodyComponent /// {@macro ball} Ball({ required this.baseColor, - }) { + }) : super( + children: [ + _BallSpriteComponent()..tint(baseColor.withOpacity(0.5)), + ], + ) { // TODO(ruimiguel): while developing Ball can be launched by clicking mouse, // and default layer is Layer.all. But on final game Ball will be always be // be launched from Plunger and LauncherRamp will modify it to Layer.board. // We need to see what happens if Ball appears from other place like nest // bumper, it will need to explicit change layer to Layer.board then. layer = Layer.board; + renderBody = false; } /// Render priority for the [Ball] while it's on the board. @@ -47,20 +52,6 @@ class Ball extends BodyComponent double _boostTimer = 0; static const _boostDuration = 2.0; - final _BallSpriteComponent _spriteComponent = _BallSpriteComponent(); - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - - await add( - _spriteComponent..tint(baseColor.withOpacity(0.5)), - ); - - renderBody = false; - } - @override Body createBody() { final shape = CircleShape()..radius = size.x / 2; @@ -132,7 +123,10 @@ class Ball extends BodyComponent ((standardizedYPosition / boardHeight) * (1 - maxShrinkValue)); body.fixtures.first.shape.radius = (size.x / 2) * scaleFactor; - _spriteComponent.scale = Vector2.all(scaleFactor); + + // TODO(alestiago): Revisit and see if there's a better way to do this. + final spriteComponent = firstChild<_BallSpriteComponent>(); + spriteComponent?.scale = Vector2.all(scaleFactor); } void _setPositionalGravity() { diff --git a/packages/pinball_components/lib/src/components/baseboard.dart b/packages/pinball_components/lib/src/components/baseboard.dart index 03826d6c..29d7285b 100644 --- a/packages/pinball_components/lib/src/components/baseboard.dart +++ b/packages/pinball_components/lib/src/components/baseboard.dart @@ -11,7 +11,12 @@ class Baseboard extends BodyComponent with InitialPosition { /// {@macro baseboard} Baseboard({ required BoardSide side, - }) : _side = side; + }) : _side = side, + super( + children: [_BaseboardSpriteComponent(side: side)], + ) { + renderBody = false; + } /// Whether the [Baseboard] is on the left or right side of the board. final BoardSide _side; @@ -79,13 +84,6 @@ class Baseboard extends BodyComponent with InitialPosition { return fixturesDef; } - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - await add(_BaseboardSpriteComponent(side: _side)); - } - @override Body createBody() { const angle = 37.1 * (math.pi / 180); diff --git a/packages/pinball_components/lib/src/components/boundaries.dart b/packages/pinball_components/lib/src/components/boundaries.dart index 6d582d8c..983b4839 100644 --- a/packages/pinball_components/lib/src/components/boundaries.dart +++ b/packages/pinball_components/lib/src/components/boundaries.dart @@ -24,7 +24,13 @@ class Boundaries extends Forge2DBlueprint { /// {@endtemplate bottom_boundary} class _BottomBoundary extends BodyComponent with InitialPosition { /// {@macro bottom_boundary} - _BottomBoundary() : super(priority: 1); + _BottomBoundary() + : super( + priority: 1, + children: [_BottomBoundarySpriteComponent()], + ) { + renderBody = false; + } List _createFixtureDefs() { final fixturesDefs = []; @@ -60,13 +66,6 @@ class _BottomBoundary extends BodyComponent with InitialPosition { return body; } - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - await add(_BottomBoundarySpriteComponent()); - } } class _BottomBoundarySpriteComponent extends SpriteComponent with HasGameRef { @@ -89,7 +88,13 @@ class _BottomBoundarySpriteComponent extends SpriteComponent with HasGameRef { /// {@endtemplate outer_boundary} class _OuterBoundary extends BodyComponent with InitialPosition { /// {@macro outer_boundary} - _OuterBoundary() : super(priority: Ball.launchRampPriority - 1); + _OuterBoundary() + : super( + priority: Ball.launchRampPriority - 1, + children: [_OuterBoundarySpriteComponent()], + ) { + renderBody = false; + } List _createFixtureDefs() { final fixturesDefs = []; @@ -131,13 +136,6 @@ class _OuterBoundary extends BodyComponent with InitialPosition { return body; } - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - await add(_OuterBoundarySpriteComponent()); - } } class _OuterBoundarySpriteComponent extends SpriteComponent with HasGameRef { diff --git a/packages/pinball_components/lib/src/components/dino_walls.dart b/packages/pinball_components/lib/src/components/dino_walls.dart index 0c287491..9bc0ba09 100644 --- a/packages/pinball_components/lib/src/components/dino_walls.dart +++ b/packages/pinball_components/lib/src/components/dino_walls.dart @@ -29,7 +29,13 @@ class DinoWalls extends Forge2DBlueprint { /// {@endtemplate} class _DinoTopWall extends BodyComponent with InitialPosition { ///{@macro dino_top_wall} - _DinoTopWall() : super(priority: 1); + _DinoTopWall() + : super( + priority: 1, + children: [_DinoTopWallSpriteComponent()], + ) { + renderBody = false; + } List _createFixtureDefs() { final fixturesDef = []; @@ -98,14 +104,6 @@ class _DinoTopWall extends BodyComponent with InitialPosition { return body; } - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - - await add(_DinoTopWallSpriteComponent()); - } } class _DinoTopWallSpriteComponent extends SpriteComponent with HasGameRef { @@ -126,7 +124,12 @@ class _DinoTopWallSpriteComponent extends SpriteComponent with HasGameRef { /// {@endtemplate} class _DinoBottomWall extends BodyComponent with InitialPosition { ///{@macro dino_top_wall} - _DinoBottomWall(); + _DinoBottomWall() + : super( + children: [_DinoBottomWallSpriteComponent()], + ) { + renderBody = false; + } List _createFixtureDefs() { final fixturesDef = []; @@ -206,14 +209,6 @@ class _DinoBottomWall extends BodyComponent with InitialPosition { return body; } - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - - await add(_DinoBottomWallSpriteComponent()); - } } class _DinoBottomWallSpriteComponent extends SpriteComponent with HasGameRef { diff --git a/packages/pinball_components/lib/src/components/flipper.dart b/packages/pinball_components/lib/src/components/flipper.dart index f825070a..1d0a3c5d 100644 --- a/packages/pinball_components/lib/src/components/flipper.dart +++ b/packages/pinball_components/lib/src/components/flipper.dart @@ -13,7 +13,11 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { /// {@macro flipper} Flipper({ required this.side, - }); + }) : super( + children: [_FlipperSpriteComponent(side: side)], + ) { + renderBody = false; + } /// The size of the [Flipper]. static final size = Vector2(13.5, 4.3); @@ -112,10 +116,8 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { @override Future onLoad() async { await super.onLoad(); - renderBody = false; await _anchorToJoint(); - await add(_FlipperSpriteComponent(side: side)); } @override diff --git a/packages/pinball_components/lib/src/components/flutter_sign_post.dart b/packages/pinball_components/lib/src/components/flutter_sign_post.dart index 026c68b9..d7999183 100644 --- a/packages/pinball_components/lib/src/components/flutter_sign_post.dart +++ b/packages/pinball_components/lib/src/components/flutter_sign_post.dart @@ -6,12 +6,12 @@ import 'package:pinball_components/pinball_components.dart'; /// A sign, found in the Flutter Forest. /// {@endtemplate} class FlutterSignPost extends BodyComponent with InitialPosition { - @override - Future onLoad() async { - await super.onLoad(); + /// {@macro flutter_sign_post} + FlutterSignPost() + : super( + children: [_FlutterSignPostSpriteComponent()], + ) { renderBody = false; - - await add(_FlutterSignPostSpriteComponent()); } @override diff --git a/packages/pinball_components/lib/src/components/kicker.dart b/packages/pinball_components/lib/src/components/kicker.dart index a932b441..26e6c8f9 100644 --- a/packages/pinball_components/lib/src/components/kicker.dart +++ b/packages/pinball_components/lib/src/components/kicker.dart @@ -16,7 +16,12 @@ class Kicker extends BodyComponent with InitialPosition { /// {@macro kicker} Kicker({ required BoardSide side, - }) : _side = side; + }) : _side = side, + super( + children: [_KickerSpriteComponent(side: side)], + ) { + renderBody = false; + } /// The size of the [Kicker] body. static final Vector2 size = Vector2(4.4, 15); @@ -120,13 +125,6 @@ class Kicker extends BodyComponent with InitialPosition { return body; } - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - await add(_KickerSpriteComponent(side: _side)); - } } class _KickerSpriteComponent extends SpriteComponent with HasGameRef { diff --git a/packages/pinball_components/lib/src/components/slingshot.dart b/packages/pinball_components/lib/src/components/slingshot.dart index a95055cb..b460565b 100644 --- a/packages/pinball_components/lib/src/components/slingshot.dart +++ b/packages/pinball_components/lib/src/components/slingshot.dart @@ -42,15 +42,17 @@ class Slingshot extends BodyComponent with InitialPosition { required String spritePath, }) : _length = length, _angle = angle, - _spritePath = spritePath, - super(priority: 1); + super( + priority: 1, + children: [_SlinghsotSpriteComponent(spritePath, angle: angle)], + ) { + renderBody = false; + } final double _length; final double _angle; - final String _spritePath; - List _createFixtureDefs() { final fixturesDef = []; const circleRadius = 1.55; @@ -104,24 +106,25 @@ class Slingshot extends BodyComponent with InitialPosition { return body; } +} + +class _SlinghsotSpriteComponent extends SpriteComponent with HasGameRef { + _SlinghsotSpriteComponent( + String path, { + required double angle, + }) : _path = path, + super( + angle: -angle, + anchor: Anchor.center, + ); + + final String _path; @override Future onLoad() async { await super.onLoad(); - await _loadSprite(); - renderBody = false; - } - - Future _loadSprite() async { - final sprite = await gameRef.loadSprite(_spritePath); - - await add( - SpriteComponent( - sprite: sprite, - size: sprite.originalSize / 10, - anchor: Anchor.center, - angle: -_angle, - ), - ); + final sprite = await gameRef.loadSprite(_path); + this.sprite = sprite; + size = sprite.originalSize / 10; } } diff --git a/packages/pinball_components/lib/src/components/spaceship.dart b/packages/pinball_components/lib/src/components/spaceship.dart index 84ee8733..7de430a6 100644 --- a/packages/pinball_components/lib/src/components/spaceship.dart +++ b/packages/pinball_components/lib/src/components/spaceship.dart @@ -92,16 +92,13 @@ class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered { /// {@endtemplate} class AndroidHead extends BodyComponent with InitialPosition, Layered { /// {@macro spaceship_bridge} - AndroidHead() : super(priority: Ball.spaceshipPriority + 1) { - layer = Layer.spaceship; - } - - @override - Future onLoad() async { - await super.onLoad(); + AndroidHead() + : super( + priority: Ball.spaceshipPriority + 1, + children: [_AndroidHeadSpriteAnimation()], + ) { renderBody = false; - - await add(_AndroidHeadSpriteAnimation()); + layer = Layer.spaceship; } @override diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp.dart index bb2eac59..5be55193 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp.dart @@ -170,8 +170,12 @@ class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent class _SpaceshipRampForegroundRailing extends BodyComponent with InitialPosition, Layered { _SpaceshipRampForegroundRailing() - : super(priority: Ball.spaceshipRampPriority + 1) { + : super( + priority: Ball.spaceshipRampPriority + 1, + children: [_SpaceshipRampForegroundRailingSpriteComponent()], + ) { layer = Layer.spaceshipEntranceRamp; + renderBody = false; } List _createFixtureDefs() { @@ -222,14 +226,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent return body; } - - @override - Future onLoad() async { - await super.onLoad(); - renderBody = false; - - await add(_SpaceshipRampForegroundRailingSpriteComponent()); - } } class _SpaceshipRampForegroundRailingSpriteComponent extends SpriteComponent diff --git a/packages/pinball_components/test/src/components/boundaries_test.dart b/packages/pinball_components/test/src/components/boundaries_test.dart index 7911026d..e62d63e3 100644 --- a/packages/pinball_components/test/src/components/boundaries_test.dart +++ b/packages/pinball_components/test/src/components/boundaries_test.dart @@ -18,6 +18,7 @@ void main() { await game.addFromBlueprint(Boundaries()); game.camera.followVector2(Vector2.zero()); game.camera.zoom = 3.9; + await game.ready(); }, verify: (game, tester) async { await expectLater( diff --git a/packages/pinball_components/test/src/components/dino_walls_test.dart b/packages/pinball_components/test/src/components/dino_walls_test.dart index f124f26e..b3a58264 100644 --- a/packages/pinball_components/test/src/components/dino_walls_test.dart +++ b/packages/pinball_components/test/src/components/dino_walls_test.dart @@ -19,6 +19,7 @@ void main() { await game.addFromBlueprint(DinoWalls()); game.camera.followVector2(Vector2.zero()); game.camera.zoom = 6.5; + await game.ready(); }, verify: (game, tester) async { await expectLater( diff --git a/packages/pinball_components/test/src/components/slingshot_test.dart b/packages/pinball_components/test/src/components/slingshot_test.dart index 9a0be664..a5535750 100644 --- a/packages/pinball_components/test/src/components/slingshot_test.dart +++ b/packages/pinball_components/test/src/components/slingshot_test.dart @@ -20,6 +20,7 @@ void main() { setUp: (game, tester) async { await game.addFromBlueprint(Slingshots()); game.camera.followVector2(Vector2.zero()); + await game.ready(); }, verify: (game, tester) async { await expectLater( 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 da97ea68..8b623461 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp_test.dart @@ -17,6 +17,7 @@ void main() { setUp: (game, tester) async { await game.addFromBlueprint(SpaceshipRamp()); game.camera.followVector2(Vector2(-13, -50)); + await game.ready(); }, verify: (game, tester) async { await expectLater( diff --git a/packages/pinball_components/test/src/components/spaceship_test.dart b/packages/pinball_components/test/src/components/spaceship_test.dart index d5c6b491..e6c38476 100644 --- a/packages/pinball_components/test/src/components/spaceship_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_test.dart @@ -47,6 +47,7 @@ void main() { final position = Vector2(30, -30); await game.addFromBlueprint(Spaceship(position: position)); game.camera.followVector2(position); + await game.ready(); }, verify: (game, tester) async { await expectLater(