From a279c0f4632d0bfbfecd8d86329ec5afc8a3a572 Mon Sep 17 00:00:00 2001 From: alestiago Date: Mon, 7 Mar 2022 17:50:33 +0000 Subject: [PATCH] feat: implemented BoardSideX --- lib/game/components/board_side.dart | 8 ++++++++ lib/game/components/flipper.dart | 15 +++++++-------- lib/game/pinball_game.dart | 1 - 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/game/components/board_side.dart b/lib/game/components/board_side.dart index 87c29917..bb44f7f5 100644 --- a/lib/game/components/board_side.dart +++ b/lib/game/components/board_side.dart @@ -11,3 +11,11 @@ enum BoardSide { /// The right side of the board. 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; +} diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index fb4656e7..f0aebe6d 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -76,14 +76,13 @@ class Flipper extends BodyComponent { List _createFixtureDefs() { final fixtures = []; - - final isLeft = side == BoardSide.left; + final isRight = side.isRight; const bigRadius = height / 2; final bigCircleShape = CircleShape() ..radius = bigRadius ..position.setValues( - isLeft ? width - bigRadius : bigRadius, + isRight ? width - bigRadius : bigRadius, -bigRadius, ); final bigCircleFixtureDef = FixtureDef(bigCircleShape); @@ -93,14 +92,14 @@ class Flipper extends BodyComponent { final smallCircleShape = CircleShape() ..radius = smallRadius ..position.setValues( - isLeft ? smallRadius : width - smallRadius, + isRight ? smallRadius : width - smallRadius, -2 * smallRadius, ); final smallCircleFixtureDef = FixtureDef(smallCircleShape); fixtures.add(smallCircleFixtureDef); const inclineSpace = (height - (2 * smallRadius)) / 2; - final trapeziumVertices = isLeft + final trapeziumVertices = isRight ? [ Vector2(smallRadius, -inclineSpace), Vector2(width - bigRadius, 0), @@ -157,7 +156,7 @@ class FlipperAnchor extends Anchor { required Flipper flipper, }) : super( position: Vector2( - flipper.side == BoardSide.left + flipper.side.isLeft ? flipper.body.position.x : flipper.body.position.x + Flipper.width, flipper.body.position.y - Flipper.height / 2, @@ -173,7 +172,6 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { FlipperAnchorRevoluteJointDef({ required Flipper flipper, required Anchor anchor, - bool isMirrored = false, }) { initialize( flipper.body, @@ -181,7 +179,8 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { anchor.body.position, ); enableLimit = true; - final angle = isMirrored ? -_sweepingAngle : _sweepingAngle; + + final angle = flipper.side.isRight ? -_sweepingAngle : _sweepingAngle; lowerAngle = angle; upperAngle = angle; } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 34127859..64db9c5d 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -68,7 +68,6 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( flipper: _rightFlipper, anchor: rightFlipperAnchor, - isMirrored: true, ); _rightFlipperRevoluteJoint = world.createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint;