From 8a435d78f0abf2c0dd2992f39095b85aa1537602 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Mon, 21 Mar 2022 17:55:49 -0300 Subject: [PATCH] feat: game uses fixed positioning now --- lib/game/components/board.dart | 12 ++--- lib/game/components/jetpack_ramp.dart | 8 +-- lib/game/components/launcher_ramp.dart | 16 +++--- lib/game/components/spaceship.dart | 5 +- lib/game/components/wall.dart | 18 +++---- lib/game/pinball_game.dart | 70 +++++++++++++------------- 6 files changed, 67 insertions(+), 62 deletions(-) diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index f7b80bd8..2e2f5a9c 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -7,25 +7,23 @@ import 'package:pinball/game/game.dart'; /// {entemplate} class Board extends Component { /// {@macro board} - Board({required Vector2 size}) : _size = size; - - final Vector2 _size; + Board(); @override Future onLoad() async { // TODO(alestiago): adjust positioning once sprites are added. final bottomGroup = _BottomGroup( position: Vector2( - _size.x / 2, - _size.y / 1.25, + PinballGame.boardBounds.center.dx, + PinballGame.boardBounds.bottom + 10, ), spacing: 2, ); final dashForest = _FlutterForest( position: Vector2( - _size.x / 1.25, - _size.y / 4.25, + PinballGame.boardBounds.right - 20, + PinballGame.boardBounds.top - 20, ), ); diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index 985c8f7d..df9db615 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -30,12 +30,14 @@ class JetpackRamp extends Component with HasGameRef { // TODO(ruialonso): Use a bezier curve once control points are defined. color: const Color.fromARGB(255, 8, 218, 241), center: position, - width: 62, - radius: 200, + width: 20, + radius: 60, angle: math.pi, ) - ..initialPosition = position + ..initialPosition = position + Vector2(60, 0) ..layer = layer; + + // TODO figure out the new values for this final leftOpening = _JetpackRampOpening(outsideLayer: Layer.spaceship) ..initialPosition = position + Vector2(-27.6, 25.3) ..layer = Layer.jetpack; diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart index 21d4d666..6d585af1 100644 --- a/lib/game/components/launcher_ramp.dart +++ b/lib/game/components/launcher_ramp.dart @@ -28,21 +28,23 @@ class LauncherRamp extends Component with HasGameRef { final straightPath = Pathway.straight( color: const Color.fromARGB(255, 34, 255, 0), - start: Vector2(0, 0), - end: Vector2(0, 700), - width: 80, + start: Vector2(position.x, position.y), + end: Vector2(position.x, 620), + width: 25, ) ..initialPosition = position ..layer = layer; final curvedPath = Pathway.arc( color: const Color.fromARGB(255, 251, 255, 0), - center: position + Vector2(-29, -8), - radius: 300, - angle: 10 * math.pi / 9, - width: 80, + center: position + Vector2(116, -20), + radius: 80, + angle: 10 * math.pi / 12, + width: 25, ) ..initialPosition = position + Vector2(-28.8, -6) ..layer = layer; + + // TODO figure the new values for the openings final leftOpening = _LauncherRampOpening(rotation: 13 * math.pi / 180) ..initialPosition = position + Vector2(-72.5, 12) ..layer = Layer.opening; diff --git a/lib/game/components/spaceship.dart b/lib/game/components/spaceship.dart index f934d943..da66c479 100644 --- a/lib/game/components/spaceship.dart +++ b/lib/game/components/spaceship.dart @@ -15,7 +15,10 @@ class Spaceship extends Forge2DBlueprint { @override void build() { - final position = Vector2(30, -50); + final position = Vector2( + PinballGame.boardBounds.left + radius + 4, + PinballGame.boardBounds.center.dy + 5, + ); addAllContactCallback([ SpaceshipHoleBallContactCallback(), diff --git a/lib/game/components/wall.dart b/lib/game/components/wall.dart index 017f8c4d..62f9033f 100644 --- a/lib/game/components/wall.dart +++ b/lib/game/components/wall.dart @@ -1,7 +1,9 @@ // ignore_for_file: avoid_renaming_method_parameters +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'; /// {@template wall} /// A continuous generic and [BodyType.static] barrier that divides a game area. @@ -39,15 +41,16 @@ class Wall extends BodyComponent { /// Create top, left, and right [Wall]s for the game board. List createBoundaries(Forge2DGame game) { - final topLeft = Vector2.zero(); - final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize); + final topLeft = PinballGame.boardBounds.topLeft.toVector2(); + final bottomRight = PinballGame.boardBounds.bottomRight.toVector2(); + final topRight = Vector2(bottomRight.x, topLeft.y); final bottomLeft = Vector2(topLeft.x, bottomRight.y); return [ Wall(start: topLeft, end: topRight), Wall(start: topRight, end: bottomRight), - Wall(start: bottomLeft, end: topLeft), + Wall(start: topLeft, end: bottomLeft), ]; } @@ -59,13 +62,10 @@ List createBoundaries(Forge2DGame game) { /// {@endtemplate} class BottomWall extends Wall { /// {@macro bottom_wall} - BottomWall(Forge2DGame game) + BottomWall() : super( - start: game.screenToWorld(game.camera.viewport.effectiveSize), - end: Vector2( - 0, - game.screenToWorld(game.camera.viewport.effectiveSize).y, - ), + start: PinballGame.boardBounds.bottomLeft.toVector2(), + end: PinballGame.boardBounds.bottomRight.toVector2(), ); } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 86bceef6..fc498cec 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -1,6 +1,7 @@ // ignore_for_file: public_member_api_docs import 'dart:async'; +import 'package:flame/extensions.dart'; import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; @@ -16,6 +17,13 @@ class PinballGame extends Forge2DGame late final Plunger plunger; + static final boardSize = Vector2(72, 128); + static final boardBounds = Rect.fromCenter( + center: Offset.zero, + width: boardSize.x, + height: -boardSize.y, + ); + @override void onAttach() { super.onAttach(); @@ -27,11 +35,16 @@ class PinballGame extends Forge2DGame _addContactCallbacks(); await _addGameBoundaries(); - unawaited(_addBoard()); + unawaited(add(Board())); unawaited(_addPlunger()); unawaited(_addBonusWord()); unawaited(_addPaths()); unawaited(addFromBlueprint(Spaceship())); + + // Fix camera on the center of the board size + camera + ..followVector2(screenToWorld(boardSize / 2)) + ..zoom = size.y / 14; } void _addContactCallbacks() { @@ -41,44 +54,27 @@ class PinballGame extends Forge2DGame } Future _addGameBoundaries() async { - await add(BottomWall(this)); + await add(BottomWall()); createBoundaries(this).forEach(add); } - Future _addBoard() async { - final board = Board( - size: screenToWorld( - Vector2( - camera.viewport.effectiveSize.x, - camera.viewport.effectiveSize.y, - ), - ), - ); - await add(board); - } - Future _addPlunger() async { - plunger = Plunger( - compressionDistance: camera.viewport.effectiveSize.y / 12, - ); - plunger.initialPosition = screenToWorld( - Vector2( - camera.viewport.effectiveSize.x / 2 + 450, - camera.viewport.effectiveSize.y - plunger.compressionDistance, - ), - ); + plunger = Plunger(compressionDistance: 2); + plunger.initialPosition = boardBounds.bottomRight.toVector2() - + Vector2( + 8, + -10, + ); await add(plunger); } Future _addBonusWord() async { await add( BonusWord( - position: screenToWorld( - Vector2( - camera.viewport.effectiveSize.x / 2, - camera.viewport.effectiveSize.y - 50, - ), + position: Vector2( + boardBounds.center.dx, + boardBounds.bottom + 10, ), ), ); @@ -86,18 +82,22 @@ class PinballGame extends Forge2DGame Future _addPaths() async { final jetpackRamp = JetpackRamp( - position: Vector2(42.6, -45), + position: Vector2( + boardBounds.left - 14, + boardBounds.top - 10, + ), ); final launcherRamp = LauncherRamp( - position: screenToWorld( - Vector2( - camera.viewport.effectiveSize.x / 2 + 400, - camera.viewport.effectiveSize.y / 2 - 330, - ), + position: Vector2( + boardBounds.right - 14, + boardBounds.top - 10, ), ); - await addAll([jetpackRamp, launcherRamp]); + await addAll([ + jetpackRamp, + launcherRamp, + ]); } void spawnBall() {