@esengine/ecs-framework 2.0.3 → 2.0.4
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/package.json +1 -1
- package/README.md +0 -277
package/package.json
CHANGED
package/README.md
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
# ECS Framework
|
|
2
|
-
|
|
3
|
-
[](https://badge.fury.io/js/%40esengine%2Fecs-framework)
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
|
|
6
|
-
一个轻量级的 TypeScript ECS(Entity-Component-System)框架,专为小游戏开发设计,适用于 Laya、Cocos 等游戏引擎。
|
|
7
|
-
|
|
8
|
-
## ✨ 特性
|
|
9
|
-
|
|
10
|
-
- 🚀 **轻量级 ECS 架构** - 基于实体组件系统,提供清晰的代码结构
|
|
11
|
-
- 📡 **事件系统** - 内置 Emitter 事件发射器,支持类型安全的事件管理
|
|
12
|
-
- ⏰ **定时器系统** - 完整的定时器管理,支持延迟和重复任务
|
|
13
|
-
- 🔍 **查询系统** - 基于位掩码的高性能实体查询
|
|
14
|
-
- 🛠️ **性能监控** - 内置性能监控工具,帮助优化游戏性能
|
|
15
|
-
- 🎯 **对象池** - 内存管理优化,减少垃圾回收压力
|
|
16
|
-
- 📊 **数学库** - 完整的 2D 数学运算支持
|
|
17
|
-
|
|
18
|
-
## 📦 安装
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install @esengine/ecs-framework
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## 🚀 快速开始
|
|
25
|
-
|
|
26
|
-
### 1. 初始化框架
|
|
27
|
-
|
|
28
|
-
```typescript
|
|
29
|
-
import * as es from '@esengine/ecs-framework';
|
|
30
|
-
|
|
31
|
-
// 创建 Core 实例
|
|
32
|
-
const core = es.Core.create(true); // true 表示开启调试模式
|
|
33
|
-
|
|
34
|
-
// 在游戏循环中更新框架
|
|
35
|
-
function gameLoop() {
|
|
36
|
-
// 发送帧更新事件
|
|
37
|
-
es.Core.emitter.emit(es.CoreEvents.frameUpdated);
|
|
38
|
-
}
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### 2. 创建场景
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
class GameScene extends es.Scene {
|
|
45
|
-
public initialize() {
|
|
46
|
-
// 创建玩家实体
|
|
47
|
-
const player = this.createEntity("Player");
|
|
48
|
-
|
|
49
|
-
// 设置位置
|
|
50
|
-
player.position = new es.Vector2(100, 100);
|
|
51
|
-
|
|
52
|
-
// 添加自定义组件
|
|
53
|
-
const movement = player.addComponent(new MovementComponent());
|
|
54
|
-
|
|
55
|
-
// 添加系统
|
|
56
|
-
this.addEntityProcessor(new MovementSystem());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public onStart() {
|
|
60
|
-
console.log("游戏场景已启动");
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// 设置当前场景
|
|
65
|
-
es.Core.scene = new GameScene();
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### 3. 创建组件
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
class MovementComponent extends es.Component {
|
|
72
|
-
public speed: number = 100;
|
|
73
|
-
public direction: es.Vector2 = es.Vector2.zero;
|
|
74
|
-
|
|
75
|
-
public update() {
|
|
76
|
-
if (this.direction.length > 0) {
|
|
77
|
-
const movement = this.direction.multiply(this.speed * es.Time.deltaTime);
|
|
78
|
-
this.entity.position = this.entity.position.add(movement);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### 4. 创建系统
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
class MovementSystem extends es.EntitySystem {
|
|
88
|
-
protected process(entities: es.Entity[]) {
|
|
89
|
-
for (const entity of entities) {
|
|
90
|
-
const movement = entity.getComponent(MovementComponent);
|
|
91
|
-
if (movement) {
|
|
92
|
-
movement.update();
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
## 📚 核心概念
|
|
100
|
-
|
|
101
|
-
### Entity(实体)
|
|
102
|
-
实体是游戏世界中的基本对象,包含位置、旋转、缩放等基本属性,可以添加组件来扩展功能。
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
const entity = scene.createEntity("MyEntity");
|
|
106
|
-
entity.position = new es.Vector2(100, 200);
|
|
107
|
-
entity.rotation = Math.PI / 4;
|
|
108
|
-
entity.scale = new es.Vector2(2, 2);
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Component(组件)
|
|
112
|
-
组件包含数据和行为,定义了实体的特性。
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
class HealthComponent extends es.Component {
|
|
116
|
-
public maxHealth: number = 100;
|
|
117
|
-
public currentHealth: number = 100;
|
|
118
|
-
|
|
119
|
-
public takeDamage(damage: number) {
|
|
120
|
-
this.currentHealth = Math.max(0, this.currentHealth - damage);
|
|
121
|
-
if (this.currentHealth <= 0) {
|
|
122
|
-
this.entity.destroy();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### System(系统)
|
|
129
|
-
系统处理实体集合,实现游戏逻辑。
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
class HealthSystem extends es.EntitySystem {
|
|
133
|
-
protected process(entities: es.Entity[]) {
|
|
134
|
-
for (const entity of entities) {
|
|
135
|
-
const health = entity.getComponent(HealthComponent);
|
|
136
|
-
if (health && health.currentHealth <= 0) {
|
|
137
|
-
entity.destroy();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
## 🎮 高级功能
|
|
145
|
-
|
|
146
|
-
### 事件系统
|
|
147
|
-
|
|
148
|
-
```typescript
|
|
149
|
-
// 监听事件
|
|
150
|
-
es.Core.emitter.addObserver(es.CoreEvents.frameUpdated, this.onFrameUpdate, this);
|
|
151
|
-
|
|
152
|
-
// 发射自定义事件
|
|
153
|
-
es.Core.emitter.emit("playerDied", { player: entity, score: 1000 });
|
|
154
|
-
|
|
155
|
-
// 移除监听
|
|
156
|
-
es.Core.emitter.removeObserver(es.CoreEvents.frameUpdated, this.onFrameUpdate);
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### 定时器系统
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
// 延迟执行
|
|
163
|
-
es.Core.schedule(2.0, false, this, (timer) => {
|
|
164
|
-
console.log("2秒后执行");
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// 重复执行
|
|
168
|
-
es.Core.schedule(1.0, true, this, (timer) => {
|
|
169
|
-
console.log("每秒执行一次");
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### 实体查询
|
|
174
|
-
|
|
175
|
-
```typescript
|
|
176
|
-
// 按名称查找
|
|
177
|
-
const player = scene.findEntity("Player");
|
|
178
|
-
|
|
179
|
-
// 按标签查找
|
|
180
|
-
const enemies = scene.findEntitiesByTag(1);
|
|
181
|
-
|
|
182
|
-
// 按ID查找
|
|
183
|
-
const entity = scene.findEntityById(123);
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### 性能监控
|
|
187
|
-
|
|
188
|
-
```typescript
|
|
189
|
-
// 获取性能数据
|
|
190
|
-
const monitor = es.PerformanceMonitor.instance;
|
|
191
|
-
console.log("平均FPS:", monitor.averageFPS);
|
|
192
|
-
console.log("内存使用:", monitor.memoryUsage);
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## 🛠️ 开发工具
|
|
196
|
-
|
|
197
|
-
### 对象池
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
// 创建对象池
|
|
201
|
-
class BulletPool extends es.Pool<Bullet> {
|
|
202
|
-
protected createObject(): Bullet {
|
|
203
|
-
return new Bullet();
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const bulletPool = new BulletPool();
|
|
208
|
-
|
|
209
|
-
// 获取对象
|
|
210
|
-
const bullet = bulletPool.obtain();
|
|
211
|
-
|
|
212
|
-
// 释放对象
|
|
213
|
-
bulletPool.free(bullet);
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
### 实体调试
|
|
217
|
-
|
|
218
|
-
```typescript
|
|
219
|
-
// 获取实体调试信息
|
|
220
|
-
const debugInfo = entity.getDebugInfo();
|
|
221
|
-
console.log("实体信息:", debugInfo);
|
|
222
|
-
|
|
223
|
-
// 获取场景统计
|
|
224
|
-
const stats = scene.getStats();
|
|
225
|
-
console.log("场景统计:", stats);
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
## 📖 文档
|
|
229
|
-
|
|
230
|
-
- [快速入门](docs/getting-started.md) - 从零开始学习框架使用
|
|
231
|
-
- [核心概念](docs/core-concepts.md) - 深入了解 ECS 架构和设计原理
|
|
232
|
-
- [查询系统使用指南](docs/query-system-usage.md) - 学习高性能查询系统的详细用法
|
|
233
|
-
|
|
234
|
-
## 🔗 扩展库
|
|
235
|
-
|
|
236
|
-
- [路径寻找库](https://github.com/esengine/ecs-astar) - A*、广度优先、Dijkstra、GOAP 算法
|
|
237
|
-
- [AI 系统](https://github.com/esengine/BehaviourTree-ai) - 行为树、效用 AI 系统
|
|
238
|
-
|
|
239
|
-
## 🤝 贡献
|
|
240
|
-
|
|
241
|
-
欢迎提交 Issue 和 Pull Request!
|
|
242
|
-
|
|
243
|
-
### 开发环境设置
|
|
244
|
-
|
|
245
|
-
```bash
|
|
246
|
-
# 克隆项目
|
|
247
|
-
git clone https://github.com/esengine/ecs-framework.git
|
|
248
|
-
|
|
249
|
-
# 进入源码目录
|
|
250
|
-
cd ecs-framework/source
|
|
251
|
-
|
|
252
|
-
# 安装依赖
|
|
253
|
-
npm install
|
|
254
|
-
|
|
255
|
-
# 构建项目
|
|
256
|
-
npm run build
|
|
257
|
-
|
|
258
|
-
# 运行测试
|
|
259
|
-
npm test
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
### 构建要求
|
|
263
|
-
|
|
264
|
-
- Node.js >= 14.0.0
|
|
265
|
-
- TypeScript >= 4.0.0
|
|
266
|
-
|
|
267
|
-
## 📄 许可证
|
|
268
|
-
|
|
269
|
-
本项目采用 [MIT](LICENSE) 许可证。
|
|
270
|
-
|
|
271
|
-
## 💬 交流群
|
|
272
|
-
|
|
273
|
-
加入 QQ 群讨论:[ecs游戏框架交流](https://jq.qq.com/?_wv=1027&k=29w1Nud6)
|
|
274
|
-
|
|
275
|
-
---
|
|
276
|
-
|
|
277
|
-
**ECS Framework** - 让游戏开发更简单、更高效!
|