|
|
@ -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,
|
|
|
|
/// The end of a [Flipper] depends on its [Flipper.side].
|
|
|
|
super(def);
|
|
|
|
/// {@endtemplate}
|
|
|
|
|
|
|
|
class FlipperAnchor extends JointAnchor {
|
|
|
|
final BoardSide side;
|
|
|
|
/// {@macro flipper_anchor}
|
|
|
|
|
|
|
|
FlipperAnchor({
|
|
|
|
// TODO(alestiago): Remove once Forge2D supports custom joints.
|
|
|
|
required Flipper flipper,
|
|
|
|
void create(World world) {
|
|
|
|
}) {
|
|
|
|
world.joints.add(this);
|
|
|
|
initialPosition = Vector2(
|
|
|
|
bodyA.joints.add(this);
|
|
|
|
flipper.side.isLeft
|
|
|
|
bodyB.joints.add(this);
|
|
|
|
? flipper.body.position.x - Flipper.size.x / 2
|
|
|
|
}
|
|
|
|
: flipper.body.position.x + Flipper.size.x / 2,
|
|
|
|
|
|
|
|
flipper.body.position.y,
|
|
|
|
/// 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({
|
|
|
|
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.
|
|
|
|
/// {@template flipper_anchor}
|
|
|
|
///
|
|
|
|
/// [JointAnchor] positioned at the end of a [Flipper].
|
|
|
|
/// The [Flipper] is locked when initialized in order to force it to be at
|
|
|
|
///
|
|
|
|
/// its resting position.
|
|
|
|
/// The end of a [Flipper] depends on its [Flipper.side].
|
|
|
|
// TODO(alestiago): consider refactor once the issue is solved:
|
|
|
|
/// {@endtemplate}
|
|
|
|
// https://github.com/flame-engine/forge2d/issues/36
|
|
|
|
class FlipperAnchor extends JointAnchor {
|
|
|
|
static void unlock(RevoluteJoint joint, BoardSide side) {
|
|
|
|
/// {@macro flipper_anchor}
|
|
|
|
late final double upperLimit, lowerLimit;
|
|
|
|
FlipperAnchor({
|
|
|
|
switch (side) {
|
|
|
|
required Flipper flipper,
|
|
|
|
case BoardSide.left:
|
|
|
|
}) {
|
|
|
|
lowerLimit = -joint.lowerLimit;
|
|
|
|
initialPosition = Vector2(
|
|
|
|
upperLimit = joint.upperLimit;
|
|
|
|
flipper.side.isLeft
|
|
|
|
break;
|
|
|
|
? flipper.body.position.x - Flipper.size.x / 2
|
|
|
|
case BoardSide.right:
|
|
|
|
: flipper.body.position.x + Flipper.size.x / 2,
|
|
|
|
lowerLimit = joint.lowerLimit;
|
|
|
|
flipper.body.position.y,
|
|
|
|
upperLimit = -joint.upperLimit;
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
joint.setLimits(lowerLimit, upperLimit);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|