👾 bitECS 👾
Functional, tiny, data-oriented, high performance ECS library written using JavaScript TypedArrays.
Features
🔮 Functional
🤏 <3kb
gzipped
🙅♂️ Zero dependencies
💾 Serialization
✨ Queries with modifiers
🌐 Node or browser
🔥 Blazing fast
Install
npm i bitecs
Example
import {
createWorld,
addEntity,
removeEntity,
defineComponent,
addComponent,
removeComponent,
hasComponent,
defineQuery,
Changed,
Not,
enterQuery,
exitQuery,
defineSystem,
defineSerializer,
defineDeserializer,
} from 'bitecs'
const world = createWorld()
const world2 = createWorld()
const { bool, i8, ui8, ui8c, i16, ui16, i32, ui32, f32, f64 } = Types
const Vector2 = { x: f32, y: f32 }
const Position = defineComponent(Vector2)
const Velocity = defineComponent(Vector2)
const Health = defineComponent({ value: ui16 })
const Alive = defineComponent()
const Mapping = defineComponent(new Map())
const movementQuery = defineQuery([Position, Velocity])
const ents = movementQuery(world)
const changedPositionQuery = defineQuery([ Changed(Position) ])
const notVelocityQuery = defineQuery([ Position, Not(Velocity) ])
enterQuery(world, movementQuery, eid => {})
exitQuery(world, movementQuery, eid => {})
const movementSystem = defineSystem(world => {
const ents = movementQuery(world)
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)
const eid2 = addEntity(world)
removeEntity(world, eid2)
addComponent(world, Position, eid)
addComponent(world, Velocity, eid)
removeComponent(world, Velocity, eid)
Position.x[eid] = 1
Position.y[eid] = 2
const pipeline = pipe(
movementSystem,
movementSystem,
movementSystem,
)
movementSystem(world)
pipeline(world)
const serialize = createSerializer()
const deserialize = createDeserializer()
let packet = serialize(world)
deserialize(world, packet)
const serializePositions = createSerializer([Positions])
const deserializePositions = createDeserializer([Positions])
packet = serializePositions(movementQuery(world))
deserializePositions(packet)
const serializeOnlyChangedPositions = createSerializer([Changed(Positions)])
packet = serializeOnlyChangedPositions(movementQuery(world))
Full documentation and feature rich examples can be found here.