feat: made Plunger use InitialPosition

pull/50/head
alestiago 4 years ago
parent 85db44a617
commit b904ef27d7

@ -9,15 +9,11 @@ import 'package:pinball/game/game.dart';
///
/// [Plunger] ignores gravity so the player controls its downward [_pull].
/// {@endtemplate}
class Plunger extends BodyComponent with KeyboardHandler {
class Plunger extends BodyComponent with KeyboardHandler, InitialPosition {
/// {@macro plunger}
Plunger({
required Vector2 position,
required this.compressionDistance,
}) : _position = position;
/// The initial position of the [Plunger] body.
final Vector2 _position;
});
/// Distance the plunger can lower.
final double compressionDistance;
@ -30,7 +26,7 @@ class Plunger extends BodyComponent with KeyboardHandler {
final bodyDef = BodyDef()
..userData = this
..position = _position
..position = initialPosition
..type = BodyType.dynamic
..gravityScale = 0;
@ -45,9 +41,9 @@ class Plunger extends BodyComponent with KeyboardHandler {
/// Set an upward velocity on the [Plunger].
///
/// The velocity's magnitude depends on how far the [Plunger] has been pulled
/// from its original [_position].
/// from its original [initialPosition].
void _release() {
final velocity = (_position.y - body.position.y) * 9;
final velocity = (initialPosition.y - body.position.y) * 9;
body.linearVelocity = Vector2(0, velocity);
}

@ -93,19 +93,16 @@ class PinballGame extends Forge2DGame
}
Future<void> _addPlunger() async {
final compressionDistance = camera.viewport.effectiveSize.y / 12;
await add(
plunger = Plunger(
position: screenToWorld(
Vector2(
camera.viewport.effectiveSize.x / 1.035,
camera.viewport.effectiveSize.y - compressionDistance,
),
plunger = Plunger(
compressionDistance: camera.viewport.effectiveSize.y / 12,
)..initialPosition = screenToWorld(
Vector2(
camera.viewport.effectiveSize.x / 1.035,
camera.viewport.effectiveSize.y - plunger.compressionDistance,
),
compressionDistance: compressionDistance,
),
);
);
await add(plunger);
}
}

@ -23,9 +23,8 @@ void main() {
(game) async {
await game.ready();
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
expect(game.contains(plunger), isTrue);
@ -38,9 +37,8 @@ void main() {
(game) async {
final position = Vector2.all(10);
final plunger = Plunger(
position: position,
compressionDistance: compressionDistance,
);
)..initialPosition = position;
await game.ensureAdd(plunger);
game.contains(plunger);
@ -52,9 +50,8 @@ void main() {
'is dynamic',
(game) async {
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
expect(plunger.body.bodyType, equals(BodyType.dynamic));
@ -65,9 +62,8 @@ void main() {
'ignores gravity',
(game) async {
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
expect(plunger.body.gravityScale, isZero);
@ -80,9 +76,8 @@ void main() {
'exists',
(game) async {
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
expect(plunger.body.fixtures[0], isA<Fixture>());
@ -93,9 +88,8 @@ void main() {
'shape is a polygon',
(game) async {
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
final fixture = plunger.body.fixtures[0];
@ -107,9 +101,8 @@ void main() {
'has density',
(game) async {
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
final fixture = plunger.body.fixtures[0];
@ -129,9 +122,8 @@ void main() {
setUp(() {
plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
});
testRawKeyUpEvents(keys, (event) {
@ -194,9 +186,8 @@ void main() {
'position is a compression distance below the Plunger',
(game) async {
final plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
await game.ensureAdd(plunger);
final plungerAnchor = PlungerAnchor(plunger: plunger);
@ -222,14 +213,14 @@ void main() {
initialState: const GameState.initial(),
);
plunger = Plunger(
position: Vector2.zero(),
compressionDistance: compressionDistance,
);
)..initialPosition = Vector2.zero();
});
final flameTester = flameBlocTester(gameBloc: () => gameBloc);
group('initializes with', () {
// FIXME(alestiago): Plunger not initialized error.
flameTester.test(
'plunger body as bodyA',
(game) async {

Loading…
Cancel
Save