Skip to content

Commit 9bbb6e7

Browse files
committed
docs(readme): update lifecycle hook registration methods
- Added support for wildcard relation lifecycle hooks in the documentation. - Updated the method names for registering lifecycle hooks to reflect the new unified approach. - Enhanced examples to demonstrate the usage of wildcard relation hooks.
1 parent 3763072 commit 9bbb6e7

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- 🏗️ 模块化:清晰的架构,支持自定义系统和组件
1010
- 📦 轻量级:零依赖,易于集成
1111
- ⚡ 内存高效:连续内存布局,优化的迭代性能
12+
- 🎣 生命周期钩子:支持组件和通配符关系的事件监听
1213

1314
## 安装
1415

@@ -58,7 +59,7 @@ ECS 支持在组件添加或移除时执行回调函数:
5859

5960
```typescript
6061
// 注册组件生命周期钩子
61-
world.registerComponentLifecycleHook(PositionId, {
62+
world.registerLifecycleHook(PositionId, {
6263
onAdded: (entityId, componentType, component) => {
6364
console.log(`组件 ${componentType} 被添加到实体 ${entityId}`);
6465
},
@@ -68,7 +69,7 @@ world.registerComponentLifecycleHook(PositionId, {
6869
});
6970

7071
// 你也可以只注册其中一个钩子
71-
world.registerComponentLifecycleHook(VelocityId, {
72+
world.registerLifecycleHook(VelocityId, {
7273
onRemoved: (entityId, componentType) => {
7374
console.log(`组件 ${componentType} 被从实体 ${entityId} 移除`);
7475
},
@@ -79,6 +80,45 @@ world.addComponent(entity, PositionId, { x: 0, y: 0 });
7980
world.flushCommands(); // 钩子在这里被调用
8081
```
8182

83+
### 通配符关系生命周期钩子
84+
85+
ECS 还支持通配符关系生命周期钩子,可以监听特定组件的所有关系变化:
86+
87+
```typescript
88+
import { World, createComponentId, createRelationId } from "@codehz/ecs";
89+
90+
// 定义组件类型
91+
type Position = { x: number; y: number };
92+
93+
// 定义组件ID
94+
const PositionId = createComponentId<Position>(1);
95+
96+
// 创建世界
97+
const world = new World();
98+
99+
// 创建实体
100+
const entity = world.createEntity();
101+
102+
// 创建通配符关系ID,用于监听所有 Position 相关的关系
103+
const wildcardPositionRelation = createRelationId(PositionId, "*");
104+
105+
// 注册通配符关系钩子
106+
world.registerLifecycleHook(wildcardPositionRelation, {
107+
onAdded: (entityId, componentType, component) => {
108+
console.log(`关系组件 ${componentType} 被添加到实体 ${entityId}`);
109+
},
110+
onRemoved: (entityId, componentType) => {
111+
console.log(`关系组件 ${componentType} 被从实体 ${entityId} 移除`);
112+
},
113+
});
114+
115+
// 创建实体间的关系
116+
const entity2 = world.createEntity();
117+
const positionRelation = createRelationId(PositionId, entity2);
118+
world.addComponent(entity, positionRelation, { x: 10, y: 20 });
119+
world.flushCommands(); // 通配符钩子会被触发
120+
```
121+
82122
### 运行示例
83123

84124
```bash
@@ -100,8 +140,8 @@ bun run examples/simple/demo.ts
100140
- `removeComponent(entity, componentId)`: 从实体移除组件
101141
- `createQuery(componentIds)`: 创建查询
102142
- `registerSystem(system)`: 注册系统
103-
- `registerComponentLifecycleHook(componentId, hook)`: 注册组件生命周期钩子
104-
- `unregisterComponentLifecycleHook(componentId, hook)`: 注销组件生命周期钩子
143+
- `registerLifecycleHook(componentId, hook)`: 注册组件或通配符关系生命周期钩子
144+
- `unregisterLifecycleHook(componentId, hook)`: 注销组件或通配符关系生命周期钩子
105145
- `update(deltaTime)`: 更新世界
106146
- `flushCommands()`: 应用命令缓冲区
107147

0 commit comments

Comments
 (0)