diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index fb6dcda3..1eb9077c 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -15,8 +15,8 @@ class Board extends Component { // TODO(alestiago): adjust positioning once sprites are added. final bottomGroup = _BottomGroup( position: Vector2( - PinballGame.boardBounds.center.dx, - PinballGame.boardBounds.bottom + 10, + BoardDimensions.bounds.center.dx, + BoardDimensions.bounds.bottom + 10, ), spacing: 2, ); @@ -26,8 +26,8 @@ class Board extends Component { // TODO(alestiago): adjust positioning to real design. final dino = ChromeDino() ..initialPosition = Vector2( - PinballGame.boardBounds.center.dx + 25, - PinballGame.boardBounds.center.dy + 10, + BoardDimensions.bounds.center.dx + 25, + BoardDimensions.bounds.center.dy + 10, ); await addAll([ diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index b58ddfa6..4c4c8be9 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -13,8 +13,8 @@ class Jetpack extends Forge2DBlueprint { @override void build(_) { final position = Vector2( - PinballGame.boardBounds.left + 40.5, - PinballGame.boardBounds.top - 31.5, + BoardDimensions.bounds.left + 40.5, + BoardDimensions.bounds.top - 31.5, ); addAllContactCallback([ diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart index b3f3cb23..79b7c831 100644 --- a/lib/game/components/launcher_ramp.dart +++ b/lib/game/components/launcher_ramp.dart @@ -13,8 +13,8 @@ class Launcher extends Forge2DBlueprint { @override void build(_) { final position = Vector2( - PinballGame.boardBounds.right - 31.3, - PinballGame.boardBounds.bottom + 33, + BoardDimensions.bounds.right - 31.3, + BoardDimensions.bounds.bottom + 33, ); addAllContactCallback([ @@ -67,8 +67,8 @@ class LauncherRamp extends BodyComponent with InitialPosition, Layered { final rightStraightShape = EdgeShape() ..set( - startPosition..rotate(PinballGame.boardPerspectiveAngle), - endPosition..rotate(PinballGame.boardPerspectiveAngle), + startPosition..rotate(BoardDimensions.perspectiveAngle), + endPosition..rotate(BoardDimensions.perspectiveAngle), ); final rightStraightFixtureDef = FixtureDef(rightStraightShape); fixturesDef.add(rightStraightFixtureDef); diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 5703e525..6a9044e9 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -1,7 +1,6 @@ import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/services.dart'; -import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; /// {@template plunger} @@ -26,7 +25,7 @@ class Plunger extends BodyComponent with KeyboardHandler, InitialPosition { 1.35, 0.5, Vector2.zero(), - PinballGame.boardPerspectiveAngle, + BoardDimensions.perspectiveAngle, ); final fixtureDef = FixtureDef(shape)..density = 20; @@ -50,7 +49,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) * 4; + final velocity = (initialPosition.y - body.position.y) * 5; body.linearVelocity = Vector2(0, velocity); } @@ -127,7 +126,7 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { plunger.body, anchor.body, anchor.body.position, - Vector2(18.6, PinballGame.boardBounds.height), + Vector2(18.6, BoardDimensions.bounds.height), ); enableLimit = true; lowerTranslation = double.negativeInfinity; diff --git a/lib/game/components/wall.dart b/lib/game/components/wall.dart index 0fb57a41..46a8c536 100644 --- a/lib/game/components/wall.dart +++ b/lib/game/components/wall.dart @@ -3,7 +3,6 @@ import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/components/components.dart'; -import 'package:pinball/game/pinball_game.dart'; import 'package:pinball_components/pinball_components.dart'; /// {@template wall} @@ -42,13 +41,12 @@ class Wall extends BodyComponent { /// Create top, left, and right [Wall]s for the game board. List createBoundaries(Forge2DGame game) { - final topLeft = - PinballGame.boardBounds.topLeft.toVector2() + Vector2(18.6, 0); - final bottomRight = PinballGame.boardBounds.bottomRight.toVector2(); + final topLeft = BoardDimensions.bounds.topLeft.toVector2() + Vector2(18.6, 0); + final bottomRight = BoardDimensions.bounds.bottomRight.toVector2(); final topRight = - PinballGame.boardBounds.topRight.toVector2() - Vector2(18.6, 0); - final bottomLeft = PinballGame.boardBounds.bottomLeft.toVector2(); + BoardDimensions.bounds.topRight.toVector2() - Vector2(18.6, 0); + final bottomLeft = BoardDimensions.bounds.bottomLeft.toVector2(); return [ Wall(start: topLeft, end: topRight), @@ -67,8 +65,8 @@ class BottomWall extends Wall { /// {@macro bottom_wall} BottomWall() : super( - start: PinballGame.boardBounds.bottomLeft.toVector2(), - end: PinballGame.boardBounds.bottomRight.toVector2(), + start: BoardDimensions.bounds.bottomLeft.toVector2(), + end: BoardDimensions.bounds.bottomRight.toVector2(), ); } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 9673b2d2..17a0f722 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -1,6 +1,5 @@ // ignore_for_file: public_member_api_docs import 'dart:async'; -import 'dart:math' as math; import 'package:flame/components.dart'; import 'package:flame/extensions.dart'; @@ -22,15 +21,6 @@ class PinballGame extends Forge2DGame late final Plunger plunger; - static final boardSize = Vector2(101.6, 143.8); - static final boardBounds = Rect.fromCenter( - center: Offset.zero, - width: boardSize.x, - height: -boardSize.y, - ); - static final boardPerspectiveAngle = - -math.atan(18.6 / PinballGame.boardBounds.height); - @override void onAttach() { super.onAttach(); @@ -80,7 +70,8 @@ class PinballGame extends Forge2DGame Future _addPlunger() async { plunger = Plunger(compressionDistance: 29) - ..initialPosition = boardBounds.center.toVector2() + Vector2(41.5, -49); + ..initialPosition = + BoardDimensions.bounds.center.toVector2() + Vector2(41.5, -49); await add(plunger); } @@ -88,8 +79,8 @@ class PinballGame extends Forge2DGame await add( BonusWord( position: Vector2( - boardBounds.center.dx - 3.07, - boardBounds.center.dy - 2.4, + BoardDimensions.bounds.center.dx - 3.07, + BoardDimensions.bounds.center.dy - 2.4, ), ), ); diff --git a/packages/pinball_components/assets/images/ball.png b/packages/pinball_components/assets/images/ball.png index af80811b..43332c9a 100644 Binary files a/packages/pinball_components/assets/images/ball.png and b/packages/pinball_components/assets/images/ball.png differ diff --git a/packages/pinball_components/lib/src/components/ball.dart b/packages/pinball_components/lib/src/components/ball.dart index 9a2da898..f6078cf7 100644 --- a/packages/pinball_components/lib/src/components/ball.dart +++ b/packages/pinball_components/lib/src/components/ball.dart @@ -23,13 +23,14 @@ class Ball extends BodyComponent } /// The size of the [Ball] - final Vector2 size = Vector2.all(3); + final Vector2 size = Vector2.all(4.5); /// The base [Color] used to tint this [Ball] final Color baseColor; double _boostTimer = 0; static const _boostDuration = 2.0; + late SpriteComponent _spriteComponent; @override Future onLoad() async { @@ -37,9 +38,9 @@ class Ball extends BodyComponent final sprite = await gameRef.loadSprite(Assets.images.ball.keyName); final tint = baseColor.withOpacity(0.5); await add( - SpriteComponent( + _spriteComponent = SpriteComponent( sprite: sprite, - size: size, + size: size * 1.15, anchor: Anchor.center, )..tint(tint), ); @@ -88,6 +89,8 @@ class Ball extends BodyComponent unawaited(gameRef.add(effect)); } + + _rescale(); } /// Applies a boost on this [Ball]. @@ -95,4 +98,18 @@ class Ball extends BodyComponent body.applyLinearImpulse(impulse); _boostTimer = _boostDuration; } + + void _rescale() { + final boardHeight = BoardDimensions.size.y; + const maxShrinkAmount = BoardDimensions.perspectiveShrinkFactor; + + final adjustedYPosition = body.position.y + (boardHeight / 2); + final adjustedHeight = (1 / (1 - maxShrinkAmount)) * boardHeight; + + final scaleFactor = + ((boardHeight - adjustedYPosition) / adjustedHeight) + maxShrinkAmount; + + body.fixtures.first.shape.radius = (size.x / 2) * scaleFactor; + _spriteComponent.scale = Vector2.all(scaleFactor); + } } diff --git a/packages/pinball_components/lib/src/components/board_dimensions.dart b/packages/pinball_components/lib/src/components/board_dimensions.dart new file mode 100644 index 00000000..34f00091 --- /dev/null +++ b/packages/pinball_components/lib/src/components/board_dimensions.dart @@ -0,0 +1,24 @@ +import 'dart:math' as math; + +import 'package:flame/extensions.dart'; + +/// {@template board_dimensions} +/// Contains various board properties and dimensions for global use. +/// {@endtemplate} +class BoardDimensions { + /// Width and height of the board. + static final size = Vector2(101.6, 143.8); + + /// [Rect] for easier access to board boundaries. + static final bounds = Rect.fromCenter( + center: Offset.zero, + width: size.x, + height: -size.y, + ); + + /// 3D perspective angle of the board in radians. + static final perspectiveAngle = -math.atan(18.6 / bounds.height); + + /// Factor the board shrinks by from the closest point to the farthest. + static const perspectiveShrinkFactor = 0.63; +} diff --git a/packages/pinball_components/lib/src/components/components.dart b/packages/pinball_components/lib/src/components/components.dart index c29f91a3..84f956b2 100644 --- a/packages/pinball_components/lib/src/components/components.dart +++ b/packages/pinball_components/lib/src/components/components.dart @@ -1,4 +1,5 @@ export 'ball.dart'; +export 'board_dimensions.dart'; export 'board_side.dart'; export 'fire_effect.dart'; export 'flipper.dart'; diff --git a/packages/pinball_components/test/src/components/ball_test.dart b/packages/pinball_components/test/src/components/ball_test.dart index a9eb05ad..f2a54c68 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.5)); + expect(fixture.shape.radius, equals(2.25)); }, );