From daede67b675e3f571af314e7322b75a555f1657d Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 16 Mar 2022 13:56:19 +0000 Subject: [PATCH] feat: made InitialPosition position the body --- lib/game/components/ball.dart | 1 - lib/game/components/baseboard.dart | 1 - lib/game/components/bonus_word.dart | 1 - lib/game/components/flipper.dart | 4 +- lib/game/components/initial_position.dart | 5 +-- lib/game/components/joint_anchor.dart | 4 +- lib/game/components/pathway.dart | 5 +-- lib/game/components/plunger.dart | 1 - lib/game/components/round_bumper.dart | 4 +- lib/game/components/sling_shot.dart | 3 +- .../components/initial_position_test.dart | 40 +++---------------- 11 files changed, 12 insertions(+), 57 deletions(-) diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index 74b4f3c6..d90cca78 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -40,7 +40,6 @@ class Ball extends BodyComponent with InitialPosition { final bodyDef = BodyDef() ..userData = this - ..position = initialPosition ..type = BodyType.dynamic; return world.createBody(bodyDef)..createFixture(fixtureDef); diff --git a/lib/game/components/baseboard.dart b/lib/game/components/baseboard.dart index 4af759d8..77f07024 100644 --- a/lib/game/components/baseboard.dart +++ b/lib/game/components/baseboard.dart @@ -58,7 +58,6 @@ class Baseboard extends BodyComponent with InitialPosition { const angle = math.pi / 7; final bodyDef = BodyDef() - ..position = initialPosition ..type = BodyType.static ..angle = _side.isLeft ? -angle : angle; diff --git a/lib/game/components/bonus_word.dart b/lib/game/components/bonus_word.dart index 378240b5..e1078e30 100644 --- a/lib/game/components/bonus_word.dart +++ b/lib/game/components/bonus_word.dart @@ -130,7 +130,6 @@ class BonusLetter extends BodyComponent final bodyDef = BodyDef() ..userData = this - ..position = initialPosition ..type = BodyType.static; return world.createBody(bodyDef)..createFixture(fixtureDef); diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index dd626d1b..03875d36 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -190,9 +190,7 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { Body createBody() { final bodyDef = BodyDef() ..gravityScale = 0 - ..type = BodyType.dynamic - ..position = initialPosition; - + ..type = BodyType.dynamic; final body = world.createBody(bodyDef); _createFixtureDefs().forEach(body.createFixture); diff --git a/lib/game/components/initial_position.dart b/lib/game/components/initial_position.dart index fef2be5f..61f19088 100644 --- a/lib/game/components/initial_position.dart +++ b/lib/game/components/initial_position.dart @@ -9,9 +9,6 @@ mixin InitialPosition on BodyComponent { @override Future onLoad() async { await super.onLoad(); - assert( - body.position == initialPosition, - 'Body position is not equal to initial position.', - ); + body.position.setFrom(initialPosition); } } diff --git a/lib/game/components/joint_anchor.dart b/lib/game/components/joint_anchor.dart index 06f603b8..36ad3ab0 100644 --- a/lib/game/components/joint_anchor.dart +++ b/lib/game/components/joint_anchor.dart @@ -22,8 +22,6 @@ class JointAnchor extends BodyComponent with InitialPosition { @override Body createBody() { - final bodyDef = BodyDef()..position = initialPosition; - - return world.createBody(bodyDef); + return world.createBody(BodyDef()); } } diff --git a/lib/game/components/pathway.dart b/lib/game/components/pathway.dart index 7174c9cd..bd8caf5d 100644 --- a/lib/game/components/pathway.dart +++ b/lib/game/components/pathway.dart @@ -148,10 +148,7 @@ class Pathway extends BodyComponent with InitialPosition { @override Body createBody() { - final bodyDef = BodyDef() - ..type = BodyType.static - ..position = initialPosition; - + final bodyDef = BodyDef()..type = BodyType.static; final body = world.createBody(bodyDef); for (final path in _paths) { final chain = ChainShape() diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 9bd8c4b7..68ccbeb0 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -26,7 +26,6 @@ class Plunger extends BodyComponent with KeyboardHandler, InitialPosition { final bodyDef = BodyDef() ..userData = this - ..position = initialPosition ..type = BodyType.dynamic ..gravityScale = 0; diff --git a/lib/game/components/round_bumper.dart b/lib/game/components/round_bumper.dart index eb3715ec..af8285af 100644 --- a/lib/game/components/round_bumper.dart +++ b/lib/game/components/round_bumper.dart @@ -27,9 +27,7 @@ class RoundBumper extends BodyComponent with ScorePoints, InitialPosition { final fixtureDef = FixtureDef(shape)..restitution = 1; - final bodyDef = BodyDef() - ..position = initialPosition - ..type = BodyType.static; + final bodyDef = BodyDef()..type = BodyType.static; return world.createBody(bodyDef)..createFixture(fixtureDef); } diff --git a/lib/game/components/sling_shot.dart b/lib/game/components/sling_shot.dart index 61d28c29..0002ed0c 100644 --- a/lib/game/components/sling_shot.dart +++ b/lib/game/components/sling_shot.dart @@ -83,8 +83,7 @@ class SlingShot extends BodyComponent with InitialPosition { @override Body createBody() { - final bodyDef = BodyDef()..position = initialPosition; - final body = world.createBody(bodyDef); + final body = world.createBody(BodyDef()); _createFixtureDefs().forEach(body.createFixture); return body; diff --git a/test/game/components/initial_position_test.dart b/test/game/components/initial_position_test.dart index 393d780a..4684ed71 100644 --- a/test/game/components/initial_position_test.dart +++ b/test/game/components/initial_position_test.dart @@ -12,13 +12,6 @@ class TestBodyComponent extends BodyComponent with InitialPosition { } } -class TestPositionedBodyComponent extends BodyComponent with InitialPosition { - @override - Body createBody() { - return world.createBody(BodyDef()..position = initialPosition); - } -} - void main() { final flameTester = FlameTester(Forge2DGame.new); group('InitialPosition', () { @@ -35,32 +28,11 @@ void main() { ); }); - flameTester.test( - 'returns normally ' - 'when the body sets the position to initial position', - (game) async { - final component = TestPositionedBodyComponent() - ..initialPosition = Vector2.zero(); - - await expectLater( - () async => game.ensureAdd(component), - returnsNormally, - ); - }, - ); - - flameTester.test( - 'throws AssertionError ' - 'when not setting initialPosition to body', - (game) async { - final component = TestBodyComponent()..initialPosition = Vector2.zero(); - await game.ensureAdd(component); - - await expectLater( - () async => game.ensureAdd(component), - throwsAssertionError, - ); - }, - ); + flameTester.test('positions correctly', (game) async { + final position = Vector2.all(10); + final component = TestBodyComponent()..initialPosition = position; + await game.ensureAdd(component); + expect(component.body.position, equals(position)); + }); }); }