@codehz/ecs 0.10.0 → 0.10.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.
- package/dist/builder.d.mts +11 -2
- package/dist/world.mjs +19 -6
- package/dist/world.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/component/singleton.test.ts +50 -5
- package/src/world/operations.ts +15 -3
- package/src/world/singleton.ts +3 -2
- package/src/world/world.ts +17 -1
package/dist/builder.d.mts
CHANGED
|
@@ -1022,8 +1022,9 @@ interface SingletonHandleOps<T> {
|
|
|
1022
1022
|
/**
|
|
1023
1023
|
* Explicit handle for a singleton component (component-as-entity).
|
|
1024
1024
|
*
|
|
1025
|
-
* This
|
|
1026
|
-
*
|
|
1025
|
+
* This is the preferred API for singleton components.
|
|
1026
|
+
* `world.set(componentId, value)` remains available only as a deprecated
|
|
1027
|
+
* compatibility shorthand.
|
|
1027
1028
|
*
|
|
1028
1029
|
* @example
|
|
1029
1030
|
* const config = world.singleton(Config);
|
|
@@ -1050,6 +1051,7 @@ declare class SingletonHandle<T = void> {
|
|
|
1050
1051
|
* Manages entities and components
|
|
1051
1052
|
*/
|
|
1052
1053
|
declare class World {
|
|
1054
|
+
private static readonly DEPRECATED_SINGLETON_SET_SHORTHAND_WARNING;
|
|
1053
1055
|
private entityIdManager;
|
|
1054
1056
|
private entityReferences;
|
|
1055
1057
|
/** Sparse relation storage (for components created with `sparse: true`), shared with all Archetype instances */
|
|
@@ -1119,6 +1121,10 @@ declare class World {
|
|
|
1119
1121
|
* @overload set(entityId: EntityId, componentType: EntityId<void>): void
|
|
1120
1122
|
* Marks a void component as present on the entity
|
|
1121
1123
|
*
|
|
1124
|
+
* @overload set<T>(componentId: ComponentId<T>, component: Exclude<NoInfer<T>, number>): void
|
|
1125
|
+
* @deprecated Use `world.singleton(componentId).set(value)` or `world.set(componentId, componentId, value)` instead.
|
|
1126
|
+
* Compatibility shorthand for singleton component data when the second argument is not a number
|
|
1127
|
+
*
|
|
1122
1128
|
* @overload set<T>(entityId: EntityId, componentType: EntityId<T>, component: NoInfer<T>): void
|
|
1123
1129
|
* Adds or updates a component with data on the entity
|
|
1124
1130
|
*
|
|
@@ -1129,9 +1135,12 @@ declare class World {
|
|
|
1129
1135
|
* world.set(entity, Position, { x: 10, y: 20 });
|
|
1130
1136
|
* world.set(entity, Marker); // void component
|
|
1131
1137
|
* world.singleton(GlobalConfig).set({ debug: true }); // singleton component
|
|
1138
|
+
* world.set(GlobalConfig, { debug: true }); // deprecated singleton compatibility shorthand
|
|
1132
1139
|
* world.sync(); // Apply changes
|
|
1133
1140
|
*/
|
|
1134
1141
|
set(entityId: EntityId, componentType: EntityId<void>): void;
|
|
1142
|
+
/** @deprecated Use `world.singleton(componentId).set(value)` or `world.set(componentId, componentId, value)` instead. */
|
|
1143
|
+
set<T>(componentId: ComponentId<T>, component: Exclude<NoInfer<T>, number>): void;
|
|
1135
1144
|
set<T>(entityId: EntityId, componentType: EntityId<T>, component: NoInfer<T>): void;
|
|
1136
1145
|
/**
|
|
1137
1146
|
* Removes a component from an entity.
|
package/dist/world.mjs
CHANGED
|
@@ -626,8 +626,9 @@ var EntityBuilder = class {
|
|
|
626
626
|
/**
|
|
627
627
|
* Explicit handle for a singleton component (component-as-entity).
|
|
628
628
|
*
|
|
629
|
-
* This
|
|
630
|
-
*
|
|
629
|
+
* This is the preferred API for singleton components.
|
|
630
|
+
* `world.set(componentId, value)` remains available only as a deprecated
|
|
631
|
+
* compatibility shorthand.
|
|
631
632
|
*
|
|
632
633
|
* @example
|
|
633
634
|
* const config = world.singleton(Config);
|
|
@@ -2746,15 +2747,25 @@ function assertSetComponentTypeValid(componentType) {
|
|
|
2746
2747
|
/**
|
|
2747
2748
|
* Resolve the (entity, componentType, value) for a set() call.
|
|
2748
2749
|
*/
|
|
2749
|
-
function resolveSetOperation(entityId, componentTypeOrComponent, maybeComponent, exists = () => true) {
|
|
2750
|
+
function resolveSetOperation(entityId, componentTypeOrComponent, maybeComponent, argCount = 3, exists = () => true) {
|
|
2750
2751
|
const targetEntityId = entityId;
|
|
2752
|
+
if (argCount === 2 && isComponentId(targetEntityId) && typeof componentTypeOrComponent !== "number") {
|
|
2753
|
+
assertEntityExists(targetEntityId, "Component entity", exists);
|
|
2754
|
+
return {
|
|
2755
|
+
entityId: targetEntityId,
|
|
2756
|
+
componentType: targetEntityId,
|
|
2757
|
+
component: componentTypeOrComponent,
|
|
2758
|
+
deprecatedSingletonShorthand: true
|
|
2759
|
+
};
|
|
2760
|
+
}
|
|
2751
2761
|
const componentType = componentTypeOrComponent;
|
|
2752
2762
|
assertEntityExists(targetEntityId, "Entity", exists);
|
|
2753
2763
|
assertSetComponentTypeValid(componentType);
|
|
2754
2764
|
return {
|
|
2755
2765
|
entityId: targetEntityId,
|
|
2756
2766
|
componentType,
|
|
2757
|
-
component: maybeComponent
|
|
2767
|
+
component: maybeComponent,
|
|
2768
|
+
deprecatedSingletonShorthand: false
|
|
2758
2769
|
};
|
|
2759
2770
|
}
|
|
2760
2771
|
/**
|
|
@@ -2944,7 +2955,8 @@ function deserializeWorld(ctx, snapshot) {
|
|
|
2944
2955
|
* World class for ECS architecture
|
|
2945
2956
|
* Manages entities and components
|
|
2946
2957
|
*/
|
|
2947
|
-
var World = class {
|
|
2958
|
+
var World = class World {
|
|
2959
|
+
static DEPRECATED_SINGLETON_SET_SHORTHAND_WARNING = "world.set(componentId, value) for singleton components is deprecated; use world.singleton(componentId).set(value) or world.set(componentId, componentId, value) instead.";
|
|
2948
2960
|
entityIdManager = new EntityIdManager();
|
|
2949
2961
|
entityReferences = /* @__PURE__ */ new Map();
|
|
2950
2962
|
/** Sparse relation storage (for components created with `sparse: true`), shared with all Archetype instances */
|
|
@@ -3096,7 +3108,8 @@ var World = class {
|
|
|
3096
3108
|
return this.entityToArchetype.has(entityId);
|
|
3097
3109
|
}
|
|
3098
3110
|
set(entityId, componentTypeOrComponent, maybeComponent) {
|
|
3099
|
-
const { entityId: targetEntityId, componentType, component } = resolveSetOperation(entityId, componentTypeOrComponent, maybeComponent, (id) => this.exists(id));
|
|
3111
|
+
const { entityId: targetEntityId, componentType, component, deprecatedSingletonShorthand } = resolveSetOperation(entityId, componentTypeOrComponent, maybeComponent, arguments.length, (id) => this.exists(id));
|
|
3112
|
+
if (deprecatedSingletonShorthand) console.warn(World.DEPRECATED_SINGLETON_SET_SHORTHAND_WARNING);
|
|
3100
3113
|
this.commandBuffer.set(targetEntityId, componentType, component);
|
|
3101
3114
|
}
|
|
3102
3115
|
remove(entityId, componentType) {
|