refactor: moved lock to FlipperJoint

pull/102/head
alestiago 4 years ago
parent 39ad63882a
commit b80b1a1098

@ -65,13 +65,9 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition {
flipper: this, flipper: this,
anchor: anchor, anchor: anchor,
); );
final joint = _FlipperJoint(jointDef)..create(world); final joint = _FlipperJoint(jointDef);
world.createJoint2(joint);
// FIXME(erickzanardo): when mounted the initial position is not fully unawaited(mounted.whenComplete(joint.unlock));
// reached.
unawaited(
mounted.whenComplete(joint.unlock),
);
} }
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
@ -166,45 +162,60 @@ class _FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
required Flipper flipper, required Flipper flipper,
required _FlipperAnchor anchor, required _FlipperAnchor anchor,
}) : side = flipper.side { }) : side = flipper.side {
enableLimit = true;
initialize( initialize(
flipper.body, flipper.body,
anchor.body, anchor.body,
anchor.body.position, anchor.body.position,
); );
enableLimit = true;
final angle = (_sweepingAngle * -side.direction) / 2;
lowerAngle = upperAngle = angle;
} }
/// The total angle of the arc motion.
static const _sweepingAngle = math.pi / 3.5;
final BoardSide side; final BoardSide side;
} }
/// {@template flipper_joint}
/// [RevoluteJoint] that controls the arc motion of a [Flipper].
/// {@endtemplate}
class _FlipperJoint extends RevoluteJoint { class _FlipperJoint extends RevoluteJoint {
/// {@macro flipper_joint}
_FlipperJoint(_FlipperAnchorRevoluteJointDef def) _FlipperJoint(_FlipperAnchorRevoluteJointDef def)
: side = def.side, : side = def.side,
super(def); super(def) {
lock();
}
/// The total angle of the arc motion.
static const _sweepingAngle = math.pi / 3.5;
final BoardSide side; final BoardSide side;
// TODO(alestiago): Remove once Forge2D supports custom joints. /// Locks the [Flipper] to its resting position.
void create(World world) { ///
world.joints.add(this); /// The joint is locked when initialized in order to force the [Flipper
bodyA.joints.add(this); /// at its resting position.
bodyB.joints.add(this); void lock() {
const angle = _sweepingAngle / 2;
setLimits(
-angle * side.direction,
-angle * side.direction,
);
} }
/// Unlocks the [Flipper] from its resting position. /// 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() { void unlock() {
setLimits( const angle = _sweepingAngle / 2;
lowerLimit * side.direction, setLimits(-angle, angle);
-upperLimit * side.direction, }
); }
extension on World {
// TODO(alestiago): Remove once Forge2D supports custom joints.
void createJoint2(Joint joint) {
assert(!isLocked, '');
joints.add(joint);
joint.bodyA.joints.add(joint);
joint.bodyB.joints.add(joint);
} }
} }

Loading…
Cancel
Save