feat: implemented BoardSide enum

pull/15/head
alestiago 4 years ago
parent 54eb44233d
commit e2d77441cc

@ -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,
}

@ -1,5 +1,6 @@
export 'anchor.dart'; export 'anchor.dart';
export 'ball.dart'; export 'ball.dart';
export 'board_side.dart';
export 'flipper.dart'; export 'flipper.dart';
export 'plunger.dart'; export 'plunger.dart';
export 'score_points.dart'; export 'score_points.dart';

@ -5,11 +5,6 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
enum FlipperType {
left,
right,
}
/// {@template flipper} /// {@template flipper}
/// A bat, typically found in pairs at the bottom of the board. /// A bat, typically found in pairs at the bottom of the board.
/// ///
@ -19,9 +14,8 @@ class Flipper extends BodyComponent {
/// {@macro flipper} /// {@macro flipper}
Flipper({ Flipper({
required Vector2 position, required Vector2 position,
required this.type, required this.side,
}) : _position = position, }) : _position = position,
_isMirrored = type == FlipperType.left,
_speed = _calculateSpeed() { _speed = _calculateSpeed() {
// TODO(alestiago): Use sprite instead of color when provided. // TODO(alestiago): Use sprite instead of color when provided.
paint = Paint() paint = Paint()
@ -64,12 +58,11 @@ class Flipper extends BodyComponent {
/// The initial position of the [Flipper] body. /// The initial position of the [Flipper] body.
final Vector2 _position; final Vector2 _position;
final FlipperType type; /// Whether the [Flipper] is on the left or right side of the board.
/// Whether the [Flipper] is mirrored.
/// ///
/// A mirrored [Flipper] is one positioned at the right of the borad. /// A [Flipper] with [BoardSide.left] has a counter-clockwise arc motion.
final bool _isMirrored; /// A [Flipper] with [BoardSide.right] has a clockwise arc motion.
final BoardSide side;
/// Applies downard linear velocity to the [Flipper] to move it up. /// Applies downard linear velocity to the [Flipper] to move it up.
void moveDown() { void moveDown() {
@ -81,18 +74,16 @@ class Flipper extends BodyComponent {
body.linearVelocity = Vector2(0, _speed); 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<void>();
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
final fixtures = <FixtureDef>[]; final fixtures = <FixtureDef>[];
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(
_isMirrored ? width - bigRadius : bigRadius, isLeft ? width - bigRadius : bigRadius,
-bigRadius, -bigRadius,
); );
final bigCircleFixtureDef = FixtureDef(bigCircleShape); final bigCircleFixtureDef = FixtureDef(bigCircleShape);
@ -102,14 +93,14 @@ class Flipper extends BodyComponent {
final smallCircleShape = CircleShape() final smallCircleShape = CircleShape()
..radius = smallRadius ..radius = smallRadius
..position.setValues( ..position.setValues(
_isMirrored ? smallRadius : width - smallRadius, isLeft ? 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 = _isMirrored final trapeziumVertices = isLeft
? [ ? [
Vector2(smallRadius, -inclineSpace), Vector2(smallRadius, -inclineSpace),
Vector2(width - bigRadius, 0), Vector2(width - bigRadius, 0),
@ -144,6 +135,10 @@ class Flipper extends BodyComponent {
return body; return body;
} }
// TODO(erickzanardo): Remove this once the issue is solved:
// https://github.com/flame-engine/flame/issues/1417
final Completer hasMounted = Completer<void>();
@override @override
void onMount() { void onMount() {
super.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 { class FlipperAnchor extends Anchor {
/// {@macro flipper_anchor_revolute_joint}
FlipperAnchor({ FlipperAnchor({
required Flipper flipper, required Flipper flipper,
}) : super( }) : super(
position: Vector2( position: Vector2(
flipper.type == FlipperType.left flipper.side == BoardSide.left
? 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,

@ -40,7 +40,7 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
(center.x - (Flipper.width / 2)) - (flipperSpace / 2), (center.x - (Flipper.width / 2)) - (flipperSpace / 2),
center.y, center.y,
), ),
type: FlipperType.left, side: BoardSide.left,
), ),
); );
final leftFlipperAnchor = FlipperAnchor(flipper: _leftFlipper); final leftFlipperAnchor = FlipperAnchor(flipper: _leftFlipper);
@ -60,7 +60,7 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
(center.x + (Flipper.width / 2)) + (flipperSpace / 2), (center.x + (Flipper.width / 2)) + (flipperSpace / 2),
center.y, center.y,
), ),
type: FlipperType.right, side: BoardSide.right,
), ),
); );
final rightFlipperAnchor = FlipperAnchor(flipper: _rightFlipper); final rightFlipperAnchor = FlipperAnchor(flipper: _rightFlipper);

Loading…
Cancel
Save