mirror of https://github.com/flutter/pinball.git
parent
902d3092ee
commit
85936df74e
@ -1,41 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
export 'ball.dart';
|
||||
export 'boundaries.dart';
|
||||
export 'plunger.dart';
|
||||
export 'score_points.dart';
|
||||
|
@ -1,43 +1,12 @@
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
|
||||
late Plunger plunger;
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
class PinballGame extends Forge2DGame with FlameBloc {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
addContactCallback(BallScorePointsCallback());
|
||||
|
||||
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)));
|
||||
|
||||
world.createJoint(
|
||||
PlungerAnchorPrismaticJointDef(plunger: plunger, anchor: bottomWall),
|
||||
);
|
||||
}
|
||||
|
||||
@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