refactor: use joint specific anchor

pull/25/head
Allison Ryan 4 years ago
parent 0ec0bf82af
commit 6cbda5bac3

@ -218,7 +218,7 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
/// {@macro flipper_anchor_revolute_joint_def} /// {@macro flipper_anchor_revolute_joint_def}
FlipperAnchorRevoluteJointDef({ FlipperAnchorRevoluteJointDef({
required Flipper flipper, required Flipper flipper,
required Anchor anchor, required FlipperAnchor anchor,
}) { }) {
initialize( initialize(
flipper.body, flipper.body,

@ -99,11 +99,8 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef {
/// {@macro plunger_anchor_prismatic_joint_def} /// {@macro plunger_anchor_prismatic_joint_def}
PlungerAnchorPrismaticJointDef({ PlungerAnchorPrismaticJointDef({
required Plunger plunger, required Plunger plunger,
required Anchor anchor, required PlungerAnchor anchor,
}) : assert( }) {
anchor.body.position.y < plunger.body.position.y,
'Anchor must be below the Plunger',
) {
initialize( initialize(
plunger.body, plunger.body,
anchor.body, anchor.body,

@ -37,6 +37,7 @@ class Wall extends BodyComponent {
} }
} }
/// Create top, left, and right [Wall]s for the game board.
List<Wall> createBoundaries(Forge2DGame game) { List<Wall> createBoundaries(Forge2DGame game) {
final topLeft = Vector2.zero(); final topLeft = Vector2.zero();
final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize); final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize);

@ -126,7 +126,7 @@ class PinballGame extends Forge2DGame
} }
Future<void> _addPlunger() async { Future<void> _addPlunger() async {
late Anchor plungerAnchor; late PlungerAnchor plungerAnchor;
await add( await add(
plunger = Plunger( plunger = Plunger(

@ -182,7 +182,6 @@ void main() {
group('PlungerAnchorPrismaticJointDef', () { group('PlungerAnchorPrismaticJointDef', () {
late GameBloc gameBloc; late GameBloc gameBloc;
late Plunger plunger; late Plunger plunger;
late Anchor anchor;
setUp(() { setUp(() {
gameBloc = MockGameBloc(); gameBloc = MockGameBloc();
@ -192,7 +191,6 @@ void main() {
initialState: const GameState.initial(), initialState: const GameState.initial(),
); );
plunger = Plunger(position: Vector2.zero()); plunger = Plunger(position: Vector2.zero());
anchor = Anchor(position: Vector2(0, -1));
}); });
final flameTester = flameBlocTester( 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', () { group('initializes with', () {
flameTester.test( flameTester.test(
'plunger body as bodyA', 'plunger body as bodyA',
(game) async { (game) async {
await game.ensureAddAll([plunger, anchor]); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
@ -253,7 +219,9 @@ void main() {
flameTester.test( flameTester.test(
'anchor body as bodyB', 'anchor body as bodyB',
(game) async { (game) async {
await game.ensureAddAll([plunger, anchor]); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
@ -268,7 +236,9 @@ void main() {
flameTester.test( flameTester.test(
'limits enabled', 'limits enabled',
(game) async { (game) async {
await game.ensureAddAll([plunger, anchor]); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
@ -283,7 +253,9 @@ void main() {
flameTester.test( flameTester.test(
'lower translation limit as negative infinity', 'lower translation limit as negative infinity',
(game) async { (game) async {
await game.ensureAddAll([plunger, anchor]); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
@ -298,7 +270,9 @@ void main() {
flameTester.test( flameTester.test(
'connected body collison enabled', 'connected body collison enabled',
(game) async { (game) async {
await game.ensureAddAll([plunger, anchor]); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
@ -315,7 +289,9 @@ void main() {
flameTester.widgetTest( flameTester.widgetTest(
'plunger cannot go below anchor', 'plunger cannot go below anchor',
(game, tester) async { (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. // Giving anchor a shape for the plunger to collide with.
anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1)); anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1));
@ -337,7 +313,9 @@ void main() {
flameTester.widgetTest( flameTester.widgetTest(
'plunger cannot excessively exceed starting position', 'plunger cannot excessively exceed starting position',
(game, tester) async { (game, tester) async {
await game.ensureAddAll([plunger, anchor]); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,

Loading…
Cancel
Save