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
16 changes: 8 additions & 8 deletions .claude/skills/new-ecs-package/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
name: new-ecs-package
description: Add an ECS component or system to one of this repo's registry packages (packages/basics, 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, 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.
---

# New component, system or package

Read `AGENTS.md` first. Reference package: `packages/basics`.
Read `AGENTS.md` first. Reference package: `packages/basics-2d`.

## 0. Pick the package

Expand All @@ -14,8 +14,8 @@ Read `AGENTS.md` first. Reference package: `packages/basics`.
- Put it in the package whose README matches what it does. A new package only for a new group of
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`;
`controls-2d` on `basics`, `physics-2d`; `combat-2d` on `basics`, `graphics-2d`, `physics-2d`.
(`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`.

## 1. A component

Expand Down Expand Up @@ -50,7 +50,7 @@ export class MyThing2D {
- 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:
`packages/basics/components/position-2d.ts`,
`packages/basics-2d/components/position-2d.ts`,
`packages/graphics-2d/components/drawable-rect-2d.ts`.
- State a system writes at runtime (not set in the editor) is a field outside the constructor, with
its doc (`collisions` in `packages/physics-2d/components/hitbox-rect-2d.ts`).
Expand All @@ -63,7 +63,7 @@ functions; the systems already on main are `export function`, and stay as they a
```ts
import type { Context } from "@nanoforge-dev/common";
import type { Registry } from "@nanoforge-dev/ecs";
import { Velocity2D } from "@nanoforge/basics/components/velocity-2d";
import { Velocity2D } from "@nanoforge/basics-2d/components/velocity-2d";

import { MyThing2D } from "../components/my-thing-2d";

Expand Down Expand Up @@ -120,7 +120,7 @@ Nothing in code: the manifest's `contents` already points at `components/` and `
"keywords": ["<new-name>", "2d", "<keyword>"],
"license": "MIT",
"suggestedPlugins": ["@nanoforge/ecs"],
"dependencies": { "@nanoforge/basics": "^2.0.0" },
"dependencies": { "@nanoforge/basics-2d": "^2.0.0" },
"libDependencies": { "@nanoforge-dev/common": "^2.0.0", "@nanoforge-dev/ecs": "^2.0.0" },
"contents": [
{ "kind": "@nanoforge/ecs:component", "path": "components/" },
Expand All @@ -130,7 +130,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`), each at the dependency's current major (`^<its manifest major>.0.0`); `^1.0.0` for
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.
- `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.
Expand Down
21 changes: 11 additions & 10 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ of `packages/` is one package of the NanoForge registry (`@nanoforge/<name>`), i
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` | shared | `Position2D`, `Velocity2D`, `Acceleration2D` and `move2D` |
| `@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 | `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 |

## Stack

Expand Down Expand Up @@ -59,7 +60,7 @@ requests to `main`.

There is no per-item manifest and no `EDITOR_COMPONENT_MANIFEST` / `EDITOR_SYSTEM_MANIFEST`: the
editor reads each item from its `.ts` file (NanoForge-dev/editor
`docs/adr/0003-component-and-system-docs.md`). Reference: `packages/basics`.
`docs/adr/0003-component-and-system-docs.md`). Reference: `packages/basics-2d`.

- **Component:** a class tagged `@component`, with a field `name = "<ClassName>"` (the ECS registry
keys on it) and `public` constructor parameters with defaults. The parameters are the fields of
Expand All @@ -85,7 +86,7 @@ editor reads each item from its `.ts` file (NanoForge-dev/editor
- Inside a package, relative paths: `import { Position2D } from "../components/position-2d"`.
- Another package, by its name and a file path, through the `@nanoforge/*` → `./packages/*` alias
(`tsconfig.json` `paths`, `vitest.config.ts` `resolve.alias`):
`import { Position2D } from "@nanoforge/basics/components/position-2d"`. A project resolves the
`import { Position2D } from "@nanoforge/basics-2d/components/position-2d"`. A project resolves the
same path in `nf_modules/@nanoforge/<name>`.
- Engine libs: `import type { Registry } from "@nanoforge-dev/ecs"`, `type` when only types are
used.
Expand All @@ -94,7 +95,7 @@ editor reads each item from its `.ts` file (NanoForge-dev/editor
to `Context`, and the editor reads an item's required libs from its imports.
- `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": "^2.0.0" }`; `^1.0.0` for a package never released), each engine lib in `libDependencies` at its `core`
`{ "@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`.

Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
for [Engine v2](https://github.com/NanoForge-dev/engine). Each folder of [`packages`](packages) is one
package of the registry:

| Package | Side | What it gives |
| ------------------------------------------------ | ------ | ---------------------------------------------------------------- |
| [`@nanoforge/basics`](packages/basics) | shared | `Position2D`, `Velocity2D`, `Acceleration2D` and `move2D` |
| [`@nanoforge/graphics-2d`](packages/graphics-2d) | client | Rectangles, circles and texts with a color or an image, `draw2D` |
| [`@nanoforge/physics-2d`](packages/physics-2d) | shared | Hitboxes, collisions, gravity, walls and platforms |
| [`@nanoforge/controls-2d`](packages/controls-2d) | client | Move and jump with the keyboard |
| [`@nanoforge/combat-2d`](packages/combat-2d) | both | Health, damage, bullets fired at the mouse |
| Package | Side | What it gives |
| ------------------------------------------------ | ------ | ---------------------------------------------------------------------------------- |
| [`@nanoforge/basics-2d`](packages/basics-2d) | shared | `Position2D`, `Velocity2D`, `Acceleration2D` and `move2D` |
| [`@nanoforge/basics-3d`](packages/basics-3d) | shared | `Position3D`, `Rotation3D`, `Scale3D`, `Velocity3D`, `Acceleration3D` and `move3D` |
| [`@nanoforge/graphics-2d`](packages/graphics-2d) | client | Rectangles, circles and texts with a color or an image, `draw2D` |
| [`@nanoforge/physics-2d`](packages/physics-2d) | shared | Hitboxes, collisions, gravity, walls and platforms |
| [`@nanoforge/controls-2d`](packages/controls-2d) | client | Move and jump with the keyboard |
| [`@nanoforge/combat-2d`](packages/combat-2d) | both | Health, damage, bullets fired at the mouse |

The packages to install in a new project are listed in [Default packages](docs/default-packages.md).

Expand All @@ -50,7 +51,7 @@ Keep the parameters few and named for beginners, with what is less common in an
group. Everything else stays reachable from code (`shape` on drawables, `collisions` on hitboxes).

A package imports another with its name and the path of a file in it:
`import { Position2D } from "@nanoforge/basics/components/position-2d"`.
`import { Position2D } from "@nanoforge/basics-2d/components/position-2d"`.

## Links

Expand Down
10 changes: 5 additions & 5 deletions docs/default-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ install by hand meanwhile.

## Installed by default

| Package | Why |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------- |
| `@nanoforge/basics` | Every game has entities that are somewhere and move. `Position2D` is also what the Scene screen uses to place entities. |
| `@nanoforge/graphics-2d` | Without it nothing is on screen. With `basics`, a beginner places a colored rectangle, gives it a velocity, and sees it move. |
| Package | Why |
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| `@nanoforge/basics-2d` | Every game has entities that are somewhere and move. `Position2D` is also what the Scene screen uses to place entities. |
| `@nanoforge/graphics-2d` | Without it nothing is on screen. With `basics-2d`, a beginner places a colored rectangle, gives it a velocity, and sees it move. |

Together they replace the `Position2D`, `DrawableCircle2D`, `draw2D` and `move2D` that the CLI's
project template copies into `src/` today.
Expand All @@ -30,7 +30,7 @@ Systems run in the order they are added. With every package:

1. `moveControl2D`, `jumpControl2D`, `shootControl2D`: read the keys
2. `applyGravity2D`
3. `move2D`
3. `move2D`, `move3D`
4. `detectCollisions2D`
5. `blockOnStaticBodies2D`, `applyDamage2D`, `destroyOnHit2D`
6. `draw2D`: last, so the screen shows where entities ended up
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @nanoforge/basics
# @nanoforge/basics-2d

Where an entity is and how it moves, in 2D.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "package",
"name": "@nanoforge/basics",
"name": "@nanoforge/basics-2d",
"version": "2.0.0",
"description": "Where an entity is and how it moves: position, velocity and acceleration in 2D.",
"keywords": ["basics", "2d", "position", "velocity", "acceleration", "movement"],
Expand Down
11 changes: 11 additions & 0 deletions packages/basics-3d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# @nanoforge/basics-3d

Where an entity is and how it moves in 3D: plain numbers, no Three.js, usable from server and
shared code.

- `Position3D`: units along the x, y and z axes.
- `Rotation3D`: degrees around the x, y and z axes.
- `Scale3D`: size multiplier on each axis, 1 by default.
- `Velocity3D`: units per second.
- `Acceleration3D`: units per second, per second.
- `move3D`: applies acceleration to velocity, then velocity to position.
20 changes: 20 additions & 0 deletions packages/basics-3d/components/acceleration-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* How fast the velocity of an entity changes, in units per second, per second. Needs
* `Velocity3D` and the `move3D` system.
*
* @component
* @side shared
* @group Acceleration color=#f0a33a - Units per second, per second.
*/
export class Acceleration3D {
name = "Acceleration3D";

constructor(
/** Added to the x velocity every second. @group Acceleration @preset vector.x */
public x = 0,
/** Added to the y velocity every second. @group Acceleration @preset vector.y */
public y = 0,
/** Added to the z velocity every second. @group Acceleration @preset vector.z */
public z = 0,
) {}
}
22 changes: 22 additions & 0 deletions packages/basics-3d/components/position-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Where an entity is in the 3D world, in units along each axis.
*
* @remarks
* Plain numbers, without Three.js: server and shared code can use it.
*
* @component
* @side shared
* @group Position color=#4aa3ff - Units along each axis.
*/
export class Position3D {
name = "Position3D";

constructor(
/** Units along the x axis. @group Position @preset vector.x */
public x = 0,
/** Units along the y axis (up). @group Position @preset vector.y */
public y = 0,
/** Units along the z axis. @group Position @preset vector.z */
public z = 0,
) {}
}
25 changes: 25 additions & 0 deletions packages/basics-3d/components/rotation-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* How an entity is turned in the 3D world, in degrees around each axis.
*
* @remarks
* Degrees, not radians. Plain numbers, without Three.js: server and shared code can use it.
*
* @example
* `new Rotation3D(0, 90, 0)` turns an entity a quarter turn around the vertical axis.
*
* @component
* @side shared
* @group Rotation color=#b07aff - Degrees around each axis.
*/
export class Rotation3D {
name = "Rotation3D";

constructor(
/** Degrees around the x axis. @group Rotation @preset vector.x */
public x = 0,
/** Degrees around the y axis (up). @group Rotation @preset vector.y */
public y = 0,
/** Degrees around the z axis. @group Rotation @preset vector.z */
public z = 0,
) {}
}
25 changes: 25 additions & 0 deletions packages/basics-3d/components/scale-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* How big an entity is in the 3D world, as a multiplier on each axis: 1 keeps its size.
*
* @remarks
* Plain numbers, without Three.js: server and shared code can use it.
*
* @example
* `new Scale3D(2, 1, 1)` makes an entity twice as wide.
*
* @component
* @side shared
* @group Scale color=#36c5a0 - Size multiplier on each axis.
*/
export class Scale3D {
name = "Scale3D";

constructor(
/** Multiplier along the x axis. @group Scale @preset vector.x */
public x = 1,
/** Multiplier along the y axis (up). @group Scale @preset vector.y */
public y = 1,
/** Multiplier along the z axis. @group Scale @preset vector.z */
public z = 1,
) {}
}
23 changes: 23 additions & 0 deletions packages/basics-3d/components/velocity-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* How fast an entity moves in the 3D world, in units per second. Needs `Position3D` and the
* `move3D` system.
*
* @example
* `new Velocity3D(0, 2, 0)` moves 2 units up every second.
*
* @component
* @side shared
* @group Velocity color=#36c5a0 - Units per second.
*/
export class Velocity3D {
name = "Velocity3D";

constructor(
/** Units per second along the x axis. @group Velocity @preset vector.x */
public x = 0,
/** Units per second along the y axis (up). @group Velocity @preset vector.y */
public y = 0,
/** Units per second along the z axis. @group Velocity @preset vector.z */
public z = 0,
) {}
}
22 changes: 22 additions & 0 deletions packages/basics-3d/nanoforge.manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "package",
"name": "@nanoforge/basics-3d",
"description": "Where an entity is and how it moves in 3D: position, rotation, scale, velocity and acceleration, as plain numbers.",
"keywords": [
"basics",
"3d",
"position",
"rotation",
"scale",
"velocity",
"acceleration",
"movement"
],
"license": "MIT",
"suggestedPlugins": ["@nanoforge/ecs"],
"libDependencies": { "@nanoforge-dev/common": "^2.0.0", "@nanoforge-dev/ecs": "^2.0.0" },
"contents": [
{ "kind": "@nanoforge/ecs:component", "path": "components/" },
{ "kind": "@nanoforge/ecs:system", "path": "systems/" }
]
}
28 changes: 28 additions & 0 deletions packages/basics-3d/systems/move-3d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Context } from "@nanoforge-dev/common";
import type { Registry } from "@nanoforge-dev/ecs";

import { Acceleration3D } from "../components/acceleration-3d";
import { Position3D } from "../components/position-3d";
import { Velocity3D } from "../components/velocity-3d";

/**
* Moves entities: their `Acceleration3D` changes their `Velocity3D`, then their `Velocity3D`
* changes their `Position3D`, by the time elapsed since the last frame.
*
* @system
* @side shared
*/
export const move3D = (registry: Registry, ctx: Context) => {
const seconds = ctx.app.delta / 1000;

registry.getZipper([Acceleration3D, Velocity3D]).forEach(({ Acceleration3D, Velocity3D }) => {
Velocity3D.x += Acceleration3D.x * seconds;
Velocity3D.y += Acceleration3D.y * seconds;
Velocity3D.z += Acceleration3D.z * seconds;
});
registry.getZipper([Position3D, Velocity3D]).forEach(({ Position3D, Velocity3D }) => {
Position3D.x += Velocity3D.x * seconds;
Position3D.y += Velocity3D.y * seconds;
Position3D.z += Velocity3D.z * seconds;
});
};
2 changes: 1 addition & 1 deletion packages/combat-2d/nanoforge.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"suggestedPlugins": ["@nanoforge/ecs"],
"dependencies": {
"@nanoforge/basics": "^2.0.0",
"@nanoforge/basics-2d": "^2.0.0",
"@nanoforge/graphics-2d": "^2.0.0",
"@nanoforge/physics-2d": "^2.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/combat-2d/systems/shoot-control-2d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Context } from "@nanoforge-dev/common";
import type { Registry } from "@nanoforge-dev/ecs";
import "@nanoforge-dev/input";
import { Position2D } from "@nanoforge/basics/components/position-2d";
import { Velocity2D } from "@nanoforge/basics/components/velocity-2d";
import { Position2D } from "@nanoforge/basics-2d/components/position-2d";
import { Velocity2D } from "@nanoforge/basics-2d/components/velocity-2d";
import { DrawableCircle2D } from "@nanoforge/graphics-2d/components/drawable-circle-2d";
import { HitboxCircle2D } from "@nanoforge/physics-2d/components/hitbox-circle-2d";
import { HitboxRect2D } from "@nanoforge/physics-2d/components/hitbox-rect-2d";
Expand Down
2 changes: 1 addition & 1 deletion packages/controls-2d/nanoforge.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"keywords": ["controls", "2d", "keyboard", "input", "player", "jump", "platformer"],
"license": "MIT",
"suggestedPlugins": ["@nanoforge/ecs"],
"dependencies": { "@nanoforge/basics": "^2.0.0", "@nanoforge/physics-2d": "^2.0.0" },
"dependencies": { "@nanoforge/basics-2d": "^2.0.0", "@nanoforge/physics-2d": "^2.0.0" },
"libDependencies": {
"@nanoforge-dev/common": "^2.0.0",
"@nanoforge-dev/ecs": "^2.0.0",
Expand Down
Loading
Loading