Flexible, minimal, data-oriented ECS library for Typescript.
bitECS
is a minimal, less opinionated, and powerful Entity Component System (ECS) library. It provides a lean API that enables developers to build their architecture to their liking, offering flexibility while maintaining efficiency where needed. Features include:
🔮 Simple, declarative API | 🍃 Lightweight (<4kb minzipped) |
🔍 Powerful querying | 📦 Zero dependencies |
🔗 Relational entity modeling | 🧵 Thread-friendly |
💾 Serialization included | 💖 Made with love |
npm i bitecs
🏁 Introduction |
💾 Serialization |
🧵 Multithreading |
📑 API Docs |
import {
createWorld,
query,
addEntity,
addComponent,
} from 'bitecs'
const world = createWorld({
components: {
Position: { x: [], y: [] },
Velocity: { x: new Float32Array(1e5), y: new Float32Array(1e5) },
Health: []
},
time: {
delta: 0,
elapsed: 0,
then: performance.now()
}
})
const { Position, Velocity, Health } = world.components
const eid = addEntity(world)
addComponent(world, eid, Position)
addComponent(world, eid, Velocity)
Position.x[eid] = 0
Position.y[eid] = 0
Velocity.x[eid] = 1.23
Velocity.y[eid] = 1.23
Health[eid] = 100
const movementSystem = (world) => {
const { Position, Velocity, time } = world.components
for (const eid of query(world, [Position, Velocity])) {
Position.x[eid] += Velocity.x[eid] * time.delta
Position.y[eid] += Velocity.y[eid] * time.delta
}
}
const timeSystem = (world) => {
const { time } = world
const now = performance.now()
const delta = now - time.then
time.delta = delta
time.elapsed += delta
time.then = now
}
const update = (world) => {
movementSystem(world)
timeSystem(world)
}
// Node environment
setInterval(() => {
update(world)
}, 1000/60)
// Browser environment
requestAnimationFrame(function animate() {
update(world)
requestAnimationFrame(animate)
})
Microbenchmarks should be taken with a grain of salt. To get a feel for performance possibilities in real scenarios, see the demos.