Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

2.2 KB
Raw Blame
/// <reference path="./types/index.d.ts" />
// raea/godot: Godot's whole API from TypeScript, through GodotJS (github.com/godotjs/GodotJS, MIT): every class,
// singleton, enum and builtin of Godot 4.6.1 (src/types, generated by tools/gen.sh from GodotJS's codegen), the same
// module GodotJS scripts import as "godot". `raea godot run SCRIPT.ts` runs a script in Godot (raea setup godot
// installs it), on Godot's own JavaScript engine: JavaScriptCore on the Mac, QuickJS on Windows and Linux.
//
//   import { MeshInstance3D, BoxMesh, Vector3, type SceneTree } from "raea/godot";
//   export default async function (tree: SceneTree, args: string[]) {
//     const box = new MeshInstance3D();
//     box.mesh = new BoxMesh();
//     tree.root.add_child(box);
//     await frame(tree);
//   }
//
// Importing "godot" works the same in a bundle `raea godot run` makes; this module also brings the types in.
import { OS, type SceneTree } from "godot";

export * from "godot";

/** The folder `raea godot run` was run from. Godot runs inside its project's folder, so a relative path there names
 * a file in the project; resolve() names it where raea ran instead. */
export function cwd(): string {
  return OS.get_environment("RAEA_CWD") || ".";
}

/** A path relative to where `raea godot run` ran (cwd()), as Godot's file APIs need it; an absolute path, res:// and
 * user:// as they are. */
export function resolve(path: string): string {
  if (/^([a-z]+:\/\/|\/|[A-Za-z]:[\\/])/.test(path)) return path;
  const base = cwd();
  return base === "." ? path : `${base.replace(/[\\/]+$/, "")}/${path}`;
}

/** What `raea godot run SCRIPT.ts` calls: the script's default export, given Godot's SceneTree and the arguments after
 * the script. Godot quits when it returns (or its promise settles): 0, or 1 when it throws. */
export type Main = (tree: SceneTree, args: string[]) => unknown;

/** Resolves on the tree's next frame (its process_frame signal). */
export function frame(tree: SceneTree): Promise<void> {
  return tree.process_frame.as_promise();
}

/** Resolves after `n` frames. */
export async function frames(tree: SceneTree, n: number): Promise<void> {
  for (let i = 0; i < n; i++) await frame(tree);
}