@codehz/ecs 0.1.3 → 0.1.4

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 (3) hide show
  1. package/README.md +3 -0
  2. package/index.js +6 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -171,6 +171,8 @@ bun run examples/simple/demo.ts
171
171
 
172
172
  - `new()`: 创建新实体
173
173
  - `set(entity, componentId, data)`: 向实体添加组件
174
+ - `get(entity, componentId)`: 获取实体的组件数据(注意:只能获取已设置的组件,使用前请先用 `has()` 检查组件是否存在)
175
+ - `has(entity, componentId)`: 检查实体是否拥有指定组件
174
176
  - `delete(entity, componentId)`: 从实体移除组件
175
177
  - `setExclusive(componentId)`: 将组件标记为独占关系
176
178
  - `createQuery(componentIds)`: 创建查询
@@ -251,6 +253,7 @@ const restored = World.deserialize(readySnapshot);
251
253
 
252
254
  注意事项
253
255
 
256
+ - **重要警告**:`get()` 方法只能获取实体已设置的组件。如果尝试获取不存在的组件,会抛出错误。由于 `undefined` 是组件的有效值,不能使用 `get()` 的返回值是否为 `undefined` 来判断组件是否存在。请在使用 `get()` 之前先用 `has()` 方法检查组件是否存在。
254
257
  - 快照只包含实体、组件、以及 `EntityIdManager` 的分配器状态(用于保留下一次分配的 ID);并不会自动恢复已注册的系统、查询缓存或生命周期钩子。恢复后应由应用负责重新注册系统与钩子。
255
258
  - 若需要跨版本兼容,建议在持久化格式中包含 `version` 字段,并在恢复时进行格式兼容性检查与迁移。
256
259
 
package/index.js CHANGED
@@ -790,6 +790,12 @@ class World {
790
790
  if (!archetype) {
791
791
  throw new Error(`Entity ${entityId} does not exist`);
792
792
  }
793
+ const detailedType = getDetailedIdType(componentType);
794
+ if (detailedType.type !== "wildcard-relation") {
795
+ if (!archetype.componentTypes.includes(componentType)) {
796
+ throw new Error(`Entity ${entityId} does not have component ${componentType}. Use has() to check component existence before calling get().`);
797
+ }
798
+ }
793
799
  return archetype.get(entityId, componentType);
794
800
  }
795
801
  registerSystem(system) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",