From 7338b95ef55a05a031a3e81dee470dd353a7ec87 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Wed, 2 Mar 2022 08:42:11 -0600 Subject: [PATCH] feat: plunger --- lib/game/components/boundaries.dart | 41 ++++++++++++++++++++++ lib/game/components/components.dart | 2 ++ lib/game/components/plunger.dart | 31 +++++++++++++++++ lib/game/game.dart | 1 + lib/game/pinball_game.dart | 54 ++++++++++++++++++++++++++++- 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 lib/game/components/boundaries.dart create mode 100644 lib/game/components/components.dart create mode 100644 lib/game/components/plunger.dart diff --git a/lib/game/components/boundaries.dart b/lib/game/components/boundaries.dart new file mode 100644 index 00000000..08e9a2c3 --- /dev/null +++ b/lib/game/components/boundaries.dart @@ -0,0 +1,41 @@ +import 'package:flame_forge2d/body_component.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flame_forge2d/forge2d_game.dart'; +import 'package:forge2d/forge2d.dart'; + +List createBoundaries(Forge2DGame game) { + final topLeft = Vector2.zero(); + final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize); + final topRight = Vector2(bottomRight.x, topLeft.y); + final bottomLeft = Vector2(topLeft.x, bottomRight.y); + + return [ + Wall(topLeft, topRight), + Wall(topRight, bottomRight), + Wall(bottomRight, bottomLeft), + Wall(bottomLeft, topLeft), + ]; +} + +class Wall extends BodyComponent { + Wall(this.start, this.end); + + final Vector2 start; + final Vector2 end; + + @override + Body createBody() { + final shape = EdgeShape()..set(start, end); + + final fixtureDef = FixtureDef(shape) + ..restitution = 0.0 + ..friction = 0.3; + + final bodyDef = BodyDef() + ..userData = this + ..position = Vector2.zero() + ..type = BodyType.static; + + return world.createBody(bodyDef)..createFixture(fixtureDef); + } +} diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart new file mode 100644 index 00000000..85ec5ae3 --- /dev/null +++ b/lib/game/components/components.dart @@ -0,0 +1,2 @@ +export 'boundaries.dart'; +export 'plunger.dart'; diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart new file mode 100644 index 00000000..7b168629 --- /dev/null +++ b/lib/game/components/plunger.dart @@ -0,0 +1,31 @@ +import 'package:flame_forge2d/body_component.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; + +class Plunger extends BodyComponent { + Plunger(this._position); + + final Vector2 _position; + + @override + Body createBody() { + final shape = PolygonShape()..setAsBoxXY(2.5, 1.5); + + final fixtureDef = FixtureDef(shape)..friction = 0.1; + + final bodyDef = BodyDef() + ..userData = this + ..position = _position + ..type = BodyType.dynamic; + + return world.createBody(bodyDef)..createFixture(fixtureDef); + } + + void pull() { + body.linearVelocity = Vector2(0, -5); + } + + void release() { + final velocity = (_position.y - body.position.y) * 9; + body.linearVelocity = Vector2(0, velocity); + } +} diff --git a/lib/game/game.dart b/lib/game/game.dart index ec8e0824..0a8dac1b 100644 --- a/lib/game/game.dart +++ b/lib/game/game.dart @@ -1,2 +1,3 @@ +export 'components/components.dart'; export 'pinball_game.dart'; export 'view/pinball_game_page.dart'; diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 306d03f0..031e51d7 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -1,3 +1,55 @@ +import 'package:flame/input.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/forge2d_game.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:pinball/game/game.dart'; -class PinballGame extends Forge2DGame {} +class PinballGame extends Forge2DGame with KeyboardEvents { + late Plunger plunger; + + @override + Future onLoad() async { + await super.onLoad(); + + final boundaries = createBoundaries(this)..forEach(add); + final bottomWall = boundaries[2]; + + final center = screenToWorld(camera.viewport.effectiveSize / 2); + + await add(plunger = Plunger(Vector2(center.x, center.y - 50))); + + final prismaticJointDef = PrismaticJointDef() + ..initialize( + plunger.body, + bottomWall.body, + bottomWall.body.position, + Vector2(0, 0), + ) + ..localAnchorA.setFrom(Vector2(0, 0)) + ..enableLimit = true + ..upperTranslation = 0 + ..lowerTranslation = -5 + ..collideConnected = true; + + world.createJoint(prismaticJointDef); + print(prismaticJointDef.localAnchorA); + print(prismaticJointDef.localAnchorB); + } + + @override + KeyEventResult onKeyEvent( + RawKeyEvent event, + Set keysPressed, + ) { + if (event is RawKeyUpEvent && + event.data.logicalKey == LogicalKeyboardKey.space) { + plunger.release(); + } + if (event is RawKeyDownEvent && + event.data.logicalKey == LogicalKeyboardKey.space) { + plunger.pull(); + } + return KeyEventResult.handled; + } +}