diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 9bcde451..77168bb2 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -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); } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 112f0522..6d8bf34b 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -93,19 +93,16 @@ class PinballGame extends Forge2DGame } Future _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); } } diff --git a/test/game/components/plunger_test.dart b/test/game/components/plunger_test.dart index ecc4265e..4512db52 100644 --- a/test/game/components/plunger_test.dart +++ b/test/game/components/plunger_test.dart @@ -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()); @@ -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 {