👾 bitECS 👾
Functional, tiny, data-oriented, high performance ECS library written using JavaScript TypedArrays.
Features
Install
npm i bitecs
Example
import {
createWorld,
registerComponent,
registerComponents,
defineComponent,
defineQuery,
enterQuery,
exitQuery,
defineSystem,
addComponent,
removeComponent,
addEntity,
removeEntity,
pipe,
Types,
} from 'bitecs'
const world = createWorld()
const { f32 } = Types
const Vector2 = { x: f32, y: f32 }
const Position = defineComponent(Vector2)
const Velocity = defineComponent(Vector2)
const Health = defineComponent(f32)
const Alive = defineComponent()
registerComponents(world, [Position, Velocity, Health])
registerComponent(world, Alive)
const movementQuery = defineQuery([Position, Velocity])
enterQuery(world, movementQuery, eid => {})
exitQuery(world, movementQuery, eid => {})
const movementSystem = defineSystem(movementQuery, ents => {
for (let i = 0; i < ents.length; i++) {
const eid = ents[i];
Position.x[eid] += Velocity.x[eid]
Position.y[eid] += Velocity.y[eid]
}
})
const eid = addEntity(world)
addComponent(world, Position, eid)
addComponent(world, Velocity, eid)
Velocity.x[eid] = 1
Velocity.y[eid] = 2
const pipeline = pipe(
movementSystem,
movementSystem,
movementSystem,
)
movementSystem(world)
pipeline(world)
Full documentation and feature rich examples can be found here.