index.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * DOOM Overlay Demo - Play DOOM as an overlay
  3. *
  4. * Usage: pi --extension ./examples/extensions/doom-overlay
  5. *
  6. * Commands:
  7. * /doom-overlay - Play DOOM in an overlay (Q to pause/exit)
  8. *
  9. * This demonstrates that overlays can handle real-time game rendering at 35 FPS.
  10. */
  11. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  12. import { DoomOverlayComponent } from "./doom-component.ts";
  13. import { DoomEngine } from "./doom-engine.ts";
  14. import { ensureWadFile } from "./wad-finder.ts";
  15. // Persistent engine instance - survives between invocations
  16. let activeEngine: DoomEngine | null = null;
  17. let activeWadPath: string | null = null;
  18. export default function (pi: ExtensionAPI) {
  19. pi.registerCommand("doom-overlay", {
  20. description: "Play DOOM as an overlay. Q to pause and exit.",
  21. handler: async (args, ctx) => {
  22. if (ctx.mode !== "tui") {
  23. ctx.ui.notify("DOOM requires interactive mode", "error");
  24. return;
  25. }
  26. // Auto-download WAD if not present
  27. ctx.ui.notify("Loading DOOM...", "info");
  28. const wad = args?.trim() ? args.trim() : await ensureWadFile();
  29. if (!wad) {
  30. ctx.ui.notify("Failed to download DOOM WAD file. Check your internet connection.", "error");
  31. return;
  32. }
  33. try {
  34. // Reuse existing engine if same WAD, otherwise create new
  35. let isResume = false;
  36. if (activeEngine && activeWadPath === wad) {
  37. ctx.ui.notify("Resuming DOOM...", "info");
  38. isResume = true;
  39. } else {
  40. ctx.ui.notify(`Loading DOOM from ${wad}...`, "info");
  41. activeEngine = new DoomEngine(wad);
  42. await activeEngine.init();
  43. activeWadPath = wad;
  44. }
  45. await ctx.ui.custom(
  46. (tui, _theme, _keybindings, done) => {
  47. return new DoomOverlayComponent(tui, activeEngine!, () => done(undefined), isResume);
  48. },
  49. {
  50. overlay: true,
  51. overlayOptions: {
  52. width: "75%",
  53. maxHeight: "95%",
  54. anchor: "center",
  55. margin: { top: 1 },
  56. },
  57. },
  58. );
  59. } catch (error) {
  60. ctx.ui.notify(`Failed to load DOOM: ${error}`, "error");
  61. activeEngine = null;
  62. activeWadPath = null;
  63. }
  64. },
  65. });
  66. }