Enable option to turn on mouselook
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -35,6 +35,7 @@ class _DifficultyScreenState extends State<DifficultyScreen> {
|
|||||||
difficulty: difficulty,
|
difficulty: difficulty,
|
||||||
startingEpisode: widget.wolf3d.activeEpisode,
|
startingEpisode: widget.wolf3d.activeEpisode,
|
||||||
audio: widget.wolf3d.audio,
|
audio: widget.wolf3d.audio,
|
||||||
|
input: widget.wolf3d.input,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import 'package:flutter/services.dart';
|
|||||||
import 'package:wolf_3d_dart/wolf_3d_data_types.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_engine.dart';
|
||||||
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
|
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
|
||||||
import 'package:wolf_3d_dart/wolf_3d_input.dart';
|
|
||||||
import 'package:wolf_3d_renderer/wolf_3d_ascii_renderer.dart';
|
import 'package:wolf_3d_renderer/wolf_3d_ascii_renderer.dart';
|
||||||
import 'package:wolf_3d_renderer/wolf_3d_flutter_renderer.dart';
|
import 'package:wolf_3d_renderer/wolf_3d_flutter_renderer.dart';
|
||||||
|
|
||||||
@@ -12,12 +11,14 @@ class GameScreen extends StatefulWidget {
|
|||||||
final Difficulty difficulty;
|
final Difficulty difficulty;
|
||||||
final int startingEpisode;
|
final int startingEpisode;
|
||||||
final EngineAudio audio;
|
final EngineAudio audio;
|
||||||
|
final Wolf3dFlutterInput input;
|
||||||
|
|
||||||
const GameScreen({
|
const GameScreen({
|
||||||
required this.data,
|
required this.data,
|
||||||
required this.difficulty,
|
required this.difficulty,
|
||||||
required this.startingEpisode,
|
required this.startingEpisode,
|
||||||
required this.audio,
|
required this.audio,
|
||||||
|
required this.input,
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -27,19 +28,17 @@ class GameScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class _GameScreenState extends State<GameScreen> {
|
class _GameScreenState extends State<GameScreen> {
|
||||||
late final WolfEngine _engine;
|
late final WolfEngine _engine;
|
||||||
late final Wolf3dInput _input;
|
|
||||||
bool _useAsciiMode = false;
|
bool _useAsciiMode = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_input = Wolf3dFlutterInput();
|
|
||||||
_engine = WolfEngine(
|
_engine = WolfEngine(
|
||||||
data: widget.data,
|
data: widget.data,
|
||||||
difficulty: widget.difficulty,
|
difficulty: widget.difficulty,
|
||||||
startingEpisode: widget.startingEpisode,
|
startingEpisode: widget.startingEpisode,
|
||||||
audio: widget.audio,
|
audio: widget.audio,
|
||||||
input: _input,
|
input: widget.input,
|
||||||
onGameWon: () => Navigator.of(context).pop(),
|
onGameWon: () => Navigator.of(context).pop(),
|
||||||
);
|
);
|
||||||
_engine.init();
|
_engine.init();
|
||||||
@@ -48,7 +47,12 @@ class _GameScreenState extends State<GameScreen> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: Stack(
|
body: Listener(
|
||||||
|
onPointerDown: widget.input.onPointerDown,
|
||||||
|
onPointerUp: widget.input.onPointerUp,
|
||||||
|
onPointerMove: widget.input.onPointerMove,
|
||||||
|
onPointerHover: widget.input.onPointerMove,
|
||||||
|
child: Stack(
|
||||||
children: [
|
children: [
|
||||||
_useAsciiMode
|
_useAsciiMode
|
||||||
? WolfAsciiRenderer(engine: _engine)
|
? WolfAsciiRenderer(engine: _engine)
|
||||||
@@ -108,6 +112,7 @@ class _GameScreenState extends State<GameScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,3 +25,16 @@ abstract class Wolf3dInput {
|
|||||||
requestedWeapon: requestedWeapon,
|
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_data_types.dart';
|
||||||
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
|
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
|
||||||
import 'package:wolf_3d_dart/wolf_3d_synth.dart';
|
import 'package:wolf_3d_dart/wolf_3d_synth.dart';
|
||||||
|
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
|
||||||
|
|
||||||
class Wolf3d {
|
class Wolf3d {
|
||||||
Wolf3d();
|
Wolf3d();
|
||||||
@@ -14,6 +15,7 @@ class Wolf3d {
|
|||||||
|
|
||||||
// --- Core Systems ---
|
// --- Core Systems ---
|
||||||
final EngineAudio audio = WolfAudio();
|
final EngineAudio audio = WolfAudio();
|
||||||
|
final Wolf3dFlutterInput input = Wolf3dFlutterInput();
|
||||||
|
|
||||||
// --- Getters ---
|
// --- Getters ---
|
||||||
WolfensteinData get activeGame {
|
WolfensteinData get activeGame {
|
||||||
|
|||||||
@@ -1,38 +1,146 @@
|
|||||||
|
import 'package:flutter/gestures.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:wolf_3d_dart/wolf_3d_entities.dart';
|
import 'package:wolf_3d_dart/wolf_3d_entities.dart';
|
||||||
import 'package:wolf_3d_dart/wolf_3d_input.dart';
|
import 'package:wolf_3d_dart/wolf_3d_input.dart';
|
||||||
|
|
||||||
class Wolf3dFlutterInput extends Wolf3dInput {
|
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 = {};
|
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
|
@override
|
||||||
void update() {
|
void update() {
|
||||||
final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
|
final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
|
||||||
final newlyPressedKeys = pressedKeys.difference(_previousKeys);
|
final newlyPressedKeys = pressedKeys.difference(_previousKeys);
|
||||||
|
|
||||||
isMovingForward = pressedKeys.contains(LogicalKeyboardKey.keyW);
|
// Evaluate keyboard first
|
||||||
isMovingBackward = pressedKeys.contains(LogicalKeyboardKey.keyS);
|
bool kbForward = _isActive(WolfInputAction.forward, pressedKeys);
|
||||||
isTurningLeft = pressedKeys.contains(LogicalKeyboardKey.keyA);
|
bool kbBackward = _isActive(WolfInputAction.backward, pressedKeys);
|
||||||
isTurningRight = pressedKeys.contains(LogicalKeyboardKey.keyD);
|
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 =
|
isFiring =
|
||||||
pressedKeys.contains(LogicalKeyboardKey.controlLeft) &&
|
_isActive(WolfInputAction.fire, pressedKeys) ||
|
||||||
!pressedKeys.contains(LogicalKeyboardKey.space);
|
(mouseLookEnabled && isMouseLeftDown);
|
||||||
|
|
||||||
requestedWeapon = null;
|
requestedWeapon = null;
|
||||||
for (final LogicalKeyboardKey key in newlyPressedKeys) {
|
if (_isNewlyPressed(WolfInputAction.weapon1, newlyPressedKeys)) {
|
||||||
if (key == LogicalKeyboardKey.digit1) requestedWeapon = WeaponType.knife;
|
requestedWeapon = WeaponType.knife;
|
||||||
if (key == LogicalKeyboardKey.digit2) requestedWeapon = WeaponType.pistol;
|
}
|
||||||
if (key == LogicalKeyboardKey.digit3) {
|
if (_isNewlyPressed(WolfInputAction.weapon2, newlyPressedKeys)) {
|
||||||
|
requestedWeapon = WeaponType.pistol;
|
||||||
|
}
|
||||||
|
if (_isNewlyPressed(WolfInputAction.weapon3, newlyPressedKeys)) {
|
||||||
requestedWeapon = WeaponType.machineGun;
|
requestedWeapon = WeaponType.machineGun;
|
||||||
}
|
}
|
||||||
if (key == LogicalKeyboardKey.digit4) {
|
if (_isNewlyPressed(WolfInputAction.weapon4, newlyPressedKeys)) {
|
||||||
requestedWeapon = WeaponType.chainGun;
|
requestedWeapon = WeaponType.chainGun;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_previousKeys = Set.from(pressedKeys);
|
_previousKeys = Set.from(pressedKeys);
|
||||||
|
_previousMouseRightDown = isMouseRightDown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user