Skip to content
Open
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
8 changes: 7 additions & 1 deletion .claude/skills/new-ecs-package/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ export const myThing2D = (registry: Registry, ctx: Context) => {
## 3. Register it

Nothing in code: the manifest's `contents` already points at `components/` and `systems/`, and
`tsconfig.json` builds `packages/**/*.ts`. Update by hand:
`tsconfig.json` builds `packages/**/*.ts`. A package that uses `@nanoforge-dev/graphics-3d`
builds in `tsconfig.3d.json` instead (add it to the `include` of `tsconfig.3d.json` and `tsconfig.spec.3d.json`, and to the
`exclude` of `tsconfig.json` and `tsconfig.spec.json`), and its
spec goes in `test/3d/`, importing `../ecs`: both graphics libs type `ctx.graphics`. Update by hand:

- `packages/<pkg>/README.md`: one line for the item (and with which system it works);
- a new system: its place in _Order of the systems_ in `docs/default-packages.md`, and the order
Expand Down Expand Up @@ -154,6 +157,9 @@ The 70% coverage thresholds cover every `packages/**/*.ts`: a new item comes wit
`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 graphics-3d item: in `test/3d/<pkg>.spec.ts`, which imports `../ecs` (`useRegistry()`,
`spawn`, `runOn(ctx)`), not `test/helpers.ts`, and builds its ctx with a real `Scene` and
`PerspectiveCamera` from `@nanoforge-dev/graphics-3d/three`.
- A recipe that crosses packages (a README's): in `test/recipes.spec.ts`.
- Import with the alias: `@nanoforge/<pkg>/systems/<file>`.

Expand Down
52 changes: 36 additions & 16 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages: the root `package.json` is private and `pnpm-workspace.yaml` lists no
| `@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/graphics-3d` | client | Boxes, spheres and planes of a color, lights, a camera, any Three.js object, `draw3D` |
| `@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 |
Expand All @@ -21,14 +22,18 @@ packages: the root `package.json` is private and `pnpm-workspace.yaml` lists no

## Stack

- TypeScript, strict, built with `tsc -b` (`tsconfig.json` includes `packages/**/*.ts`).
- TypeScript, strict, built with `tsc -b` in two programs: `tsconfig.json` (`packages/**/*.ts`
but graphics-3d) and `tsconfig.3d.json` (the graphics-3d packages).
- Engine libs from the `core` catalog of `pnpm-workspace.yaml`: `@nanoforge-dev/common`,
`@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/scene`, `@nanoforge-dev/sound`, `@nanoforge-dev/music`, `@nanoforge-dev/network`,
`@nanoforge-dev/graphics-3d` (all `^3.0.0`), and `three` (`^0.187.0`, graphics-3d's peer).
`@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`.
- Vitest, in a `2d` and a `3d` project, and v8 coverage on `packages/**/*.ts`. Vitest typechecks
nothing: `pnpm run typecheck` (in `lint`) runs `tsc` on `tsconfig.spec.json` and
`tsconfig.spec.3d.json`, which cover the packages and the specs of each program, libs included,
so a program that loads both graphics libs fails (TS2717).
- 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).
Expand All @@ -46,17 +51,20 @@ 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/
├── helpers.ts # the ECS registry, the fake ctx (`fake`, `frame`), `spawn`, `run`
├── ecs.ts # the ECS registry, `spawn`, `runOn`, `useRegistry`: no engine typings
├── helpers.ts # re-exports ecs.ts; the fake ctx (`fake`, `frame`), `run`, `useEcs`
├── <name>.spec.ts # one per package: its items, on a real ECS registry
└── recipes.spec.ts # the READMEs' recipes end to end, across packages
├── recipes.spec.ts # the READMEs' recipes end to end, across packages
└── 3d/ # specs of the graphics-3d packages: they import ecs.ts, not helpers.ts
docs/default-packages.md # packages a new project gets, and the order of every system
```

## Commands

```sh
pnpm run build # tsc -b
pnpm run lint # prettier --check . && eslint --format=pretty packages
pnpm run build # tsc -b tsconfig.json tsconfig.3d.json
pnpm run lint # prettier --check . && eslint --format=pretty packages && typecheck
pnpm run typecheck # tsc --noEmit on tsconfig.spec.json and tsconfig.spec.3d.json
pnpm run format # prettier --write . && eslint --fix --format=pretty packages
pnpm run test # = test:unit: vitest, fails under 70% lines, functions, branches or statements
```
Expand Down Expand Up @@ -107,6 +115,13 @@ editor reads each item from its `.ts` file (NanoForge-dev/editor
- `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`).
- `ctx.graphics` is declared by both graphics-2d and graphics-3d, with other types, so one program
can't load both (TS2717); a game never does. The packages that use graphics-3d build apart: they
are in `tsconfig.3d.json` and out of `tsconfig.json`, and their specs are in `test/3d/`
(`tsconfig.spec.3d.json`, the `3d` Vitest project). A package never imports both graphics libs.
`draw3D` imports `@nanoforge-dev/graphics-3d` like `draw2D` imports graphics-2d; every
graphics-3d component file imports `@nanoforge-dev/graphics-3d/three`, even a bare `import`, so
the editor offers it only to 3D apps.
- 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`
Expand All @@ -120,7 +135,9 @@ editor reads each item from its `.ts` file (NanoForge-dev/editor

Nothing to register in code: every manifest's `contents` points at the folders
(`{ "kind": "@nanoforge/ecs:component", "path": "components/" }` and `…:system` on `systems/`), and
`tsconfig.json` builds every `packages/**/*.ts`. By hand:
`tsconfig.json` builds every `packages/**/*.ts` but the graphics-3d ones, which `tsconfig.3d.json`
builds (a new 3D package goes in the `include` of `tsconfig.3d.json` and `tsconfig.spec.3d.json`,
and in the `exclude` of `tsconfig.json` and `tsconfig.spec.json`). By hand:

- the package's `README.md`: one line for the item;
- a new system: its place in _Order of the systems_ in `docs/default-packages.md`, and in its
Expand All @@ -145,8 +162,10 @@ The skill `.claude/skills/new-ecs-package` walks through it.
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. Vitest's typecheck misses some declaration
errors in `test/`: run `pnpm exec tsc --noEmit -p tsconfig.spec.json` too.
`packages/**/*.ts`: a new item comes with its test. Vitest typechecks nothing (only
`*.spec-d.ts` files, and there are none): `pnpm run typecheck` does.
- A graphics-3d spec goes in `test/3d/` and imports `../ecs`: `helpers.ts` loads graphics-2d's
typings (its fake Konva layers), which type `ctx.graphics` for 2D.

## Releases

Expand Down Expand Up @@ -184,11 +203,12 @@ release fails.
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`).
- Only shape components (`Drawable*`, `Hitbox*`, `Box3D`, `Sphere3D`, `Plane3D`) take `width`,
`height`, `depth` 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`.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package of the registry:
| [`@nanoforge/basics-2d`](packages/basics-2d) | shared | Position, velocity and `move2D`; tags, models, lifetimes, following another entity |
| [`@nanoforge/basics-3d`](packages/basics-3d) | shared | `Position3D`, `Rotation3D`, `Scale3D`, `Velocity3D`, `Acceleration3D` and `move3D` |
| [`@nanoforge/graphics-2d`](packages/graphics-2d) | client | Shapes and texts with a color or an image, sprite animations, facing, a camera, a HUD, `draw2D` |
| [`@nanoforge/graphics-3d`](packages/graphics-3d) | client | Boxes, spheres and planes of a color, lights, a camera, any Three.js object, `draw3D` |
| [`@nanoforge/physics-2d`](packages/physics-2d) | shared | Hitboxes, collisions, gravity, walls, bounces and deflectors, walking, jumping, chasing and bounds |
| [`@nanoforge/controls-2d`](packages/controls-2d) | client | Keyboard, mouse and gamepad controls for walking, jumping and shooting; click, drag and follow the mouse |
| [`@nanoforge/combat-2d`](packages/combat-2d) | both | Health, damage, shooters fired by a player, a turret or the server; health bars |
Expand Down
4 changes: 3 additions & 1 deletion docs/default-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Not installed by default, so that a new project only shows what everyone needs:
| Menus, scores, coins to pick | `@nanoforge/scenes-2d`, `@nanoforge/controls-2d` (`Clickable2D` buttons) |
| Multiplayer (server + clients) | `@nanoforge/network-2d` |
| Any game with sound or music | `@nanoforge/audio` |
| 3D game | `@nanoforge/basics-3d`, `@nanoforge/graphics-3d` (instead of the 2D ones) |

## Order of the systems

Expand All @@ -46,7 +47,8 @@ Systems run in the order they are added. With every package:
- **6. Once everything moved:** `follow2D`, `animateMoves2D`, `faceVelocity2D`, `faceTarget2D`,
`faceAim2D`, `followCamera2D`, `animateSprites2D`, `colorOnHover2D`, `fadeOut2D`,
`showSceneVars2D`.
- **7.** `draw2D`, so the screen shows where entities ended up, then `drawHealthBars2D`.
- **7.** `draw2D`, so the screen shows where entities ended up, then `drawHealthBars2D`; in 3D,
`draw3D`.
- **8. Last:** `expireLifetimes`; on a network server, `syncToClients2D`.

`loadSounds` and `playMusic` can go anywhere on the client.
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
},
"funding": "https://github.com/NanoForge-dev/components-systems?sponsor",
"scripts": {
"build": "tsc -b",
"lint": "prettier --check . && eslint --format=pretty packages",
"build": "tsc -b tsconfig.json tsconfig.3d.json",
"lint": "prettier --check . && eslint --format=pretty packages && pnpm run typecheck",
"typecheck": "tsc --noEmit -p tsconfig.spec.json && tsc --noEmit -p tsconfig.spec.3d.json",
"format": "prettier --write . && eslint --fix --format=pretty packages",
"test:unit": "vitest run --config vitest.config.ts --coverage.thresholds.lines=70 --coverage.thresholds.functions=70 --coverage.thresholds.branches=70 --coverage.thresholds.statements=70",
"test": "pnpm run test:unit",
Expand All @@ -47,6 +48,7 @@
"@nanoforge-dev/ecs": "catalog:core",
"@nanoforge-dev/editor-lib": "catalog:core",
"@nanoforge-dev/graphics-2d": "catalog:core",
"@nanoforge-dev/graphics-3d": "catalog:core",
"@nanoforge-dev/input": "catalog:core",
"@nanoforge-dev/music": "catalog:core",
"@nanoforge-dev/network": "catalog:core",
Expand All @@ -60,6 +62,7 @@
"husky": "catalog:ci",
"lint-staged": "catalog:ci",
"prettier": "catalog:lint",
"three": "catalog:core",
"typescript": "catalog:core",
"vitest": "catalog:test"
},
Expand Down
50 changes: 50 additions & 0 deletions packages/graphics-3d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# @nanoforge/graphics-3d

Shows entities in the 3D scene of `@nanoforge-dev/graphics-3d`, without writing Three.js: add
`draw3D` after the systems that move them. Place them with `Position3D`, `Rotation3D` (degrees)
and `Scale3D` from `@nanoforge/basics-3d`. The project needs `@nanoforge-dev/graphics-3d` and its
peer `three`, and registers `Graphics3DLibrary` in the client's `main.ts`.

- `Box3D`, `Sphere3D`, `Plane3D`: lit shapes of one color. A `Plane3D` stands upright: lay it down
as a ground with `Rotation3D(-90, 0, 0)`.
- `Light3D`: an `ambient`, `directional` (the sun) or `point` (a bulb) light. Until the scene has
one, `draw3D` adds a default light, so shapes are never black.
- `Camera3D`: the point of view, looking along -z. The first active one is shown; with none, the
library's camera at `(0, 0, 5)` looking at the origin.
- `Object3DComponent`: any Three.js object you built, such as an `InstancedMesh` for many identical
objects. It moves with the transform components like the shapes.
- `draw3D`: gives each component its Three.js object and keeps it up to date. A removed component,
or a killed entity, loses its object, and its geometries, materials and textures are disposed.

`toObject3D` (`object-3d.ts`) is the mapping from a component's params to its Three.js object.
`draw3D` uses it, and so can the editor, so both show the same thing.

## Order

`draw3D` last but one, after the systems that move entities (`move3D`), where `draw2D` goes in 2D.

## Recipes

- A cube, a ball, a light and a camera, in the client's `main.ts`:

```ts
const cube = registry.spawnEntity();
registry.addComponent(cube, new Box3D(1, 1, 1, "#44aa88"));
registry.addComponent(cube, new Position3D(-1, 0, 0));
registry.addComponent(cube, new Velocity3D(0, 0.5, 0));

const ball = registry.spawnEntity();
registry.addComponent(ball, new Sphere3D(0.5, "#ff6b6b"));
registry.addComponent(ball, new Position3D(1, 0, 0));

const sun = registry.spawnEntity();
registry.addComponent(sun, new Light3D("directional", "#ffffff", 3));
registry.addComponent(sun, new Position3D(3, 5, 4));

const camera = registry.spawnEntity();
registry.addComponent(camera, new Camera3D());
registry.addComponent(camera, new Position3D(0, 1, 6));

registry.addSystem(move3D);
registry.addSystem(draw3D);
```
27 changes: 27 additions & 0 deletions packages/graphics-3d/components/box-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "@nanoforge-dev/graphics-3d/three";

/**
* Shows the entity as a lit box. Add `draw3D` to show it, and `Position3D`, `Rotation3D` or
* `Scale3D` (basics-3d) to place it.
*
* @example
* `new Box3D(1, 1, 1, "#44aa88")` is a green cube of one unit.
*
* @component
* @side client
* @group Box color=#ff9f43 - Size and color.
*/
export class Box3D {
name = "Box3D";

constructor(
/** Units along the x axis. @group Box */
public width = 1,
/** Units along the y axis (up). @group Box */
public height = 1,
/** Units along the z axis. @group Box */
public depth = 1,
/** Any CSS color. @group Box @preset color */
public color = "#ffffff",
) {}
}
30 changes: 30 additions & 0 deletions packages/graphics-3d/components/camera-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "@nanoforge-dev/graphics-3d/three";

/**
* The point of view the game is shown from. Add `draw3D`; place it with `Position3D` and turn it
* with `Rotation3D` (basics-3d).
*
* @remarks
* It looks along -z, from `(0, 0, 5)` until it has a `Position3D`. The first active camera is the
* one shown; with none, the library's default camera is shown: at `(0, 0, 5)`, looking at the
* origin.
*
* @component
* @side client
* @group Camera color=#54a0ff - What the camera sees.
* @group Advanced hidden - What is too near or too far to be shown.
*/
export class Camera3D {
name = "Camera3D";

constructor(
/** Vertical field of view, in degrees. @group Camera */
public fov = 50,
/** Units from the camera under which nothing is shown. @group Advanced */
public near = 0.1,
/** Units from the camera past which nothing is shown. @group Advanced */
public far = 1000,
/** Show the game from this camera. @group Camera */
public active = true,
) {}
}
29 changes: 29 additions & 0 deletions packages/graphics-3d/components/light-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "@nanoforge-dev/graphics-3d/three";

/**
* Lights the scene. Add `draw3D`; place it with `Position3D` (basics-3d).
*
* @remarks
* Until the scene has a light, `draw3D` adds a default one, so shapes are never black.
*
* - `ambient`: lights everything evenly, from nowhere.
* - `directional`: parallel rays like the sun, from its position toward the origin; from
* `(5, 10, 7)` until it has a `Position3D`.
* - `point`: shines all around from its position, like a bulb; it fades with the distance.
*
* @component
* @side client
* @group Light color=#feca57 - Kind, color and strength.
*/
export class Light3D {
name = "Light3D";

constructor(
/** How it shines. @group Light */
public kind: "ambient" | "directional" | "point" = "directional",
/** Any CSS color. @group Light @preset color */
public color = "#ffffff",
/** How strong it is: about 1 for ambient, 3 for the sun, more for a point light. @group Light */
public intensity = 3,
) {}
}
27 changes: 27 additions & 0 deletions packages/graphics-3d/components/object-3d-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Object3D } from "@nanoforge-dev/graphics-3d/three";

/**
* Shows any Three.js object you built: a mesh with your own geometry or material, a group, an
* `InstancedMesh` for many identical objects. Add `draw3D`; `Position3D`, `Rotation3D` and `Scale3D`
* (basics-3d) move it like the shapes.
*
* @remarks
* Removing the component, or the entity, removes the object and disposes its geometries,
* materials and textures.
*
* @example
* `new Object3DComponent(new Mesh(new TorusGeometry(), new MeshStandardMaterial()))`, with the
* classes from `@nanoforge-dev/graphics-3d/three`.
*
* @component
* @side client
* @group Object color=#a29bfe - The Three.js object.
*/
export class Object3DComponent {
name = "Object3DComponent";

constructor(
/** The object to show. @group Object */
public object = new Object3D(),
) {}
}
Loading