176 lines
6.0 KiB
Dart
176 lines
6.0 KiB
Dart
/// Active gameplay screen for the Flutter host.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:wolf_3d_dart/wolf_3d_engine.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';
|
|
|
|
/// Launches a [WolfEngine] via [Wolf3d] and exposes renderer/input integrations.
|
|
class GameScreen extends StatefulWidget {
|
|
/// Shared application facade owning the engine, audio, and input.
|
|
final Wolf3d wolf3d;
|
|
|
|
/// Creates a gameplay screen driven by [wolf3d].
|
|
const GameScreen({
|
|
required this.wolf3d,
|
|
super.key,
|
|
});
|
|
|
|
@override
|
|
State<GameScreen> createState() => _GameScreenState();
|
|
}
|
|
|
|
class _GameScreenState extends State<GameScreen> {
|
|
late final WolfEngine _engine;
|
|
bool _useAsciiMode = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_engine = widget.wolf3d.launchEngine(
|
|
onGameWon: () => Navigator.of(context).pop(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return PopScope<Object?>(
|
|
canPop: _engine.difficulty != null,
|
|
onPopInvokedWithResult: (didPop, result) {
|
|
if (!didPop && _engine.difficulty == null) {
|
|
widget.wolf3d.input.queueBackAction();
|
|
}
|
|
},
|
|
child: Scaffold(
|
|
body: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final viewportRect = _menuViewportRect(
|
|
Size(constraints.maxWidth, constraints.maxHeight),
|
|
);
|
|
|
|
return Listener(
|
|
onPointerDown: (event) {
|
|
widget.wolf3d.input.onPointerDown(event);
|
|
if (_engine.difficulty == null &&
|
|
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(),
|
|
),
|
|
|
|
// 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),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|