@esengine/ecs-framework 2.1.12 → 2.1.14
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/index.d.ts +15 -17
- 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 +1 -2
- package/COCOS_USAGE.md +0 -259
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esengine/ecs-framework",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.14",
|
|
4
4
|
"description": "用于Laya、Cocos等游戏引擎的高性能ECS框架",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
"index.umd.js",
|
|
19
19
|
"index.umd.js.map",
|
|
20
20
|
"index.d.ts",
|
|
21
|
-
"wasm",
|
|
22
21
|
"README.md",
|
|
23
22
|
"LICENSE",
|
|
24
23
|
"SECURITY.md",
|
package/COCOS_USAGE.md
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
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/wasm/ecs_wasm_core.d.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/* tslint:disable */
|
|
2
|
-
/* eslint-disable */
|
|
3
|
-
/**
|
|
4
|
-
* 创建组件掩码的辅助函数
|
|
5
|
-
*/
|
|
6
|
-
export function create_component_mask(component_ids: Uint32Array): bigint;
|
|
7
|
-
/**
|
|
8
|
-
* 检查掩码是否包含指定组件
|
|
9
|
-
*/
|
|
10
|
-
export function mask_contains_component(mask: bigint, component_id: number): boolean;
|
|
11
|
-
/**
|
|
12
|
-
* 初始化函数
|
|
13
|
-
*/
|
|
14
|
-
export function main(): void;
|
|
15
|
-
/**
|
|
16
|
-
* 高性能ECS核心,专注于实体查询和掩码管理
|
|
17
|
-
*/
|
|
18
|
-
export class EcsCore {
|
|
19
|
-
free(): void;
|
|
20
|
-
/**
|
|
21
|
-
* 创建新的ECS核心
|
|
22
|
-
*/
|
|
23
|
-
constructor();
|
|
24
|
-
/**
|
|
25
|
-
* 创建新实体
|
|
26
|
-
*/
|
|
27
|
-
create_entity(): number;
|
|
28
|
-
/**
|
|
29
|
-
* 删除实体
|
|
30
|
-
*/
|
|
31
|
-
destroy_entity(entity_id: number): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* 更新实体的组件掩码
|
|
34
|
-
*/
|
|
35
|
-
update_entity_mask(entity_id: number, mask: bigint): void;
|
|
36
|
-
/**
|
|
37
|
-
* 批量更新实体掩码
|
|
38
|
-
*/
|
|
39
|
-
batch_update_masks(entity_ids: Uint32Array, masks: BigUint64Array): void;
|
|
40
|
-
/**
|
|
41
|
-
* 查询实体
|
|
42
|
-
*/
|
|
43
|
-
query_entities(mask: bigint, max_results: number): number;
|
|
44
|
-
/**
|
|
45
|
-
* 获取查询结果数量
|
|
46
|
-
*/
|
|
47
|
-
get_query_result_count(): number;
|
|
48
|
-
/**
|
|
49
|
-
* 缓存查询实体
|
|
50
|
-
*/
|
|
51
|
-
query_cached(mask: bigint): number;
|
|
52
|
-
/**
|
|
53
|
-
* 获取缓存查询结果数量
|
|
54
|
-
*/
|
|
55
|
-
get_cached_query_count(mask: bigint): number;
|
|
56
|
-
/**
|
|
57
|
-
* 多组件查询
|
|
58
|
-
*/
|
|
59
|
-
query_multiple_components(masks: BigUint64Array, max_results: number): number;
|
|
60
|
-
/**
|
|
61
|
-
* 排除查询
|
|
62
|
-
*/
|
|
63
|
-
query_with_exclusion(include_mask: bigint, exclude_mask: bigint, max_results: number): number;
|
|
64
|
-
/**
|
|
65
|
-
* 获取实体的组件掩码
|
|
66
|
-
*/
|
|
67
|
-
get_entity_mask(entity_id: number): bigint;
|
|
68
|
-
/**
|
|
69
|
-
* 检查实体是否存在
|
|
70
|
-
*/
|
|
71
|
-
entity_exists(entity_id: number): boolean;
|
|
72
|
-
/**
|
|
73
|
-
* 获取实体数量
|
|
74
|
-
*/
|
|
75
|
-
get_entity_count(): number;
|
|
76
|
-
/**
|
|
77
|
-
* 获取性能统计信息
|
|
78
|
-
*/
|
|
79
|
-
get_performance_stats(): Array<any>;
|
|
80
|
-
/**
|
|
81
|
-
* 清理所有数据
|
|
82
|
-
*/
|
|
83
|
-
clear(): void;
|
|
84
|
-
/**
|
|
85
|
-
* 重建查询缓存
|
|
86
|
-
*/
|
|
87
|
-
rebuild_query_cache(): void;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
91
|
-
|
|
92
|
-
export interface InitOutput {
|
|
93
|
-
readonly memory: WebAssembly.Memory;
|
|
94
|
-
readonly __wbg_ecscore_free: (a: number, b: number) => void;
|
|
95
|
-
readonly ecscore_new: () => number;
|
|
96
|
-
readonly ecscore_create_entity: (a: number) => number;
|
|
97
|
-
readonly ecscore_destroy_entity: (a: number, b: number) => number;
|
|
98
|
-
readonly ecscore_update_entity_mask: (a: number, b: number, c: bigint) => void;
|
|
99
|
-
readonly ecscore_batch_update_masks: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
100
|
-
readonly ecscore_query_entities: (a: number, b: bigint, c: number) => number;
|
|
101
|
-
readonly ecscore_get_query_result_count: (a: number) => number;
|
|
102
|
-
readonly ecscore_query_cached: (a: number, b: bigint) => number;
|
|
103
|
-
readonly ecscore_get_cached_query_count: (a: number, b: bigint) => number;
|
|
104
|
-
readonly ecscore_query_multiple_components: (a: number, b: number, c: number, d: number) => number;
|
|
105
|
-
readonly ecscore_query_with_exclusion: (a: number, b: bigint, c: bigint, d: number) => number;
|
|
106
|
-
readonly ecscore_get_entity_mask: (a: number, b: number) => bigint;
|
|
107
|
-
readonly ecscore_entity_exists: (a: number, b: number) => number;
|
|
108
|
-
readonly ecscore_get_entity_count: (a: number) => number;
|
|
109
|
-
readonly ecscore_get_performance_stats: (a: number) => any;
|
|
110
|
-
readonly ecscore_clear: (a: number) => void;
|
|
111
|
-
readonly ecscore_rebuild_query_cache: (a: number) => void;
|
|
112
|
-
readonly create_component_mask: (a: number, b: number) => bigint;
|
|
113
|
-
readonly mask_contains_component: (a: bigint, b: number) => number;
|
|
114
|
-
readonly main: () => void;
|
|
115
|
-
readonly __wbindgen_exn_store: (a: number) => void;
|
|
116
|
-
readonly __externref_table_alloc: () => number;
|
|
117
|
-
readonly __wbindgen_export_2: WebAssembly.Table;
|
|
118
|
-
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
119
|
-
readonly __wbindgen_start: () => void;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
123
|
-
/**
|
|
124
|
-
* Instantiates the given `module`, which can either be bytes or
|
|
125
|
-
* a precompiled `WebAssembly.Module`.
|
|
126
|
-
*
|
|
127
|
-
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
128
|
-
*
|
|
129
|
-
* @returns {InitOutput}
|
|
130
|
-
*/
|
|
131
|
-
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
135
|
-
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
136
|
-
*
|
|
137
|
-
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
138
|
-
*
|
|
139
|
-
* @returns {Promise<InitOutput>}
|
|
140
|
-
*/
|
|
141
|
-
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|