mirror of https://github.com/flutter/pinball.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
import 'package:flame/components.dart';
|
|
import 'package:flame_bloc/flame_bloc.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:pinball/game/game.dart';
|
|
import 'package:pinball_components/pinball_components.dart';
|
|
import 'package:pinball_flame/pinball_flame.dart';
|
|
|
|
/// {@template controlled_plunger}
|
|
/// A [Plunger] with a [PlungerController] attached.
|
|
/// {@endtemplate}
|
|
class ControlledPlunger extends Plunger with Controls<PlungerController> {
|
|
/// {@macro controlled_plunger}
|
|
ControlledPlunger({required double compressionDistance})
|
|
: super(compressionDistance: compressionDistance) {
|
|
controller = PlungerController(this);
|
|
}
|
|
}
|
|
|
|
/// {@template plunger_controller}
|
|
/// A [ComponentController] that controls a [Plunger]s movement.
|
|
/// {@endtemplate}
|
|
class PlungerController extends ComponentController<Plunger>
|
|
with KeyboardHandler, BlocComponent<GameBloc, GameState> {
|
|
/// {@macro plunger_controller}
|
|
PlungerController(Plunger plunger) : super(plunger);
|
|
|
|
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
|
///
|
|
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
|
static const List<LogicalKeyboardKey> _keys = [
|
|
LogicalKeyboardKey.arrowDown,
|
|
LogicalKeyboardKey.space,
|
|
LogicalKeyboardKey.keyS,
|
|
];
|
|
|
|
@override
|
|
bool onKeyEvent(
|
|
RawKeyEvent event,
|
|
Set<LogicalKeyboardKey> keysPressed,
|
|
) {
|
|
if (state?.isGameOver ?? false) return true;
|
|
if (!_keys.contains(event.logicalKey)) return true;
|
|
|
|
if (event is RawKeyDownEvent) {
|
|
component.pull();
|
|
} else if (event is RawKeyUpEvent) {
|
|
component.release();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|