Enable option to turn on mouselook
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -25,3 +25,16 @@ abstract class Wolf3dInput {
|
||||
requestedWeapon: requestedWeapon,
|
||||
);
|
||||
}
|
||||
|
||||
enum WolfInputAction {
|
||||
forward,
|
||||
backward,
|
||||
turnLeft,
|
||||
turnRight,
|
||||
fire,
|
||||
interact,
|
||||
weapon1,
|
||||
weapon2,
|
||||
weapon3,
|
||||
weapon4,
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:wolf_3d_dart/wolf_3d_data.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_synth.dart';
|
||||
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
|
||||
|
||||
class Wolf3d {
|
||||
Wolf3d();
|
||||
@@ -14,6 +15,7 @@ class Wolf3d {
|
||||
|
||||
// --- Core Systems ---
|
||||
final EngineAudio audio = WolfAudio();
|
||||
final Wolf3dFlutterInput input = Wolf3dFlutterInput();
|
||||
|
||||
// --- Getters ---
|
||||
WolfensteinData get activeGame {
|
||||
|
||||
@@ -1,38 +1,146 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_entities.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_input.dart';
|
||||
|
||||
class Wolf3dFlutterInput extends Wolf3dInput {
|
||||
// 1. Customizable Key Bindings (Multiple keys per action)
|
||||
Map<WolfInputAction, Set<LogicalKeyboardKey>> bindings = {
|
||||
WolfInputAction.forward: {
|
||||
LogicalKeyboardKey.keyW,
|
||||
LogicalKeyboardKey.arrowUp,
|
||||
},
|
||||
WolfInputAction.backward: {
|
||||
LogicalKeyboardKey.keyS,
|
||||
LogicalKeyboardKey.arrowDown,
|
||||
},
|
||||
WolfInputAction.turnLeft: {
|
||||
LogicalKeyboardKey.keyA,
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
},
|
||||
WolfInputAction.turnRight: {
|
||||
LogicalKeyboardKey.keyD,
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
},
|
||||
WolfInputAction.fire: {
|
||||
LogicalKeyboardKey.controlLeft,
|
||||
LogicalKeyboardKey.controlRight,
|
||||
},
|
||||
WolfInputAction.interact: {LogicalKeyboardKey.space},
|
||||
WolfInputAction.weapon1: {LogicalKeyboardKey.digit1},
|
||||
WolfInputAction.weapon2: {LogicalKeyboardKey.digit2},
|
||||
WolfInputAction.weapon3: {LogicalKeyboardKey.digit3},
|
||||
WolfInputAction.weapon4: {LogicalKeyboardKey.digit4},
|
||||
};
|
||||
|
||||
// 2. Mouse State Variables
|
||||
bool isMouseLeftDown = false;
|
||||
bool isMouseRightDown = false;
|
||||
double _mouseDeltaX = 0.0;
|
||||
double _mouseDeltaY = 0.0;
|
||||
bool _previousMouseRightDown = false;
|
||||
|
||||
// 3. Mouselook Toggle
|
||||
bool _isMouseLookEnabled = false;
|
||||
|
||||
bool get mouseLookEnabled => _isMouseLookEnabled;
|
||||
|
||||
set mouseLookEnabled(bool value) {
|
||||
_isMouseLookEnabled = value;
|
||||
// Clear any built-up delta when turning it off so it doesn't
|
||||
// suddenly jump if they turn it back on.
|
||||
if (!value) {
|
||||
_mouseDeltaX = 0.0;
|
||||
_mouseDeltaY = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
Set<LogicalKeyboardKey> _previousKeys = {};
|
||||
|
||||
// --- Customization Helpers ---
|
||||
void bindKey(WolfInputAction action, LogicalKeyboardKey key) =>
|
||||
bindings[action]?.add(key);
|
||||
void unbindKey(WolfInputAction action, LogicalKeyboardKey key) =>
|
||||
bindings[action]?.remove(key);
|
||||
|
||||
// --- Mouse Event Handlers ---
|
||||
void onPointerDown(PointerDownEvent event) {
|
||||
if (event.buttons & kPrimaryMouseButton != 0) isMouseLeftDown = true;
|
||||
if (event.buttons & kSecondaryMouseButton != 0) isMouseRightDown = true;
|
||||
}
|
||||
|
||||
void onPointerUp(PointerUpEvent event) {
|
||||
if (event.buttons & kPrimaryMouseButton == 0) isMouseLeftDown = false;
|
||||
if (event.buttons & kSecondaryMouseButton == 0) isMouseRightDown = false;
|
||||
}
|
||||
|
||||
void onPointerMove(PointerEvent event) {
|
||||
// Only capture movement if mouselook is actually enabled
|
||||
if (_isMouseLookEnabled) {
|
||||
_mouseDeltaX += event.delta.dx;
|
||||
_mouseDeltaY += event.delta.dy;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Input Helpers ---
|
||||
bool _isActive(WolfInputAction action, Set<LogicalKeyboardKey> pressedKeys) {
|
||||
return bindings[action]!.any((key) => pressedKeys.contains(key));
|
||||
}
|
||||
|
||||
bool _isNewlyPressed(
|
||||
WolfInputAction action,
|
||||
Set<LogicalKeyboardKey> newlyPressed,
|
||||
) {
|
||||
return bindings[action]!.any((key) => newlyPressed.contains(key));
|
||||
}
|
||||
|
||||
@override
|
||||
void update() {
|
||||
final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
|
||||
final newlyPressedKeys = pressedKeys.difference(_previousKeys);
|
||||
|
||||
isMovingForward = pressedKeys.contains(LogicalKeyboardKey.keyW);
|
||||
isMovingBackward = pressedKeys.contains(LogicalKeyboardKey.keyS);
|
||||
isTurningLeft = pressedKeys.contains(LogicalKeyboardKey.keyA);
|
||||
isTurningRight = pressedKeys.contains(LogicalKeyboardKey.keyD);
|
||||
// Evaluate keyboard first
|
||||
bool kbForward = _isActive(WolfInputAction.forward, pressedKeys);
|
||||
bool kbBackward = _isActive(WolfInputAction.backward, pressedKeys);
|
||||
bool kbLeft = _isActive(WolfInputAction.turnLeft, pressedKeys);
|
||||
bool kbRight = _isActive(WolfInputAction.turnRight, pressedKeys);
|
||||
|
||||
isInteracting = newlyPressedKeys.contains(LogicalKeyboardKey.space);
|
||||
// Add mouse delta if mouselook is enabled
|
||||
isMovingForward = kbForward || (_isMouseLookEnabled && _mouseDeltaY < -1.5);
|
||||
isMovingBackward =
|
||||
kbBackward || (_isMouseLookEnabled && _mouseDeltaY > 1.5);
|
||||
isTurningLeft = kbLeft || (_isMouseLookEnabled && _mouseDeltaX < -1.5);
|
||||
isTurningRight = kbRight || (_isMouseLookEnabled && _mouseDeltaX > 1.5);
|
||||
|
||||
// Reset mouse deltas after consumption for digital engine movement
|
||||
_mouseDeltaX = 0.0;
|
||||
_mouseDeltaY = 0.0;
|
||||
|
||||
// Right click or Spacebar to interact
|
||||
isInteracting =
|
||||
_isNewlyPressed(WolfInputAction.interact, newlyPressedKeys) ||
|
||||
(mouseLookEnabled && isMouseRightDown && !_previousMouseRightDown);
|
||||
|
||||
// Left click or Ctrl to fire
|
||||
isFiring =
|
||||
pressedKeys.contains(LogicalKeyboardKey.controlLeft) &&
|
||||
!pressedKeys.contains(LogicalKeyboardKey.space);
|
||||
_isActive(WolfInputAction.fire, pressedKeys) ||
|
||||
(mouseLookEnabled && isMouseLeftDown);
|
||||
|
||||
requestedWeapon = null;
|
||||
for (final LogicalKeyboardKey key in newlyPressedKeys) {
|
||||
if (key == LogicalKeyboardKey.digit1) requestedWeapon = WeaponType.knife;
|
||||
if (key == LogicalKeyboardKey.digit2) requestedWeapon = WeaponType.pistol;
|
||||
if (key == LogicalKeyboardKey.digit3) {
|
||||
requestedWeapon = WeaponType.machineGun;
|
||||
}
|
||||
if (key == LogicalKeyboardKey.digit4) {
|
||||
requestedWeapon = WeaponType.chainGun;
|
||||
}
|
||||
if (_isNewlyPressed(WolfInputAction.weapon1, newlyPressedKeys)) {
|
||||
requestedWeapon = WeaponType.knife;
|
||||
}
|
||||
if (_isNewlyPressed(WolfInputAction.weapon2, newlyPressedKeys)) {
|
||||
requestedWeapon = WeaponType.pistol;
|
||||
}
|
||||
if (_isNewlyPressed(WolfInputAction.weapon3, newlyPressedKeys)) {
|
||||
requestedWeapon = WeaponType.machineGun;
|
||||
}
|
||||
if (_isNewlyPressed(WolfInputAction.weapon4, newlyPressedKeys)) {
|
||||
requestedWeapon = WeaponType.chainGun;
|
||||
}
|
||||
|
||||
_previousKeys = Set.from(pressedKeys);
|
||||
_previousMouseRightDown = isMouseRightDown;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user