feat: game uses fixed positioning now

pull/70/head
Erick Zanardo 4 years ago
parent aa1a2d7674
commit 8a435d78f0

@ -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<void> 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,
),
);

@ -30,12 +30,14 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
// 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;

@ -28,21 +28,23 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
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;

@ -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(),

@ -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<Wall> 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<Wall> 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(),
);
}

@ -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<void> _addGameBoundaries() async {
await add(BottomWall(this));
await add(BottomWall());
createBoundaries(this).forEach(add);
}
Future<void> _addBoard() async {
final board = Board(
size: screenToWorld(
Vector2(
camera.viewport.effectiveSize.x,
camera.viewport.effectiveSize.y,
),
),
);
await add(board);
}
Future<void> _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<void> _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<void> _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() {

Loading…
Cancel
Save