flock-ecs
flock-ecs copied to clipboard
🐦🐦🐦 A simple, but powerful ECS written in TypeScript.
⚠️ This Project is Unmaintained
After many rewrites and finally finding a pattern I like, I realized that using TypedArrays is the way forward for a performant ECS in JS/TS. There are some patterns and such that were required to do it that are solved in this branch.
I recommend using this: bitECS. They are maintaining it better than I would and it is basically exactly what I was converging towards for Flock anyways. Plus, Phaser4 is using bitECS.
An entity component system (ECS) created with TypeScript in mind.
Features
- No dependencies
- Very good TypeScript typings
- Simple, but powerful API
- Designed for performance
- Struct of Arrays for storing entity component values
- Entities are queryable by:
- Component
- Absence of Component
- Added Entity
- Removed Entity
- Systems can have 1 or more queries
Roadmap
Install
npm install flock-ecs
const flock = require("flock-ecs");
//or
import * as flock from "flock-ecs";
API Docs
Examples
Simple
import * as flock from 'flock-ecs';
const world = new flock.World();
const Position = new flock.Component(
() => ({
x: 0,
y: 0,
})
);
world.registerComponent(Position);
const logSystem = new flock.System(
(entities) => {
entities.forEach(entity => {
const position = entity.getComponent(Position)!;
console.log(`{ x: ${position.value.x}, y: ${position.value.y} }`);
});
},
[ Position ],
);
for (let i=0; i<10; i++) {
const entity = world.createEntity();
entity.addComponent(Position, { x: Math.random() * 100, y: Math.random() * 100 });
}
logSystem.run(world);
world.maintain();
Boids
Development
This repo uses Yarn workspaces, so make sure you're using yarn instead of npm.
To build the library in watch mode:
yarn workspace flock-ecs dev
And then to live build one of the examples, in another terminal:
yarn workspace boids start
To run tests:
yarn test
To run tests in watch mode:
yarn test:dev