Files
wolf_dart/apps/wolf_3d_cli/bin/main.dart

112 lines
3.1 KiB
Dart

import 'dart:async';
import 'dart:io';
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_input.dart';
// Helper to gracefully exit and restore the terminal
void exitCleanly(int code) {
stdout.write('\x1b[0m'); // Reset color
stdout.write('\x1b[2J\x1b[H'); // Clear screen
stdout.write('\x1b[?25h'); // SHOW the cursor again
exit(code);
}
void main() async {
stdin.echoMode = false;
stdin.lineMode = false;
// HIDE the blinking cursor and clear the screen to prep the canvas
stdout.write('\x1b[?25l\x1b[2J');
print("Discovering game data...");
// 1. Get the absolute URI of where this exact script lives
final scriptUri = Platform.script;
// 2. Resolve the path mathematically.
final targetUri = scriptUri.resolve(
'../../../packages/wolf_3d_assets/assets/retail',
);
final targetPath = targetUri.toFilePath();
final availableGames = await WolfensteinLoader.discover(
directoryPath: targetPath,
recursive: true,
);
final AsciiRasterizer asciiRasterizer = AsciiRasterizer(isTerminal: true);
final SixelRasterizer sixelRasterizer = SixelRasterizer();
CliRasterizer rasterizer = sixelRasterizer;
final FrameBuffer initialFrameBuffer = FrameBuffer(
stdout.terminalColumns,
stdout.terminalLines,
);
final engine = WolfEngine(
data: availableGames.values.first,
difficulty: Difficulty.medium,
startingEpisode: 0,
frameBuffer: initialFrameBuffer,
audio: CliSilentAudio(),
input: CliInput(),
onGameWon: () {
exitCleanly(0);
print("YOU WON!");
},
);
engine.init();
stdin.listen((List<int> bytes) {
if (bytes.contains(113) || bytes.contains(27)) {
exitCleanly(0);
}
if (bytes.contains(9)) {
rasterizer = identical(rasterizer, sixelRasterizer)
? asciiRasterizer
: sixelRasterizer;
stdout.write('\x1b[2J\x1b[H');
return;
}
(engine.input as CliInput).handleKey(bytes);
});
Stopwatch stopwatch = Stopwatch()..start();
Duration lastTick = Duration.zero;
Timer.periodic(const Duration(milliseconds: 33), (timer) {
// 1. Terminal Size Safety Check
if (stdout.hasTerminal) {
int cols = stdout.terminalColumns;
int rows = stdout.terminalLines;
if (!rasterizer.prepareTerminalFrame(engine, columns: cols, rows: rows)) {
// Clear the screen and print the warning at the top left
stdout.write('\x1b[2J\x1b[H');
stdout.write(
rasterizer.buildTerminalSizeWarning(columns: cols, rows: rows),
);
// Prevent the engine from simulating a massive time jump when resumed
lastTick = stopwatch.elapsed;
return;
}
}
// 2. Normal Game Loop
Duration currentTick = stopwatch.elapsed;
Duration elapsed = currentTick - lastTick;
lastTick = currentTick;
// Move cursor to top-left (0,0) before drawing the frame
stdout.write('\x1b[H');
engine.tick(elapsed);
stdout.write(rasterizer.render(engine));
});
}