87 lines
No EOL
2.3 KiB
Dart
87 lines
No EOL
2.3 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
/// Manages input from both keyboard and touch controls
|
|
class InputManager {
|
|
// Movement state
|
|
Vector2 _movementInput = Vector2.zero();
|
|
|
|
// Action states
|
|
bool _poopBagPressed = false;
|
|
bool _doorbellPressed = false;
|
|
|
|
/// Set movement input from touch controls (-1 to 1 range)
|
|
void setTouchMovement(Vector2 input) {
|
|
_movementInput = input;
|
|
}
|
|
|
|
/// Set movement input from keyboard
|
|
void setKeyboardMovement(Set<LogicalKeyboardKey> keysPressed) {
|
|
Vector2 keyboardInput = Vector2.zero();
|
|
|
|
if (keysPressed.contains(LogicalKeyboardKey.arrowUp) ||
|
|
keysPressed.contains(LogicalKeyboardKey.keyW)) {
|
|
keyboardInput.y -= 1;
|
|
}
|
|
if (keysPressed.contains(LogicalKeyboardKey.arrowDown) ||
|
|
keysPressed.contains(LogicalKeyboardKey.keyS)) {
|
|
keyboardInput.y += 1;
|
|
}
|
|
if (keysPressed.contains(LogicalKeyboardKey.arrowLeft) ||
|
|
keysPressed.contains(LogicalKeyboardKey.keyA)) {
|
|
keyboardInput.x -= 1;
|
|
}
|
|
if (keysPressed.contains(LogicalKeyboardKey.arrowRight) ||
|
|
keysPressed.contains(LogicalKeyboardKey.keyD)) {
|
|
keyboardInput.x += 1;
|
|
}
|
|
|
|
_movementInput = keyboardInput;
|
|
}
|
|
|
|
/// Trigger poop bag action from touch
|
|
void setTouchPoopBag(bool pressed) {
|
|
if (pressed && !_poopBagPressed) {
|
|
_poopBagPressed = true;
|
|
} else if (!pressed) {
|
|
_poopBagPressed = false;
|
|
}
|
|
}
|
|
|
|
/// Trigger doorbell action from touch
|
|
void setTouchDoorbell(bool pressed) {
|
|
if (pressed && !_doorbellPressed) {
|
|
_doorbellPressed = true;
|
|
} else if (!pressed) {
|
|
_doorbellPressed = false;
|
|
}
|
|
}
|
|
|
|
/// Check if poop bag action was triggered this frame
|
|
bool consumePoopBagAction() {
|
|
if (_poopBagPressed) {
|
|
_poopBagPressed = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// Check if doorbell action was triggered this frame
|
|
bool consumeDoorbellAction() {
|
|
if (_doorbellPressed) {
|
|
_doorbellPressed = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// Get current movement input
|
|
Vector2 get movementInput => _movementInput;
|
|
|
|
/// Reset all input states
|
|
void reset() {
|
|
_movementInput = Vector2.zero();
|
|
_poopBagPressed = false;
|
|
_doorbellPressed = false;
|
|
}
|
|
} |