@esengine/ecs-framework 2.1.11 → 2.1.13
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/COCOS_USAGE.md +287 -0
- package/index.d.ts +43 -1
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/index.umd.js +1 -1
- package/index.umd.js.map +1 -1
- package/package.json +2 -2
- package/wasm/ecs_wasm_core.d.ts +0 -141
- package/wasm/ecs_wasm_core.js +0 -415
- package/wasm/ecs_wasm_core_bg.wasm +0 -0
- package/wasm/ecs_wasm_core_bg.wasm.d.ts +0 -29
- package/wasm/package.json +0 -15
package/COCOS_USAGE.md
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# Cocos Creator 使用指南
|
|
2
|
+
|
|
3
|
+
本指南专门针对在 Cocos Creator 3.8+ 中使用 @esengine/ecs-framework。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @esengine/ecs-framework
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## WASM 支持(可选)
|
|
12
|
+
|
|
13
|
+
🚀 **WASM 模块已独立发布,需要手动下载和配置**
|
|
14
|
+
|
|
15
|
+
WASM 模块不再包含在 npm 包中,如需使用请从 [GitHub Release](https://github.com/esengine/ecs-framework/releases) 下载。
|
|
16
|
+
|
|
17
|
+
- 不使用 WASM:框架自动使用 JavaScript 实现,功能完全正常
|
|
18
|
+
- 使用 WASM:提供查询性能优化,需要手动配置
|
|
19
|
+
|
|
20
|
+
## 基本使用
|
|
21
|
+
|
|
22
|
+
### 1. 创建 ECS 核心
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { Core } from '@esengine/ecs-framework';
|
|
26
|
+
|
|
27
|
+
// 在Cocos组件中初始化
|
|
28
|
+
export class GameManager extends Component {
|
|
29
|
+
private core: Core;
|
|
30
|
+
|
|
31
|
+
onLoad() {
|
|
32
|
+
// 创建核心实例
|
|
33
|
+
this.core = Core.create(true);
|
|
34
|
+
console.log('ECS核心已初始化');
|
|
35
|
+
|
|
36
|
+
// 可选:加载WASM支持(需要先下载WASM包)
|
|
37
|
+
this.loadWasmSupport();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private async loadWasmSupport() {
|
|
41
|
+
try {
|
|
42
|
+
// 1. 导入WASM胶水代码(需要将文件放到项目中)
|
|
43
|
+
const { default: wasmFactory } = await import('./ecs_wasm_core.js');
|
|
44
|
+
|
|
45
|
+
// 2. 使用Cocos API加载WASM文件(需要先导入到资源管理器)
|
|
46
|
+
const wasmFile = await this.loadWasmOrAsm("wasmFiles", "ecs_wasm_core", "your-wasm-uuid");
|
|
47
|
+
|
|
48
|
+
// 3. 初始化WASM支持
|
|
49
|
+
const { ecsCore } = await import('@esengine/ecs-framework');
|
|
50
|
+
const success = await ecsCore.initializeWasm(wasmFactory, wasmFile);
|
|
51
|
+
|
|
52
|
+
if (success) {
|
|
53
|
+
console.log('✅ ECS WASM加速已启用');
|
|
54
|
+
} else {
|
|
55
|
+
console.log('⚠️ WASM初始化失败,使用JavaScript实现');
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.log('⚠️ WASM不可用,使用JavaScript实现:', error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. 创建组件
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { Component } from '@esengine/ecs-framework';
|
|
68
|
+
|
|
69
|
+
// 位置组件
|
|
70
|
+
export class Position extends Component {
|
|
71
|
+
constructor(public x: number = 0, public y: number = 0) {
|
|
72
|
+
super();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 速度组件
|
|
77
|
+
export class Velocity extends Component {
|
|
78
|
+
constructor(public dx: number = 0, public dy: number = 0) {
|
|
79
|
+
super();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 精灵组件(关联Cocos节点)
|
|
84
|
+
export class SpriteComponent extends Component {
|
|
85
|
+
constructor(public node: Node) {
|
|
86
|
+
super();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 3. 创建系统
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { EntitySystem, Family } from '@esengine/ecs-framework';
|
|
95
|
+
|
|
96
|
+
export class MovementSystem extends EntitySystem {
|
|
97
|
+
constructor() {
|
|
98
|
+
super(Family.all(Position, Velocity).get());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public processEntity(entity: Entity, deltaTime: number): void {
|
|
102
|
+
const pos = entity.getComponent(Position);
|
|
103
|
+
const vel = entity.getComponent(Velocity);
|
|
104
|
+
|
|
105
|
+
pos.x += vel.dx * deltaTime;
|
|
106
|
+
pos.y += vel.dy * deltaTime;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class SpriteRenderSystem extends EntitySystem {
|
|
111
|
+
constructor() {
|
|
112
|
+
super(Family.all(Position, SpriteComponent).get());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public processEntity(entity: Entity, deltaTime: number): void {
|
|
116
|
+
const pos = entity.getComponent(Position);
|
|
117
|
+
const sprite = entity.getComponent(SpriteComponent);
|
|
118
|
+
|
|
119
|
+
// 同步位置到Cocos节点
|
|
120
|
+
sprite.node.setPosition(pos.x, pos.y);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 4. 创建场景
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { Scene } from '@esengine/ecs-framework';
|
|
129
|
+
|
|
130
|
+
export class GameScene extends Scene {
|
|
131
|
+
private movementSystem: MovementSystem;
|
|
132
|
+
private spriteRenderSystem: SpriteRenderSystem;
|
|
133
|
+
|
|
134
|
+
public initialize(): void {
|
|
135
|
+
// 添加系统
|
|
136
|
+
this.movementSystem = this.addEntityProcessor(new MovementSystem());
|
|
137
|
+
this.spriteRenderSystem = this.addEntityProcessor(new SpriteRenderSystem());
|
|
138
|
+
|
|
139
|
+
// 创建一些实体用于测试
|
|
140
|
+
this.createTestEntities();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private createTestEntities(): void {
|
|
144
|
+
for (let i = 0; i < 10; i++) {
|
|
145
|
+
const entity = this.createEntity();
|
|
146
|
+
|
|
147
|
+
// 添加位置组件
|
|
148
|
+
entity.addComponent(new Position(
|
|
149
|
+
Math.random() * 800,
|
|
150
|
+
Math.random() * 600
|
|
151
|
+
));
|
|
152
|
+
|
|
153
|
+
// 添加速度组件
|
|
154
|
+
entity.addComponent(new Velocity(
|
|
155
|
+
(Math.random() - 0.5) * 200,
|
|
156
|
+
(Math.random() - 0.5) * 200
|
|
157
|
+
));
|
|
158
|
+
|
|
159
|
+
// 创建Cocos节点并添加精灵组件
|
|
160
|
+
const node = new Node();
|
|
161
|
+
const sprite = node.addComponent(Sprite);
|
|
162
|
+
// 设置精灵贴图...
|
|
163
|
+
|
|
164
|
+
entity.addComponent(new SpriteComponent(node));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### 5. 在Cocos组件中集成
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { Component, _decorator } from 'cc';
|
|
174
|
+
import { Core } from '@esengine/ecs-framework';
|
|
175
|
+
|
|
176
|
+
const { ccclass } = _decorator;
|
|
177
|
+
|
|
178
|
+
@ccclass('ECSGameManager')
|
|
179
|
+
export class ECSGameManager extends Component {
|
|
180
|
+
private core: Core;
|
|
181
|
+
private gameScene: GameScene;
|
|
182
|
+
|
|
183
|
+
onLoad() {
|
|
184
|
+
// 初始化ECS核心
|
|
185
|
+
this.core = Core.create(true);
|
|
186
|
+
|
|
187
|
+
// 创建游戏场景
|
|
188
|
+
this.gameScene = new GameScene();
|
|
189
|
+
Core.scene = this.gameScene;
|
|
190
|
+
|
|
191
|
+
console.log('ECS系统已启动');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
update(deltaTime: number) {
|
|
195
|
+
// ECS系统会自动处理更新
|
|
196
|
+
// Core.emitter 会自动触发 frameUpdated 事件
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
onDestroy() {
|
|
200
|
+
// 清理资源
|
|
201
|
+
if (this.core) {
|
|
202
|
+
// 执行必要的清理
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## 高级功能
|
|
209
|
+
|
|
210
|
+
### 事件系统
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
import { EventBus, ECSEventType } from '@esengine/ecs-framework';
|
|
214
|
+
|
|
215
|
+
// 监听实体创建事件
|
|
216
|
+
EventBus.subscribe(ECSEventType.EntityCreated, (data) => {
|
|
217
|
+
console.log('实体已创建:', data.entityId);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// 发射自定义事件
|
|
221
|
+
EventBus.emit('player-scored', { score: 100, playerId: 'player1' });
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### 性能监控
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { PerformanceMonitor } from '@esengine/ecs-framework';
|
|
228
|
+
|
|
229
|
+
// 获取性能统计
|
|
230
|
+
const stats = PerformanceMonitor.instance.getStats();
|
|
231
|
+
console.log('系统性能:', stats);
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### 对象池
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import { PoolManager } from '@esengine/ecs-framework';
|
|
238
|
+
|
|
239
|
+
// 获取对象池管理器
|
|
240
|
+
const poolManager = PoolManager.getInstance();
|
|
241
|
+
|
|
242
|
+
// 创建对象池
|
|
243
|
+
const bulletPool = poolManager.createPool('bullets', () => new BulletComponent(), 100);
|
|
244
|
+
|
|
245
|
+
// 获取对象
|
|
246
|
+
const bullet = bulletPool.obtain();
|
|
247
|
+
|
|
248
|
+
// 归还对象
|
|
249
|
+
bulletPool.free(bullet);
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## 性能建议
|
|
253
|
+
|
|
254
|
+
1. **合理使用Family查询**: 避免过于复杂的组件查询
|
|
255
|
+
2. **批量操作**: 使用事件系统进行批量更新
|
|
256
|
+
3. **对象池**: 频繁创建/销毁的对象使用对象池
|
|
257
|
+
4. **定期清理**: 及时移除不需要的实体和组件
|
|
258
|
+
|
|
259
|
+
## 注意事项
|
|
260
|
+
|
|
261
|
+
1. 🔧 **WASM 支持**: 在 Cocos Creator 中自动禁用,使用 JavaScript 实现
|
|
262
|
+
2. 🎯 **内存管理**: 注意及时清理不需要的实体,避免内存泄漏
|
|
263
|
+
3. 🔄 **更新循环**: ECS 系统会自动集成到 Cocos 的更新循环中
|
|
264
|
+
4. 📦 **模块化**: 建议按功能拆分不同的系统和组件
|
|
265
|
+
|
|
266
|
+
## 故障排除
|
|
267
|
+
|
|
268
|
+
### 常见问题
|
|
269
|
+
|
|
270
|
+
**Q: 为什么看到"检测到Cocos Creator环境,WASM需要手动配置"的警告?**
|
|
271
|
+
A: 这是正常的。框架会自动回退到JavaScript实现,功能完全正常。
|
|
272
|
+
|
|
273
|
+
**Q: 如何确认ECS系统正在运行?**
|
|
274
|
+
A: 查看控制台输出,应该能看到"ECS核心已初始化"等日志。
|
|
275
|
+
|
|
276
|
+
**Q: 性能是否受到影响?**
|
|
277
|
+
A: JavaScript实现的性能已经很好,对于大多数游戏场景足够使用。
|
|
278
|
+
|
|
279
|
+
## 示例项目
|
|
280
|
+
|
|
281
|
+
完整的示例项目请参考:[GitHub示例仓库](https://github.com/esengine/ecs-framework/tree/master/examples/cocos)
|
|
282
|
+
|
|
283
|
+
## 技术支持
|
|
284
|
+
|
|
285
|
+
如遇问题,请访问:
|
|
286
|
+
- [GitHub Issues](https://github.com/esengine/ecs-framework/issues)
|
|
287
|
+
- [官方文档](https://github.com/esengine/ecs-framework#readme)
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @esengine/ecs-framework v2.1.
|
|
2
|
+
* @esengine/ecs-framework v2.1.13
|
|
3
3
|
* TypeScript definitions
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
@@ -5465,6 +5465,40 @@ declare class WasmLoader {
|
|
|
5465
5465
|
private wasmCore;
|
|
5466
5466
|
private silent;
|
|
5467
5467
|
setSilent(silent: boolean): void;
|
|
5468
|
+
/**
|
|
5469
|
+
* 手动初始化WASM模块
|
|
5470
|
+
*
|
|
5471
|
+
* @param wasmFactory WASM工厂函数(从.js文件导入的default)
|
|
5472
|
+
* @param wasmFile WASM文件数据
|
|
5473
|
+
* @returns 是否初始化成功
|
|
5474
|
+
*
|
|
5475
|
+
* @example
|
|
5476
|
+
* ```typescript
|
|
5477
|
+
* import { ecsCore } from '@esengine/ecs-framework';
|
|
5478
|
+
*
|
|
5479
|
+
* // 1. 导入WASM的JS文件
|
|
5480
|
+
* import('./ecs_wasm_core.js').then(({ default: wasmFactory }) => {
|
|
5481
|
+
* // 2. 加载WASM文件(具体方式根据引擎而定)
|
|
5482
|
+
* // Cocos Creator: this.loadWasmOrAsm("wasmFiles", "ecs_wasm_core", "uuid")
|
|
5483
|
+
* // 其他环境: fetch('./ecs_wasm_core_bg.wasm')
|
|
5484
|
+
* loadWasmFile().then((wasmFile) => {
|
|
5485
|
+
* // 3. 手动初始化ECS的WASM支持
|
|
5486
|
+
* ecsCore.initializeWasm(wasmFactory, wasmFile).then((success) => {
|
|
5487
|
+
* if (success) {
|
|
5488
|
+
* console.log("ECS WASM初始化成功");
|
|
5489
|
+
* } else {
|
|
5490
|
+
* console.log("ECS WASM初始化失败,使用JavaScript回退");
|
|
5491
|
+
* }
|
|
5492
|
+
* });
|
|
5493
|
+
* });
|
|
5494
|
+
* });
|
|
5495
|
+
* ```
|
|
5496
|
+
*/
|
|
5497
|
+
initializeWasm(wasmFactory: any, wasmFile: any): Promise<boolean>;
|
|
5498
|
+
/**
|
|
5499
|
+
* 通用WASM初始化方法
|
|
5500
|
+
*/
|
|
5501
|
+
private initWasmInstance;
|
|
5468
5502
|
loadWasmModule(): Promise<boolean>;
|
|
5469
5503
|
private initializeWasmModule;
|
|
5470
5504
|
getWasmCore(): WasmEcsCoreInstance | null;
|
|
@@ -5509,6 +5543,14 @@ declare class WasmEcsCore {
|
|
|
5509
5543
|
private usingWasm;
|
|
5510
5544
|
constructor();
|
|
5511
5545
|
setSilent(silent: boolean): void;
|
|
5546
|
+
/**
|
|
5547
|
+
* 手动初始化WASM模块
|
|
5548
|
+
*
|
|
5549
|
+
* @param wasmFactory WASM工厂函数(从.js文件导入的default)
|
|
5550
|
+
* @param wasmFile WASM文件数据
|
|
5551
|
+
* @returns 是否初始化成功
|
|
5552
|
+
*/
|
|
5553
|
+
initializeWasm(wasmFactory: any, wasmFile: any): Promise<boolean>;
|
|
5512
5554
|
initialize(): Promise<boolean>;
|
|
5513
5555
|
createEntity(): EntityId;
|
|
5514
5556
|
destroyEntity(entityId: EntityId): boolean;
|