Skip to content

Coverage

Archetype ECS Lib

A tiny archetype based ECS (Entity Component System) for TypeScript.

This documentation is split into 4 parts :


Install

NPM package available here

1
npm i archetype-ecs-lib

Quick start

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { World, Schedule } from "archetype-ecs-lib";

class Position { constructor(public x = 0, public y = 0) {} }
class Velocity { constructor(public x = 0, public y = 0) {} }

const world = new World();

// Spawn immediately
const e = world.spawn();
world.add(e, Position, new Position(0, 0));
world.add(e, Velocity, new Velocity(1, 0));

// A simple system
world.addSystem((w) => {
  for (const { e, c1: pos, c2: vel } of w.query(Position, Velocity)) {
    pos.x += vel.x * dt;
    pos.y += vel.y * dt;

    // Defer structural changes safely
    if (pos.x > 10) w.cmd().despawn(e);
  }
});

world.update(1 / 60);

Note: SystemFn is typed as (world: WorldApi, dt) => void.
Checkout the tutorials for more!


Notes & limitations

  • This is intentionally minimal: no parallelism, no borrow-checking, no automatic conflict detection.
  • Query results use c1/c2/... fields for stability and speed; you can wrap this in helpers if you prefer tuple returns.
  • TypeId assignment is process-local and based on constructor identity (WeakMap).

License

This code is distributed under the terms and conditions of the MIT license.