From 8dc3c8487fe47ebea3a8d983b04cfdcf1440e770 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Mon, 14 Mar 2022 10:56:30 -0500 Subject: [PATCH] chore: anchor plunger internally --- lib/game/components/plunger.dart | 45 ++++++++++++++++++++++++++++++++ lib/game/pinball_game.dart | 9 ------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index bf63b462..692e06ac 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -74,6 +74,25 @@ class Plunger extends BodyComponent with KeyboardHandler { return true; } + + /// Anchors the [Plunger] to the [PrismaticJoint] + /// that controls its vertical motion. + Future _anchorToJoint() async { + final anchor = PlungerAnchor(plunger: this); + await add(anchor); + + final jointDef = PlungerAnchorPrismaticJointDef( + plunger: this, + anchor: anchor, + ); + world.createJoint(jointDef); + } + + @override + Future onLoad() async { + await super.onLoad(); + await _anchorToJoint(); + } } /// {@template plunger_anchor} @@ -118,3 +137,29 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { collideConnected = true; } } + +// /// {@template plunger_group} +// /// Loads a [Plunger]. +// /// {@endtemplate} +// class PlungerGroup extends Component { +// /// {@macro plunger_group} +// PlungerGroup({ +// required this.position, +// required this.compressionDistance, +// }); + +// /// The position of this [PlungerGroup] +// final Vector2 position; + +// /// Distance the plunger can lower. +// final double compressionDistance; + +// @override +// Future onLoad() async { +// final plunger = Plunger( +// position: position, +// compressionDistance: compressionDistance, +// ); +// await add(plunger); +// } +// } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 633c2351..b67a8dad 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -101,7 +101,6 @@ class PinballGame extends Forge2DGame } Future _addPlunger() async { - late PlungerAnchor plungerAnchor; final compressionDistance = camera.viewport.effectiveSize.y / 12; await add( @@ -115,14 +114,6 @@ class PinballGame extends Forge2DGame compressionDistance: compressionDistance, ), ); - await add(plungerAnchor = PlungerAnchor(plunger: plunger)); - - world.createJoint( - PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: plungerAnchor, - ), - ); } Future _addBaseboards() async {