Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 3x 14x 14x 62x 62x 62x 62x 62x 112x 62x 4x 2341x 13x 13x 13x 3x 3x 3x 10x 10x 10x 10x 10x 10x 4x 4x 4x 216x 1697x 1697x 3747x 523x 1697x 3245x 3245x | /**
* @packageDocumentation
*
* Lightweight entity-component-system (ECS) manager. Provides type-safe
* component access and entity queries without framework dependencies.
*
* ## Design
*
* - `Entity` — a plain object with `id`, `version`, and a `components` bag.
* - `EntityComponentType<T>` — a typed handle for a component. Encodes the
* component type at compile time and stores the runtime string key.
* - `EntityManager` — holds entities in a `Map<string, Entity>` and provides
* query methods. Mutable in place.
*
* ## Usage
*
* ```ts
* const Position = new EntityComponentType<{ x: number; y: number }>("position");
* const manager = EntityManager.from(entities);
*
* const withPos = manager.entitiesWithComponent(Position);
* const pos = manager.getComponent(entity, Position); // { x, y } | undefined
* ```
*/
import { Type, type Static, type TSchema } from "typebox";
import { uuidv7 } from "uuidv7";
import { atom } from "nanostores";
// ---------------------------------------------------------------------------
// Entity
// ---------------------------------------------------------------------------
export const EntitySchema = Type.Object(
{
id: Type.String({
description: "UUIDv7 unique identifier, immutable once created.",
}),
version: Type.String({
description: "UUIDv7 revision timestamp. Updated on every edit.",
}),
components: Type.Record(Type.String(), Type.Any(), {
description:
"ECS-style component bag. Keys are component names. Values are component-specific objects.",
}),
},
{
additionalProperties: false,
description: "An entity is a uniquely identified object with a versioned bag of components.",
},
);
export type Entity = Static<typeof EntitySchema>;
export { EntityBuilder, entity } from "./builder";
// ---------------------------------------------------------------------------
// EntityComponentType
// ---------------------------------------------------------------------------
/**
* A typed handle for an ECS component. Carries both the runtime string key
* and the TypeScript type of the component data.
*/
export class EntityComponentType<T extends TSchema> {
key: string;
schema: T;
constructor(key: string, schema: T) {
this.key = key;
this.schema = schema;
}
}
// ---------------------------------------------------------------------------
// EntityManager
// ---------------------------------------------------------------------------
export class EntityManager {
private entities = new Map<string, Entity>();
private _mutationVersion = 1;
$mutationVersion = atom<number>(1);
static from(array: Entity[]): EntityManager {
const manager = new EntityManager();
for (const entity of array) {
manager.entities.set(entity.id, entity);
}
return manager;
}
toArray(): Entity[] {
return Array.from(this.entities.values());
}
get(id: string): Entity | undefined {
return this.entities.get(id);
}
insert(entity: Entity): void {
this.entities.set(entity.id, entity);
this._mutationVersion++;
this.$mutationVersion.set(this._mutationVersion);
}
remove(id: string): void {
this.entities.delete(id);
this._mutationVersion++;
this.$mutationVersion.set(this._mutationVersion);
}
/**
* Deletes an entity by stripping all its components and bumping its version.
* The entity remains in the manager but is invisible to component queries.
*/
delete(id: string): void {
const entity = this.entities.get(id);
Iif (!entity) return;
entity.components = {};
entity.version = uuidv7();
this._mutationVersion++;
this.$mutationVersion.set(this._mutationVersion);
}
/**
* Restores a previously deleted entity by re-inserting its full snapshot.
*/
restore(entity: Entity): void {
this.entities.set(entity.id, entity);
this._mutationVersion++;
this.$mutationVersion.set(this._mutationVersion);
}
getMutationVersion(): number {
return this._mutationVersion;
}
/**
* Returns all entities that carry the given component.
*/
entitiesWithComponent<T extends TSchema>(component: EntityComponentType<T>): Entity[] {
const result: Entity[] = [];
for (const entity of this.entities.values()) {
if (component.key in entity.components) {
result.push(entity);
}
}
return result;
}
/**
* Returns the component data for an entity, or `undefined` if the entity
* does not carry this component.
*/
getComponent<T extends TSchema>(
entity: Entity,
component: EntityComponentType<T>,
): Static<T> | undefined {
const value = entity.components[component.key];
return value as Static<T> | undefined;
}
}
|