From e6dca1ed7fca5a9ef50d9eab6db26b7938fcded4 Mon Sep 17 00:00:00 2001 From: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> Date: Fri, 25 Mar 2022 08:00:35 -0500 Subject: [PATCH] refactor: dimension and position `Plunger` (#96) * refactor: dimension plunger * refactor: adjust launch ramp and ball size * refactor: use board center for positioning Co-authored-by: Erick --- lib/game/components/launcher_ramp.dart | 23 ++++++++----------- lib/game/components/plunger.dart | 18 ++++++++++----- lib/game/pinball_game.dart | 13 +++++------ .../lib/src/components/ball.dart | 2 +- .../test/src/components/ball_test.dart | 2 +- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart index aae9265f..61b9e26f 100644 --- a/lib/game/components/launcher_ramp.dart +++ b/lib/game/components/launcher_ramp.dart @@ -27,33 +27,30 @@ class LauncherRamp extends Component with HasGameRef { RampOpeningBallContactCallback<_LauncherRampOpening>(), ); - final launcherRampRotation = - -math.atan(18.6 / PinballGame.boardBounds.height); - final straightPath = Pathway.straight( color: const Color.fromARGB(255, 34, 255, 0), - start: position + Vector2(-1.2, 10), - end: position + Vector2(-1.2, 117), - width: 5, - rotation: launcherRampRotation, + start: position + Vector2(-4.5, -10), + end: position + Vector2(-4.5, 117), + width: 4, + rotation: PinballGame.boardPerspectiveAngle, ) ..initialPosition = position ..layer = layer; final curvedPath = Pathway.arc( color: const Color.fromARGB(255, 251, 255, 0), - center: position + Vector2(-2.8, 87.2), + center: position + Vector2(-7, 87.2), radius: 16.3, angle: math.pi / 2, - width: 5, + width: 4, rotation: 3 * math.pi / 2, )..layer = layer; final leftOpening = _LauncherRampOpening(rotation: math.pi / 2) - ..initialPosition = position + Vector2(-11.8, 66.3) + ..initialPosition = position + Vector2(-13.8, 66.7) ..layer = Layer.opening; final rightOpening = _LauncherRampOpening(rotation: 0) - ..initialPosition = position + Vector2(-4.9, 59.4) + ..initialPosition = position + Vector2(-6.8, 59.4) ..layer = Layer.opening; await addAll([ @@ -81,9 +78,9 @@ class _LauncherRampOpening extends RampOpening { final double _rotation; - // TODO(ruialonso): Avoid magic number 3, should be propotional to + // TODO(ruialonso): Avoid magic number 2.5, should be propotional to // [JetpackRamp]. - static final Vector2 _size = Vector2(3, .1); + static final Vector2 _size = Vector2(2.5, .1); @override Shape get shape => PolygonShape() diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 9791ec66..d9137457 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -21,9 +21,15 @@ class Plunger extends BodyComponent with KeyboardHandler, InitialPosition { @override Body createBody() { - final shape = PolygonShape()..setAsBoxXY(2, 0.75); + final shape = PolygonShape() + ..setAsBox( + 1.35, + 0.5, + Vector2.zero(), + PinballGame.boardPerspectiveAngle, + ); - final fixtureDef = FixtureDef(shape)..density = 5; + final fixtureDef = FixtureDef(shape)..density = 20; final bodyDef = BodyDef() ..position = initialPosition @@ -36,7 +42,7 @@ class Plunger extends BodyComponent with KeyboardHandler, InitialPosition { /// Set a constant downward velocity on the [Plunger]. void _pull() { - body.linearVelocity = Vector2(0, -3); + body.linearVelocity = Vector2(0, -7); } /// Set an upward velocity on the [Plunger]. @@ -44,7 +50,7 @@ class Plunger extends BodyComponent with KeyboardHandler, InitialPosition { /// The velocity's magnitude depends on how far the [Plunger] has been pulled /// from its original [initialPosition]. void _release() { - final velocity = (initialPosition.y - body.position.y) * 9; + final velocity = (initialPosition.y - body.position.y) * 4; body.linearVelocity = Vector2(0, velocity); } @@ -121,12 +127,12 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { plunger.body, anchor.body, anchor.body.position, - Vector2(0, -1), + Vector2(18.6, PinballGame.boardBounds.height), ); enableLimit = true; lowerTranslation = double.negativeInfinity; enableMotor = true; - motorSpeed = 50; + motorSpeed = 80; maxMotorForce = motorSpeed; collideConnected = true; } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 39cecb61..057809be 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -1,5 +1,7 @@ // ignore_for_file: public_member_api_docs import 'dart:async'; +import 'dart:math' as math; + import 'package:flame/extensions.dart'; import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; @@ -24,6 +26,8 @@ class PinballGame extends Forge2DGame width: boardSize.x, height: -boardSize.y, ); + static final boardPerspectiveAngle = + -math.atan(18.6 / PinballGame.boardBounds.height); @override void onAttach() { @@ -60,13 +64,8 @@ class PinballGame extends Forge2DGame } Future _addPlunger() async { - plunger = Plunger(compressionDistance: 2); - - plunger.initialPosition = boardBounds.bottomRight.toVector2() + - Vector2( - -5, - 10, - ); + plunger = Plunger(compressionDistance: 29) + ..initialPosition = boardBounds.center.toVector2() + Vector2(41.5, -49); await add(plunger); } diff --git a/packages/pinball_components/lib/src/components/ball.dart b/packages/pinball_components/lib/src/components/ball.dart index ec51cb47..674cdbf3 100644 --- a/packages/pinball_components/lib/src/components/ball.dart +++ b/packages/pinball_components/lib/src/components/ball.dart @@ -22,7 +22,7 @@ class Ball extends BodyComponent } /// The size of the [Ball] - final Vector2 size = Vector2.all(2); + final Vector2 size = Vector2.all(3); /// The base [Color] used to tint this [Ball] final Color baseColor; diff --git a/packages/pinball_components/test/src/components/ball_test.dart b/packages/pinball_components/test/src/components/ball_test.dart index 682f1f73..14a3de35 100644 --- a/packages/pinball_components/test/src/components/ball_test.dart +++ b/packages/pinball_components/test/src/components/ball_test.dart @@ -86,7 +86,7 @@ void main() { final fixture = ball.body.fixtures[0]; expect(fixture.shape.shapeType, equals(ShapeType.circle)); - expect(fixture.shape.radius, equals(1)); + expect(fixture.shape.radius, equals(1.5)); }, );