fix: made Flippers start from bottom position (#392)

pull/358/head
Alejandro Santiago 3 years ago committed by alestiago
parent 42382f3b8a
commit 1e70881751

@ -45,8 +45,11 @@ class GameBlocStatusListener extends Component
} }
} }
void _addFlipperKeyControls(Flipper flipper) => void _addFlipperKeyControls(Flipper flipper) {
flipper.add(FlipperKeyControllingBehavior()); flipper
..add(FlipperKeyControllingBehavior())
..moveDown();
}
void _removeFlipperKeyControls(Flipper flipper) => flipper void _removeFlipperKeyControls(Flipper flipper) => flipper
.descendants() .descendants()

@ -6,8 +6,6 @@ import 'package:pinball_flame/pinball_flame.dart';
/// Joints the [Flipper] to allow pivoting around one end. /// Joints the [Flipper] to allow pivoting around one end.
class FlipperJointingBehavior extends Component class FlipperJointingBehavior extends Component
with ParentIsA<Flipper>, HasGameRef { with ParentIsA<Flipper>, HasGameRef {
late final RevoluteJoint _joint;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
@ -19,15 +17,7 @@ class FlipperJointingBehavior extends Component
flipper: parent, flipper: parent,
anchor: anchor, anchor: anchor,
); );
_joint = _FlipperJoint(jointDef); parent.world.createJoint(RevoluteJoint(jointDef));
parent.world.createJoint(_joint);
}
@override
void onMount() {
gameRef.ready().whenComplete(
() => parent.body.joints.whereType<_FlipperJoint>().first.unlock(),
);
} }
} }
@ -58,46 +48,15 @@ class _FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
_FlipperAnchorRevoluteJointDef({ _FlipperAnchorRevoluteJointDef({
required Flipper flipper, required Flipper flipper,
required _FlipperAnchor anchor, required _FlipperAnchor anchor,
}) : side = flipper.side { }) {
enableLimit = true;
initialize( initialize(
flipper.body, flipper.body,
anchor.body, anchor.body,
flipper.body.position + anchor.body.position, flipper.body.position + anchor.body.position,
); );
}
final BoardSide side;
}
/// {@template flipper_joint} enableLimit = true;
/// [RevoluteJoint] that controls the pivoting motion of a [Flipper]. upperAngle = 0.611;
/// {@endtemplate} lowerAngle = -upperAngle;
class _FlipperJoint extends RevoluteJoint {
/// {@macro flipper_joint}
_FlipperJoint(_FlipperAnchorRevoluteJointDef def)
: side = def.side,
super(def) {
lock();
}
/// Half the angle of the arc motion.
static const _halfSweepingAngle = 0.611;
final BoardSide side;
/// Locks the [Flipper] to its resting position.
///
/// The joint is locked when initialized in order to force the [Flipper]
/// at its resting position.
void lock() {
final angle = _halfSweepingAngle * side.direction;
setLimits(angle, angle);
}
/// Unlocks the [Flipper] from its resting position.
void unlock() {
const angle = _halfSweepingAngle;
setLimits(-angle, angle);
} }
} }

Loading…
Cancel
Save