游戏引擎 活跃维护

bitECS

NateTheGreatt/bitECS

针对 Typescript 的灵活、简约、数据导向的 ECS 库

1521
Stars 标星
117
Forks 分支
27
Watchers 关注
21
Open Issues
TypeScript
主要语言
MPL-2.0
开源协议
4.7 MB
仓库大小
1 个月前
最后推送
一键安装扩展 / 插件指令
dsh plugin --profile web add github:NateTheGreatt/bitECS
git clone https://github.com/NateTheGreatt/bitECS.git
git clone git@github.com:NateTheGreatt/bitECS.git
README.md main

机器翻译正文由机器翻译自项目原始文档(英文),排版经程序统一处理,可能存在偏差,请以原项目仓库为准。

❤ ❤ ❤
bitECS

适应性强、简约、面向数据的 数据库 ECS TypeScript 库。

✨ 功能

bitECS 是一个极简、低偏见且强大的实体组件系统(ECS)库。它提供了一个精简的 API,使开发者能够按照自己的喜好构建架构,在提供灵活性的同时在必要时保持高效。其功能包括:
🔮 简单、声明式 API 🍃 轻量级(~5kb 压缩包最小)
🔍 强大的查询功能 📦 零依赖
🔗 关系实体建模 🧵 线程友好
💾 包含序列化 💖 由爱制作

💿 安装

npm i bitecs

📘 文档

🏁 介绍
💾 序列化
🧵 多线程
📑 API 文档

🕹 示例

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])) {
    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)
})

🔌 使用者

🌟 星标历史

本站来源与版权声明
  • 本文标题:bitECS - 针对 Typescript 的灵活
  • 本文链接:https://www.cn121.com/gameengine/natethegreatt-bitecs.html
  • 原项目:NateTheGreatt/bitECS 版权归原作者 NateTheGreatt 及贡献者所有
  • 收录信息:本站于 2026-10-18 收录本项目,本页所列协议与仓库指标均为收录当时的状态;该日期之后原项目的版本更新与协议变更,本页不作同步。
  • 开源协议:收录时本项目采用 MPL-2.0(查看 LICENSE 原文),本站译文为其衍生内容;使用、修改、分发请以该仓库 LICENSE 原文为准。
  • 站点出处:本文首发于 OneTwoOne,收录自 GitHub 开源项目 NateTheGreatt/bitECS。
  • 翻译说明:本页正文为人工智能生成内容——由机器翻译对原项目 README 初译、经程序校验排版,可能存在错漏,请以原项目文档为准。
  • 引用声明:商业转载、第三方聚合或 AI 检索训练引用时,请务必保留以上来源出处、本文永久链接,以及原项目的版权声明与许可信息。
  • 下架通道:若原项目此后变更或收紧了许可协议、或作者/权利人认为本站的收录方式(译文、排版适配、简介翻译等)超出其授权范围,请通过 xyd3302001@163.com 发送下架通知,并附上项目地址与本页链接。本站核实后将第一时间删除本页内容,或改为不复制原文的目录性收录;署名更正等其他要求可一并提出。