Core concepts
Game
Game owns the canvas, the loop and the shared services: camera, random, params, stats, hooks, inspect.
import { Game } from "@debuffjp/core";
const game = new Game({ canvas, width: 320, height: 180, scale: "integer", pixelArt: true });Game code always works in logical units (here 320x180). The canvas scales to its container and keeps its backing store sharp on high-DPI screens.
scale | Behaviour |
|---|---|
fit (default) | Fill the container, keep the aspect ratio |
integer | Whole-pixel scales only, for crisp pixel art |
expand | Like fit, but the logical size grows along the spare axis instead of letterboxing |
pixel | A fixed pixelSize CSS pixels per logical unit (default 3); the logical size grows with the container, so bigger screens show more of the game |
none | Logical size in CSS pixels |
With expand and pixel, width and height are the minimum, and game.width and game.height change when the window does (game.hooks.resize). Lay out from them each frame. Show more of the game on big screens walks through pixel.
Scenes
A scene is a plain object. Every method is optional:
game
.add({
id: "play",
enter() {},
update(dt) {}, // fixed step, seconds
render(draw, alpha) {}, // draw in logical units; alpha interpolates between steps
exit() {},
inspect: () => ({ score }), // JSON for tools and tests
})
.go("play");
game.start();The fixed-step loop
The simulation advances in exact steps (default 1/60 s), whatever the display's refresh rate. Rendering gets alpha so movement can be interpolated smoothly. Combined with the seeded game.random, the same inputs always produce the same game, which is what makes scenario tests reproducible.
game.freeze(0.08): hit-stop. Simulation pauses, rendering continues (Game feel).game.timeScale = 0.5: slow motion.new Game({ clock: { budgetMs: 8 } }): from 0.28, for simulations that can outgrow a frame. A frame stops catching up after 8 ms of steps, so the game runs slower instead of dropping to a few frames per second.- The loop pauses automatically when the tab is hidden.
Smooth motion between steps
Drawn at their stepped positions, moving objects jump on displays faster than 60 Hz. The camera already blends by alpha. From 0.26, smoothMotion does the same for your objects:
import { smoothMotion } from "@debuffjp/core";
const smooth = smoothMotion(game, () => [player, enemies, shots]); // objects, or arrays of them
// in render:
draw.sprite(heroImg, smooth.x(player), smooth.y(player));
// after moving something instantly (respawn, warp), so it doesn't streak for a step:
smooth.snap(player);It saves each object's position before every step, so new objects need no registering and removed ones no clean-up. It only reads positions, so replays and tests are unaffected. Only x and y blend.
From 0.26, render gets alpha 1 while the game is frozen, and the camera draws where it is. Before 0.26 it got the leftover time, so the camera and anything blended by alpha wobbled on every frame of a hit-stop.
Finding what's near
Checking every enemy against every shot is fine for a few dozen. For hundreds, put them in a SpatialHash once per step and ask only about the cells around a point (from 0.26):
import { SpatialHash } from "@debuffjp/core";
const grid = new SpatialHash<Enemy>(24); // cell size: about the radius you query most
// in update, after things moved:
grid.rebuild(enemies);
for (const shot of shots)
grid.near(shot.x, shot.y, 12, (e) => {
hit(e, shot);
return true; // stop at the first
});
const target = grid.nearest(player.x, player.y, 200, (e) => !e.dead);nearvisits every item whose centre is within the radius, with the squared distance.nearestreturns the closest item thatacceptallows.- Items are points. For large ones, add their size to the query radius.
- Results come in a fixed order, so replays stay deterministic, and after the first steps it allocates nothing.
- With every enemy querying 16 px around itself, it measured 4 to 15 times faster than checking every pair, for 500 to 2,000 enemies.
Saves
import { Storage } from "@debuffjp/core";
const save = new Storage("my-game", { best: 0, volume: 10 });
save.set("best", Math.max(save.get("best"), score));- It never throws: in private mode or with storage full, the game keeps an in-memory copy. From 0.28,
setreturnsfalsewhen the write didn't reach storage, so a reload would get the last save that fitted. localStorage usually holds about 5 MB per origin, so check the result if your saves can grow. - An update can't break old saves. A saved field is used only if its type matches the default's; otherwise it gets the default, and the old save is kept once under
my-game:backup. A field that defaults tonullkeeps any saved value. Before 0.34.1 it lost them on the next load (this page said 0.29.1, but that fix never shipped). - To change a save's shape on purpose, give it a version and a migration:
const save = new Storage("my-game", { best: { score: 0, time: 0 } }, {
version: 2,
migrate: (old) => ({ best: { score: Number(old.best) || 0, time: 0 } }),
});debuff build checks every save against saves.json, which you commit. It fails if a save's key disappears (players would lose their progress) and warns when a field changes type without a version bump. Run debuff build --accept-saves when a change is intended.
Hooks and plugins
game.hooks exposes beforeUpdate, afterUpdate, beforeRender, afterRender and sceneChange. game.use(plugin) installs reusable behaviour built on them. See Plugins.