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