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
14 changes: 13 additions & 1 deletion modules/ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## About

`@nanoforge-dev/ecs` is NanoForge's built-in entity-component-system library, backed by a WebAssembly registry (compiled from C++). It ships two subpath entry points — `@nanoforge-dev/ecs/client` and `@nanoforge-dev/ecs/server` — each loading its own WASM build (browser-targeted vs Node-targeted) behind the same `EcsLibrary` API, so a client bundle never pulls in the server build or vice versa.
`@nanoforge-dev/ecs` is NanoForge's built-in entity-component-system library, backed by a WebAssembly registry (compiled from C++). It ships two subpath entry points — `@nanoforge-dev/ecs/client` and `@nanoforge-dev/ecs/server` — each loading its own WASM build (browser-targeted vs Node-targeted) behind the same `EcsLibrary` API, so a client bundle never pulls in the server build or vice versa. The root entry point, `@nanoforge-dev/ecs`, only exposes the shared types (`Registry`, `Component`, `System`, `Entity`, `SparseArray`, `Context`, `EcsContextApi`) and pulls in no WASM binary, so it's safe to import from code that targets both client and server.

## Installation

Expand Down Expand Up @@ -46,6 +46,18 @@ import { EcsLibrary } from "@nanoforge-dev/ecs/server";
app.use(new EcsLibrary());
```

Shared types like `Registry`, `Component`, and `System` are available from the root entry point, regardless of which platform you're targeting:

```ts
import type { Component, Context, Registry, System } from "@nanoforge-dev/ecs";

const Position: Component = { name: "Position" };

const gravity: System = (registry: Registry, ctx: Context) => {
// ...
};
```

Once registered, `Context.ecs.registry` gives every other library access to the entity/component registry:

```ts
Expand Down
16 changes: 13 additions & 3 deletions modules/ecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@
"files": [
"dist"
],
"main": "./dist/client/index.cjs",
"module": "./dist/client/index.js",
"types": "./dist/client/index.d.cts",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.cts",
"exports": {
".": {
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"./client": {
"require": {
"types": "./dist/client/index.d.cts",
Expand Down
6 changes: 6 additions & 0 deletions modules/ecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "./shared/context-augmentation";

export type { Context } from "@nanoforge-dev/common";
export type { EcsContextApi } from "./shared/ecs-context.type";
export type { EditorComponentManifest, EditorSystemManifest } from "./shared/editor-manifest.type";
export type { Component, Entity, Registry, SparseArray, System } from "../lib/web/libecs";
142 changes: 142 additions & 0 deletions modules/ecs/src/shared/editor-manifest.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* * Editor Component Element Defaults
* Basic fields for Editor Component Element
* @param Type - Type of the element
* @param Default - Type of the element's default value
*/
type ECSElementDefaults<Type, Default> = {
/**
* Type of the element
*/
type: Type;

/**
* Name of the element
*/
name: string;

/**
* Description of the element
*/
description?: string;

/**
* Example of the element
*/
example?: Default;

/**
* Is the element optional
* @default false
*/
optional?: boolean;

/**
* Default value of the element
* Force optional to true if set
*/
default?: Default;
};

/**
* * Editor Component String Element
* Type for string element
*/
type ECSStringElement = {
/**
* Values allowed for the element
*/
enum?: string[] | Record<string, any>;
} & ECSElementDefaults<"string", string>;

/**
* * Editor Component Number Element
* Type for number element
*/
type ECSNumberElement = ECSElementDefaults<"number", number>;

/**
* * Editor Component Boolean Element
* Type for boolean element
*/
type ECSBooleanElement = ECSElementDefaults<"boolean", boolean>;

/**
* * Editor Component Asset Element
* Type for asset element
*/
type ECSAssetElement = ECSElementDefaults<"asset", string>;

/**
* * Editor Component Array Element
* Type for array element
*/
type ECSArrayElement = {
/**
* Items of the array
*/
items: ECSElement;
} & ECSElementDefaults<"array", any[]>;

/**
* * Editor Component Object Element
* Type for object element
*/
type ECSObjectElement = {
/**
* Properties of the object
*/
properties: Record<string, ECSElement>;
} & ECSElementDefaults<"object", object>;

/**
* * Editor Component Element
* Type for component element
*/
type ECSElement =
| ECSStringElement
| ECSNumberElement
| ECSBooleanElement
| ECSAssetElement
| ECSArrayElement
| ECSObjectElement;

/**
* Manifest for a component to be used in the NanoForge Editor
*/
export type EditorComponentManifest = {
/**
* Displayed name of the component
*/
name: string;

/**
* Description of the component
*/
description?: string;

/**
* Parameters of the component
*/
params: ECSElement[];
};

/**
* Manifest for a system to be used in the NanoForge Editor
*/
export type EditorSystemManifest = {
/**
* Displayed name of the system
*/
name: string;

/**
* Description of the system
*/
description?: string;

/**
* Component names needed by the system
*/
dependencies: string[];
};
1 change: 1 addition & 0 deletions modules/ecs/tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createTsdownConfig } from "../../tsdown.config";

export default [
createTsdownConfig(),
createTsdownConfig({ entry: "src/client/index.ts", outDir: "dist/client" }),
createTsdownConfig({ entry: "src/server/index.ts", outDir: "dist/server" }),
];