@esengine/ecs-framework 2.1.27 → 2.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,438 +1,465 @@
1
- # ECS Framework
2
-
3
- [![CI](https://github.com/esengine/ecs-framework/workflows/CI/badge.svg)](https://github.com/esengine/ecs-framework/actions)
4
- [![npm version](https://badge.fury.io/js/%40esengine%2Fecs-framework.svg)](https://badge.fury.io/js/%40esengine%2Fecs-framework)
5
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
-
7
- TypeScript ECS (Entity-Component-System) 框架,专为游戏开发设计。
8
-
9
- > 🤔 **什么是 ECS?** 不熟悉 ECS 架构?建议先阅读 [ECS 架构基础](docs/concepts-explained.md#ecs-架构基础) 了解核心概念
10
-
11
- ## 特性
12
-
13
- - 🔧 **完整的 TypeScript 支持** - 强类型检查和代码提示
14
- - 📡 **[类型安全事件系统](docs/concepts-explained.md#事件系统)** - 事件装饰器和异步事件处理
15
- - 🔍 **[查询系统](docs/concepts-explained.md#实体管理)** - 流式 API 和智能缓存
16
- - ⚡ **[性能优化](docs/concepts-explained.md#性能优化技术)** - 组件索引、Archetype 系统、脏标记
17
- - 🎯 **[实体管理器](docs/concepts-explained.md#实体管理)** - 统一的实体生命周期管理
18
- - 🧰 **调试工具** - 内置性能监控和调试信息
19
-
20
- > 📖 **不熟悉这些概念?** 查看我们的 [技术概念详解](docs/concepts-explained.md) 了解它们的作用和应用场景
21
-
22
- ## 安装
23
-
24
- ```bash
25
- npm install @esengine/ecs-framework
26
- ```
27
-
28
- ## 快速开始
29
-
30
- ### 基础设置
31
-
32
- ```typescript
33
- import { Core, Scene, Entity, Component, EntitySystem } from '@esengine/ecs-framework';
34
-
35
- // 创建核心实例 - 使用配置对象(推荐)
36
- const core = Core.create({
37
- debug: true, // 启用调试模式
38
- enableEntitySystems: true, // 启用实体系统
39
- debugConfig: { // 可选:调试配置
40
- enabled: true,
41
- websocketUrl: 'ws://localhost:8080',
42
- autoReconnect: true,
43
- updateInterval: 1000,
44
- channels: {
45
- entities: true,
46
- systems: true,
47
- performance: true,
48
- components: true,
49
- scenes: true
50
- }
51
- }
52
- });
53
-
54
- // 简化创建 - 向后兼容(仍然支持)
55
- const core2 = Core.create(true); // 等同于 { debug: true, enableEntitySystems: true }
56
-
57
- // 创建场景
58
- const scene = new Scene();
59
- Core.scene = scene;
60
- ```
61
-
62
- ### 定义组件
63
-
64
- ```typescript
65
- class PositionComponent extends Component {
66
- constructor(public x: number = 0, public y: number = 0) {
67
- super();
68
- }
69
- }
70
-
71
- class VelocityComponent extends Component {
72
- constructor(public dx: number = 0, public dy: number = 0) {
73
- super();
74
- }
75
- }
76
-
77
- class HealthComponent extends Component {
78
- constructor(
79
- public maxHealth: number = 100,
80
- public currentHealth: number = 100
81
- ) {
82
- super();
83
- }
84
- }
85
- ```
86
-
87
- ### 创建实体
88
-
89
- ```typescript
90
- // 基础实体创建
91
- const player = scene.createEntity("Player");
92
- player.addComponent(new PositionComponent(100, 100));
93
- player.addComponent(new VelocityComponent(5, 0));
94
- player.addComponent(new HealthComponent(100, 100));
95
-
96
- // 批量创建实体
97
- const enemies = scene.createEntities(50, "Enemy");
98
- ```
99
-
100
- ### 创建系统
101
-
102
- ```typescript
103
- class MovementSystem extends EntitySystem {
104
- constructor() {
105
- super();
106
- }
107
-
108
- public process(entities: Entity[]) {
109
- for (const entity of entities) {
110
- const position = entity.getComponent(PositionComponent);
111
- const velocity = entity.getComponent(VelocityComponent);
112
-
113
- if (position && velocity) {
114
- position.x += velocity.dx;
115
- position.y += velocity.dy;
116
- }
117
- }
118
- }
119
- }
120
-
121
- // 添加系统到场景
122
- scene.addEntityProcessor(new MovementSystem());
123
- ```
124
-
125
- ### 游戏循环
126
-
127
- ECS框架需要在游戏引擎的更新循环中调用:
128
-
129
- ```typescript
130
- // 统一的API:传入deltaTime
131
- Core.update(deltaTime);
132
- ```
133
-
134
- **不同平台的集成示例:**
135
-
136
- ```typescript
137
- // Laya引擎
138
- Laya.timer.frameLoop(1, this, () => {
139
- const deltaTime = Laya.timer.delta / 1000; // 转换为秒
140
- Core.update(deltaTime);
141
- });
142
-
143
- // Cocos Creator
144
- update(deltaTime: number) {
145
- Core.update(deltaTime);
146
- }
147
-
148
- // 原生浏览器环境
149
- let lastTime = 0;
150
- function gameLoop(currentTime: number) {
151
- const deltaTime = lastTime > 0 ? (currentTime - lastTime) / 1000 : 0.016;
152
- lastTime = currentTime;
153
- Core.update(deltaTime);
154
- requestAnimationFrame(gameLoop);
155
- }
156
- requestAnimationFrame(gameLoop);
157
- ```
158
-
159
- ## 实体管理器
160
-
161
- EntityManager 提供了统一的实体管理接口:
162
-
163
- ```typescript
164
- import { EntityManager } from '@esengine/ecs-framework';
165
-
166
- const entityManager = new EntityManager();
167
-
168
- // 流式查询 API
169
- const results = entityManager
170
- .query()
171
- .withAll(PositionComponent, VelocityComponent)
172
- .withNone(HealthComponent)
173
- .withTag(1)
174
- .execute();
175
-
176
- // 批量操作(使用Scene的方法)
177
- const bullets = scene.createEntities(100, "bullet");
178
-
179
- // 按标签查询
180
- const enemies = entityManager.getEntitiesByTag(2);
181
- ```
182
-
183
- ## 事件系统
184
-
185
- ### [基础事件](docs/concepts-explained.md#类型安全事件)
186
-
187
- 类型安全的事件系统,编译时检查事件名和数据类型。
188
-
189
- ```typescript
190
- import { EventBus, ECSEventType } from '@esengine/ecs-framework';
191
-
192
- const eventBus = entityManager.eventBus;
193
-
194
- // 监听预定义事件
195
- eventBus.onEntityCreated((data) => {
196
- console.log(`实体创建: ${data.entityName}`);
197
- });
198
-
199
- eventBus.onComponentAdded((data) => {
200
- console.log(`组件添加: ${data.componentType}`);
201
- });
202
-
203
- // 自定义事件
204
- eventBus.emit('player:death', { playerId: 123, reason: 'fall' });
205
- ```
206
-
207
- ### [事件装饰器](docs/concepts-explained.md#事件装饰器)
208
-
209
- 使用装饰器语法自动注册事件监听器,减少样板代码。
210
-
211
- ```typescript
212
- import { EventHandler, ECSEventType } from '@esengine/ecs-framework';
213
-
214
- class GameSystem {
215
- @EventHandler(ECSEventType.ENTITY_DESTROYED)
216
- onEntityDestroyed(data: EntityDestroyedEventData) {
217
- console.log('实体销毁:', data.entityName);
218
- }
219
-
220
- @EventHandler('player:levelup')
221
- onPlayerLevelUp(data: { playerId: number; newLevel: number }) {
222
- console.log(`玩家 ${data.playerId} 升级到 ${data.newLevel} 级`);
223
- }
224
- }
225
- ```
226
-
227
- ## 性能优化
228
-
229
- ### [组件索引](docs/concepts-explained.md#组件索引系统)
230
-
231
- 通过建立索引避免线性搜索,将查询复杂度从 O(n) 降低到 O(1)。
232
-
233
- ```typescript
234
- // 使用Scene的查询系统进行组件索引
235
- const querySystem = scene.querySystem;
236
-
237
- // 查询具有特定组件的实体
238
- const entitiesWithPosition = querySystem.queryAll(PositionComponent).entities;
239
- const entitiesWithVelocity = querySystem.queryAll(VelocityComponent).entities;
240
-
241
- // 性能统计
242
- const stats = querySystem.getStats();
243
- console.log('查询效率:', stats.hitRate);
244
- ```
245
-
246
- **索引类型选择:**
247
- - **哈希索引** - 适合稳定的、大量的组件(如位置、生命值)
248
- - **位图索引** - 适合频繁变化的组件(如Buff、状态)
249
-
250
- > 📋 详细选择指南参见 [索引类型选择指南](docs/concepts-explained.md#索引类型选择指南)
251
-
252
- ### [Archetype 系统](docs/concepts-explained.md#archetype-系统)
253
-
254
- 将具有相同组件组合的实体分组,减少查询时的组件检查开销。
255
-
256
- ```typescript
257
- // 使用查询系统的Archetype功能
258
- const querySystem = scene.querySystem;
259
-
260
- // 查询统计
261
- const stats = querySystem.getStats();
262
- console.log('缓存命中率:', stats.hitRate);
263
- ```
264
-
265
- ### [脏标记系统](docs/concepts-explained.md#脏标记系统)
266
-
267
- 追踪数据变化,只处理发生改变的实体,避免不必要的计算。
268
-
269
- ```typescript
270
- // 脏标记通过组件系统自动管理
271
- // 组件变化时会自动标记为脏数据
272
-
273
- // 查询系统会自动处理脏标记优化
274
- const movingEntities = scene.querySystem.queryAll(PositionComponent, VelocityComponent);
275
- ```
276
-
277
- > 💡 **不确定何时使用这些优化?** 查看 [性能优化建议](docs/concepts-explained.md#性能建议) 了解适用场景
278
-
279
- ## API 参考
280
-
281
- ### 核心类
282
-
283
- | 类 | 描述 |
284
- |---|---|
285
- | `Core` | 框架核心管理类 |
286
- | `Scene` | 场景容器,管理实体和系统 |
287
- | `Entity` | 实体对象,包含组件集合 |
288
- | `Component` | 组件基类 |
289
- | `EntitySystem` | 系统基类 |
290
- | `EntityManager` | 实体管理器 |
291
-
292
- ### 查询 API
293
-
294
- ```typescript
295
- entityManager
296
- .query()
297
- .withAll(...components) // 包含所有指定组件
298
- .withAny(...components) // 包含任意指定组件
299
- .withNone(...components) // 不包含指定组件
300
- .withTag(tag) // 包含指定标签
301
- .withoutTag(tag) // 不包含指定标签
302
- .execute() // 执行查询
303
- ```
304
-
305
- ### 事件类型
306
-
307
- ```typescript
308
- enum ECSEventType {
309
- ENTITY_CREATED = 'entity:created',
310
- ENTITY_DESTROYED = 'entity:destroyed',
311
- COMPONENT_ADDED = 'component:added',
312
- COMPONENT_REMOVED = 'component:removed',
313
- SYSTEM_ADDED = 'system:added',
314
- SYSTEM_REMOVED = 'system:removed'
315
- }
316
- ```
317
-
318
- ## 与其他框架对比
319
-
320
- | 特性 | @esengine/ecs-framework | bitECS | Miniplex |
321
- |------|-------------------------|--------|----------|
322
- | TypeScript 支持 | ✅ 原生支持 | ✅ 完整支持 | ✅ 原生支持 |
323
- | 事件系统 | ✅ 内置+装饰器 | ❌ 需自己实现 | ✅ 响应式 |
324
- | 查询系统 | ✅ 流式 API | ✅ 函数式 | ✅ 响应式 |
325
- | 实体管理器 | ✅ 统一接口 | ❌ 低级 API | ✅ 高级接口 |
326
- | 性能优化 | ✅ 多重优化 | ✅ 极致性能 | ✅ React 优化 |
327
- | JavaScript引擎集成 | ✅ 专为JS引擎设计 | ✅ 通用设计 | ⚠️ 主要 React |
328
- | 可视化调试工具 | ✅ [Cocos插件](https://store.cocos.com/app/detail/7823) | ❌ 无官方工具 | ✅ React DevTools |
329
-
330
- **选择指南:**
331
- - 选择本框架:需要完整的游戏开发工具链和中文社区支持
332
- - 选择 bitECS:需要极致性能和最小化设计
333
- - 选择 Miniplex:主要用于 React 应用开发
334
-
335
- ## 项目结构
336
-
337
- ```
338
- ecs-framework/
339
- ├── src/
340
- │ ├── ECS/ # ECS 核心系统
341
- │ │ ├── Core/ # 核心管理器
342
- │ │ ├── Systems/ # 系统类型
343
- │ │ └── Utils/ # ECS 工具
344
- │ ├── Types/ # TypeScript接口定义
345
- │ └── Utils/ # 通用工具
346
- ├── docs/ # 文档
347
- └── scripts/ # 构建脚本
348
- ```
349
-
350
- ## 文档
351
-
352
- ### 🎯 新手入门
353
- - **[📖 新手教程完整指南](docs/beginner-tutorials.md)** - 完整学习路径,从零开始 **强烈推荐**
354
- - **[🚀 快速入门](docs/getting-started.md)** - 详细的入门教程,包含Laya/Cocos/Node.js集成指南 **平台集成必读**
355
- - 💡 **Cocos Creator用户特别提示**:我们提供[专用调试插件](https://store.cocos.com/app/detail/7823),支持可视化ECS调试
356
- - [🧠 技术概念详解](docs/concepts-explained.md) - 通俗易懂的技术概念解释 ⭐ **推荐新手阅读**
357
- - [🎯 位掩码使用指南](docs/bitmask-guide.md) - 位掩码概念、原理和高级使用技巧
358
- - [💡 使用场景示例](docs/use-cases.md) - 不同类型游戏的具体应用案例
359
- - [🔧 框架类型系统](docs/concepts-explained.md#框架类型系统) - TypeScript接口设计和使用指南
360
-
361
- ### 📚 核心功能
362
- - [🎭 实体管理指南](docs/entity-guide.md) - 实体的创建和使用方法
363
- - [🧩 组件设计指南](docs/component-design-guide.md) - 如何设计高质量组件 ⭐ **设计必读**
364
- - [⚙️ 系统详解指南](docs/system-guide.md) - 四种系统类型的详细使用
365
- - [🎬 场景管理指南](docs/scene-management-guide.md) - 场景切换和数据管理
366
- - [⏰ 定时器系统指南](docs/timer-guide.md) - 定时器的完整使用方法
367
-
368
- ### API 参考
369
- - [核心 API 参考](docs/core-concepts.md) - 完整的 API 使用说明
370
- - [实体基础指南](docs/entity-guide.md) - 实体的基本概念和操作
371
- - [EntityManager 指南](docs/entity-manager-example.md) - 高性能查询和批量操作
372
- - [事件系统指南](docs/event-system-example.md) - 事件系统完整用法
373
- - [查询系统指南](docs/query-system-usage.md) - 查询系统使用方法
374
-
375
- ### 性能相关
376
- - [性能优化指南](docs/performance-optimization.md) - 性能优化技术和策略
377
-
378
- ## 构建
379
-
380
- ```bash
381
- # 安装依赖
382
- npm install
383
-
384
- # 构建项目
385
- npm run build
386
-
387
- # 监听模式
388
- npm run build:watch
389
-
390
- # 清理构建文件
391
- npm run clean
392
-
393
- # 重新构建
394
- npm run rebuild
395
- ```
396
-
397
- ## 性能监控
398
-
399
- 框架提供内置性能统计:
400
-
401
- ```typescript
402
- // 场景统计
403
- const sceneStats = scene.getStats();
404
- console.log('性能统计:', {
405
- 实体数量: sceneStats.entityCount,
406
- 系统数量: sceneStats.processorCount
407
- });
408
-
409
- // 查询系统统计
410
- const queryStats = scene.querySystem.getStats();
411
- console.log('查询统计:', {
412
- 缓存命中率: queryStats.hitRate + '%',
413
- 查询次数: queryStats.queryCount
414
- });
415
- ```
416
-
417
- ## 扩展库
418
-
419
- - [路径寻找库](https://github.com/esengine/ecs-astar) - A*、BFS、Dijkstra 算法
420
- - [AI 系统](https://github.com/esengine/BehaviourTree-ai) - 行为树、效用 AI
421
-
422
- ## 社区
423
-
424
- - QQ 群:[ecs游戏框架交流](https://jq.qq.com/?_wv=1027&k=29w1Nud6)
425
- - GitHub:[提交 Issue](https://github.com/esengine/ecs-framework/issues)
426
-
427
- ## 贡献
428
-
429
- 欢迎提交 Pull Request 和 Issue!
430
-
431
- ### 开发要求
432
-
433
- - Node.js >= 14.0.0
434
- - TypeScript >= 4.0.0
435
-
436
- ## 许可证
437
-
1
+ # ECS Framework
2
+
3
+ [![CI](https://github.com/esengine/ecs-framework/workflows/CI/badge.svg)](https://github.com/esengine/ecs-framework/actions)
4
+ [![npm version](https://badge.fury.io/js/%40esengine%2Fecs-framework.svg)](https://badge.fury.io/js/%40esengine%2Fecs-framework)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ TypeScript ECS (Entity-Component-System) 框架,专为游戏开发设计。
8
+
9
+ > 🤔 **什么是 ECS?** 不熟悉 ECS 架构?建议先阅读 [ECS 架构基础](docs/concepts-explained.md#ecs-架构基础) 了解核心概念
10
+
11
+ ## 特性
12
+
13
+ - 🔧 **完整的 TypeScript 支持** - 强类型检查和代码提示
14
+ - 📡 **[类型安全事件系统](docs/concepts-explained.md#事件系统)** - 事件装饰器和异步事件处理
15
+ - 🔍 **[查询系统](docs/concepts-explained.md#实体管理)** - 流式 API 和智能缓存
16
+ - ⚡ **[性能优化](docs/concepts-explained.md#性能优化技术)** - 组件索引、Archetype 系统、脏标记
17
+ - 🚀 **[SoA 存储优化](docs/soa-storage-guide.md)** - 大规模实体的向量化批量操作和内存优化
18
+ - 🎯 **[实体管理器](docs/concepts-explained.md#实体管理)** - 统一的实体生命周期管理
19
+ - 🧰 **调试工具** - 内置性能监控和调试信息
20
+
21
+ > 📖 **不熟悉这些概念?** 查看我们的 [技术概念详解](docs/concepts-explained.md) 了解它们的作用和应用场景
22
+
23
+ ## 安装
24
+
25
+ ```bash
26
+ npm install @esengine/ecs-framework
27
+ ```
28
+
29
+ ## 快速开始
30
+
31
+ ### 基础设置
32
+
33
+ ```typescript
34
+ import { Core, Scene, Entity, Component, EntitySystem } from '@esengine/ecs-framework';
35
+
36
+ // 创建核心实例 - 使用配置对象(推荐)
37
+ const core = Core.create({
38
+ debug: true, // 启用调试模式
39
+ enableEntitySystems: true, // 启用实体系统
40
+ debugConfig: { // 可选:调试配置
41
+ enabled: true,
42
+ websocketUrl: 'ws://localhost:8080',
43
+ autoReconnect: true,
44
+ updateInterval: 1000,
45
+ channels: {
46
+ entities: true,
47
+ systems: true,
48
+ performance: true,
49
+ components: true,
50
+ scenes: true
51
+ }
52
+ }
53
+ });
54
+
55
+ // 简化创建 - 向后兼容(仍然支持)
56
+ const core2 = Core.create(true); // 等同于 { debug: true, enableEntitySystems: true }
57
+
58
+ // 创建场景
59
+ const scene = new Scene();
60
+ Core.scene = scene;
61
+ ```
62
+
63
+ ### 定义组件
64
+
65
+ ```typescript
66
+ class PositionComponent extends Component {
67
+ constructor(public x: number = 0, public y: number = 0) {
68
+ super();
69
+ }
70
+ }
71
+
72
+ class VelocityComponent extends Component {
73
+ constructor(public dx: number = 0, public dy: number = 0) {
74
+ super();
75
+ }
76
+ }
77
+
78
+ class HealthComponent extends Component {
79
+ constructor(
80
+ public maxHealth: number = 100,
81
+ public currentHealth: number = 100
82
+ ) {
83
+ super();
84
+ }
85
+ }
86
+ ```
87
+
88
+ ### SoA 高性能组件 (大规模场景推荐)
89
+
90
+ 对于需要处理大量实体的场景,可以使用 SoA 存储优化获得显著性能提升:
91
+
92
+ ```typescript
93
+ import { Component, EnableSoA, Float32, Int32 } from '@esengine/ecs-framework';
94
+
95
+ // 启用 SoA 优化的高性能组件
96
+ @EnableSoA
97
+ class OptimizedTransformComponent extends Component {
98
+ @Float32 public x: number = 0;
99
+ @Float32 public y: number = 0;
100
+ @Float32 public rotation: number = 0;
101
+ }
102
+
103
+ @EnableSoA
104
+ class ParticleComponent extends Component {
105
+ @Float32 public velocityX: number = 0;
106
+ @Float32 public velocityY: number = 0;
107
+ @Int32 public lifeTime: number = 1000;
108
+ }
109
+ ```
110
+
111
+ > ⚠️ **使用建议**: SoA 优化适用于大规模场景和批量操作。小规模应用请使用普通组件以避免不必要的复杂度。详见 [SoA 存储优化指南](docs/soa-storage-guide.md)
112
+
113
+ ### 创建实体
114
+
115
+ ```typescript
116
+ // 基础实体创建
117
+ const player = scene.createEntity("Player");
118
+ player.addComponent(new PositionComponent(100, 100));
119
+ player.addComponent(new VelocityComponent(5, 0));
120
+ player.addComponent(new HealthComponent(100, 100));
121
+
122
+ // 批量创建实体
123
+ const enemies = scene.createEntities(50, "Enemy");
124
+ ```
125
+
126
+ ### 创建系统
127
+
128
+ ```typescript
129
+ class MovementSystem extends EntitySystem {
130
+ constructor() {
131
+ super();
132
+ }
133
+
134
+ public process(entities: Entity[]) {
135
+ for (const entity of entities) {
136
+ const position = entity.getComponent(PositionComponent);
137
+ const velocity = entity.getComponent(VelocityComponent);
138
+
139
+ if (position && velocity) {
140
+ position.x += velocity.dx;
141
+ position.y += velocity.dy;
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ // 添加系统到场景
148
+ scene.addEntityProcessor(new MovementSystem());
149
+ ```
150
+
151
+ ### 游戏循环
152
+
153
+ ECS框架需要在游戏引擎的更新循环中调用:
154
+
155
+ ```typescript
156
+ // 统一的API:传入deltaTime
157
+ Core.update(deltaTime);
158
+ ```
159
+
160
+ **不同平台的集成示例:**
161
+
162
+ ```typescript
163
+ // Laya引擎
164
+ Laya.timer.frameLoop(1, this, () => {
165
+ const deltaTime = Laya.timer.delta / 1000; // 转换为秒
166
+ Core.update(deltaTime);
167
+ });
168
+
169
+ // Cocos Creator
170
+ update(deltaTime: number) {
171
+ Core.update(deltaTime);
172
+ }
173
+
174
+ // 原生浏览器环境
175
+ let lastTime = 0;
176
+ function gameLoop(currentTime: number) {
177
+ const deltaTime = lastTime > 0 ? (currentTime - lastTime) / 1000 : 0.016;
178
+ lastTime = currentTime;
179
+ Core.update(deltaTime);
180
+ requestAnimationFrame(gameLoop);
181
+ }
182
+ requestAnimationFrame(gameLoop);
183
+ ```
184
+
185
+ ## 实体管理器
186
+
187
+ EntityManager 提供了统一的实体管理接口:
188
+
189
+ ```typescript
190
+ import { EntityManager } from '@esengine/ecs-framework';
191
+
192
+ const entityManager = new EntityManager();
193
+
194
+ // 流式查询 API
195
+ const results = entityManager
196
+ .query()
197
+ .withAll(PositionComponent, VelocityComponent)
198
+ .withNone(HealthComponent)
199
+ .withTag(1)
200
+ .execute();
201
+
202
+ // 批量操作(使用Scene的方法)
203
+ const bullets = scene.createEntities(100, "bullet");
204
+
205
+ // 按标签查询
206
+ const enemies = entityManager.getEntitiesByTag(2);
207
+ ```
208
+
209
+ ## 事件系统
210
+
211
+ ### [基础事件](docs/concepts-explained.md#类型安全事件)
212
+
213
+ 类型安全的事件系统,编译时检查事件名和数据类型。
214
+
215
+ ```typescript
216
+ import { EventBus, ECSEventType } from '@esengine/ecs-framework';
217
+
218
+ const eventBus = entityManager.eventBus;
219
+
220
+ // 监听预定义事件
221
+ eventBus.onEntityCreated((data) => {
222
+ console.log(`实体创建: ${data.entityName}`);
223
+ });
224
+
225
+ eventBus.onComponentAdded((data) => {
226
+ console.log(`组件添加: ${data.componentType}`);
227
+ });
228
+
229
+ // 自定义事件
230
+ eventBus.emit('player:death', { playerId: 123, reason: 'fall' });
231
+ ```
232
+
233
+ ### [事件装饰器](docs/concepts-explained.md#事件装饰器)
234
+
235
+ 使用装饰器语法自动注册事件监听器,减少样板代码。
236
+
237
+ ```typescript
238
+ import { EventHandler, ECSEventType } from '@esengine/ecs-framework';
239
+
240
+ class GameSystem {
241
+ @EventHandler(ECSEventType.ENTITY_DESTROYED)
242
+ onEntityDestroyed(data: EntityDestroyedEventData) {
243
+ console.log('实体销毁:', data.entityName);
244
+ }
245
+
246
+ @EventHandler('player:levelup')
247
+ onPlayerLevelUp(data: { playerId: number; newLevel: number }) {
248
+ console.log(`玩家 ${data.playerId} 升级到 ${data.newLevel} 级`);
249
+ }
250
+ }
251
+ ```
252
+
253
+ ## 性能优化
254
+
255
+ ### [组件索引](docs/concepts-explained.md#组件索引系统)
256
+
257
+ 通过建立索引避免线性搜索,将查询复杂度从 O(n) 降低到 O(1)。
258
+
259
+ ```typescript
260
+ // 使用Scene的查询系统进行组件索引
261
+ const querySystem = scene.querySystem;
262
+
263
+ // 查询具有特定组件的实体
264
+ const entitiesWithPosition = querySystem.queryAll(PositionComponent).entities;
265
+ const entitiesWithVelocity = querySystem.queryAll(VelocityComponent).entities;
266
+
267
+ // 性能统计
268
+ const stats = querySystem.getStats();
269
+ console.log('查询效率:', stats.hitRate);
270
+ ```
271
+
272
+ **索引类型选择:**
273
+ - **哈希索引** - 适合稳定的、大量的组件(如位置、生命值)
274
+ - **位图索引** - 适合频繁变化的组件(如Buff、状态)
275
+
276
+ > 📋 详细选择指南参见 [索引类型选择指南](docs/concepts-explained.md#索引类型选择指南)
277
+
278
+ ### [Archetype 系统](docs/concepts-explained.md#archetype-系统)
279
+
280
+ 将具有相同组件组合的实体分组,减少查询时的组件检查开销。
281
+
282
+ ```typescript
283
+ // 使用查询系统的Archetype功能
284
+ const querySystem = scene.querySystem;
285
+
286
+ // 查询统计
287
+ const stats = querySystem.getStats();
288
+ console.log('缓存命中率:', stats.hitRate);
289
+ ```
290
+
291
+ ### [脏标记系统](docs/concepts-explained.md#脏标记系统)
292
+
293
+ 追踪数据变化,只处理发生改变的实体,避免不必要的计算。
294
+
295
+ ```typescript
296
+ // 脏标记通过组件系统自动管理
297
+ // 组件变化时会自动标记为脏数据
298
+
299
+ // 查询系统会自动处理脏标记优化
300
+ const movingEntities = scene.querySystem.queryAll(PositionComponent, VelocityComponent);
301
+ ```
302
+
303
+ > 💡 **不确定何时使用这些优化?** 查看 [性能优化建议](docs/concepts-explained.md#性能建议) 了解适用场景
304
+
305
+ ## API 参考
306
+
307
+ ### 核心类
308
+
309
+ | | 描述 |
310
+ |---|---|
311
+ | `Core` | 框架核心管理类 |
312
+ | `Scene` | 场景容器,管理实体和系统 |
313
+ | `Entity` | 实体对象,包含组件集合 |
314
+ | `Component` | 组件基类 |
315
+ | `EntitySystem` | 系统基类 |
316
+ | `EntityManager` | 实体管理器 |
317
+
318
+ ### 查询 API
319
+
320
+ ```typescript
321
+ entityManager
322
+ .query()
323
+ .withAll(...components) // 包含所有指定组件
324
+ .withAny(...components) // 包含任意指定组件
325
+ .withNone(...components) // 不包含指定组件
326
+ .withTag(tag) // 包含指定标签
327
+ .withoutTag(tag) // 不包含指定标签
328
+ .execute() // 执行查询
329
+ ```
330
+
331
+ ### 事件类型
332
+
333
+ ```typescript
334
+ enum ECSEventType {
335
+ ENTITY_CREATED = 'entity:created',
336
+ ENTITY_DESTROYED = 'entity:destroyed',
337
+ COMPONENT_ADDED = 'component:added',
338
+ COMPONENT_REMOVED = 'component:removed',
339
+ SYSTEM_ADDED = 'system:added',
340
+ SYSTEM_REMOVED = 'system:removed'
341
+ }
342
+ ```
343
+
344
+ ## 与其他框架对比
345
+
346
+ | 特性 | @esengine/ecs-framework | bitECS | Miniplex |
347
+ |------|-------------------------|--------|----------|
348
+ | TypeScript 支持 | ✅ 原生支持 | ✅ 完整支持 | ✅ 原生支持 |
349
+ | 事件系统 | ✅ 内置+装饰器 | ❌ 需自己实现 | ✅ 响应式 |
350
+ | 查询系统 | ✅ 流式 API | ✅ 函数式 | ✅ 响应式 |
351
+ | 实体管理器 | ✅ 统一接口 | ❌ 低级 API | ✅ 高级接口 |
352
+ | 性能优化 | ✅ 多重优化 | ✅ 极致性能 | ✅ React 优化 |
353
+ | JavaScript引擎集成 | 专为JS引擎设计 | ✅ 通用设计 | ⚠️ 主要 React |
354
+ | 可视化调试工具 | ✅ [Cocos插件](https://store.cocos.com/app/detail/7823) | 无官方工具 | ✅ React DevTools |
355
+
356
+ **选择指南:**
357
+ - 选择本框架:需要完整的游戏开发工具链和中文社区支持
358
+ - 选择 bitECS:需要极致性能和最小化设计
359
+ - 选择 Miniplex:主要用于 React 应用开发
360
+
361
+ ## 项目结构
362
+
363
+ ```
364
+ ecs-framework/
365
+ ├── src/
366
+ │ ├── ECS/ # ECS 核心系统
367
+ │ │ ├── Core/ # 核心管理器
368
+ │ │ ├── Systems/ # 系统类型
369
+ │ │ └── Utils/ # ECS 工具
370
+ │ ├── Types/ # TypeScript接口定义
371
+ │ └── Utils/ # 通用工具
372
+ ├── docs/ # 文档
373
+ └── scripts/ # 构建脚本
374
+ ```
375
+
376
+ ## 文档
377
+
378
+ ### 🎯 新手入门
379
+ - **[📖 新手教程完整指南](docs/beginner-tutorials.md)** - 完整学习路径,从零开始 ⭐ **强烈推荐**
380
+ - **[🚀 快速入门](docs/getting-started.md)** - 详细的入门教程,包含Laya/Cocos/Node.js集成指南 ⭐ **平台集成必读**
381
+ - 💡 **Cocos Creator用户特别提示**:我们提供[专用调试插件](https://store.cocos.com/app/detail/7823),支持可视化ECS调试
382
+ - [🧠 技术概念详解](docs/concepts-explained.md) - 通俗易懂的技术概念解释 ⭐ **推荐新手阅读**
383
+ - [🎯 位掩码使用指南](docs/bitmask-guide.md) - 位掩码概念、原理和高级使用技巧
384
+ - [💡 使用场景示例](docs/use-cases.md) - 不同类型游戏的具体应用案例
385
+ - [🔧 框架类型系统](docs/concepts-explained.md#框架类型系统) - TypeScript接口设计和使用指南
386
+
387
+ ### 📚 核心功能
388
+ - [🎭 实体管理指南](docs/entity-guide.md) - 实体的创建和使用方法
389
+ - [🧩 组件设计指南](docs/component-design-guide.md) - 如何设计高质量组件 ⭐ **设计必读**
390
+ - [⚙️ 系统详解指南](docs/system-guide.md) - 四种系统类型的详细使用
391
+ - [🎬 场景管理指南](docs/scene-management-guide.md) - 场景切换和数据管理
392
+ - [⏰ 定时器系统指南](docs/timer-guide.md) - 定时器的完整使用方法
393
+
394
+ ### API 参考
395
+ - [核心 API 参考](docs/core-concepts.md) - 完整的 API 使用说明
396
+ - [实体基础指南](docs/entity-guide.md) - 实体的基本概念和操作
397
+ - [EntityManager 指南](docs/entity-manager-example.md) - 高性能查询和批量操作
398
+ - [事件系统指南](docs/event-system-example.md) - 事件系统完整用法
399
+ - [查询系统指南](docs/query-system-usage.md) - 查询系统使用方法
400
+
401
+ ### 性能相关
402
+ - [性能优化指南](docs/performance-optimization.md) - 性能优化技术和策略
403
+ - [SoA 存储优化指南](docs/soa-storage-guide.md) - 大规模实体系统的高级性能优化 ⭐ **大规模项目推荐**
404
+
405
+ ## 构建
406
+
407
+ ```bash
408
+ # 安装依赖
409
+ npm install
410
+
411
+ # 构建项目
412
+ npm run build
413
+
414
+ # 监听模式
415
+ npm run build:watch
416
+
417
+ # 清理构建文件
418
+ npm run clean
419
+
420
+ # 重新构建
421
+ npm run rebuild
422
+ ```
423
+
424
+ ## 性能监控
425
+
426
+ 框架提供内置性能统计:
427
+
428
+ ```typescript
429
+ // 场景统计
430
+ const sceneStats = scene.getStats();
431
+ console.log('性能统计:', {
432
+ 实体数量: sceneStats.entityCount,
433
+ 系统数量: sceneStats.processorCount
434
+ });
435
+
436
+ // 查询系统统计
437
+ const queryStats = scene.querySystem.getStats();
438
+ console.log('查询统计:', {
439
+ 缓存命中率: queryStats.hitRate + '%',
440
+ 查询次数: queryStats.queryCount
441
+ });
442
+ ```
443
+
444
+ ## 扩展库
445
+
446
+ - [路径寻找库](https://github.com/esengine/ecs-astar) - A*、BFS、Dijkstra 算法
447
+ - [AI 系统](https://github.com/esengine/BehaviourTree-ai) - 行为树、效用 AI
448
+
449
+ ## 社区
450
+
451
+ - QQ 群:[ecs游戏框架交流](https://jq.qq.com/?_wv=1027&k=29w1Nud6)
452
+ - GitHub:[提交 Issue](https://github.com/esengine/ecs-framework/issues)
453
+
454
+ ## 贡献
455
+
456
+ 欢迎提交 Pull Request 和 Issue!
457
+
458
+ ### 开发要求
459
+
460
+ - Node.js >= 14.0.0
461
+ - TypeScript >= 4.0.0
462
+
463
+ ## 许可证
464
+
438
465
  [MIT](LICENSE)