@codehz/ecs 0.2.1 → 0.2.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/entity.d.ts CHANGED
@@ -95,9 +95,13 @@ export declare function getDetailedIdType(id: EntityId<any>): {
95
95
  componentId?: never;
96
96
  targetId?: never;
97
97
  } | {
98
- type: "entity-relation" | "component-relation" | "wildcard-relation";
98
+ type: "entity-relation" | "wildcard-relation";
99
99
  componentId: ComponentId<any>;
100
100
  targetId: EntityId<any>;
101
+ } | {
102
+ type: "component-relation";
103
+ componentId: ComponentId<any>;
104
+ targetId: ComponentId<any>;
101
105
  };
102
106
  /**
103
107
  * Inspect an EntityId and return a human-readable string representation
package/index.js CHANGED
@@ -814,12 +814,20 @@ class World {
814
814
  throw new Error(`Unknown component name in snapshot: ${componentTypeRaw}`);
815
815
  }
816
816
  componentType = compId;
817
- } else if (typeof componentTypeRaw === "object" && componentTypeRaw !== null && typeof componentTypeRaw.component === "string" && typeof componentTypeRaw.target === "number") {
817
+ } else if (typeof componentTypeRaw === "object" && componentTypeRaw !== null && typeof componentTypeRaw.component === "string") {
818
818
  const compId = getComponentIdByName(componentTypeRaw.component);
819
819
  if (compId === undefined) {
820
820
  throw new Error(`Unknown component name in snapshot: ${componentTypeRaw.component}`);
821
821
  }
822
- componentType = relation(compId, componentTypeRaw.target);
822
+ if (typeof componentTypeRaw.target === "string") {
823
+ const targetCompId = getComponentIdByName(componentTypeRaw.target);
824
+ if (targetCompId === undefined) {
825
+ throw new Error(`Unknown target component name in snapshot: ${componentTypeRaw.target}`);
826
+ }
827
+ componentType = relation(compId, targetCompId);
828
+ } else {
829
+ componentType = relation(compId, componentTypeRaw.target);
830
+ }
823
831
  } else {
824
832
  throw new Error(`Invalid component type in snapshot: ${JSON.stringify(componentTypeRaw)}`);
825
833
  }
@@ -1272,12 +1280,20 @@ class World {
1272
1280
  type = getComponentNameById(rawType) || rawType;
1273
1281
  break;
1274
1282
  case "entity-relation":
1275
- case "component-relation":
1276
1283
  componentName = getComponentNameById(detailedType.componentId);
1277
1284
  if (componentName) {
1278
1285
  type = { component: componentName, target: detailedType.targetId };
1279
1286
  }
1280
1287
  break;
1288
+ case "component-relation":
1289
+ componentName = getComponentNameById(detailedType.componentId);
1290
+ if (componentName) {
1291
+ type = {
1292
+ component: componentName,
1293
+ target: getComponentNameById(detailedType.targetId) || detailedType.targetId
1294
+ };
1295
+ }
1296
+ break;
1281
1297
  }
1282
1298
  return { type, value: value === MISSING_COMPONENT ? undefined : value };
1283
1299
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
package/world.d.ts CHANGED
@@ -207,7 +207,7 @@ export type SerializedEntity = {
207
207
  export type SerializedComponent = {
208
208
  type: number | string | {
209
209
  component: string;
210
- target: number;
210
+ target: number | string;
211
211
  };
212
212
  value: any;
213
213
  };