mirror of https://github.com/flutter/pinball.git
parent
0595e82649
commit
424f35a958
@ -0,0 +1,2 @@
|
|||||||
|
export 'flipper_jointing_behavior.dart';
|
||||||
|
export 'flipper_key_listening_behavior.dart';
|
@ -0,0 +1,103 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// Joints the [Flipper] to allow pivoting around one end.
|
||||||
|
class FlipperJointingBehavior extends Component
|
||||||
|
with ParentIsA<Flipper>, HasGameRef {
|
||||||
|
late final RevoluteJoint _joint;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
|
||||||
|
final anchor = _FlipperAnchor(flipper: parent);
|
||||||
|
await add(anchor);
|
||||||
|
|
||||||
|
final jointDef = _FlipperAnchorRevoluteJointDef(
|
||||||
|
flipper: parent,
|
||||||
|
anchor: anchor,
|
||||||
|
);
|
||||||
|
_joint = _FlipperJoint(jointDef);
|
||||||
|
parent.world.createJoint(_joint);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onMount() {
|
||||||
|
gameRef.ready().whenComplete(
|
||||||
|
() => parent.body.joints.whereType<_FlipperJoint>().first.unlock(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template flipper_anchor}
|
||||||
|
/// [JointAnchor] positioned at the end of a [Flipper].
|
||||||
|
///
|
||||||
|
/// The end of a [Flipper] depends on its [Flipper.side].
|
||||||
|
/// {@endtemplate}
|
||||||
|
class _FlipperAnchor extends JointAnchor {
|
||||||
|
/// {@macro flipper_anchor}
|
||||||
|
_FlipperAnchor({
|
||||||
|
required Flipper flipper,
|
||||||
|
}) {
|
||||||
|
initialPosition = Vector2(
|
||||||
|
(Flipper.size.x * flipper.side.direction) / 2 -
|
||||||
|
(1.65 * flipper.side.direction),
|
||||||
|
-0.15,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template flipper_anchor_revolute_joint_def}
|
||||||
|
/// Hinges one end of [Flipper] to a [_FlipperAnchor] to achieve a potivoting
|
||||||
|
/// motion.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class _FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
|
||||||
|
/// {@macro flipper_anchor_revolute_joint_def}
|
||||||
|
_FlipperAnchorRevoluteJointDef({
|
||||||
|
required Flipper flipper,
|
||||||
|
required _FlipperAnchor anchor,
|
||||||
|
}) : side = flipper.side {
|
||||||
|
enableLimit = true;
|
||||||
|
initialize(
|
||||||
|
flipper.body,
|
||||||
|
anchor.body,
|
||||||
|
flipper.body.position + anchor.body.position,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final BoardSide side;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template flipper_joint}
|
||||||
|
/// [RevoluteJoint] that controls the pivoting motion of a [Flipper].
|
||||||
|
/// {@endtemplate}
|
||||||
|
class _FlipperJoint extends RevoluteJoint {
|
||||||
|
/// {@macro flipper_joint}
|
||||||
|
_FlipperJoint(_FlipperAnchorRevoluteJointDef def)
|
||||||
|
: side = def.side,
|
||||||
|
super(def) {
|
||||||
|
lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Half the angle of the arc motion.
|
||||||
|
static const _halfSweepingAngle = 0.611;
|
||||||
|
|
||||||
|
final BoardSide side;
|
||||||
|
|
||||||
|
/// Locks the [Flipper] to its resting position.
|
||||||
|
///
|
||||||
|
/// The joint is locked when initialized in order to force the [Flipper]
|
||||||
|
/// at its resting position.
|
||||||
|
void lock() {
|
||||||
|
final angle = _halfSweepingAngle * side.direction;
|
||||||
|
setLimits(angle, angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unlocks the [Flipper] from its resting position.
|
||||||
|
void unlock() {
|
||||||
|
const angle = _halfSweepingAngle;
|
||||||
|
setLimits(-angle, angle);
|
||||||
|
}
|
||||||
|
}
|
@ -1,49 +1,33 @@
|
|||||||
import 'package:flame/components.dart';
|
import 'package:flame/components.dart';
|
||||||
import 'package:flame_bloc/flame_bloc.dart';
|
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
import 'package:pinball_flame/pinball_flame.dart';
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
/// {@template controlled_flipper}
|
/// Allows controlling the [Flipper]'s movement with keyboard input.
|
||||||
/// A [Flipper] with a [FlipperController] attached.
|
class FlipperKeyListeningBehavior extends Component
|
||||||
/// {@endtemplate}
|
with KeyboardHandler, ParentIsA<Flipper> {
|
||||||
class ControlledFlipper extends Flipper with Controls<FlipperController> {
|
|
||||||
/// {@macro controlled_flipper}
|
|
||||||
ControlledFlipper({
|
|
||||||
required BoardSide side,
|
|
||||||
}) : super(side: side) {
|
|
||||||
controller = FlipperController(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// {@template flipper_controller}
|
|
||||||
/// A [ComponentController] that controls a [Flipper]s movement.
|
|
||||||
/// {@endtemplate}
|
|
||||||
class FlipperController extends ComponentController<Flipper>
|
|
||||||
with KeyboardHandler, BlocComponent<GameBloc, GameState> {
|
|
||||||
/// {@macro flipper_controller}
|
|
||||||
FlipperController(Flipper flipper)
|
|
||||||
: _keys = flipper.side.flipperKeys,
|
|
||||||
super(flipper);
|
|
||||||
|
|
||||||
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
||||||
///
|
///
|
||||||
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
||||||
final List<LogicalKeyboardKey> _keys;
|
late final List<LogicalKeyboardKey> _keys;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
_keys = parent.side.flipperKeys;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool onKeyEvent(
|
bool onKeyEvent(
|
||||||
RawKeyEvent event,
|
RawKeyEvent event,
|
||||||
Set<LogicalKeyboardKey> keysPressed,
|
Set<LogicalKeyboardKey> keysPressed,
|
||||||
) {
|
) {
|
||||||
if (state?.isGameOver ?? false) return true;
|
|
||||||
if (!_keys.contains(event.logicalKey)) return true;
|
if (!_keys.contains(event.logicalKey)) return true;
|
||||||
|
|
||||||
if (event is RawKeyDownEvent) {
|
if (event is RawKeyDownEvent) {
|
||||||
component.moveUp();
|
parent.moveUp();
|
||||||
} else if (event is RawKeyUpEvent) {
|
} else if (event is RawKeyUpEvent) {
|
||||||
component.moveDown();
|
parent.moveDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
@ -0,0 +1 @@
|
|||||||
|
|
Loading…
Reference in new issue