@nanoforge-dev/ecs-server
v1.0.0
Published
NanoForge Engine - ECS Server
Readme
About
@nanoforge-dev/ecs-server is a wrapper of @nanoforge-dev/ecs-lib for server-side usage.
Installation
Node.js 24.11.0 or newer is required.
npm install @nanoforge-dev/ecs-server
yarn add @nanoforge-dev/ecs-server
pnpm add @nanoforge-dev/ecs-server
bun add @nanoforge-dev/ecs-serverExample usage
Initialize the library in your main file.
import { type IRunOptions } from "@nanoforge-dev/common";
import { NanoforgeFactory } from "@nanoforge-dev/core";
import { ECSServerLibrary } from "@nanoforge-dev/ecs-server";
export async function main(options: IRunServerOptions) {
const app = NanoforgeFactory.createServer();
const ecs = new ECSServerLibrary();
app.useComponentSystem(ecs);
await app.init(options);
const registry = ecs.registry;
const entity = registry.spawnEntity();
registry.addComponent(entity, new ExampleComponent("foo", 42));
registry.addSystem(exampleSystem);
await app.run();
}With component
export class ExampleComponent {
name = this.constructor.name;
constructor(
public paramA: string,
public paramB: number,
public paramC: boolean = false,
) {}
get foo() {
return "bar";
}
get paramAOrDefault() {
return this.paramC ? this.paramA : "default";
}
addOne() {
this.paramB += 1;
}
}And system
export const exampleSystem = (registry: Registry, ctx: Context) => {
const entities = registry.getZipper([ExampleComponent]);
entities.forEach((entity) => {
if (entity.ExampleComponent.paramA === "end") {
ctx.app.setIsRunning(false);
return;
}
if (entity.ExampleComponent.paramB === 0) entity.ExampleComponent.paramA = "end";
if (entity.ExampleComponent.paramB >= 0)
entity.ExampleComponent.paramB = entity.ExampleComponent.paramB - 1;
});
};Links
Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
documentation.
See the contribution guide if you'd like to submit a PR.
Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to ask questions in discussions.
