@codehz/ecs 0.3.0 → 0.3.2

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.
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
 
@@ -26,11 +26,11 @@ export declare class CommandBuffer {
26
26
  /**
27
27
  * Remove a component from an entity (deferred)
28
28
  */
29
- delete<T>(entityId: EntityId, componentType: EntityId<T>): void;
29
+ remove<T>(entityId: EntityId, componentType: EntityId<T>): void;
30
30
  /**
31
31
  * Destroy an entity (deferred)
32
32
  */
33
- destroy(entityId: EntityId): void;
33
+ delete(entityId: EntityId): void;
34
34
  /**
35
35
  * Execute all commands and clear the buffer
36
36
  */
package/index.js CHANGED
@@ -558,10 +558,10 @@ class CommandBuffer {
558
558
  set(entityId, componentType, component2) {
559
559
  this.commands.push({ type: "set", entityId, componentType, component: component2 });
560
560
  }
561
- delete(entityId, componentType) {
561
+ remove(entityId, componentType) {
562
562
  this.commands.push({ type: "delete", entityId, componentType });
563
563
  }
564
- destroy(entityId) {
564
+ delete(entityId) {
565
565
  this.commands.push({ type: "destroy", entityId });
566
566
  }
567
567
  execute() {
@@ -698,6 +698,14 @@ class Query {
698
698
  this.cachedArchetypes.push(archetype);
699
699
  }
700
700
  }
701
+ removeArchetype(archetype) {
702
+ if (this.isDisposed)
703
+ return;
704
+ const index = this.cachedArchetypes.indexOf(archetype);
705
+ if (index !== -1) {
706
+ this.cachedArchetypes.splice(index, 1);
707
+ }
708
+ }
701
709
  dispose() {
702
710
  this.world.releaseQuery(this);
703
711
  }
@@ -929,10 +937,10 @@ class World {
929
937
  if (detailedType.type === "invalid") {
930
938
  throw new Error(`Invalid component type: ${componentType}`);
931
939
  }
932
- this.commandBuffer.delete(entityId, componentType);
940
+ this.commandBuffer.remove(entityId, componentType);
933
941
  }
934
942
  delete(entityId) {
935
- this.commandBuffer.destroy(entityId);
943
+ this.commandBuffer.delete(entityId);
936
944
  }
937
945
  has(entityId, componentType) {
938
946
  const archetype = this.entityToArchetype.get(entityId);
@@ -1242,6 +1250,9 @@ class World {
1242
1250
  }
1243
1251
  }
1244
1252
  }
1253
+ for (const query of this.queries) {
1254
+ query.removeArchetype(archetype);
1255
+ }
1245
1256
  }
1246
1257
  triggerLifecycleHooks(entityId, addedComponents, removedComponents) {
1247
1258
  for (const [componentType, component2] of addedComponents) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
package/query.d.ts CHANGED
@@ -47,6 +47,10 @@ export declare class Query {
47
47
  * Check if a new archetype matches this query and add to cache if it does
48
48
  */
49
49
  checkNewArchetype(archetype: Archetype): void;
50
+ /**
51
+ * Remove an archetype from the cached archetypes
52
+ */
53
+ removeArchetype(archetype: Archetype): void;
50
54
  /**
51
55
  * Dispose the query and disconnect from world
52
56
  */