@@ -55,7 +55,11 @@ query.forEach([PositionId, VelocityId], (entity, position, velocity) => {
5555
5656### 组件生命周期钩子
5757
58- ECS 支持在组件添加或移除时执行回调函数:
58+ ECS 支持在组件添加或移除时执行回调函数。钩子回调函数的参数如下:
59+
60+ - ` entityId ` : 实体的 ID (number)
61+ - ` componentType ` : 组件类型 ID (EntityId)
62+ - ` component ` : 组件数据值 (T)
5963
6064``` typescript
6165// 注册组件生命周期钩子
@@ -130,15 +134,12 @@ ECS 支持 Exclusive Relations,确保实体对于指定的组件类型最多
130134``` typescript
131135import { World , component , relation } from " @codehz/ecs" ;
132136
133- // 定义组件ID
134- const ChildOf = component (); // 空组件,用于关系
137+ // 定义组件ID,设置为独占关系
138+ const ChildOf = component ({ exclusive: true } ); // 空组件,用于关系
135139
136140// 创建世界
137141const world = new World ();
138142
139- // 设置 ChildOf 为独占关系
140- world .setExclusive (ChildOf );
141-
142143// 创建实体
143144const child = world .new ();
144145const parent1 = world .new ();
@@ -173,22 +174,27 @@ bun run examples/simple/demo.ts
173174### World
174175
175176- ` new() ` : 创建新实体
177+ - ` spawn() ` : 创建 EntityBuilder 用于流式实体创建
178+ - ` spawnMany(count, configure) ` : 批量创建多个实体
179+ - ` exists(entity) ` : 检查实体是否存在
176180- ` set(entity, componentId, data) ` : 向实体添加组件
177181- ` get(entity, componentId) ` : 获取实体的组件数据(注意:只能获取已设置的组件,使用前请先用 ` has() ` 检查组件是否存在)
178182- ` has(entity, componentId) ` : 检查实体是否拥有指定组件
179- - ` delete(entity, componentId) ` : 从实体移除组件
180- - ` setExclusive(componentId) ` : 将组件标记为独占关系
181- - ` createQuery(componentIds) ` : 创建查询
183+ - ` remove(entity, componentId) ` : 从实体移除组件
184+ - ` delete(entity) ` : 销毁实体及其所有组件
185+ - ` query(componentIds) ` : 快速查询具有指定组件的实体
186+ - ` createQuery(componentIds) ` : 创建可重用的查询对象
182187- ` hook(componentId, hook) ` : 注册组件或通配符关系生命周期钩子
183188- ` unhook(componentId, hook) ` : 注销组件或通配符关系生命周期钩子
189+ - ` serialize() ` : 序列化世界状态为快照对象
184190- ` sync() ` : 执行所有延迟命令
185191
186192### 序列化(快照)
187193
188194库提供了对世界状态的「内存快照」序列化接口,用于保存/恢复实体与组件的数据。注意关键点:
189195
190- - ` World .serialize()` 返回一个内存中的快照对象(snapshot),快照会按引用保存组件的实际值;它不会对数据做 JSON.stringify 操作,也不会尝试把组件值转换为可序列化格式。
191- - ` World.deserialize (snapshot) ` 接受由 ` World .serialize()` 生成的快照对象并重建世界状态。它期望一个内存对象(非 JSON 字符串)。
196+ - ` world .serialize()` 返回一个内存中的快照对象(snapshot),快照会按引用保存组件的实际值;它不会对数据做 JSON.stringify 操作,也不会尝试把组件值转换为可序列化格式。
197+ - ` new World(snapshot)` 通过构造函数接受由 ` world .serialize()` 生成的快照对象并重建世界状态。它期望一个内存对象(非 JSON 字符串)。
192198
193199为什么采用这种设计?很多情况下组件值可能包含函数、类实例、循环引用或其他无法用 JSON 表示的值。库不对组件值强行进行序列化/字符串化,以避免数据丢失或不可信的自动转换。
194200
@@ -199,7 +205,7 @@ bun run examples/simple/demo.ts
199205const snapshot = world .serialize ();
200206
201207// 在同一进程内直接恢复
202- const restored = World . deserialize (snapshot );
208+ const restored = new World (snapshot );
203209```
204210
205211持久化到磁盘或跨进程传输
@@ -220,7 +226,7 @@ const text = JSON.stringify(snapshot);
220226
221227// 恢复:parse -> deserialize
222228const parsed = JSON .parse (text );
223- const restored = World . deserialize (parsed );
229+ const restored = new World (parsed );
224230```
225231
226232示例:带自定义编码的持久化(伪代码)
@@ -249,7 +255,7 @@ const readySnapshot = {
249255 })),
250256};
251257
252- const restored = World . deserialize (readySnapshot );
258+ const restored = new World (readySnapshot );
253259```
254260
255261注意事项
@@ -264,11 +270,24 @@ const restored = World.deserialize(readySnapshot);
264270
265271### Query
266272
267- - ` forEach(componentIds, callback) ` : 遍历匹配的实体
273+ - ` forEach(componentIds, callback) ` : 遍历匹配的实体,为每个实体调用回调函数
268274- ` getEntities() ` : 获取所有匹配实体的ID列表
269- - ` getEntitiesWithComponents(componentIds) ` : 获取实体及其组件数据
275+ - ` getEntitiesWithComponents(componentIds) ` : 获取实体及其组件数据的对象数组
276+ - ` iterate(componentIds) ` : 返回一个生成器,用于遍历匹配的实体及其组件数据
277+ - ` getComponentData(componentType) ` : 获取指定组件类型的所有匹配实体的数据数组
278+ - ` dispose() ` : 释放查询资源,停止接收世界更新通知
279+
280+ ### EntityBuilder
270281
271- ## 从 System 迁移到 Pipeline
282+ EntityBuilder 提供流式 API 用于便捷的实体创建:
283+
284+ - ` with(componentId, value) ` : 添加组件到构建器
285+ - ` withTag(componentId) ` : 添加标记组件(无值)到构建器
286+ - ` withRelation(componentId, targetEntity, value) ` : 添加关系组件到构建器
287+ - ` withRelationTag(componentId, targetEntity) ` : 添加关系标记(无值)到构建器
288+ - ` build() ` : 创建实体并应用所有组件(需要手动调用 ` world.sync() ` )
289+
290+ ### World
272291
273292从 v0.4.0 开始,本库移除了内置的 ` System ` 和 ` SystemScheduler ` 功能。推荐使用 ` @codehz/pipeline ` 作为替代方案来组织游戏循环逻辑。
274293
0 commit comments