diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index ffca4df7..23a03404 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -69,12 +69,11 @@ class _BottomGroupSide extends Component { ); final slingShot = SlingShot( side: _side, - position: _position + - Vector2( - (Flipper.width) * direction, - Flipper.height + SlingShot.size.y, - ), - ); + )..initialPosition = _position + + Vector2( + (Flipper.width) * direction, + Flipper.height + SlingShot.size.y, + ); await addAll([flipper, baseboard, slingShot]); } diff --git a/lib/game/components/sling_shot.dart b/lib/game/components/sling_shot.dart index 0791cf4f..61d28c29 100644 --- a/lib/game/components/sling_shot.dart +++ b/lib/game/components/sling_shot.dart @@ -10,22 +10,17 @@ import 'package:pinball/game/game.dart'; /// /// [SlingShot]s are usually positioned above each [Flipper]. /// {@endtemplate sling_shot} -class SlingShot extends BodyComponent { +class SlingShot extends BodyComponent with InitialPosition { /// {@macro sling_shot} SlingShot({ - required Vector2 position, required BoardSide side, - }) : _position = position, - _side = side { + }) : _side = side { // TODO(alestiago): Use sprite instead of color when provided. paint = Paint() ..color = const Color(0xFF00FF00) ..style = PaintingStyle.fill; } - /// The initial position of the [SlingShot] body. - final Vector2 _position; - /// Whether the [SlingShot] is on the left or right side of the board. /// /// A [SlingShot] with [BoardSide.left] propels the [Ball] to the right, @@ -88,7 +83,7 @@ class SlingShot extends BodyComponent { @override Body createBody() { - final bodyDef = BodyDef()..position = _position; + final bodyDef = BodyDef()..position = initialPosition; final body = world.createBody(bodyDef); _createFixtureDefs().forEach(body.createFixture); diff --git a/test/game/components/sling_shot_test.dart b/test/game/components/sling_shot_test.dart index e7e89ead..73a4fc61 100644 --- a/test/game/components/sling_shot_test.dart +++ b/test/game/components/sling_shot_test.dart @@ -13,9 +13,8 @@ void main() { 'loads correctly', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); expect(game.contains(slingShot), isTrue); @@ -28,9 +27,8 @@ void main() { (game) async { final position = Vector2.all(10); final slingShot = SlingShot( - position: position, side: BoardSide.left, - ); + )..initialPosition = position; await game.ensureAdd(slingShot); expect(slingShot.body.position, equals(position)); @@ -41,9 +39,8 @@ void main() { 'is static', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); expect(slingShot.body.bodyType, equals(BodyType.static)); @@ -56,9 +53,8 @@ void main() { 'exists', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); expect(slingShot.body.fixtures[0], isA()); @@ -69,9 +65,8 @@ void main() { 'shape is triangular', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[0]; @@ -85,13 +80,11 @@ void main() { 'when side is left or right', (game) async { final leftSlingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); final rightSlingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.right, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(leftSlingShot); await game.ensureAdd(rightSlingShot); @@ -109,9 +102,8 @@ void main() { 'has no friction', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[0]; @@ -125,9 +117,8 @@ void main() { 'exists', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); expect(slingShot.body.fixtures[1], isA()); @@ -138,9 +129,8 @@ void main() { 'shape is edge', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[1]; @@ -152,9 +142,8 @@ void main() { 'has restitution', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[1]; @@ -166,9 +155,8 @@ void main() { 'has no friction', (game) async { final slingShot = SlingShot( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2(0, 0); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[1];