0.3.0 • Published 5 years ago

micro-ecs v0.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

micro-ecs

A small ECS framework that works best with TypeScript.

I made this for a game I'm making.

Testing

The unit tests for the project can be run with npm run test.

Example Usages

First, let's set up the world and Model we'll be using:

interface Model {
    name: string,
    age: number
}

const world = new World<Model>();
const baseQuery = query<Model>();

Adding entities

world.addEntities(
    {name: 'john'},
    {age: 10},
    {name: 'both', age: 20}
);

Selecting all entities with certain keys

const nameAndAge = baseQuery.select('name', 'age').forEach(console.log);
world.run(nameAndAge);

Selecting on certain keys and then filtering

const namedAdults = baseQuery
    .select('name', 'age')
    .filter(x => x.age >= 18)
    .forEach(console.log);
world.run(namedAdults);

Selecting just the first matching item

const firstAdult = baseQuery
    .select('age')
    .filter(x => x.age >= 18) 
    .first()
    .forEach(console.log);
world.run(namedAdults);

Deleting a key from all entities

const deleteAge = baseQuery.select('age').map(_ => {age: undefined});
world.run(deleteAge);

Deleting all entities

const deleteAll = baseQuery.select().map(_ => undefined);
world.run(deleteAll);