mirror of https://github.com/flutter/pinball.git
parent
ab77ebec67
commit
7338b95ef5
@ -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<Wall> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
export 'boundaries.dart';
|
||||||
|
export '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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
|
export 'components/components.dart';
|
||||||
export 'pinball_game.dart';
|
export 'pinball_game.dart';
|
||||||
export 'view/pinball_game_page.dart';
|
export 'view/pinball_game_page.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: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<void> 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<LogicalKeyboardKey> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue