2025-06-07 20:32:43 +08:00
|
|
|
|
# ECS Framework
|
|
|
|
|
|
|
2025-07-18 14:59:00 +08:00
|
|
|
|
[](https://github.com/esengine/ecs-framework/actions)
|
2025-06-07 20:32:43 +08:00
|
|
|
|
[](https://badge.fury.io/js/%40esengine%2Fecs-framework)
|
2025-09-28 12:26:51 +08:00
|
|
|
|
[](https://www.typescriptlang.org/)
|
2025-06-07 20:32:43 +08:00
|
|
|
|
[](https://opensource.org/licenses/MIT)
|
2025-08-06 17:04:02 +08:00
|
|
|
|
[](https://github.com/esengine/ecs-framework/stargazers)
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
一个高性能的 TypeScript ECS (Entity-Component-System) 框架,专为现代游戏开发而设计。
|
2025-08-06 17:04:02 +08:00
|
|
|
|
|
|
|
|
|
|
## 特性
|
|
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
- **高性能** - 针对大规模实体优化,支持SoA存储和批量处理
|
|
|
|
|
|
- **类型安全** - 完整的TypeScript支持,编译时类型检查
|
|
|
|
|
|
- **现代架构** - 支持多World、多Scene的分层架构设计
|
|
|
|
|
|
- **开发友好** - 内置调试工具和性能监控
|
|
|
|
|
|
- **跨平台** - 支持Cocos Creator、Laya引擎和Web平台
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
|
|
|
|
|
## 安装
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
npm install @esengine/ecs-framework
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2025-06-10 13:12:14 +08:00
|
|
|
|
## 快速开始
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
|
|
|
|
|
```typescript
|
2025-09-28 12:26:51 +08:00
|
|
|
|
import { Core, Scene, Component, EntitySystem, ECSComponent, ECSSystem, Matcher, Time } from '@esengine/ecs-framework';
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-08-06 17:04:02 +08:00
|
|
|
|
// 定义组件
|
2025-09-28 12:26:51 +08:00
|
|
|
|
@ECSComponent('Position')
|
|
|
|
|
|
class Position extends Component {
|
|
|
|
|
|
constructor(public x = 0, public y = 0) {
|
2025-06-09 13:25:10 +08:00
|
|
|
|
super();
|
2025-06-07 20:32:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-09 13:25:10 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
@ECSComponent('Velocity')
|
|
|
|
|
|
class Velocity extends Component {
|
|
|
|
|
|
constructor(public dx = 0, public dy = 0) {
|
2025-06-09 13:25:10 +08:00
|
|
|
|
super();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 17:04:02 +08:00
|
|
|
|
// 创建系统
|
2025-09-28 12:26:51 +08:00
|
|
|
|
@ECSSystem('Movement')
|
2025-08-14 18:38:09 +08:00
|
|
|
|
class MovementSystem extends EntitySystem {
|
|
|
|
|
|
constructor() {
|
2025-09-28 12:26:51 +08:00
|
|
|
|
super(Matcher.all(Position, Velocity));
|
2025-08-14 18:38:09 +08:00
|
|
|
|
}
|
2025-06-12 09:42:35 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
protected process(entities: readonly Entity[]): void {
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
|
|
const position = entity.getComponent(Position)!;
|
|
|
|
|
|
const velocity = entity.getComponent(Velocity)!;
|
2025-06-09 13:25:10 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
position.x += velocity.dx * Time.deltaTime;
|
|
|
|
|
|
position.y += velocity.dy * Time.deltaTime;
|
|
|
|
|
|
}
|
2025-08-11 09:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
// 创建场景并启动
|
|
|
|
|
|
class GameScene extends Scene {
|
|
|
|
|
|
protected initialize(): void {
|
|
|
|
|
|
this.addSystem(new MovementSystem());
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
const player = this.createEntity("Player");
|
|
|
|
|
|
player.addComponent(new Position(100, 100));
|
|
|
|
|
|
player.addComponent(new Velocity(50, 0));
|
2025-06-09 13:25:10 +08:00
|
|
|
|
}
|
2025-06-10 13:12:14 +08:00
|
|
|
|
}
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
// 启动游戏
|
|
|
|
|
|
Core.create();
|
|
|
|
|
|
Core.setScene(new GameScene());
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
// 游戏循环中更新
|
|
|
|
|
|
function gameLoop(deltaTime: number) {
|
2025-08-06 17:04:02 +08:00
|
|
|
|
Core.update(deltaTime);
|
|
|
|
|
|
}
|
2025-06-10 13:12:14 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
## 核心特性
|
2025-06-08 21:50:50 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
- **实体查询** - 使用 Matcher API 进行高效的实体过滤
|
|
|
|
|
|
- **事件系统** - 类型安全的事件发布/订阅机制
|
|
|
|
|
|
- **性能优化** - SoA 存储优化,支持大规模实体处理
|
|
|
|
|
|
- **多场景** - 支持 World/Scene 分层架构
|
|
|
|
|
|
- **时间管理** - 内置定时器和时间控制系统
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
## 平台支持
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
支持主流游戏引擎和 Web 平台:
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
- **Cocos Creator** - 内置引擎集成支持,提供[专用调试插件](https://store.cocos.com/app/detail/7823)
|
|
|
|
|
|
- **Laya 引擎** - 完整的生命周期管理
|
|
|
|
|
|
- **原生 Web** - 浏览器环境直接运行
|
|
|
|
|
|
- **小游戏平台** - 微信、支付宝等小游戏
|
2025-06-10 13:12:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
## 示例项目
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
- [割草机演示](https://github.com/esengine/lawn-mower-demo) - 完整的游戏示例
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-06-10 13:12:14 +08:00
|
|
|
|
## 文档
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
- [快速入门](https://esengine.github.io/ecs-framework/guide/getting-started.html) - 详细教程和平台集成
|
|
|
|
|
|
- [完整指南](https://esengine.github.io/ecs-framework/guide/) - ECS 概念和使用指南
|
|
|
|
|
|
- [API 参考](https://esengine.github.io/ecs-framework/api/) - 完整 API 文档
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
## 生态系统
|
2025-06-09 13:25:10 +08:00
|
|
|
|
|
2025-08-06 17:04:02 +08:00
|
|
|
|
- [路径寻找](https://github.com/esengine/ecs-astar) - A*、BFS、Dijkstra 算法
|
2025-06-10 13:12:14 +08:00
|
|
|
|
- [AI 系统](https://github.com/esengine/BehaviourTree-ai) - 行为树、效用 AI
|
|
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
## 社区与支持
|
2025-06-09 13:25:10 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
- [问题反馈](https://github.com/esengine/ecs-framework/issues) - Bug 报告和功能建议
|
|
|
|
|
|
- [QQ 交流群](https://jq.qq.com/?_wv=1027&k=29w1Nud6) - ecs游戏框架交流
|
2025-06-09 13:25:10 +08:00
|
|
|
|
|
2025-06-10 13:12:14 +08:00
|
|
|
|
## 许可证
|
2025-06-07 20:32:43 +08:00
|
|
|
|
|
2025-09-28 12:26:51 +08:00
|
|
|
|
[MIT](LICENSE) © 2025 ECS Framework
|