From a6d58ef3ab1400f1bf670cb2d5aced0591427b8a Mon Sep 17 00:00:00 2001 From: alestiago Date: Tue, 22 Mar 2022 17:16:33 +0000 Subject: [PATCH] refactor: defined FlipperJoint --- lib/game/components/flipper.dart | 87 +++++++++++++++++--------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index cf3fed4f..fc4f8ccc 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -113,16 +113,12 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { flipper: this, anchor: anchor, ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final joint = world.createJoint(jointDef) as RevoluteJoint; + final joint = _FlipperJoint(jointDef)..create(world); // FIXME(erickzanardo): when mounted the initial position is not fully // reached. unawaited( - mounted.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock(joint, side), - ), + mounted.whenComplete(joint.unlock), ); } @@ -211,21 +207,28 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { } } -/// {@template flipper_anchor} -/// [JointAnchor] positioned at the end of a [Flipper]. -/// -/// The end of a [Flipper] depends on its [Flipper.side]. -/// {@endtemplate} -class FlipperAnchor extends JointAnchor { - /// {@macro flipper_anchor} - FlipperAnchor({ - required Flipper flipper, - }) { - initialPosition = Vector2( - flipper.side.isLeft - ? flipper.body.position.x - Flipper.size.x / 2 - : flipper.body.position.x + Flipper.size.x / 2, - flipper.body.position.y, +class _FlipperJoint extends RevoluteJoint { + _FlipperJoint(FlipperAnchorRevoluteJointDef def) + : side = def.side, + super(def); + + final BoardSide side; + + // TODO(alestiago): Remove once Forge2D supports custom joints. + void create(World world) { + world.joints.add(this); + bodyA.joints.add(this); + bodyB.joints.add(this); + } + + /// Unlocks the [Flipper] from its resting position. + /// + /// The [Flipper] is locked when initialized in order to force it to be at + /// its resting position. + void unlock() { + setLimits( + lowerLimit * side.direction, + -upperLimit * side.direction, ); } } @@ -238,39 +241,39 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { FlipperAnchorRevoluteJointDef({ required Flipper flipper, required FlipperAnchor anchor, - }) { + }) : side = flipper.side { initialize( flipper.body, anchor.body, anchor.body.position, ); - enableLimit = true; + enableLimit = true; final angle = (flipper.side.isLeft ? _sweepingAngle : -_sweepingAngle) / 2; lowerAngle = upperAngle = angle; } + final BoardSide side; + /// The total angle of the arc motion. static const _sweepingAngle = math.pi / 3.5; +} - /// Unlocks the [Flipper] from its resting position. - /// - /// The [Flipper] is locked when initialized in order to force it to be at - /// its resting position. - // TODO(alestiago): consider refactor once the issue is solved: - // https://github.com/flame-engine/forge2d/issues/36 - static void unlock(RevoluteJoint joint, BoardSide side) { - late final double upperLimit, lowerLimit; - switch (side) { - case BoardSide.left: - lowerLimit = -joint.lowerLimit; - upperLimit = joint.upperLimit; - break; - case BoardSide.right: - lowerLimit = joint.lowerLimit; - upperLimit = -joint.upperLimit; - } - - joint.setLimits(lowerLimit, upperLimit); +/// {@template flipper_anchor} +/// [JointAnchor] positioned at the end of a [Flipper]. +/// +/// The end of a [Flipper] depends on its [Flipper.side]. +/// {@endtemplate} +class FlipperAnchor extends JointAnchor { + /// {@macro flipper_anchor} + FlipperAnchor({ + required Flipper flipper, + }) { + initialPosition = Vector2( + flipper.side.isLeft + ? flipper.body.position.x - Flipper.size.x / 2 + : flipper.body.position.x + Flipper.size.x / 2, + flipper.body.position.y, + ); } }