diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 7b168629..d52c7c31 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -1,4 +1,3 @@ -import 'package:flame_forge2d/body_component.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; class Plunger extends BodyComponent { @@ -20,10 +19,12 @@ class Plunger extends BodyComponent { return world.createBody(bodyDef)..createFixture(fixtureDef); } + // Unused for now - from the previous kinematic plunger implementation. void pull() { body.linearVelocity = Vector2(0, -5); } + // Unused for now - from the previous kinematic plunger implementation. void release() { final velocity = (_position.y - body.position.y) * 9; body.linearVelocity = Vector2(0, velocity); diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 597c53ca..ff112a43 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -7,6 +7,7 @@ import 'package:pinball/game/game.dart'; class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { late Plunger plunger; + late PrismaticJointDef prismaticJointDef; @override Future onLoad() async { @@ -20,22 +21,30 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { await add(plunger = Plunger(Vector2(center.x, center.y - 50))); - final prismaticJointDef = PrismaticJointDef() + prismaticJointDef = PrismaticJointDef() ..initialize( plunger.body, bottomWall.body, - bottomWall.body.position, - Vector2(0, 0), + plunger.body.position, + // Logically, I feel like this should be (0, 1), but it has to be + // negative for lowerTranslation limit to work as expected. + Vector2(0, -1), ) - ..localAnchorA.setFrom(Vector2(0, 0)) ..enableLimit = true - ..upperTranslation = 0 - ..lowerTranslation = -5 + // Given the above inverted vertical axis, the lowerTranslation works as + // expected and this lets the plunger fall down 10 units before being + // stopped. + // + // Ideally, we shouldn't need to set any limits here - this is just for + // demo purposes to see how the limits work. We should be leaving this at + // 0 and altering it as the user holds the space bar. The longer they hold + // it, the lower the lowerTranslation becomes - allowing the plunger to + // slowly fall down (see key event handlers below). + ..lowerTranslation = -10 + // This prevents the plunger from falling through the bottom wall. ..collideConnected = true; world.createJoint(prismaticJointDef); - print(prismaticJointDef.localAnchorA); - print(prismaticJointDef.localAnchorB); } @override @@ -45,11 +54,19 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { ) { if (event is RawKeyUpEvent && event.data.logicalKey == LogicalKeyboardKey.space) { - plunger.release(); + // I haven't been able to successfully pull down the plunger, so this is + // completely untested. I imagine we could calculate the distance between + // the prismaticJoinDef.upperTranslation (plunger starting position) and + // the ground, then use that value as a multiplier on the speed so the + // ball moves faster when you pull the plunger farther down. + prismaticJointDef.motorSpeed = 5; } if (event is RawKeyDownEvent && event.data.logicalKey == LogicalKeyboardKey.space) { - plunger.pull(); + // This was my attempt to decrement the lower limit but it doesn't seem to + // render. If you debug, you can see that this value is being lowered, + // but the game isn't reflecting these value changes. + prismaticJointDef.lowerTranslation--; } return KeyEventResult.handled; }