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

@@ -3,36 +3,19 @@ library;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wolf_3d_dart/wolf_3d_data_types.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_flutter.dart';
import 'package:wolf_3d_renderer/wolf_3d_ascii_renderer.dart';
import 'package:wolf_3d_renderer/wolf_3d_flutter_renderer.dart';
/// Owns a [WolfEngine] instance and exposes renderer/input integrations to Flutter.
/// Launches a [WolfEngine] via [Wolf3d] and exposes renderer/input integrations.
class GameScreen extends StatefulWidget {
/// Fully parsed game data for the selected version.
final WolfensteinData data;
/// Shared application facade owning the engine, audio, and input.
final Wolf3d wolf3d;
/// Difficulty applied when creating the engine session.
final Difficulty difficulty;
/// Episode index used as the starting world.
final int startingEpisode;
/// Shared audio backend reused across menu and gameplay screens.
final EngineAudio audio;
/// Flutter input adapter that translates widget events into engine input.
final Wolf3dFlutterInput input;
/// Creates a gameplay screen with the supplied game session configuration.
/// Creates a gameplay screen driven by [wolf3d].
const GameScreen({
required this.data,
required this.difficulty,
required this.startingEpisode,
required this.audio,
required this.input,
required this.wolf3d,
super.key,
});
@@ -47,90 +30,147 @@ class _GameScreenState extends State<GameScreen> {
@override
void initState() {
super.initState();
_engine = WolfEngine(
data: widget.data,
difficulty: widget.difficulty,
startingEpisode: widget.startingEpisode,
frameBuffer: FrameBuffer(320, 200),
audio: widget.audio,
input: widget.input,
_engine = widget.wolf3d.launchEngine(
onGameWon: () => Navigator.of(context).pop(),
);
_engine.init();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Listener(
onPointerDown: widget.input.onPointerDown,
onPointerUp: widget.input.onPointerUp,
onPointerMove: widget.input.onPointerMove,
onPointerHover: widget.input.onPointerMove,
child: Stack(
children: [
// Keep both renderers behind the same engine so mode switching does
// not reset level state or audio playback.
_useAsciiMode
? WolfAsciiRenderer(engine: _engine)
: WolfFlutterRenderer(engine: _engine),
return WillPopScope(
onWillPop: () async {
if (_engine.isDifficultySelectionPending) {
widget.wolf3d.input.queueBackAction();
return false;
}
return true;
},
child: Scaffold(
body: LayoutBuilder(
builder: (context, constraints) {
final viewportRect = _menuViewportRect(
Size(constraints.maxWidth, constraints.maxHeight),
);
if (!_engine.isInitialized)
Container(
color: Colors.black,
child: const Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(color: Colors.teal),
SizedBox(height: 20),
Text(
"GET PSYCHED!",
style: TextStyle(
color: Colors.teal,
fontFamily: 'monospace',
return Listener(
onPointerDown: (event) {
widget.wolf3d.input.onPointerDown(event);
if (_engine.isDifficultySelectionPending &&
viewportRect.width > 0 &&
viewportRect.height > 0 &&
viewportRect.contains(event.localPosition)) {
final normalizedX =
(event.localPosition.dx - viewportRect.left) /
viewportRect.width;
final normalizedY =
(event.localPosition.dy - viewportRect.top) /
viewportRect.height;
widget.wolf3d.input.queueMenuTap(
x: normalizedX,
y: normalizedY,
);
}
},
onPointerUp: widget.wolf3d.input.onPointerUp,
onPointerMove: widget.wolf3d.input.onPointerMove,
onPointerHover: widget.wolf3d.input.onPointerMove,
child: Stack(
children: [
// Keep both renderers behind the same engine so mode switching does
// not reset level state or audio playback.
_useAsciiMode
? WolfAsciiRenderer(engine: _engine)
: WolfFlutterRenderer(engine: _engine),
if (!_engine.isInitialized)
Container(
color: Colors.black,
child: const Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(color: Colors.teal),
SizedBox(height: 20),
Text(
"GET PSYCHED!",
style: TextStyle(
color: Colors.teal,
fontFamily: 'monospace',
),
),
],
),
),
],
),
// Tab toggles the renderer implementation for quick visual debugging.
Focus(
autofocus: true,
onKeyEvent: (node, event) {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.tab) {
setState(() => _useAsciiMode = !_useAsciiMode);
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
child: const SizedBox.shrink(),
),
),
),
// Tab toggles the renderer implementation for quick visual debugging.
Focus(
autofocus: true,
onKeyEvent: (node, event) {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.tab) {
setState(() => _useAsciiMode = !_useAsciiMode);
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
child: const SizedBox.shrink(),
),
// A second full-screen overlay keeps the presentation simple while
// the engine is still warming up or decoding the first frame.
if (!_engine.isInitialized)
Container(
color: Colors.black,
child: const Center(
child: CircularProgressIndicator(color: Colors.teal),
),
),
// A second full-screen overlay keeps the presentation simple while
// the engine is still warming up or decoding the first frame.
if (!_engine.isInitialized)
Container(
color: Colors.black,
child: const Center(
child: CircularProgressIndicator(color: Colors.teal),
),
Positioned(
top: 16,
right: 16,
child: Text(
'TAB: Swap Renderer',
style: TextStyle(
color: Colors.white.withValues(alpha: 0.5),
),
),
),
],
),
Positioned(
top: 16,
right: 16,
child: Text(
'TAB: Swap Renderer',
style: TextStyle(color: Colors.white.withValues(alpha: 0.5)),
),
),
],
);
},
),
),
);
}
Rect _menuViewportRect(Size availableSize) {
if (availableSize.width <= 0 || availableSize.height <= 0) {
return Rect.zero;
}
const double aspect = 4 / 3;
final double outerPadding = _useAsciiMode ? 0.0 : 16.0;
final double maxWidth = (availableSize.width - (outerPadding * 2)).clamp(
1.0,
double.infinity,
);
final double maxHeight = (availableSize.height - (outerPadding * 2)).clamp(
1.0,
double.infinity,
);
double viewportWidth = maxWidth;
double viewportHeight = viewportWidth / aspect;
if (viewportHeight > maxHeight) {
viewportHeight = maxHeight;
viewportWidth = viewportHeight * aspect;
}
final double left = (availableSize.width - viewportWidth) / 2;
final double top = (availableSize.height - viewportHeight) / 2;
return Rect.fromLTWH(left, top, viewportWidth, viewportHeight);
}
}