docs: plunger issue comments

pull/10/head
Allison Ryan 4 years ago
parent 6cf2ccb5e0
commit 92590b9c82

@ -1,4 +1,3 @@
import 'package:flame_forge2d/body_component.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
class Plunger extends BodyComponent { class Plunger extends BodyComponent {
@ -20,10 +19,12 @@ class Plunger extends BodyComponent {
return world.createBody(bodyDef)..createFixture(fixtureDef); return world.createBody(bodyDef)..createFixture(fixtureDef);
} }
// Unused for now - from the previous kinematic plunger implementation.
void pull() { void pull() {
body.linearVelocity = Vector2(0, -5); body.linearVelocity = Vector2(0, -5);
} }
// Unused for now - from the previous kinematic plunger implementation.
void release() { void release() {
final velocity = (_position.y - body.position.y) * 9; final velocity = (_position.y - body.position.y) * 9;
body.linearVelocity = Vector2(0, velocity); body.linearVelocity = Vector2(0, velocity);

@ -7,6 +7,7 @@ import 'package:pinball/game/game.dart';
class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
late Plunger plunger; late Plunger plunger;
late PrismaticJointDef prismaticJointDef;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
@ -20,22 +21,30 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
await add(plunger = Plunger(Vector2(center.x, center.y - 50))); await add(plunger = Plunger(Vector2(center.x, center.y - 50)));
final prismaticJointDef = PrismaticJointDef() prismaticJointDef = PrismaticJointDef()
..initialize( ..initialize(
plunger.body, plunger.body,
bottomWall.body, bottomWall.body,
bottomWall.body.position, plunger.body.position,
Vector2(0, 0), // 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 ..enableLimit = true
..upperTranslation = 0 // Given the above inverted vertical axis, the lowerTranslation works as
..lowerTranslation = -5 // 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; ..collideConnected = true;
world.createJoint(prismaticJointDef); world.createJoint(prismaticJointDef);
print(prismaticJointDef.localAnchorA);
print(prismaticJointDef.localAnchorB);
} }
@override @override
@ -45,11 +54,19 @@ class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
) { ) {
if (event is RawKeyUpEvent && if (event is RawKeyUpEvent &&
event.data.logicalKey == LogicalKeyboardKey.space) { 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 && if (event is RawKeyDownEvent &&
event.data.logicalKey == LogicalKeyboardKey.space) { 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; return KeyEventResult.handled;
} }

Loading…
Cancel
Save