@babylonjsmarket/ecs

中文文档
0.9.22发布于 2026/11/04MITTypeScriptES Module零依赖近 30 天下载 1868

Entity-Component-System framework with Babylon and Three renderer adapters

原始使用文档
文档目录11 节
  1. Where this fits
  2. Install
  3. Entry points
  4. Quick start
  5. Concepts
  6. Components are data, Systems are logic
  7. Events, not direct references
  8. Renderer adapters are a thin port
  9. Race detection in development
  10. See also
  11. Notes

@babylonjsmarket/ecs

The Entity-Component-System framework that powers BabylonJS Market games — extracted from the in-house CodeLibrary and published as a standalone runtime so any project can depend on it.

Where this fits

                      @babylonjsmarket/ecs              ← you are here
                                ▲
                                │
        ┌───────────────────────┼───────────────────────┐
        │                       │                       │
@babylonjsmarket/arcade   create-arcade games    your game
(curated components)     (scaffolded projects)

@babylonjsmarket/ecs is the foundation. It owns:

  • Core ECS — Component, Entity, System, World, EventBus, SceneLoader, SaveLoad, RaceDetector.
  • The renderer contract — RendererAdapter is the interface that decouples your game logic from any specific 3D engine.
  • Babylon and Three.js adapters — BabylonAdapter (Havok physics, full scene support) and ThreeAdapter (with a pluggable JS physics integrator). Both live in this package so they evolve together, but each is behind a subpath so only the renderer you actually use gets pulled in.
  • Test infrastructure — MockRendererAdapter for unit tests, and a runMechanicContract battery for proving a mechanic obeys the framework contract.

Everything is renderer-agnostic at the entity/system layer. Pick Babylon if you want first-class Havok physics and the full Babylon stack; pick Three if you want a leaner runtime and you're comfortable wiring physics yourself.

Install

npm install @babylonjsmarket/ecs

The core entry has zero runtime dependencies. Renderer adapters are optional peer dependencies — install only the one you use:

# For BabylonJS
npm install @babylonjs/core @babylonjs/loaders @babylonjs/havok

# For Three.js
npm install three

Entry points

ImportContentsPeer deps
@babylonjsmarket/ecsCore: Component, Entity, System, World, EventBus, SceneLoader, SaveLoad, RaceDetector, RendererAdapter interface, MockRendererAdapter, withRaceTrackingnone
@babylonjsmarket/ecs/babylonBabylonAdapter@babylonjs/core, @babylonjs/loaders, @babylonjs/havok
@babylonjsmarket/ecs/threeThreeAdapter (accepts an injectable physicsFactory)three
@babylonjsmarket/ecs/renderer-typesThe full RendererAdapter type surface — tuple Vec3, handle types, spec interfacesnone
@babylonjsmarket/ecs/testingrunMechanicContract and related vitest helpersvitest

Quick start

import { World, Component, System, type ISystemQuery } from '@babylonjsmarket/ecs';
import { BabylonAdapter } from '@babylonjsmarket/ecs/babylon';

class Position extends Component {
  x = 0;
  y = 0;
  z = 0;
}

class MoveSystem extends System {
  query: ISystemQuery = { required: [Position] };
  onUpdate(dt: number) {
    for (const entity of this.entities) {
      const p = entity.get(Position)!;
      p.x += dt;
    }
  }
}

const renderer = new BabylonAdapter();
await renderer.init(canvas);

const world = new World({ renderer });
world.addSystem(new MoveSystem(world.eventBus));
const player = world.createEntity('Player');
player.add(new Position());

renderer.startLoop((dt) => world.update(dt));

Concepts

Components are data, Systems are logic

Components extend the Component base class and hold state. Systems extend System, declare a query (required components, excluded components, required tags), and implement onUpdate(dt). The World matches entities to systems and runs them each frame.

Events, not direct references

Systems publish via eventBus.emit(name, data) and subscribe via this.listen(name, cb). This keeps systems decoupled — PlayerInput can fire MOVE events without knowing or caring that Movement, Animation, or CameraShake will listen.

Since 0.4.0, the System base class provides listen(), listenOnce(), and listenForEntity() — auto-disposed subscriptions whose lifetime is tied to the system. Subscribe in onInitialize() and the base class unsubscribes everything when the system is removed from the World; no manual unsubscribe bookkeeping. Raw eventBus.on(name, cb) remains the primitive for non-System code.

Renderer adapters are a thin port

The RendererAdapter interface is what every system uses to talk to the 3D engine. Adapters return opaque handles (MeshHandle, LightHandle, CameraHandle, …) that only the adapter that created them can interpret. This is what lets the same MoveSystem run unchanged under Babylon, Three, or the MockRendererAdapter used in unit tests.

Race detection in development

If you pass { detectRaces: true } to the World constructor, the renderer adapter is wrapped in a Proxy that records every handle mutation. When two systems write to the same handle in the same frame, you get a stack trace pointing at the conflict. Off in production.

See also

  • @babylonjsmarket/arcade — A curated set of 14 reusable components (mesh, camera, lights, input, physics, scoring, animation) built on this framework. Start here if you want batteries included.
  • @babylonjsmarket/create-arcade — CLI scaffolder for new arcade-style games. Sets up a project that uses both packages above.
  • BabylonJS Market — Marketplace for additional components, full courses, and example games.

Notes

  • ESM only. "type": "module".
  • Built with TypeScript; ships .d.ts declarations.
  • Debug viz panels (Solid.js) are not shipped from this package — they live in the consuming application or @babylonjsmarket/arcade/viz.

版本与发布记录

@babylonjsmarket/ecs 共发布 54 个版本,首次发布于 2026/05/20;过去 12 个月发布了 54 个版本。

版本发布日期距今
0.9.22latest2026/11/044 天前
0.9.212026/11/026 天前
0.9.202026/10/2514 天前
0.9.192026/10/1524 天前
0.9.182026/10/1029 天前
0.9.172026/10/1029 天前

关于 @babylonjsmarket/ecs 的常见问题

@babylonjsmarket/ecs 是什么?

Entity-Component-System framework with Babylon and Three renderer adapters

如何安装 @babylonjsmarket/ecs?

在项目目录运行 npm install @babylonjsmarket/ecs,或使用 pnpm add / yarn add / bun add。

@babylonjsmarket/ecs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@babylonjsmarket/ecs 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@babylonjsmarket/ecs 可以商用吗?

@babylonjsmarket/ecs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@babylonjsmarket/ecs 的下载量有多少?

根据 npm 官方下载统计,@babylonjsmarket/ecs 在 2026/10/08 – 2026/11/06 这 30 天内被下载 1,868 次,日均约 62 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@babylonjsmarket/ecs 还在维护吗?

最新版本 0.9.22 发布于 2026/11/04(4 天前);至今共发布 54 个版本,其中过去 12 个月发布了 54 个;首次发布于 2026/05/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。