@codehz/ecs 0.2.1 → 0.2.3

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
@@ -601,7 +601,12 @@ function matchesComponentTypes(archetype, componentTypes) {
601
601
  return componentTypes.every((type) => {
602
602
  const detailedType = getDetailedIdType(type);
603
603
  if (detailedType.type === "wildcard-relation") {
604
- return archetype.componentTypes.includes(detailedType.componentId);
604
+ return archetype.componentTypes.some((archetypeType) => {
605
+ if (!isRelationId(archetypeType))
606
+ return false;
607
+ const decoded = decodeRelationId(archetypeType);
608
+ return decoded.componentId === detailedType.componentId;
609
+ });
605
610
  } else {
606
611
  return archetype.componentTypes.includes(type);
607
612
  }
@@ -814,12 +819,20 @@ class World {
814
819
  throw new Error(`Unknown component name in snapshot: ${componentTypeRaw}`);
815
820
  }
816
821
  componentType = compId;
817
- } else if (typeof componentTypeRaw === "object" && componentTypeRaw !== null && typeof componentTypeRaw.component === "string" && typeof componentTypeRaw.target === "number") {
822
+ } else if (typeof componentTypeRaw === "object" && componentTypeRaw !== null && typeof componentTypeRaw.component === "string") {
818
823
  const compId = getComponentIdByName(componentTypeRaw.component);
819
824
  if (compId === undefined) {
820
825
  throw new Error(`Unknown component name in snapshot: ${componentTypeRaw.component}`);
821
826
  }
822
- componentType = relation(compId, componentTypeRaw.target);
827
+ if (typeof componentTypeRaw.target === "string") {
828
+ const targetCompId = getComponentIdByName(componentTypeRaw.target);
829
+ if (targetCompId === undefined) {
830
+ throw new Error(`Unknown target component name in snapshot: ${componentTypeRaw.target}`);
831
+ }
832
+ componentType = relation(compId, targetCompId);
833
+ } else {
834
+ componentType = relation(compId, componentTypeRaw.target);
835
+ }
823
836
  } else {
824
837
  throw new Error(`Invalid component type in snapshot: ${JSON.stringify(componentTypeRaw)}`);
825
838
  }
@@ -1272,12 +1285,20 @@ class World {
1272
1285
  type = getComponentNameById(rawType) || rawType;
1273
1286
  break;
1274
1287
  case "entity-relation":
1275
- case "component-relation":
1276
1288
  componentName = getComponentNameById(detailedType.componentId);
1277
1289
  if (componentName) {
1278
1290
  type = { component: componentName, target: detailedType.targetId };
1279
1291
  }
1280
1292
  break;
1293
+ case "component-relation":
1294
+ componentName = getComponentNameById(detailedType.componentId);
1295
+ if (componentName) {
1296
+ type = {
1297
+ component: componentName,
1298
+ target: getComponentNameById(detailedType.targetId) || detailedType.targetId
1299
+ };
1300
+ }
1301
+ break;
1281
1302
  }
1282
1303
  return { type, value: value === MISSING_COMPONENT ? undefined : value };
1283
1304
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
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
  };