@esengine/ecs-framework 2.1.5 → 2.1.7

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
@@ -3,18 +3,17 @@
3
3
  [![npm version](https://badge.fury.io/js/%40esengine%2Fecs-framework.svg)](https://badge.fury.io/js/%40esengine%2Fecs-framework)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
5
 
6
- 一个轻量级的 TypeScript ECS(Entity-Component-System)框架,专为小游戏开发设计,适用于 Laya、Cocos 等游戏引擎。
6
+ 一个专业级的 TypeScript ECS(Entity-Component-System)框架,采用现代化架构设计,专为高性能游戏开发打造。
7
7
 
8
- ## ✨ 特性
8
+ ## ✨ 核心特性
9
9
 
10
- - 🚀 **轻量级 ECS 架构** - 基于实体组件系统,提供清晰的代码结构
11
- - **高性能** - 实体创建速度可达64万实体/秒,支持大规模实体管理
12
- - 🎯 **智能优化** - 组件对象池、位掩码优化器、延迟索引更新等性能优化技术
13
- - 📡 **事件系统** - 内置 Emitter 事件发射器,支持类型安全的事件管理
14
- - **定时器系统** - 完整的定时器管理,支持延迟和重复任务
15
- - 🔍 **查询系统** - 基于位掩码的高性能实体查询,支持批量操作
16
- - 🛠️ **性能监控** - 内置性能监控工具,帮助优化游戏性能
17
- - 🔧 **批量操作** - 支持批量实体创建、组件添加等高效操作
10
+ - 🏗️ **现代化 ECS 架构** - 完整的实体组件系统,提供清晰的代码结构
11
+ - 📡 **类型安全事件系统** - 增强的事件总线,支持异步事件、优先级、批处理和装饰器
12
+ - **定时器管理系统** - 完整的定时器管理,支持延迟和重复任务
13
+ - 🔍 **智能查询系统** - 支持复杂的实体查询,流式API设计
14
+ - **高性能优化** - 组件索引、Archetype系统、脏标记机制三重优化
15
+ - 🛠️ **开发者友好** - 完整的TypeScript支持,丰富的调试工具
16
+ - 📦 **轻量级设计** - 最小化依赖,适用于各种游戏引擎
18
17
 
19
18
  ## 📦 安装
20
19
 
@@ -22,162 +21,194 @@
22
21
  npm install @esengine/ecs-framework
23
22
  ```
24
23
 
25
- ## 📊 性能基准
26
-
27
- ```bash
28
- # 运行快速性能基准测试
29
- npm run benchmark
30
-
31
- # 运行完整性能测试
32
- npm run test:performance
33
- ```
34
-
35
- **框架性能数据**:
36
-
37
- ### 🚀 实体创建性能
38
- - **小规模**: 640,697 实体/秒 (1,000个实体/1.56ms)
39
- - **中规模**: 250,345 实体/秒 (10,000个实体/39.94ms)
40
- - **大规模**: 161,990 实体/秒 (500,000个实体/3.09秒)
41
-
42
- ### 🎯 核心操作性能
43
- ```
44
- 📊 核心操作性能
45
- 实体创建: 640,697个/秒
46
- 组件添加: 596,929组件/秒
47
- 位掩码操作: 5,000,000次/秒
48
- 查询缓存: 零延迟访问
49
- 批量操作: 高效处理
50
-
51
- 🔧 优化技术效果
52
- 组件对象池: 减少30-50%内存分配
53
- 位掩码优化器: 提升20-40%掩码性能
54
- 批量操作: 大幅减少创建时间
55
- 索引优化: 避免O(n)重复检查
56
- 缓存策略: 延迟清理机制
57
- ```
58
-
59
24
  ## 🚀 快速开始
60
25
 
61
- ### 1. 初始化框架
26
+ ### 1. 基础设置
62
27
 
63
28
  ```typescript
64
- import { Core, CoreEvents } from '@esengine/ecs-framework';
29
+ import { Core, CoreEvents, Scene } from '@esengine/ecs-framework';
65
30
 
66
31
  // 创建 Core 实例
67
- const core = Core.create(true); // true 表示开启调试模式
32
+ const core = Core.create(true); // 开启调试模式
33
+
34
+ // 创建场景
35
+ class GameScene extends Scene {
36
+ public initialize() {
37
+ // 场景初始化逻辑
38
+ }
39
+ }
68
40
 
69
41
  // 在游戏循环中更新框架
70
42
  function gameLoop() {
71
- // 发送帧更新事件
72
43
  Core.emitter.emit(CoreEvents.frameUpdated);
73
44
  }
74
45
  ```
75
46
 
76
- ### 2. 高性能批量创建实体
47
+ ### 2. 创建实体和组件
77
48
 
78
49
  ```typescript
79
- import { Scene, EntitySystem } from '@esengine/ecs-framework';
50
+ import { Component, Entity } from '@esengine/ecs-framework';
80
51
 
81
- class GameScene extends Scene {
82
- public initialize() {
83
- // 批量创建实体
84
- const entities = this.createEntities(1000, "Enemy");
85
-
86
- // 批量添加组件
87
- entities.forEach((entity, index) => {
88
- entity.addComponent(new PositionComponent(
89
- Math.random() * 1000,
90
- Math.random() * 1000
91
- ));
92
- entity.addComponent(new VelocityComponent());
93
- });
94
-
95
- // 添加系统
96
- this.addEntityProcessor(new MovementSystem());
52
+ // 定义组件
53
+ class PositionComponent extends Component {
54
+ constructor(public x: number = 0, public y: number = 0) {
55
+ super();
97
56
  }
98
57
  }
58
+
59
+ class VelocityComponent extends Component {
60
+ constructor(public dx: number = 0, public dy: number = 0) {
61
+ super();
62
+ }
63
+ }
64
+
65
+ // 创建实体
66
+ const entity = scene.createEntity("Player");
67
+ entity.addComponent(new PositionComponent(100, 100));
68
+ entity.addComponent(new VelocityComponent(10, 0));
99
69
  ```
100
70
 
101
- ### 3. 使用组件对象池优化内存
71
+ ### 3. 创建处理系统
102
72
 
103
73
  ```typescript
104
- import { Component, ComponentPoolManager } from '@esengine/ecs-framework';
74
+ import { EntitySystem } from '@esengine/ecs-framework';
105
75
 
106
- class BulletComponent extends Component {
107
- public damage: number = 10;
108
- public speed: number = 300;
109
-
110
- // 重置方法用于对象池
111
- public reset() {
112
- this.damage = 10;
113
- this.speed = 300;
76
+ class MovementSystem extends EntitySystem {
77
+ public process(entities: Entity[]) {
78
+ for (const entity of entities) {
79
+ const position = entity.getComponent(PositionComponent);
80
+ const velocity = entity.getComponent(VelocityComponent);
81
+
82
+ if (position && velocity) {
83
+ position.x += velocity.dx;
84
+ position.y += velocity.dy;
85
+ }
86
+ }
114
87
  }
115
88
  }
116
89
 
117
- // 注册组件池
118
- ComponentPoolManager.getInstance().registerPool(BulletComponent, 1000);
90
+ // 添加系统到场景
91
+ scene.addEntityProcessor(new MovementSystem());
92
+ ```
119
93
 
120
- // 使用对象池获取组件
121
- const bullet = ComponentPoolManager.getInstance().getComponent(BulletComponent);
122
- entity.addComponent(bullet);
94
+ ### 4. 实体查询和管理
123
95
 
124
- // 释放回对象池
125
- ComponentPoolManager.getInstance().releaseComponent(bullet);
96
+ ```typescript
97
+ import { EntityManager } from '@esengine/ecs-framework';
98
+
99
+ // 使用EntityManager进行高级查询
100
+ const entityManager = new EntityManager();
101
+
102
+ // 查询具有特定组件的实体
103
+ const movingEntities = entityManager
104
+ .query()
105
+ .withAll(PositionComponent, VelocityComponent)
106
+ .execute();
107
+
108
+ // 查询带标签的实体
109
+ const enemies = entityManager.getEntitiesByTag(1);
110
+
111
+ // 批量创建实体
112
+ const bullets = entityManager.createEntities(100, "bullet");
126
113
  ```
127
114
 
128
- ### 4. 位掩码优化器加速查询
115
+ ### 5. 事件系统
129
116
 
130
117
  ```typescript
131
- import { BitMaskOptimizer } from '@esengine/ecs-framework';
118
+ import { EventBus, ECSEventType, EventHandler } from '@esengine/ecs-framework';
132
119
 
133
- // 注册常用组件类型
134
- const optimizer = BitMaskOptimizer.getInstance();
135
- optimizer.registerComponentType(PositionComponent);
136
- optimizer.registerComponentType(VelocityComponent);
137
- optimizer.registerComponentType(RenderComponent);
120
+ // 获取事件总线
121
+ const eventBus = entityManager.eventBus;
138
122
 
139
- // 预计算常用掩码组合
140
- optimizer.precomputeCommonMasks();
123
+ // 监听实体创建事件
124
+ eventBus.onEntityCreated((data) => {
125
+ console.log(`Entity created: ${data.entityName}`);
126
+ });
127
+
128
+ // 监听组件添加事件
129
+ eventBus.onComponentAdded((data) => {
130
+ console.log(`Component ${data.componentType} added to entity ${data.entityId}`);
131
+ });
132
+
133
+ // 使用装饰器自动注册事件监听器
134
+ class GameManager {
135
+ @EventHandler(ECSEventType.ENTITY_DESTROYED)
136
+ onEntityDestroyed(data) {
137
+ console.log('Entity destroyed:', data.entityName);
138
+ }
139
+ }
141
140
 
142
- // 高效的掩码操作
143
- const positionMask = optimizer.getComponentMask(PositionComponent);
144
- const movementMask = optimizer.getCombinedMask([PositionComponent, VelocityComponent]);
141
+ // 自定义事件
142
+ eventBus.emit('player:levelup', { playerId: 123, newLevel: 5 });
145
143
  ```
146
144
 
145
+ ## 🆚 框架对比
146
+
147
+ 与其他 TypeScript ECS 框架相比,我们的优势:
148
+
149
+ | 特性 | @esengine/ecs-framework | bitecs | ecsy | Miniplex |
150
+ |------|-------------------------|-------|------|----------|
151
+ | **TypeScript 支持** | ✅ 原生支持 | ✅ 完整支持 | ⚠️ 部分支持 | ✅ 原生支持 |
152
+ | **事件系统** | ✅ 类型安全+装饰器 | ❌ 无内置事件系统 | ⚠️ 基础事件 | ✅ 响应式事件 |
153
+ | **查询系统** | ✅ 智能查询+流式API | ✅ 高性能 | ✅ 基础查询 | ✅ 响应式查询 |
154
+ | **性能优化** | ✅ 多层优化系统 | ✅ WASM优化 | ⚠️ 基础优化 | ✅ React集成优化 |
155
+ | **实体管理器** | ✅ 统一管理接口 | ❌ 无统一接口 | ✅ 基础管理 | ✅ 响应式管理 |
156
+ | **组件索引** | ✅ 哈希+位图索引 | ✅ 原生支持 | ❌ 无索引系统 | ✅ 自动索引 |
157
+ | **Archetype系统** | ✅ 内置支持 | ✅ 内置支持 | ❌ 无Archetype | ❌ 无Archetype |
158
+ | **脏标记系统** | ✅ 细粒度追踪 | ⚠️ 基础支持 | ❌ 无脏标记 | ✅ React级追踪 |
159
+ | **批量操作** | ✅ 全面的批量API | ✅ 批量支持 | ⚠️ 有限支持 | ⚠️ 有限支持 |
160
+ | **游戏引擎集成** | ✅ 通用设计 | ✅ 通用设计 | ✅ 通用设计 | ⚠️ 主要针对React |
161
+ | **学习曲线** | 🟢 中等 | 🟡 较陡峭 | 🟢 简单 | 🟡 需要React知识 |
162
+ | **社区生态** | 🟡 成长中 | 🟢 活跃 | 🟡 稳定 | 🟡 小众但精品 |
163
+
164
+ ### 为什么选择我们?
165
+
166
+ **相比 bitecs**:
167
+ - 更友好的 TypeScript API,无需手动管理内存
168
+ - 完整的实体管理器,开发体验更佳
169
+ - 内置类型安全事件系统,bitecs需要自己实现
170
+ - 多种索引系统可选,适应不同场景
171
+
172
+ **相比 ecsy**:
173
+ - 现代化的性能优化系统(组件索引、Archetype、脏标记)
174
+ - 更完整的 TypeScript 类型定义
175
+ - 增强的事件系统,支持装饰器和异步事件
176
+ - 活跃的维护和功能更新
177
+
178
+ **相比 Miniplex**:
179
+ - 不依赖 React 生态,可用于任何游戏引擎
180
+ - 专门针对游戏开发优化
181
+ - 更轻量级的核心设计
182
+ - 传统事件模式,更适合游戏开发习惯
183
+
147
184
  ## 📚 核心概念
148
185
 
149
186
  ### Entity(实体)
150
- 实体是游戏世界中的基本对象,支持批量操作和高性能创建。
187
+ 实体是游戏世界中的基本对象,可以挂载组件和运行系统。
151
188
 
152
189
  ```typescript
153
- // 单个实体创建
154
- const entity = scene.createEntity("MyEntity");
155
-
156
- // 批量实体创建
157
- const entities = scene.createEntities(1000, "Bullets");
190
+ // 创建实体
191
+ const entity = scene.createEntity("Player");
158
192
 
159
- // 实体属性设置
193
+ // 设置实体属性
160
194
  entity.tag = 1;
161
195
  entity.updateOrder = 0;
162
196
  entity.enabled = true;
197
+
198
+ // 批量创建实体
199
+ const entities = scene.createEntities(100, "Enemy");
163
200
  ```
164
201
 
165
202
  ### Component(组件)
166
- 组件包含数据和行为,支持对象池优化。
203
+ 组件存储数据,定义实体的属性和状态。
167
204
 
168
205
  ```typescript
169
- import { Component, ComponentPoolManager } from '@esengine/ecs-framework';
206
+ import { Component } from '@esengine/ecs-framework';
170
207
 
171
208
  class HealthComponent extends Component {
172
209
  public maxHealth: number = 100;
173
210
  public currentHealth: number = 100;
174
211
 
175
- // 对象池重置方法
176
- public reset() {
177
- this.maxHealth = 100;
178
- this.currentHealth = 100;
179
- }
180
-
181
212
  public takeDamage(damage: number) {
182
213
  this.currentHealth = Math.max(0, this.currentHealth - damage);
183
214
  if (this.currentHealth <= 0) {
@@ -186,30 +217,22 @@ class HealthComponent extends Component {
186
217
  }
187
218
  }
188
219
 
189
- // 注册到对象池
190
- ComponentPoolManager.getInstance().registerPool(HealthComponent, 500);
220
+ // 添加组件到实体
221
+ entity.addComponent(new HealthComponent());
191
222
  ```
192
223
 
193
224
  ### System(系统)
194
- 系统处理实体集合,支持批量处理优化。
225
+ 系统处理具有特定组件的实体集合,实现游戏逻辑。
195
226
 
196
227
  ```typescript
197
228
  import { EntitySystem, Entity } from '@esengine/ecs-framework';
198
229
 
199
230
  class HealthSystem extends EntitySystem {
200
231
  protected process(entities: Entity[]) {
201
- // 批量处理实体
202
- const batchSize = 1000;
203
- for (let i = 0; i < entities.length; i += batchSize) {
204
- const batch = entities.slice(i, i + batchSize);
205
- this.processBatch(batch);
206
- }
207
- }
208
-
209
- private processBatch(entities: Entity[]) {
210
232
  for (const entity of entities) {
211
233
  const health = entity.getComponent(HealthComponent);
212
234
  if (health && health.currentHealth <= 0) {
235
+ // 处理实体死亡逻辑
213
236
  entity.destroy();
214
237
  }
215
238
  }
@@ -217,130 +240,22 @@ class HealthSystem extends EntitySystem {
217
240
  }
218
241
  ```
219
242
 
220
- ## 🎮 高级功能
221
-
222
- ### 批量操作API
223
-
224
- ```typescript
225
- // 批量创建实体
226
- const entities = scene.createEntities(5000, "Enemies");
227
-
228
- // 批量查询
229
- const movingEntities = scene.getEntitiesWithComponents([PositionComponent, VelocityComponent]);
230
-
231
- // 延迟缓存清理
232
- scene.addEntity(entity, false); // 延迟缓存清理
233
- // ... 添加更多实体
234
- scene.querySystem.clearCache(); // 手动清理缓存
235
- ```
236
-
237
- ### 性能监控
238
-
239
- ```typescript
240
- import { Core } from '@esengine/ecs-framework';
241
-
242
- // 获取性能统计
243
- const stats = scene.getPerformanceStats();
244
- console.log(`实体数量: ${stats.entityCount}`);
245
- console.log(`查询缓存大小: ${stats.queryCacheSize}`);
246
- console.log(`组件池统计:`, stats.componentPoolStats);
247
- ```
248
-
249
- ### 内存优化
250
-
251
- ```typescript
252
- // 预热组件池
253
- ComponentPoolManager.getInstance().preWarmPools({
254
- BulletComponent: 1000,
255
- EffectComponent: 500,
256
- PickupComponent: 200
257
- });
258
-
259
- // 清理未使用的组件
260
- ComponentPoolManager.getInstance().clearUnusedComponents();
261
- ```
262
-
263
- ## 🧪 测试和基准
264
-
265
- ### 运行测试套件
243
+ ## 🧪 测试
266
244
 
267
245
  ```bash
268
246
  # 运行所有测试
269
247
  npm run test
270
248
 
271
- # 单元测试
272
- npm run test:unit
273
-
274
- # 性能测试
275
- npm run test:performance
276
-
277
- # 快速基准测试
249
+ # 性能基准测试
278
250
  npm run benchmark
279
251
  ```
280
252
 
281
- ### 自定义性能测试
282
-
283
- ```typescript
284
- import { runEntityCreationBenchmark } from './Testing/Performance/benchmark';
285
-
286
- // 运行自定义基准测试
287
- await runEntityCreationBenchmark();
288
- ```
289
-
290
- ## 🔧 优化建议
291
-
292
- ### 大规模实体处理
293
-
294
- 1. **使用批量API**
295
- ```typescript
296
- // ✅ 推荐:批量创建
297
- const entities = scene.createEntities(10000, "Units");
298
-
299
- // ❌ 避免:循环单个创建
300
- for (let i = 0; i < 10000; i++) {
301
- scene.createEntity("Unit" + i);
302
- }
303
- ```
304
-
305
- 2. **启用对象池**
306
- ```typescript
307
- // 预先注册常用组件池
308
- ComponentPoolManager.getInstance().registerPool(BulletComponent, 2000);
309
- ComponentPoolManager.getInstance().registerPool(EffectComponent, 1000);
310
- ```
311
-
312
- 3. **优化查询频率**
313
- ```typescript
314
- // 缓存查询结果
315
- if (frameCount % 5 === 0) {
316
- this.cachedEnemies = scene.getEntitiesWithComponent(EnemyComponent);
317
- }
318
- ```
319
-
320
- ### 移动端优化
321
-
322
- - 实体数量建议 ≤ 20,000
323
- - 启用组件对象池
324
- - 减少查询频率
325
- - 使用批量操作
326
-
327
- ## 📈 版本更新
328
-
329
- ### v2.0.6 (最新)
330
- - 🚀 **高性能实体创建**: 支持64万实体/秒的创建速度
331
- - 🎯 **组件对象池**: 减少内存分配开销
332
- - ⚡ **位掩码优化器**: 加速组件查询和操作
333
- - 🔧 **批量操作API**: 支持高效的批量实体创建
334
- - 📊 **性能监控**: 完整的性能分析工具
335
- - 🧪 **测试套件**: 单元测试、性能测试、集成测试
336
-
337
- ### 历史版本
338
- - v1.x.x: 基础ECS架构实现
339
-
340
253
  ## 📖 文档
341
254
 
342
255
  - [快速入门](docs/getting-started.md) - 从零开始学习框架使用
343
- - [实体使用指南](docs/entity-guide.md) - 详细了解实体的所有功能和用法
256
+ - [EntityManager 使用指南](docs/entity-manager-example.md) - 详细了解实体管理器的高级功能
257
+ - [事件系统使用指南](docs/event-system-example.md) - 学习类型安全事件系统的完整用法
258
+ - [性能优化指南](docs/performance-optimization.md) - 深入了解三大性能优化系统
344
259
  - [核心概念](docs/core-concepts.md) - 深入了解 ECS 架构和设计原理
345
260
  - [查询系统使用指南](docs/query-system-usage.md) - 学习高性能查询系统的详细用法
346
261
 
@@ -380,6 +295,32 @@ cd source && npm install && npm run build
380
295
 
381
296
  加入 QQ 群讨论:[ecs游戏框架交流](https://jq.qq.com/?_wv=1027&k=29w1Nud6)
382
297
 
298
+ ### 🚀 核心性能指标
299
+
300
+ ```bash
301
+ 实体创建: 640,000+ 个/秒
302
+ 组件查询: O(1) 复杂度(使用索引)
303
+ 内存优化: 30-50% 减少分配
304
+ 批量操作: 显著提升处理效率
305
+ ```
306
+
307
+ ### 🎯 性能优化技术
308
+
309
+ - **组件索引系统**: 哈希和位图双重索引,支持 O(1) 查询
310
+ - **Archetype 系统**: 按组件组合分组,减少查询开销
311
+ - **脏标记机制**: 细粒度变更追踪,避免不必要的计算
312
+ - **批量操作 API**: 减少函数调用开销,提升大规模操作效率
313
+ - **智能缓存**: 查询结果缓存和延迟清理机制
314
+
315
+ ### 🔧 性能建议
316
+
317
+ 1. **大规模场景**: 使用批量API和组件索引
318
+ 2. **频繁查询**: 启用Archetype系统进行快速筛选
319
+ 3. **实时游戏**: 利用脏标记减少无效更新
320
+ 4. **移动端**: 建议实体数量控制在20,000以内
321
+
322
+ 运行 `npm run benchmark` 查看在您的环境中的具体性能表现。
323
+
383
324
  ---
384
325
 
385
326
  **ECS Framework** - 让游戏开发更简单、更高效!
package/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * @esengine/ecs-framework
3
3
  * 高性能ECS框架 - 适用于Cocos Creator和Laya引擎
4
- * 版本: 2.1.5
5
- * 构建时间: 2025-06-09T01:48:54.822Z
4
+ * 版本: 2.1.7
5
+ * 构建时间: 2025-06-09T05:30:23.949Z
6
6
  */
7
7
 
8
8
  /**