Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions .claude/skills/new-ecs-package/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: new-ecs-package
description: Add an ECS component or system to one of this repo's registry packages (packages/basics-2d, combat-2d, controls-2d, graphics-2d, physics-2d), or create a new package for a new group, the way the existing ones are written. Use when asked to add a NanoForge component, system or package in components-systems.
description: Add an ECS component or system to one of this repo's registry packages (packages/basics-2d, basics-3d, graphics-2d, physics-2d, controls-2d, combat-2d, scenes-2d, network-2d, audio), or create a new package for a new group, the way the existing ones are written. Use when asked to add a NanoForge component, system or package in components-systems.
---

# New component, system or package
Expand All @@ -15,7 +15,10 @@ Read `AGENTS.md` first. Reference package: `packages/basics-2d`.
items that a game would install together (like `controls-2d`).
- An item may use another package's items only if that package doesn't already depend on this one
(`dependencies` in the manifests): `physics-2d` and `graphics-2d` depend on `basics-2d`;
`controls-2d` on `basics-2d`, `physics-2d`; `combat-2d` on `basics-2d`, `graphics-2d`, `physics-2d`.
`combat-2d` on `basics-2d`, `graphics-2d`, `physics-2d`; `controls-2d` on `basics-2d`,
`combat-2d`, `graphics-2d`, `physics-2d`; `scenes-2d` on
`basics-2d`, `graphics-2d`, `physics-2d`; `network-2d` on `basics-2d`. Nothing depends on
`controls-2d`.

## 1. A component

Expand Down Expand Up @@ -44,9 +47,10 @@ export class MyThing2D {

- `name` is a literal equal to the class name: the ECS registry keys on it.
- Every constructor parameter is `public` and has a default: they are the inspector's fields. Give
each an inline doc, at least its `@group` (`MoveController2D` has only that on its keys). A string
literal union (`"both" | "horizontal"`) becomes a choice, an engine enum (`InputEnum`) too.
- `@side client` when it uses graphics or input, `shared` otherwise.
each an inline doc, at least its `@group` (`KeyboardControls2D` has only that on its keys). A
string literal union (`"both" | "horizontal"`) becomes a choice, an engine enum (`InputEnum`) too.
- `@side client` when it uses graphics or input or runs only on a client, `server` when it runs
only on a server, `shared` otherwise.
- Layout tags: `@group <Name>` on each parameter,
`@preset vector.x|vector.y|size.width|size.height|color`, `@asset .png .jpg` for a file of the
project's assets, `@label <text>`, `@color <css>`, `@hidden`. Examples:
Expand Down Expand Up @@ -86,10 +90,11 @@ export const myThing2D = (registry: Registry, ctx: Context) => {
`@nanoforge/<pkg>/components/<file>`.
- Drop `ctx` if unused (`noUnusedParameters`), like `detectCollisions2D(registry)`.
- `ctx.app.delta` is in milliseconds.
- A system that reads `ctx.input` or `ctx.graphics` also has a bare `import "@nanoforge-dev/input";`
or `import "@nanoforge-dev/graphics-2d";`: it brings in their `Context` types, and the editor
reads the libs an item requires from its imports (`controls-2d/systems/move-control-2d.ts`,
`graphics-2d/systems/draw-2d.ts`).
- A system that reads `ctx.input` or `ctx.graphics` also imports the lib: a bare
`import "@nanoforge-dev/input";` or `import "@nanoforge-dev/graphics-2d";`
(`controls-2d/systems/click-2d.ts`, `graphics-2d/systems/draw-2d.ts`), or a value it uses
(`InputEnum` in `controls-2d/systems/read-keyboard-2d.ts`). It brings in their `Context` types,
and the editor reads the libs an item requires from its imports.
- Code shared by several items goes in a file at the package root (`collisions.ts`), as exported
arrow functions: every export under `components/` or `systems/` is read as an item.

Expand All @@ -103,7 +108,7 @@ Nothing in code: the manifest's `contents` already points at `components/` and `
line of the package README if it has one;
- `packages/<pkg>/nanoforge.manifest.json`: only if the new code imports a package or an engine lib
not listed yet: `@nanoforge/<pkg>` in `dependencies`, at the dependency's current major
(`^<its manifest major>.0.0`); `^1.0.0` for a package never released; `@nanoforge-dev/<lib>` in
(`^<its manifest major>.0.0`); `^1.0.0` for a manifest without a `version`; `@nanoforge-dev/<lib>` in
`libDependencies`, at its `core` catalog range. Never touch `version`: the release PR bumps it
from the commits.

Expand All @@ -121,7 +126,7 @@ Nothing in code: the manifest's `contents` already points at `components/` and `
"license": "MIT",
"suggestedPlugins": ["@nanoforge/ecs"],
"dependencies": { "@nanoforge/basics-2d": "^2.0.0" },
"libDependencies": { "@nanoforge-dev/common": "^2.0.0", "@nanoforge-dev/ecs": "^2.0.0" },
"libDependencies": { "@nanoforge-dev/common": "^3.0.0", "@nanoforge-dev/ecs": "^3.0.0" },
"contents": [
{ "kind": "@nanoforge/ecs:component", "path": "components/" },
{ "kind": "@nanoforge/ecs:system", "path": "systems/" }
Expand All @@ -131,7 +136,7 @@ Nothing in code: the manifest's `contents` already points at `components/` and `

- `dependencies`: the `@nanoforge/*` packages its files import, nothing else (drop the key if none,
like `basics-2d`), each at the dependency's current major (`^<its manifest major>.0.0`); `^1.0.0` for
a package never released. Each must be a folder of `packages/`, or the release fails.
a manifest without a `version`. Each must be a folder of `packages/`, or the release fails.
- `libDependencies`: the `@nanoforge-dev/*` libs its files import, at the `core` catalog range. A
lib not in the `core` catalog is a new dependency: ask first.
- README: `# @nanoforge/<new-name>`, then one line per item, like `packages/physics-2d/README.md`.
Expand All @@ -142,10 +147,14 @@ Nothing in code: the manifest's `contents` already points at `components/` and `

The 70% coverage thresholds cover every `packages/**/*.ts`: a new item comes with a test.

- A system: in `test/systems.spec.ts`, in the `describe` of its package (add one for a new package).
Spawn entities with `spawn(...)`, `registry.addSystem(mySystem)`, `run(frame(ms))`, then check the
components. Held keys go in `keys`, the mouse in `mouse`.
- A graphics item: in `test/graphics.spec.ts`, which stubs `document` and uses `fakeLayer()`.
- A system: in `test/<pkg>.spec.ts`, the spec file of its package (a new package gets one, calling
`useEcs()` from `test/helpers.ts` at the top). Spawn entities with `spawn(...)`,
`registry.addSystem(mySystem)`, `run(frame(ms))`, then check the components. Held keys go in
`fake.keys`, the mouse in `fake.mouse`, the gamepad's sticks and buttons in `fake.sticks` and
`fake.buttons`.
- A graphics item: in the same file, which calls `stubDocument()` so Konva shapes run without a
canvas; `fakeLayer()` records what is drawn.
- A recipe that crosses packages (a README's): in `test/recipes.spec.ts`.
- Import with the alias: `@nanoforge/<pkg>/systems/<file>`.

## 6. Check and commit
Expand Down
104 changes: 76 additions & 28 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,39 @@

## Role

NanoForge's official component and system packages for the editor, made for Engine v2. Each folder
NanoForge's official component and system packages for the editor, made for Engine v3. Each folder
of `packages/` is one package of the NanoForge registry (`@nanoforge/<name>`), installed into a
project's `nf_modules/@nanoforge/<name>` from the editor (**File › Packages…**). They are not npm
packages: the root `package.json` is private and `pnpm-workspace.yaml` lists no workspace packages.

| Package | Side | What it gives |
| ------------------------ | ------ | ---------------------------------------------------------------------------------- |
| `@nanoforge/basics-2d` | shared | `Position2D`, `Velocity2D`, `Acceleration2D` and `move2D` |
| `@nanoforge/basics-3d` | shared | `Position3D`, `Rotation3D`, `Scale3D`, `Velocity3D`, `Acceleration3D` and `move3D` |
| `@nanoforge/graphics-2d` | client | Rectangles, circles and texts with a color or an image, `draw2D` |
| `@nanoforge/physics-2d` | shared | Hitboxes, collisions, gravity, walls and platforms |
| `@nanoforge/controls-2d` | client | Move and jump with the keyboard |
| `@nanoforge/combat-2d` | both | Health, damage, bullets fired at the mouse |
| Package | Side | What it gives |
| ------------------------ | ------ | -------------------------------------------------------------------------------------------------------- |
| `@nanoforge/basics-2d` | shared | Position, velocity and `move2D`; tags, models, lifetimes, following another entity |
| `@nanoforge/basics-3d` | shared | `Position3D`, `Rotation3D`, `Scale3D`, `Velocity3D`, `Acceleration3D` and `move3D` |
| `@nanoforge/graphics-2d` | client | Shapes and texts with a color or an image, sprite animations, facing, a camera, a HUD, `draw2D` |
| `@nanoforge/physics-2d` | shared | Hitboxes, collisions, gravity, walls, bounces and deflectors, walking, jumping, chasing and bounds |
| `@nanoforge/controls-2d` | client | Keyboard, mouse and gamepad controls for walking, jumping and shooting; click, drag and follow the mouse |
| `@nanoforge/combat-2d` | both | Health, damage, shooters fired by a player, a turret or the server; health bars |
| `@nanoforge/scenes-2d` | both | Scene vars shown in texts, coins and zones that add to them |
| `@nanoforge/network-2d` | both | The server sends where its entities are, how they move and which are gone; the clients follow |
| `@nanoforge/audio` | client | Sound effects, music per level, a mute key |

## Stack

- TypeScript, strict, built with `tsc -b` (`tsconfig.json` includes `packages/**/*.ts`).
- Engine libs from the `core` catalog of `pnpm-workspace.yaml`: `@nanoforge-dev/common`,
`@nanoforge-dev/ecs`, `@nanoforge-dev/graphics-2d`, `@nanoforge-dev/input` (all `^2.0.0`).
`@nanoforge-dev/ecs`, `@nanoforge-dev/graphics-2d`, `@nanoforge-dev/input`,
`@nanoforge-dev/scene`, `@nanoforge-dev/sound`, `@nanoforge-dev/music`, `@nanoforge-dev/network`
(all `^3.0.0`).
`@nanoforge-dev/editor-lib` is there only for the typings of `scene`, `sound`, `music` and
`network`, which import it.
- Vitest with typechecking (`tsconfig.spec.json`) and v8 coverage on `packages/**/*.ts`.
- ESLint and Prettier from the shared `@nanoforge-dev/utils-eslint-config` and
`@nanoforge-dev/utils-prettier-config` (double quotes, trailing commas, 100 columns, sorted
imports).
- Husky: `commit-msg` runs commitlint (conventional commits), `pre-commit` runs lint-staged,
`pre-push` runs `pnpm run lint`.
- Node 25 (`engines`, `.nvmrc`), pnpm 11.9.0 (`packageManager`).
- Node 26 (`engines`, `.nvmrc`), pnpm 11.9.0 (`packageManager`).

## Layout

Expand All @@ -39,8 +46,9 @@ packages/<name>/
├── systems/<kebab>.ts # one system function per file
└── <helper>.ts # shared code that is not an item (collisions.ts, image-fill.ts...)
test/
├── systems.spec.ts # every system, on a real ECS registry
└── graphics.spec.ts # graphics-2d drawables and imageFill, with a fake DOM
├── helpers.ts # the ECS registry, the fake ctx (`fake`, `frame`), `spawn`, `run`
├── <name>.spec.ts # one per package: its items, on a real ECS registry
└── recipes.spec.ts # the READMEs' recipes end to end, across packages
docs/default-packages.md # packages a new project gets, and the order of every system
```

Expand Down Expand Up @@ -69,11 +77,12 @@ editor reads each item from its `.ts` file (NanoForge-dev/editor
(new code uses arrow functions; the systems already on main are `export function`, and the editor
reads both). Drop `ctx` when the system doesn't use it (`detectCollisions2D`). Queries go through
`registry.getZipper([A, B])`; the editor infers them, and the `ctx.<lib>` it uses.
- **`@side`:** `client` for what uses graphics or input, `shared` for the rest (`server` exists too,
unused here). Default `shared`.
- **`@side`:** `client` for what uses graphics or input, or runs only on a client
(`syncFromServer2D`), `shared` for the rest, `server` for what
runs only on a server (`syncToClients2D`). Default `shared`.
- **Docs:** the summary says what it does and what it needs (other components, the system to add);
`@remarks` and `@example` add detail. Constructor parameters have an inline `/** … */` doc, at
least their `@group` (`MoveController2D`'s keys have only that).
least their `@group` (`KeyboardControls2D`'s keys have only that).
- **Inspector layout:** on the class, `@group <Name> [color=<css>] [hidden] - <description>`; on a
parameter, `@group <Name>`, `@preset vector.x|vector.y|size.width|size.height|color`,
`@asset .png .jpg …` for a project file, `@label <text>`, `@color <css>`, `@hidden`. Less common
Expand All @@ -90,14 +99,22 @@ editor reads each item from its `.ts` file (NanoForge-dev/editor
same path in `nf_modules/@nanoforge/<name>`.
- Engine libs: `import type { Registry } from "@nanoforge-dev/ecs"`, `type` when only types are
used.
- A system that reads `ctx.input` or `ctx.graphics` has a bare `import "@nanoforge-dev/input";` or
`import "@nanoforge-dev/graphics-2d";` (`move-control-2d.ts`, `draw-2d.ts`): it adds their fields
to `Context`, and the editor reads an item's required libs from its imports.
- A system that reads `ctx.input` or `ctx.graphics` imports the lib: a bare
`import "@nanoforge-dev/input";` or `import "@nanoforge-dev/graphics-2d";` (`click-2d.ts`,
`draw-2d.ts`), or a value it uses (`InputEnum` in `read-keyboard-2d.ts`). It adds their fields to
`Context`, and the editor reads an item's required libs from its imports. Same for
`ctx.controller` (`@nanoforge-dev/input/controller`), `ctx.scenes`, `ctx.sound` and `ctx.music`.
- `ctx.network` is the exception: `@nanoforge-dev/network/client` and `/server` both declare it,
with other types, so one program can't import both. `network-2d` imports `PacketDefinition`
from the root and types the part of `ctx.network` it uses (`sync-to-clients-2d.ts`).
- Mouse positions are on the screen: an item placed in the world converts them with `toWorld` or
`pointFor` (`graphics-2d/world.ts`), so it works under a `Camera2D` and on `Hud2D` entities.
- `nanoforge.manifest.json` mirrors the imports: each `@nanoforge/<pkg>` used goes in `dependencies`
at the dependency's current major (`^<its manifest major>.0.0`, e.g.
`{ "@nanoforge/basics-2d": "^2.0.0" }`; `^1.0.0` for a package never released), each engine lib in `libDependencies` at its `core`
catalog range. `combat-2d` also lists `@nanoforge-dev/graphics-2d`, which it uses through
`DrawableCircle2D`.
`{ "@nanoforge/basics-2d": "^2.0.0" }`; `^1.0.0` for a manifest without a `version`), each engine lib
in `libDependencies` at its `core` catalog range. `controls-2d` and `scenes-2d` also list
`@nanoforge-dev/graphics-2d`, which they use through graphics-2d's items: the mouse items read
the camera through `pointFor`, and `showSceneVars2D` writes a `DrawableText2D`.

## Registering a new item

Expand All @@ -114,12 +131,22 @@ The skill `.claude/skills/new-ecs-package` walks through it.

## Tests

- `test/systems.spec.ts` loads the server ECS (`EcsLibrary` from `@nanoforge-dev/ecs/server`, its
wasm from `node_modules`), then each test spawns entities, adds systems and runs frames made by
`frame(ms)` (fake `ctx.app.delta` and `ctx.input`). One `describe` per package.
- `test/graphics.spec.ts` stubs `document` and uses a fake layer, so drawables run without a canvas.
- `test/helpers.ts` loads the server ECS (`EcsLibrary` from `@nanoforge-dev/ecs/server`, its wasm
from `node_modules`) once per spec file (`useEcs()`), and clears the systems, the entities and
`fake` before each test. A test spawns entities (`spawn`), adds systems, sets what the player
does in `fake`, and runs frames made by `frame(ms)` (`run(frame(16))`): fake `ctx.app.delta`,
`ctx.input` (`keys`, `mouse` with `deltaX`/`deltaY` and `focus`, `drag`), `ctx.controller`
(`left`/`right` `sticks`, `buttons`), `ctx.graphics` (a stage and a `baseLayer` that records the
camera), `ctx.scenes.vars` (`vars`), `ctx.sound` and `ctx.music` (spies), and one
`ctx.network` per test (`sent`, `sentTo`, `sentToServer`, `received`). `frame(ms, ["controller"])`
leaves a library out.
- One `test/<name>.spec.ts` per package, and `test/recipes.spec.ts` for the recipes that cross
packages. A spec file whose tests make Konva shapes calls `stubDocument()`, so they run without a
canvas. A system's module state (`click2D`'s buttons, `draw2D`'s shapes, a warning flag) lasts the
whole spec file.
- Import from the packages with the `@nanoforge/*` alias. The 70% thresholds apply to all of
`packages/**/*.ts`: a new item comes with its test.
`packages/**/*.ts`: a new item comes with its test. Vitest's typecheck misses some declaration
errors in `test/`: run `pnpm exec tsc --noEmit -p tsconfig.spec.json` too.

## Releases

Expand All @@ -134,13 +161,34 @@ Both steps are a maintainer's action: an agent never dispatches the workflow or
tells the user.

So never edit a `version` by hand. A new package's manifest has no `version`: its first release is
`1.0.0`. Every `dependencies` entry must be a package of `packages/`, or the release fails.
`1.0.0`. A manifest `version` that has no git tag and is not on the registry is published as it is,
whatever the commits say (the action's `plan.ts`, "not on the registry yet"): so the first release
keeps each `2.0.0`. After a release, a break raises the version, and the dependents' ranges must be
raised in that release PR. Every `dependencies` entry must be a package of `packages/`, or the
release fails.

## Conventions

- Files in kebab-case (`move-2d.ts`), components in PascalCase with the `2D` suffix for 2D ones
(`Velocity2D`), systems in camelCase (`move2D`).
- Systems run every frame: query only the components they need.
- Intents: what an entity is told to do is a public field of the logic component that acts on it,
named after the action: `WalkAndJump2D.moveX`/`moveY` (-1 to 1), `Shoot2D.fire` and
`aimX`/`aimY` (a direction), and one-shot `<verb>Requested` (`jumpRequested`, `fireRequested`).
A device reader, an AI or the server's packet handler writes them; held ones are never cleared,
and a `<verb>Requested` is cleared by the system that reads it. The device components
(`KeyboardControls2D`, `GamepadControls2D`) keep what they read, for a network sender.
- Models: an entity with a `Model` and a `Tag` is a template, not part of the game. A spawner finds
it by its word (`modelOf`) and copies a known list of its components (`copyOf`), and marks the
copy `FromModel` (`basics-2d/models.ts`). `expireLifetimes`, `fadeOut2D` and `animateSprites2D`
skip models, so copies start fresh.
- Runtime-only components, which the editor must not offer (`FromModel`), live in a helper file at
the package root, not in `components/`.
- Only shape components (`Drawable*`, `Hitbox*`) take `width`, `height` or `radius`, and only
position and vector components (`Position2D`, `Velocity2D`, `Acceleration2D`, the 3D ones) take a
numeric `x`/`y`: the editor's Scene screen places an entity by its first numeric `x`/`y`, and
draws a shape for any `radius` or `width` and `height`. Name the others for what they are
(`offsetX`, `aimX`, `moveX`, `jumpHeight`, `barWidth`, `areaWidth`, `worldWidth`).
- Commits: conventional commits (`@commitlint/config-conventional`), lowercase subject, the package
as scope when the change is in one (`feat(physics-2d): …`). The commit type sets the release bump.
Never `--no-verify`.
Loading