feat: implemented BoardSideX

pull/15/head
alestiago 4 years ago
parent e219f867d5
commit a279c0f463

@ -11,3 +11,11 @@ enum BoardSide {
/// The right side of the board. /// The right side of the board.
right, right,
} }
extension BoardSideX on BoardSide {
/// Whether this side is [BoardSide.left].
bool get isLeft => this == BoardSide.left;
/// Whether this side is [BoardSide.right].
bool get isRight => this == BoardSide.right;
}

@ -76,14 +76,13 @@ class Flipper extends BodyComponent {
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
final fixtures = <FixtureDef>[]; final fixtures = <FixtureDef>[];
final isRight = side.isRight;
final isLeft = side == BoardSide.left;
const bigRadius = height / 2; const bigRadius = height / 2;
final bigCircleShape = CircleShape() final bigCircleShape = CircleShape()
..radius = bigRadius ..radius = bigRadius
..position.setValues( ..position.setValues(
isLeft ? width - bigRadius : bigRadius, isRight ? width - bigRadius : bigRadius,
-bigRadius, -bigRadius,
); );
final bigCircleFixtureDef = FixtureDef(bigCircleShape); final bigCircleFixtureDef = FixtureDef(bigCircleShape);
@ -93,14 +92,14 @@ class Flipper extends BodyComponent {
final smallCircleShape = CircleShape() final smallCircleShape = CircleShape()
..radius = smallRadius ..radius = smallRadius
..position.setValues( ..position.setValues(
isLeft ? smallRadius : width - smallRadius, isRight ? smallRadius : width - smallRadius,
-2 * smallRadius, -2 * smallRadius,
); );
final smallCircleFixtureDef = FixtureDef(smallCircleShape); final smallCircleFixtureDef = FixtureDef(smallCircleShape);
fixtures.add(smallCircleFixtureDef); fixtures.add(smallCircleFixtureDef);
const inclineSpace = (height - (2 * smallRadius)) / 2; const inclineSpace = (height - (2 * smallRadius)) / 2;
final trapeziumVertices = isLeft final trapeziumVertices = isRight
? [ ? [
Vector2(smallRadius, -inclineSpace), Vector2(smallRadius, -inclineSpace),
Vector2(width - bigRadius, 0), Vector2(width - bigRadius, 0),
@ -157,7 +156,7 @@ class FlipperAnchor extends Anchor {
required Flipper flipper, required Flipper flipper,
}) : super( }) : super(
position: Vector2( position: Vector2(
flipper.side == BoardSide.left flipper.side.isLeft
? flipper.body.position.x ? flipper.body.position.x
: flipper.body.position.x + Flipper.width, : flipper.body.position.x + Flipper.width,
flipper.body.position.y - Flipper.height / 2, flipper.body.position.y - Flipper.height / 2,
@ -173,7 +172,6 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
FlipperAnchorRevoluteJointDef({ FlipperAnchorRevoluteJointDef({
required Flipper flipper, required Flipper flipper,
required Anchor anchor, required Anchor anchor,
bool isMirrored = false,
}) { }) {
initialize( initialize(
flipper.body, flipper.body,
@ -181,7 +179,8 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
anchor.body.position, anchor.body.position,
); );
enableLimit = true; enableLimit = true;
final angle = isMirrored ? -_sweepingAngle : _sweepingAngle;
final angle = flipper.side.isRight ? -_sweepingAngle : _sweepingAngle;
lowerAngle = angle; lowerAngle = angle;
upperAngle = angle; upperAngle = angle;
} }

@ -68,7 +68,6 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef(
flipper: _rightFlipper, flipper: _rightFlipper,
anchor: rightFlipperAnchor, anchor: rightFlipperAnchor,
isMirrored: true,
); );
_rightFlipperRevoluteJoint = _rightFlipperRevoluteJoint =
world.createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint; world.createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint;

Loading…
Cancel
Save