MCPcopy Index your code
hub / github.com/NateTheGreatt/bitECS

github.com/NateTheGreatt/bitECS @0.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.4.0 ↗ · + Follow
188 symbols 572 edges 39 files 18 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

❤ ❤ ❤ bitECS

Version Minzipped Downloads License Discord

Flexible, minimal, data-oriented ECS library for Typescript.

✨ Features

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 (~5kb minzipped) | | 🔍 Powerful querying | 📦 Zero dependencies | | 🔗 Relational entity modeling | 🧵 Thread-friendly | | 💾 Serialization included | 💖 Made with love |

💿 Install

npm i bitecs

📘 Documentation

🏁 Introduction
💾 Serialization
🧵 Multithreading
📑 API Docs

🕹 Example

import {
  createWorld,
  query,
  addEntity,
  removeEntity,
  addComponent,
} from 'bitecs'

// Put components wherever you want
const Health = [] as number[]

const world = createWorld({
  components: {
    // They can be any shape you want
    // SoA:
    Position: { x: [], y: [] },
    Velocity: { x: new Float32Array(1e5), y: new Float32Array(1e5) },
    // AoS:
    Player: [] as { level: number; experience: number; name: string }[]
  },
  time: {
    delta: 0, 
    elapsed: 0, 
    then: performance.now()
  }
})

const { Position, Velocity, Player } = world.components

const eid = addEntity(world)
addComponent(world, eid, Position)
addComponent(world, eid, Velocity)
addComponent(world, eid, Player)
addComponent(world, eid, Health)

// SoA access pattern
Position.x[eid] = 0
Position.y[eid] = 0
Velocity.x[eid] = 1.23
Velocity.y[eid] = 1.23
Health[eid] = 100

// AoS access pattern  
Player[eid] = { level: 1, experience: 0, name: "Hero" }

const movementSystem = (world) => {
  const { Position, Velocity } = world.components

  for (const eid of query(world, [Position, Velocity])) {
    Position.x[eid] += Velocity.x[eid] * world.time.delta
    Position.y[eid] += Velocity.y[eid] * world.time.delta
  }
}

const experienceSystem = (world) => {
  const { Player } = world.components

  for (const eid of query(world, [Player])) {
    // Gain experience over time (1 per second instead of per frame)
    Player[eid].experience += world.time.delta / 1000
    if (Player[eid].experience >= 100) {
      Player[eid].level++
      Player[eid].experience = 0
    }
  }
}

const healthSystem = (world) => {
  for (const eid of query(world, [Health])) {
    if (Health[eid] <= 0) removeEntity(world, eid)
  }
}

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) => {
  timeSystem(world)
  movementSystem(world)
  experienceSystem(world)
  healthSystem(world)
}

// Node environment
setInterval(() => {
  update(world)
}, 1000/60)

// Browser environment
requestAnimationFrame(function animate() {
  update(world)
  requestAnimationFrame(animate)
})

🔌 Used by

🌟 Star History

Star History Chart

Extension points exported contracts — how you extend this code

ISpatialGrid (Interface)
(no doc)
src/utils/threading/SpatialGrid/SpatialGrid.ts
IWorld (Interface)
(no doc)
src/legacy/index.ts
QueryOptions (Interface)
(no doc)
src/core/Query.ts
IUint32SparseSet (Interface)
(no doc)
src/utils/threading/Uint32SparseSet/Uint32SparseSet.ts
IComponentProp (Interface)
(no doc)
src/legacy/index.ts
ComponentData (Interface)
(no doc)
src/core/Component.ts
IComponent (Interface)
(no doc)
src/legacy/index.ts
Observable (Interface)
(no doc)
src/core/utils/Observer.ts

Core symbols most depended-on inside this repo

addEntity
called by 182
src/core/Entity.ts
addComponent
called by 173
src/core/Component.ts
query
called by 120
src/core/Query.ts
createWorld
called by 106
src/core/World.ts
addComponent
called by 76
src/legacy/index.ts
hasComponent
called by 45
src/core/Component.ts
createRelation
called by 44
src/core/Relation.ts
hasComponent
called by 42
src/legacy/index.ts

Shape

Function 177
Interface 9
Enum 2

Languages

TypeScript100%

Modules by API surface

src/core/Query.ts23 symbols
src/core/Hierarchy.ts20 symbols
src/legacy/index.ts18 symbols
src/core/Relation.ts16 symbols
src/utils/threading/SpatialGrid/SpatialGrid.ts15 symbols
src/serialization/SoASerializer.ts13 symbols
src/utils/threading/Uint32SparseSet/Uint32SparseSet.ts11 symbols
src/core/Component.ts11 symbols
src/core/utils/SparseSet.ts8 symbols
src/core/EntityIndex.ts8 symbols
src/serialization/SnapshotSerializer.ts7 symbols
src/core/World.ts6 symbols

For agents

$ claude mcp add bitECS \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact