From 54eb44233d3d9a6a52430c84eba8d533adb4a0ab Mon Sep 17 00:00:00 2001 From: alestiago Date: Mon, 7 Mar 2022 17:10:56 +0000 Subject: [PATCH] feat: implemented FlipperAnchor --- lib/game/components/flipper.dart | 44 +++++++++++++++++--------------- lib/game/pinball_game.dart | 20 +++++---------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index f3f4fafe..da8fcd44 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -5,6 +5,11 @@ 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. /// @@ -12,11 +17,11 @@ import 'package:pinball/game/game.dart'; /// {@endtemplate flipper} class Flipper extends BodyComponent { /// {@macro flipper} - Flipper._({ + Flipper({ required Vector2 position, - bool isMirrored = false, + required this.type, }) : _position = position, - _isMirrored = isMirrored, + _isMirrored = type == FlipperType.left, _speed = _calculateSpeed() { // TODO(alestiago): Use sprite instead of color when provided. paint = Paint() @@ -24,22 +29,6 @@ class Flipper extends BodyComponent { ..style = PaintingStyle.fill; } - /// {@macro flipper} - Flipper.right({ - required Vector2 position, - }) : this._( - position: position, - isMirrored: true, - ); - - /// {@macro flipper} - Flipper.left({ - required Vector2 position, - }) : this._( - position: position, - isMirrored: false, - ); - /// The width of the [Flipper]. static const width = 12.0; @@ -75,6 +64,8 @@ class Flipper extends BodyComponent { /// The initial position of the [Flipper] body. final Vector2 _position; + final FlipperType type; + /// Whether the [Flipper] is mirrored. /// /// A mirrored [Flipper] is one positioned at the right of the borad. @@ -160,8 +151,21 @@ class Flipper extends BodyComponent { } } +class FlipperAnchor extends Anchor { + FlipperAnchor({ + required Flipper flipper, + }) : super( + position: Vector2( + flipper.type == FlipperType.left + ? flipper.body.position.x + : flipper.body.position.x + Flipper.width, + flipper.body.position.y - Flipper.height / 2, + ), + ); +} + /// {@template flipper_anchor_revolute_joint_def} -/// Hinges one end of [Flipper] to an [Anchor] to achieve an arc motion. +/// Hinges one end of [Flipper] to a [Anchor] to achieve an arc motion. /// {@endtemplate} class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { /// {@macro flipper_anchor_revolute_joint_def} diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 37df3c8c..32e38474 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -35,19 +35,15 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { final center = screenToWorld(camera.viewport.effectiveSize / 2); const flipperSpace = 2; await add( - _leftFlipper = Flipper.left( + _leftFlipper = Flipper( position: Vector2( (center.x - (Flipper.width / 2)) - (flipperSpace / 2), center.y, ), + type: FlipperType.left, ), ); - final leftFlipperAnchor = Anchor( - position: Vector2( - _leftFlipper.body.position.x, - _leftFlipper.body.position.y - Flipper.height / 2, - ), - ); + final leftFlipperAnchor = FlipperAnchor(flipper: _leftFlipper); await add(leftFlipperAnchor); final leftFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( flipper: _leftFlipper, @@ -59,19 +55,15 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { world.createJoint(leftFlipperRevoluteJointDef) as RevoluteJoint; await add( - _rightFlipper = Flipper.right( + _rightFlipper = Flipper( position: Vector2( (center.x + (Flipper.width / 2)) + (flipperSpace / 2), center.y, ), + type: FlipperType.right, ), ); - final rightFlipperAnchor = Anchor( - position: Vector2( - _rightFlipper.body.position.x + Flipper.width, - _rightFlipper.body.position.y - Flipper.height / 2, - ), - ); + final rightFlipperAnchor = FlipperAnchor(flipper: _rightFlipper); await add(rightFlipperAnchor); final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( flipper: _rightFlipper,