refactor: defined FlipperJoint

pull/72/head
alestiago 4 years ago
parent 4af09b64fa
commit a6d58ef3ab

@ -113,16 +113,12 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition {
flipper: this, flipper: this,
anchor: anchor, anchor: anchor,
); );
// TODO(alestiago): Remove casting once the following is closed: final joint = _FlipperJoint(jointDef)..create(world);
// https://github.com/flame-engine/forge2d/issues/36
final joint = world.createJoint(jointDef) as RevoluteJoint;
// FIXME(erickzanardo): when mounted the initial position is not fully // FIXME(erickzanardo): when mounted the initial position is not fully
// reached. // reached.
unawaited( unawaited(
mounted.whenComplete( mounted.whenComplete(joint.unlock),
() => FlipperAnchorRevoluteJointDef.unlock(joint, side),
),
); );
} }
@ -211,21 +207,28 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition {
} }
} }
/// {@template flipper_anchor} class _FlipperJoint extends RevoluteJoint {
/// [JointAnchor] positioned at the end of a [Flipper]. _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 end of a [Flipper] depends on its [Flipper.side]. /// The [Flipper] is locked when initialized in order to force it to be at
/// {@endtemplate} /// its resting position.
class FlipperAnchor extends JointAnchor { void unlock() {
/// {@macro flipper_anchor} setLimits(
FlipperAnchor({ lowerLimit * side.direction,
required Flipper flipper, -upperLimit * side.direction,
}) {
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,
); );
} }
} }
@ -238,39 +241,39 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
FlipperAnchorRevoluteJointDef({ FlipperAnchorRevoluteJointDef({
required Flipper flipper, required Flipper flipper,
required FlipperAnchor anchor, required FlipperAnchor anchor,
}) { }) : side = flipper.side {
initialize( initialize(
flipper.body, flipper.body,
anchor.body, anchor.body,
anchor.body.position, anchor.body.position,
); );
enableLimit = true;
enableLimit = true;
final angle = (flipper.side.isLeft ? _sweepingAngle : -_sweepingAngle) / 2; final angle = (flipper.side.isLeft ? _sweepingAngle : -_sweepingAngle) / 2;
lowerAngle = upperAngle = angle; lowerAngle = upperAngle = angle;
} }
final BoardSide side;
/// The total angle of the arc motion. /// The total angle of the arc motion.
static const _sweepingAngle = math.pi / 3.5; 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,
);
} }
} }

Loading…
Cancel
Save