diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index ff4754ea..82493a0c 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -238,14 +238,14 @@ class FlipperAnchor extends JointAnchor { /// {@macro flipper_anchor} FlipperAnchor({ required Flipper flipper, - }) : super( - position: Vector2( - flipper.side.isLeft - ? flipper.body.position.x - Flipper.width / 2 - : flipper.body.position.x + Flipper.width / 2, - flipper.body.position.y, - ), - ); + }) { + initialPosition = Vector2( + flipper.side.isLeft + ? flipper.body.position.x - Flipper.width / 2 + : flipper.body.position.x + Flipper.width / 2, + flipper.body.position.y, + ); + } } /// {@template flipper_anchor_revolute_joint_def} diff --git a/lib/game/components/joint_anchor.dart b/lib/game/components/joint_anchor.dart index 05e62b73..06f603b8 100644 --- a/lib/game/components/joint_anchor.dart +++ b/lib/game/components/joint_anchor.dart @@ -1,11 +1,12 @@ import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:pinball/game/game.dart'; /// {@template joint_anchor} /// Non visual [BodyComponent] used to hold a [BodyType.dynamic] in [Joint]s /// with this [BodyType.static]. /// -/// It is recommended to [_position] the anchor first and then use the body -/// position as the anchor point when initializing a [JointDef]. +/// It is recommended to use [JointAnchor.body.position] to position the anchor +/// point when initializing a [JointDef]. /// /// ```dart /// initialize( @@ -15,17 +16,13 @@ import 'package:flame_forge2d/flame_forge2d.dart'; /// ); /// ``` /// {@endtemplate} -class JointAnchor extends BodyComponent { +class JointAnchor extends BodyComponent with InitialPosition { /// {@macro joint_anchor} - JointAnchor({ - required Vector2 position, - }) : _position = position; - - final Vector2 _position; + JointAnchor(); @override Body createBody() { - final bodyDef = BodyDef()..position = _position; + final bodyDef = BodyDef()..position = initialPosition; return world.createBody(bodyDef); } diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 77168bb2..9bd8c4b7 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -98,12 +98,12 @@ class PlungerAnchor extends JointAnchor { /// {@macro plunger_anchor} PlungerAnchor({ required Plunger plunger, - }) : super( - position: Vector2( - plunger.body.position.x, - plunger.body.position.y - plunger.compressionDistance, - ), - ); + }) { + initialPosition = Vector2( + plunger.body.position.x, + plunger.body.position.y - plunger.compressionDistance, + ); + } } /// {@template plunger_anchor_prismatic_joint_def} diff --git a/test/game/components/joint_anchor_test.dart b/test/game/components/joint_anchor_test.dart index 8278d43a..7062a180 100644 --- a/test/game/components/joint_anchor_test.dart +++ b/test/game/components/joint_anchor_test.dart @@ -13,7 +13,7 @@ void main() { flameTester.test( 'loads correctly', (game) async { - final anchor = JointAnchor(position: Vector2.zero()); + final anchor = JointAnchor()..initialPosition = Vector2.zero(); await game.ready(); await game.ensureAdd(anchor); @@ -27,7 +27,7 @@ void main() { (game) async { await game.ready(); final position = Vector2.all(10); - final anchor = JointAnchor(position: position); + final anchor = JointAnchor()..initialPosition = position; await game.ensureAdd(anchor); game.contains(anchor); @@ -39,7 +39,7 @@ void main() { 'is static', (game) async { await game.ready(); - final anchor = JointAnchor(position: Vector2.zero()); + final anchor = JointAnchor()..initialPosition = Vector2.zero(); await game.ensureAdd(anchor); expect(anchor.body.bodyType, equals(BodyType.static)); @@ -51,7 +51,7 @@ void main() { flameTester.test( 'has none', (game) async { - final anchor = JointAnchor(position: Vector2.zero()); + final anchor = JointAnchor()..initialPosition = Vector2.zero(); await game.ensureAdd(anchor); expect(anchor.body.fixtures, isEmpty);