WIP moving difficulty selection to engine

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-18 12:25:59 +01:00
parent 0f7c77e85a
commit e39dfd5da0
29 changed files with 1337 additions and 3360 deletions

View File

@@ -21,6 +21,12 @@ class Wolf3d {
/// Shared engine audio backend used by menus and gameplay sessions.
final EngineAudio audio = WolfAudio();
/// Engine menu background color as 24-bit RGB.
int menuBackgroundRgb = 0x890000;
/// Engine menu panel color as 24-bit RGB.
int menuPanelRgb = 0x590002;
/// Shared Flutter input adapter reused by gameplay screens.
final Wolf3dFlutterInput input = Wolf3dFlutterInput();
@@ -41,6 +47,54 @@ class Wolf3d {
/// Index of the episode currently selected in the UI flow.
int get activeEpisode => _activeEpisode;
Difficulty? _activeDifficulty;
/// The difficulty applied when [launchEngine] creates a new session.
Difficulty? get activeDifficulty => _activeDifficulty;
/// Stores [difficulty] so the next [launchEngine] call uses it.
void setActiveDifficulty(Difficulty difficulty) {
_activeDifficulty = difficulty;
}
/// Clears any previously selected difficulty so the engine can prompt for one.
void clearActiveDifficulty() {
_activeDifficulty = null;
}
WolfEngine? _engine;
/// The most recently launched engine.
///
/// Throws a [StateError] until [launchEngine] has been called.
WolfEngine get engine {
if (_engine == null) {
throw StateError('No engine launched. Call launchEngine() first.');
}
return _engine!;
}
/// Creates and initializes a [WolfEngine] for the current session config.
///
/// Uses [activeGame], [activeEpisode], and [activeDifficulty]. Stores the
/// engine so it can be retrieved via [engine]. [onGameWon] is invoked when
/// the player completes the final level of the episode.
WolfEngine launchEngine({required void Function() onGameWon}) {
_engine = WolfEngine(
data: activeGame,
difficulty: _activeDifficulty,
startingEpisode: _activeEpisode,
frameBuffer: FrameBuffer(320, 200),
menuBackgroundRgb: menuBackgroundRgb,
menuPanelRgb: menuPanelRgb,
audio: audio,
input: input,
onGameWon: onGameWon,
);
_engine!.init();
return _engine!;
}
/// Sets the active episode for the current [activeGame].
void setActiveEpisode(int episodeIndex) {
if (_activeGame == null) {

View File

@@ -37,7 +37,12 @@ class Wolf3dFlutterInput extends Wolf3dInput {
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.controlRight,
},
WolfInputAction.interact: {LogicalKeyboardKey.space},
WolfInputAction.interact: {
LogicalKeyboardKey.space,
LogicalKeyboardKey.enter,
LogicalKeyboardKey.numpadEnter,
},
WolfInputAction.back: {LogicalKeyboardKey.escape},
WolfInputAction.weapon1: {LogicalKeyboardKey.digit1},
WolfInputAction.weapon2: {LogicalKeyboardKey.digit2},
WolfInputAction.weapon3: {LogicalKeyboardKey.digit3},
@@ -52,6 +57,9 @@ class Wolf3dFlutterInput extends Wolf3dInput {
double _mouseDeltaX = 0.0;
double _mouseDeltaY = 0.0;
bool _previousMouseRightDown = false;
bool _queuedBack = false;
double? _queuedMenuTapX;
double? _queuedMenuTapY;
// Mouse-look is optional so touch or keyboard-only hosts can keep the same
// adapter without incurring accidental pointer-driven movement.
@@ -105,6 +113,17 @@ class Wolf3dFlutterInput extends Wolf3dInput {
}
}
/// Queues a one-frame back action, typically from system back gestures.
void queueBackAction() {
_queuedBack = true;
}
/// Queues a one-frame menu tap with normalized coordinates [0..1].
void queueMenuTap({required double x, required double y}) {
_queuedMenuTapX = x.clamp(0.0, 1.0);
_queuedMenuTapY = y.clamp(0.0, 1.0);
}
/// Returns whether any bound key for [action] is currently pressed.
bool _isActive(WolfInputAction action, Set<LogicalKeyboardKey> pressedKeys) {
return bindings[action]!.any((key) => pressedKeys.contains(key));
@@ -146,6 +165,11 @@ class Wolf3dFlutterInput extends Wolf3dInput {
_isNewlyPressed(WolfInputAction.interact, newlyPressedKeys) ||
(mouseLookEnabled && isMouseRightDown && !_previousMouseRightDown);
isBack =
_isNewlyPressed(WolfInputAction.back, newlyPressedKeys) || _queuedBack;
menuTapX = _queuedMenuTapX;
menuTapY = _queuedMenuTapY;
// Left click or Ctrl to fire
isFiring =
_isActive(WolfInputAction.fire, pressedKeys) ||
@@ -169,5 +193,8 @@ class Wolf3dFlutterInput extends Wolf3dInput {
// weapon switching only fire once per physical key press.
_previousKeys = Set.from(pressedKeys);
_previousMouseRightDown = isMouseRightDown;
_queuedBack = false;
_queuedMenuTapX = null;
_queuedMenuTapY = null;
}
}