Skip to content

Commit 293467b

Browse files
committed
initial commit
0 parents  commit 293467b

31 files changed

+3608
-0
lines changed

.github/copilot-instructions.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# @codehz/ecs - AI Coding Guidelines
2+
3+
## Project Overview
4+
This is an Entity Component System (ECS) library built with TypeScript and Bun runtime. The project follows modern TypeScript practices with strict type checking and ESNext features.
5+
6+
## Runtime & Environment
7+
- **Runtime**: Bun (not Node.js) - use `bun` commands instead of `npm`/`yarn`
8+
- **Language**: TypeScript with ESNext target and strict mode enabled
9+
- **Module System**: ES modules with `"module": "Preserve"` and bundler resolution
10+
11+
## Development Workflow
12+
- **Install**: `bun install` (not `npm install`)
13+
- **Run**: `bun run src/index.ts` (direct execution, no build step)
14+
- **TypeScript**: `bunx tsc --noEmit` (Configured for modern features with strict checking - noEmit mode for bundler compatibility)
15+
- **Testing**: `bun test` (set up tests as needed)
16+
17+
## Code Patterns
18+
- **Imports**: Use ES module syntax with `.ts` extensions allowed due to `"allowImportingTsExtensions": true`
19+
- **Type Checking**: Strict mode with additional checks like `noUncheckedIndexedAccess` and `noImplicitOverride`
20+
21+
## Architecture Notes
22+
- **Entry Point**: `src/index.ts` - single file currently, expand with ECS components/systems
23+
- **Dependencies**: Minimal setup - add peer dependencies for TypeScript integration
24+
25+
## Key Files
26+
- `tsconfig.json`: Modern TypeScript config with bundler mode
27+
- `package.json`: Module configuration with Bun-specific settings
28+
- `src/index.ts`: Main entry point (currently minimal)</content>
29+
<parameter name="filePath">d:\Developer\ecs\.github\copilot-instructions.md

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 120
3+
}

README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# @codehz/ecs
2+
3+
一个高性能的Entity Component System (ECS) 库,使用 TypeScript 和 Bun 运行时构建。
4+
5+
## 特性
6+
7+
- 🚀 高性能:基于原型的组件存储和高效的查询系统
8+
- 🔧 类型安全:完整的 TypeScript 支持
9+
- 🏗️ 模块化:清晰的架构,支持自定义系统和组件
10+
- 📦 轻量级:零依赖,易于集成
11+
12+
## 安装
13+
14+
```bash
15+
bun install
16+
```
17+
18+
## 用法
19+
20+
### 基本示例
21+
22+
```typescript
23+
import { World } from "@codehz/ecs";
24+
import { createComponentId } from "@codehz/ecs";
25+
26+
// 定义组件类型
27+
type Position = { x: number; y: number };
28+
type Velocity = { x: number; y: number };
29+
30+
// 定义组件ID
31+
const PositionId = createComponentId<Position>(1);
32+
const VelocityId = createComponentId<Velocity>(2);
33+
34+
// 创建世界
35+
const world = new World();
36+
37+
// 创建实体
38+
const entity = world.createEntity();
39+
world.addComponent(entity, PositionId, { x: 0, y: 0 });
40+
world.addComponent(entity, VelocityId, { x: 1, y: 0.5 });
41+
42+
// 应用更改
43+
world.flushCommands();
44+
45+
// 创建查询并更新
46+
const query = world.createQuery([PositionId, VelocityId]);
47+
query.forEach([PositionId, VelocityId], (entity, position, velocity) => {
48+
position.x += velocity.x * deltaTime;
49+
position.y += velocity.y * deltaTime;
50+
});
51+
```
52+
53+
### 运行示例
54+
55+
```bash
56+
bun run examples/simple/demo.ts
57+
```
58+
59+
## API 概述
60+
61+
### World
62+
63+
- `createEntity()`: 创建新实体
64+
- `addComponent(entity, componentId, data)`: 向实体添加组件
65+
- `removeComponent(entity, componentId)`: 从实体移除组件
66+
- `createQuery(componentIds)`: 创建查询
67+
- `registerSystem(system)`: 注册系统
68+
- `update(deltaTime)`: 更新世界
69+
- `flushCommands()`: 应用命令缓冲区
70+
71+
### Entity
72+
73+
- `createComponentId<T>(id)`: 创建类型安全的组件ID
74+
75+
### Query
76+
77+
- `forEach(componentIds, callback)`: 遍历匹配的实体
78+
79+
### System
80+
81+
实现 `System` 接口来创建自定义系统:
82+
83+
```typescript
84+
class MySystem implements System {
85+
update(world: World, deltaTime: number): void {
86+
// 系统逻辑
87+
}
88+
}
89+
```
90+
91+
## 开发
92+
93+
### 运行测试
94+
95+
```bash
96+
bun test
97+
```
98+
99+
### 类型检查
100+
101+
```bash
102+
bunx tsc --noEmit
103+
```
104+
105+
## 项目结构
106+
107+
```
108+
src/
109+
├── index.ts # 入口文件
110+
├── entity.ts # 实体和组件管理
111+
├── world.ts # 世界管理
112+
├── archetype.ts # 原型系统
113+
├── query.ts # 查询系统
114+
├── system.ts # 系统接口
115+
├── command-buffer.ts # 命令缓冲区
116+
├── types.ts # 类型定义
117+
└── utils.ts # 工具函数
118+
119+
examples/
120+
└── simple/
121+
├── demo.ts # 基本示例
122+
└── README.md # 示例说明
123+
```
124+
125+
## 许可证
126+
127+
MIT
128+
129+
## 贡献
130+
131+
欢迎提交 Issue 和 Pull Request!

bun.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/simple/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ECS Simple Demo
2+
3+
这是一个简单的Entity Component System (ECS) 演示,展示了基本的ECS概念:
4+
5+
- **实体 (Entities)**: 游戏对象的基本单位
6+
- **组件 (Components)**: 实体的属性数据
7+
- **系统 (Systems)**: 处理组件数据的逻辑
8+
- **世界 (World)**: 管理所有实体、组件和系统的容器
9+
10+
## 演示内容
11+
12+
这个demo创建了两个实体,每个实体都有位置(Position)和速度(Velocity)组件:
13+
14+
- 实体1: 从 (0, 0) 开始,以 (1, 0.5) 的速度移动
15+
- 实体2: 从 (10, 10) 开始,以 (-0.5, 1) 的速度移动
16+
17+
移动系统每帧更新实体的位置,并打印当前位置。
18+
19+
## 运行方法
20+
21+
确保你已经安装了Bun运行时,然后在项目根目录运行:
22+
23+
```bash
24+
bun run examples/simple/demo.ts
25+
```
26+
27+
## 优化说明
28+
29+
为了提高性能和代码简洁性,demo使用了以下优化:
30+
31+
- **预先缓存Query**: 在系统初始化时创建查询并缓存,而不是在每次update中重新创建。这避免了重复的查询创建开销。
32+
- **使用forEach接口**: 使用Query的`forEach`方法直接获取组件数据,避免手动调用`world.getComponent()`,减少函数调用开销并提高代码可读性。
33+
34+
## 输出示例
35+
36+
```
37+
ECS Simple Demo
38+
39+
Update 1:
40+
Entity 1024: Position (1.00, 0.50)
41+
Entity 1025: Position (9.50, 11.00)
42+
43+
Update 2:
44+
Entity 1024: Position (2.00, 1.00)
45+
Entity 1025: Position (9.00, 12.00)
46+
47+
...
48+
```

examples/simple/demo.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { World } from "../../src/world";
2+
import { createComponentId } from "../../src/entity";
3+
import type { System } from "../../src/system";
4+
import type { Query } from "../../src/query";
5+
6+
// 定义组件类型
7+
type Position = { x: number; y: number };
8+
type Velocity = { x: number; y: number };
9+
10+
// 定义组件ID
11+
const PositionId = createComponentId<Position>(1);
12+
const VelocityId = createComponentId<Velocity>(2);
13+
14+
// 移动系统
15+
class MovementSystem implements System {
16+
private query: Query; // 缓存查询
17+
18+
constructor(world: World) {
19+
// 在构造函数中预先创建并缓存查询
20+
this.query = world.createQuery([PositionId, VelocityId]);
21+
}
22+
23+
update(world: World, deltaTime: number): void {
24+
// 使用缓存的查询的forEach方法,直接获取组件数据
25+
this.query.forEach([PositionId, VelocityId], (entity, position, velocity) => {
26+
// 更新位置
27+
position.x += velocity.x * deltaTime;
28+
position.y += velocity.y * deltaTime;
29+
30+
console.log(`Entity ${entity}: Position (${position.x.toFixed(2)}, ${position.y.toFixed(2)})`);
31+
});
32+
}
33+
}
34+
35+
function main() {
36+
console.log("ECS Simple Demo");
37+
38+
// 创建世界
39+
const world = new World();
40+
41+
// 注册系统(传递world参数)
42+
world.registerSystem(new MovementSystem(world));
43+
44+
// 创建实体1
45+
const entity1 = world.createEntity();
46+
world.addComponent(entity1, PositionId, { x: 0, y: 0 });
47+
world.addComponent(entity1, VelocityId, { x: 1, y: 0.5 });
48+
49+
// 创建实体2
50+
const entity2 = world.createEntity();
51+
world.addComponent(entity2, PositionId, { x: 10, y: 10 });
52+
world.addComponent(entity2, VelocityId, { x: -0.5, y: 1 });
53+
54+
// 执行命令以应用组件添加
55+
world.flushCommands();
56+
57+
// 运行几个更新循环
58+
const deltaTime = 1.0; // 1秒
59+
for (let i = 0; i < 5; i++) {
60+
console.log(`\nUpdate ${i + 1}:`);
61+
world.update(deltaTime);
62+
}
63+
64+
console.log("\nDemo completed!");
65+
}
66+
67+
// 运行demo
68+
main();

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@codehz/ecs",
3+
"module": "src/index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest"
7+
},
8+
"peerDependencies": {
9+
"typescript": "~5.9.2"
10+
}
11+
}

0 commit comments

Comments
 (0)