diff --git a/lib/game/components/board_side.dart b/lib/game/components/board_side.dart new file mode 100644 index 00000000..87c29917 --- /dev/null +++ b/lib/game/components/board_side.dart @@ -0,0 +1,13 @@ +import 'package:pinball/game/game.dart'; + +/// Indicates a side of the board. +/// +/// Usually used to position or mirror elements of a [PinballGame]; such as a +/// [Flipper]. +enum BoardSide { + /// The left side of the board. + left, + + /// The right side of the board. + right, +} diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index 4d8601b7..bd5f5437 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -1,5 +1,6 @@ export 'anchor.dart'; export 'ball.dart'; +export 'board_side.dart'; export 'flipper.dart'; export 'plunger.dart'; export 'score_points.dart'; diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index da8fcd44..0e85d216 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -5,11 +5,6 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/material.dart'; import 'package:pinball/game/game.dart'; -enum FlipperType { - left, - right, -} - /// {@template flipper} /// A bat, typically found in pairs at the bottom of the board. /// @@ -19,9 +14,8 @@ class Flipper extends BodyComponent { /// {@macro flipper} Flipper({ required Vector2 position, - required this.type, + required this.side, }) : _position = position, - _isMirrored = type == FlipperType.left, _speed = _calculateSpeed() { // TODO(alestiago): Use sprite instead of color when provided. paint = Paint() @@ -64,12 +58,11 @@ class Flipper extends BodyComponent { /// The initial position of the [Flipper] body. final Vector2 _position; - final FlipperType type; - - /// Whether the [Flipper] is mirrored. + /// Whether the [Flipper] is on the left or right side of the board. /// - /// A mirrored [Flipper] is one positioned at the right of the borad. - final bool _isMirrored; + /// A [Flipper] with [BoardSide.left] has a counter-clockwise arc motion. + /// A [Flipper] with [BoardSide.right] has a clockwise arc motion. + final BoardSide side; /// Applies downard linear velocity to the [Flipper] to move it up. void moveDown() { @@ -81,18 +74,16 @@ class Flipper extends BodyComponent { body.linearVelocity = Vector2(0, _speed); } - // TODO(erickzanardo): Remove this once the issue is solved: - // https://github.com/flame-engine/flame/issues/1417 - final Completer hasMounted = Completer(); - List _createFixtureDefs() { final fixtures = []; + final isLeft = side == BoardSide.left; + const bigRadius = height / 2; final bigCircleShape = CircleShape() ..radius = bigRadius ..position.setValues( - _isMirrored ? width - bigRadius : bigRadius, + isLeft ? width - bigRadius : bigRadius, -bigRadius, ); final bigCircleFixtureDef = FixtureDef(bigCircleShape); @@ -102,14 +93,14 @@ class Flipper extends BodyComponent { final smallCircleShape = CircleShape() ..radius = smallRadius ..position.setValues( - _isMirrored ? smallRadius : width - smallRadius, + isLeft ? smallRadius : width - smallRadius, -2 * smallRadius, ); final smallCircleFixtureDef = FixtureDef(smallCircleShape); fixtures.add(smallCircleFixtureDef); const inclineSpace = (height - (2 * smallRadius)) / 2; - final trapeziumVertices = _isMirrored + final trapeziumVertices = isLeft ? [ Vector2(smallRadius, -inclineSpace), Vector2(width - bigRadius, 0), @@ -144,6 +135,10 @@ class Flipper extends BodyComponent { return body; } + // TODO(erickzanardo): Remove this once the issue is solved: + // https://github.com/flame-engine/flame/issues/1417 + final Completer hasMounted = Completer(); + @override void onMount() { super.onMount(); @@ -151,12 +146,18 @@ class Flipper extends BodyComponent { } } +/// {@template flipper_anchor_revolute_joint} +/// [Anchor] positioned at the end of a [Flipper]. +/// +/// The end of a [Flipper] depends on its [Flipper.side]. +/// {@endtemplate} class FlipperAnchor extends Anchor { + /// {@macro flipper_anchor_revolute_joint} FlipperAnchor({ required Flipper flipper, }) : super( position: Vector2( - flipper.type == FlipperType.left + flipper.side == BoardSide.left ? flipper.body.position.x : flipper.body.position.x + Flipper.width, flipper.body.position.y - Flipper.height / 2, diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 32e38474..34127859 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -40,7 +40,7 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { (center.x - (Flipper.width / 2)) - (flipperSpace / 2), center.y, ), - type: FlipperType.left, + side: BoardSide.left, ), ); final leftFlipperAnchor = FlipperAnchor(flipper: _leftFlipper); @@ -60,7 +60,7 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { (center.x + (Flipper.width / 2)) + (flipperSpace / 2), center.y, ), - type: FlipperType.right, + side: BoardSide.right, ), ); final rightFlipperAnchor = FlipperAnchor(flipper: _rightFlipper);