From 85db44a6171d7bf79bf9cecc1fb75bd0940d3551 Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 16 Mar 2022 11:52:59 +0000 Subject: [PATCH] feat: made Baseboard use InitialPosition --- lib/game/components/baseboard.dart | 11 +++------- lib/game/components/board.dart | 8 +++----- test/game/components/baseboard_test.dart | 26 ++++++++++-------------- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/lib/game/components/baseboard.dart b/lib/game/components/baseboard.dart index 60e51593..4af759d8 100644 --- a/lib/game/components/baseboard.dart +++ b/lib/game/components/baseboard.dart @@ -6,13 +6,11 @@ import 'package:pinball/game/game.dart'; /// {@template baseboard} /// Straight, angled board piece to corral the [Ball] towards the [Flipper]s. /// {@endtemplate} -class Baseboard extends BodyComponent { +class Baseboard extends BodyComponent with InitialPosition { /// {@macro baseboard} Baseboard({ required BoardSide side, - required Vector2 position, - }) : _side = side, - _position = position; + }) : _side = side; /// The width of the [Baseboard]. static const width = 10.0; @@ -20,9 +18,6 @@ class Baseboard extends BodyComponent { /// The height of the [Baseboard]. static const height = 2.0; - /// The position of the [Baseboard] body. - final Vector2 _position; - /// Whether the [Baseboard] is on the left or right side of the board. final BoardSide _side; @@ -63,7 +58,7 @@ class Baseboard extends BodyComponent { const angle = math.pi / 7; final bodyDef = BodyDef() - ..position = _position + ..position = initialPosition ..type = BodyType.static ..angle = _side.isLeft ? -angle : angle; diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index 674c3e43..ffca4df7 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -61,14 +61,12 @@ class _BottomGroupSide extends Component { side: _side, position: _position, ); - final baseboard = Baseboard( - side: _side, - position: _position + + final baseboard = Baseboard(side: _side) + ..initialPosition = _position + Vector2( (Flipper.width * direction) - direction, Flipper.height, - ), - ); + ); final slingShot = SlingShot( side: _side, position: _position + diff --git a/test/game/components/baseboard_test.dart b/test/game/components/baseboard_test.dart index 8f1874b1..8b76b4c2 100644 --- a/test/game/components/baseboard_test.dart +++ b/test/game/components/baseboard_test.dart @@ -15,13 +15,12 @@ void main() { (game) async { await game.ready(); final leftBaseboard = Baseboard( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2.zero(); final rightBaseboard = Baseboard( - position: Vector2.zero(), side: BoardSide.right, - ); + )..initialPosition = Vector2.zero(); + await game.ensureAddAll([leftBaseboard, rightBaseboard]); expect(game.contains(leftBaseboard), isTrue); @@ -35,13 +34,13 @@ void main() { (game) async { final position = Vector2.all(10); final baseboard = Baseboard( - position: position, side: BoardSide.left, - ); + )..initialPosition = position; + await game.ensureAdd(baseboard); game.contains(baseboard); - expect(baseboard.body.position, position); + expect(baseboard.body.position, equals(position)); }, ); @@ -49,9 +48,9 @@ void main() { 'is static', (game) async { final baseboard = Baseboard( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2.zero(); + await game.ensureAdd(baseboard); expect(baseboard.body.bodyType, equals(BodyType.static)); @@ -62,13 +61,11 @@ void main() { 'is at an angle', (game) async { final leftBaseboard = Baseboard( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2.zero(); final rightBaseboard = Baseboard( - position: Vector2.zero(), side: BoardSide.right, - ); + )..initialPosition = Vector2.zero(); await game.ensureAddAll([leftBaseboard, rightBaseboard]); expect(leftBaseboard.body.angle, isNegative); @@ -82,9 +79,8 @@ void main() { 'has three', (game) async { final baseboard = Baseboard( - position: Vector2.zero(), side: BoardSide.left, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(baseboard); expect(baseboard.body.fixtures.length, equals(3));