feat: implemented FlipperAnchor

pull/15/head
alestiago 4 years ago
parent 4263c7a340
commit 54eb44233d

@ -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}

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

Loading…
Cancel
Save