createHeadlessGamefunctionnacatamalon/headless
createHeadlessGame(options: THeadlessGameOptions) => THeadlessGameMakes a game that runs with no browser and no screen, in Node or Bun: the same scenes, hooks and
updates as in a page, moved a frame at a time by whoever holds it. Everything the game would show
is worked out, so a test can ask where things are and what is shown, and capture() draws the
screen without a graphics card when a picture of it is wanted.
It is the same every time it runs. Every frame is one step of the game (step, 60 a second
unless it says), the random numbers start from seed (1 unless it says), and keys are pressed
and let go between frames, exactly when they are asked to be.
What a game loads is read from the assets folder (assets, default 'public'): a picture's size
and pixels when it is a PNG, its other files as they are. Physics runs as in a page, once the game
installs it (installPhysics2d(), installPhysics3d()); loaded() waits for its world too. Sound
is silent and plays on, so a sound waited on still ends. Without a browser there is no pointer and
no gamepad.
Parámetros
optionsTHeadlessGameOptionsThe game's options, its scenes, and where its files are.
Ejemplo
import { expect, test } from 'bun:test';
import { writeFileSync } from 'node:fs';
import { createHeadlessGame } from 'nacatamalon/headless';
import { encodePng } from 'nacatamalon/authoring';
import { Level } from '../src/scenes/level';
test('the player walks right', async () => {
const game = createHeadlessGame({ width: 320, height: 180, scenes: { Level } });
await game.loaded();
const start = game.find('Player')!.transform!.x;
game.press('ArrowRight');
game.run(60);
game.release('ArrowRight');
expect(game.find('Player')!.transform!.x).toBeGreaterThan(start);
expect(game.drawn().some((p) => p.text === 'SCORE 0')).toBe(true);
writeFileSync('level.png', encodePng(game.capture({ scale: 4 }))); // to look at it
game.destroy();
});