@codehz/ecs 0.3.0 → 0.3.1

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.
Files changed (2) hide show
  1. package/README.md +14 -10
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -60,18 +60,22 @@ ECS 支持在组件添加或移除时执行回调函数:
60
60
 
61
61
  ```typescript
62
62
  // 注册组件生命周期钩子
63
- world.registerLifecycleHook(PositionId, {
64
- onAdded: (entityId, componentType, component) => {
63
+ world.hook(PositionId, {
64
+ on_init: (entityId, componentType, component) => {
65
+ // 当钩子注册时,为现有实体上的组件调用
66
+ console.log(`现有组件 ${componentType} 在实体 ${entityId}`);
67
+ },
68
+ on_set: (entityId, componentType, component) => {
65
69
  console.log(`组件 ${componentType} 被添加到实体 ${entityId}`);
66
70
  },
67
- onRemoved: (entityId, componentType) => {
71
+ on_remove: (entityId, componentType, component) => {
68
72
  console.log(`组件 ${componentType} 被从实体 ${entityId} 移除`);
69
73
  },
70
74
  });
71
75
 
72
76
  // 你也可以只注册其中一个钩子
73
- world.registerLifecycleHook(VelocityId, {
74
- onRemoved: (entityId, componentType) => {
77
+ world.hook(VelocityId, {
78
+ on_remove: (entityId, componentType, component) => {
75
79
  console.log(`组件 ${componentType} 被从实体 ${entityId} 移除`);
76
80
  },
77
81
  });
@@ -104,11 +108,11 @@ const entity = world.new();
104
108
  const wildcardPositionRelation = relation(PositionId, "*");
105
109
 
106
110
  // 注册通配符关系钩子
107
- world.registerLifecycleHook(wildcardPositionRelation, {
108
- onAdded: (entityId, componentType, component) => {
111
+ world.hook(wildcardPositionRelation, {
112
+ on_set: (entityId, componentType, component) => {
109
113
  console.log(`关系组件 ${componentType} 被添加到实体 ${entityId}`);
110
114
  },
111
- onRemoved: (entityId, componentType) => {
115
+ on_remove: (entityId, componentType, component) => {
112
116
  console.log(`关系组件 ${componentType} 被从实体 ${entityId} 移除`);
113
117
  },
114
118
  });
@@ -177,8 +181,8 @@ bun run examples/simple/demo.ts
177
181
  - `setExclusive(componentId)`: 将组件标记为独占关系
178
182
  - `createQuery(componentIds)`: 创建查询
179
183
  - `registerSystem(system, dependencies?)`: 注册系统
180
- - `registerLifecycleHook(componentId, hook)`: 注册组件或通配符关系生命周期钩子
181
- - `unregisterLifecycleHook(componentId, hook)`: 注销组件或通配符关系生命周期钩子
184
+ - `hook(componentId, hook)`: 注册组件或通配符关系生命周期钩子
185
+ - `unhook(componentId, hook)`: 注销组件或通配符关系生命周期钩子
182
186
  - `update(...params)`: 更新世界(参数取决于泛型配置)
183
187
  - `sync()`: 应用命令缓冲区
184
188
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",