Moved everything to a monorepo and fixed the CLI app rendering
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
107
apps/wolf_3d_cli/bin/main.dart
Normal file
107
apps/wolf_3d_cli/bin/main.dart
Normal file
@@ -0,0 +1,107 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:wolf_3d_data/wolf_3d_data.dart';
|
||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_engine/wolf_3d_engine.dart';
|
||||
import 'package:wolf_3d_input/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 data = availableGames.values.first;
|
||||
|
||||
final input = CliInput();
|
||||
final cliAudio = CliSilentAudio();
|
||||
|
||||
final engine = WolfEngine(
|
||||
data: data,
|
||||
difficulty: Difficulty.bringEmOn,
|
||||
startingEpisode: 0,
|
||||
audio: cliAudio,
|
||||
onGameWon: () {
|
||||
exitCleanly(0);
|
||||
print("YOU WON!");
|
||||
},
|
||||
);
|
||||
|
||||
final rasterizer = AsciiRasterizer();
|
||||
final buffer = FrameBuffer(120, 40);
|
||||
|
||||
engine.init();
|
||||
|
||||
stdin.listen((List<int> bytes) {
|
||||
if (bytes.contains(113) || bytes.contains(27)) {
|
||||
exitCleanly(0);
|
||||
}
|
||||
input.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) {
|
||||
if (stdout.terminalColumns < 120 || stdout.terminalLines < 40) {
|
||||
// Clear the screen and print the warning at the top left
|
||||
stdout.write('\x1b[2J\x1b[H');
|
||||
stdout.write('\x1b[31m[ ERROR ] TERMINAL TOO SMALL\x1b[0m\n\n');
|
||||
stdout.write(
|
||||
'Wolfenstein 3D requires a minimum resolution of 120x40.\n',
|
||||
);
|
||||
stdout.write(
|
||||
'Current size: \x1b[33m${stdout.terminalColumns}x${stdout.terminalLines}\x1b[0m\n\n',
|
||||
);
|
||||
stdout.write('Please resize your window to resume the game...');
|
||||
|
||||
// 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');
|
||||
|
||||
input.update();
|
||||
|
||||
engine.tick(elapsed, input.currentInput);
|
||||
rasterizer.render(engine, buffer);
|
||||
rasterizer.finalizeFrame();
|
||||
|
||||
stdout.write(rasterizer.toAnsiString());
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user