Outcome: you’ll run a tiny simulation loop where entities with Position + Velocity move over time, using World, spawn, add, query, addSystem, and update(dt).
ECS is a way to build simulations where entities are IDs, components are data, and systems are functions that iterate entities with specific components.
import{World,WorldApi}from"archetype-ecs-lib";// 1) Components = data (any class can be a component type)classPosition{constructor(publicx=0,publicy=0){}}classVelocity{constructor(publicx=0,publicy=0){}}// 2) Create a World (owns entities, components, systems)constworld=newWorld();// 3) Spawn an entity and add componentsconste=world.spawnMany(newPosition(0,0,0),newVelocity(2,0)// 2 units/sec along x)// 4) Add a system (runs each update)world.addSystem((w,dt)=>{for(const{e,c1:pos,c2:vel}ofw.query(Position,Velocity)){pos.x+=vel.x*dt;pos.y+=vel.y*dt;}});// 5) Run a small simulation loop (60 frames)constdt=1/60;for(letframe=1;frame<=60;frame++){world.update(dt);// Read back Position and print itconstpos=world.get(e,Position)!;if(frame%10===0){console.log(`frame ${frame}: x=${pos.x.toFixed(2)} y=${pos.y.toFixed(2)}`);}}