doom-component.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * DOOM Component for overlay mode
  3. *
  4. * Renders DOOM frames using half-block characters (▀) with 24-bit color.
  5. * Height is calculated from width to maintain DOOM's aspect ratio.
  6. */
  7. import type { Component } from "@earendil-works/pi-tui";
  8. import { isKeyRelease, type TUI } from "@earendil-works/pi-tui";
  9. import type { DoomEngine } from "./doom-engine.ts";
  10. import { DoomKeys, mapKeyToDoom } from "./doom-keys.ts";
  11. function renderHalfBlock(
  12. rgba: Uint8Array,
  13. width: number,
  14. height: number,
  15. targetCols: number,
  16. targetRows: number,
  17. ): string[] {
  18. const lines: string[] = [];
  19. const scaleX = width / targetCols;
  20. const scaleY = height / (targetRows * 2);
  21. for (let row = 0; row < targetRows; row++) {
  22. let line = "";
  23. const srcY1 = Math.floor(row * 2 * scaleY);
  24. const srcY2 = Math.floor((row * 2 + 1) * scaleY);
  25. for (let col = 0; col < targetCols; col++) {
  26. const srcX = Math.floor(col * scaleX);
  27. const idx1 = (srcY1 * width + srcX) * 4;
  28. const idx2 = (srcY2 * width + srcX) * 4;
  29. const r1 = rgba[idx1] ?? 0,
  30. g1 = rgba[idx1 + 1] ?? 0,
  31. b1 = rgba[idx1 + 2] ?? 0;
  32. const r2 = rgba[idx2] ?? 0,
  33. g2 = rgba[idx2 + 1] ?? 0,
  34. b2 = rgba[idx2 + 2] ?? 0;
  35. line += `\x1b[38;2;${r1};${g1};${b1}m\x1b[48;2;${r2};${g2};${b2}m▀`;
  36. }
  37. line += "\x1b[0m";
  38. lines.push(line);
  39. }
  40. return lines;
  41. }
  42. export class DoomOverlayComponent implements Component {
  43. private engine: DoomEngine;
  44. private tui: TUI;
  45. private interval: ReturnType<typeof setInterval> | null = null;
  46. private onExit: () => void;
  47. // Opt-in to key release events for smooth movement
  48. wantsKeyRelease = true;
  49. constructor(tui: TUI, engine: DoomEngine, onExit: () => void, resume = false) {
  50. this.tui = tui;
  51. this.engine = engine;
  52. this.onExit = onExit;
  53. // Unpause if resuming
  54. if (resume) {
  55. this.engine.pushKey(true, DoomKeys.KEY_PAUSE);
  56. this.engine.pushKey(false, DoomKeys.KEY_PAUSE);
  57. }
  58. this.startGameLoop();
  59. }
  60. private startGameLoop(): void {
  61. this.interval = setInterval(() => {
  62. try {
  63. this.engine.tick();
  64. this.tui.requestRender();
  65. } catch {
  66. // WASM error (e.g., exit via DOOM menu) - treat as quit
  67. this.dispose();
  68. this.onExit();
  69. }
  70. }, 1000 / 35);
  71. }
  72. handleInput(data: string): void {
  73. // Q to pause and exit (but not on release)
  74. if (!isKeyRelease(data) && (data === "q" || data === "Q")) {
  75. // Send DOOM's pause key before exiting
  76. this.engine.pushKey(true, DoomKeys.KEY_PAUSE);
  77. this.engine.pushKey(false, DoomKeys.KEY_PAUSE);
  78. this.dispose();
  79. this.onExit();
  80. return;
  81. }
  82. const doomKeys = mapKeyToDoom(data);
  83. if (doomKeys.length === 0) return;
  84. const released = isKeyRelease(data);
  85. for (const key of doomKeys) {
  86. this.engine.pushKey(!released, key);
  87. }
  88. }
  89. render(width: number): string[] {
  90. // DOOM renders at 640x400 (1.6:1 ratio)
  91. // With half-block characters, each terminal row = 2 pixels
  92. // So effective ratio is 640:200 = 3.2:1 (width:height in terminal cells)
  93. // Add 1 row for footer
  94. const ASPECT_RATIO = 3.2;
  95. const MIN_HEIGHT = 10;
  96. const height = Math.max(MIN_HEIGHT, Math.floor(width / ASPECT_RATIO));
  97. const rgba = this.engine.getFrameRGBA();
  98. const lines = renderHalfBlock(rgba, this.engine.width, this.engine.height, width, height);
  99. // Footer
  100. const footer = " DOOM | Q=Pause | WASD=Move | Shift+WASD=Run | Space=Use | F=Fire | 1-7=Weapons";
  101. const truncatedFooter = footer.length > width ? footer.slice(0, width) : footer;
  102. lines.push(`\x1b[2m${truncatedFooter}\x1b[0m`);
  103. return lines;
  104. }
  105. invalidate(): void {}
  106. dispose(): void {
  107. if (this.interval) {
  108. clearInterval(this.interval);
  109. this.interval = null;
  110. }
  111. }
  112. }