From 6cbda5bac3d90db81881f20754e62917a6e92d95 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Thu, 10 Mar 2022 10:39:38 -0600 Subject: [PATCH] refactor: use joint specific anchor --- lib/game/components/flipper.dart | 2 +- lib/game/components/plunger.dart | 7 +-- lib/game/components/wall.dart | 1 + lib/game/pinball_game.dart | 2 +- test/game/components/plunger_test.dart | 64 +++++++++----------------- 5 files changed, 26 insertions(+), 50 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 16754ed3..a14679f0 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -218,7 +218,7 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { /// {@macro flipper_anchor_revolute_joint_def} FlipperAnchorRevoluteJointDef({ required Flipper flipper, - required Anchor anchor, + required FlipperAnchor anchor, }) { initialize( flipper.body, diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index ea5742da..4dd4285a 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -99,11 +99,8 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { /// {@macro plunger_anchor_prismatic_joint_def} PlungerAnchorPrismaticJointDef({ required Plunger plunger, - required Anchor anchor, - }) : assert( - anchor.body.position.y < plunger.body.position.y, - 'Anchor must be below the Plunger', - ) { + required PlungerAnchor anchor, + }) { initialize( plunger.body, anchor.body, diff --git a/lib/game/components/wall.dart b/lib/game/components/wall.dart index 3e9b1f8b..762d404e 100644 --- a/lib/game/components/wall.dart +++ b/lib/game/components/wall.dart @@ -37,6 +37,7 @@ class Wall extends BodyComponent { } } +/// Create top, left, and right [Wall]s for the game board. List createBoundaries(Forge2DGame game) { final topLeft = Vector2.zero(); final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize); diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index c287a378..c89e064f 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -126,7 +126,7 @@ class PinballGame extends Forge2DGame } Future _addPlunger() async { - late Anchor plungerAnchor; + late PlungerAnchor plungerAnchor; await add( plunger = Plunger( diff --git a/test/game/components/plunger_test.dart b/test/game/components/plunger_test.dart index bb654d56..4d159e63 100644 --- a/test/game/components/plunger_test.dart +++ b/test/game/components/plunger_test.dart @@ -182,7 +182,6 @@ void main() { group('PlungerAnchorPrismaticJointDef', () { late GameBloc gameBloc; late Plunger plunger; - late Anchor anchor; setUp(() { gameBloc = MockGameBloc(); @@ -192,7 +191,6 @@ void main() { initialState: const GameState.initial(), ); plunger = Plunger(position: Vector2.zero()); - anchor = Anchor(position: Vector2(0, -1)); }); final flameTester = flameBlocTester( @@ -201,45 +199,13 @@ void main() { }, ); - flameTester.test( - 'throws AssertionError ' - 'when anchor is above plunger', - (game) async { - final anchor = Anchor(position: Vector2(0, 1)); - await game.ensureAddAll([plunger, anchor]); - - expect( - () => PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: anchor, - ), - throwsAssertionError, - ); - }, - ); - - flameTester.test( - 'throws AssertionError ' - 'when anchor is in same position as plunger', - (game) async { - final anchor = Anchor(position: Vector2.zero()); - await game.ensureAddAll([plunger, anchor]); - - expect( - () => PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: anchor, - ), - throwsAssertionError, - ); - }, - ); - group('initializes with', () { flameTester.test( 'plunger body as bodyA', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -253,7 +219,9 @@ void main() { flameTester.test( 'anchor body as bodyB', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -268,7 +236,9 @@ void main() { flameTester.test( 'limits enabled', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -283,7 +253,9 @@ void main() { flameTester.test( 'lower translation limit as negative infinity', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -298,7 +270,9 @@ void main() { flameTester.test( 'connected body collison enabled', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -315,7 +289,9 @@ void main() { flameTester.widgetTest( 'plunger cannot go below anchor', (game, tester) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); // Giving anchor a shape for the plunger to collide with. anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1)); @@ -337,7 +313,9 @@ void main() { flameTester.widgetTest( 'plunger cannot excessively exceed starting position', (game, tester) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger,