ecspresso 0.12.9 → 0.13.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.
Files changed (101) hide show
  1. package/dist/index.js +2 -2
  2. package/dist/index.js.map +5 -5
  3. package/dist/plugin.d.ts +89 -22
  4. package/dist/plugins/ai/detection.d.ts +118 -0
  5. package/dist/plugins/ai/detection.js +4 -0
  6. package/dist/plugins/ai/detection.js.map +10 -0
  7. package/dist/plugins/{audio.d.ts → audio/audio.d.ts} +2 -3
  8. package/dist/plugins/{audio.js → audio/audio.js} +2 -2
  9. package/dist/plugins/audio/audio.js.map +10 -0
  10. package/dist/plugins/combat/health.d.ts +98 -0
  11. package/dist/plugins/combat/health.js +4 -0
  12. package/dist/plugins/combat/health.js.map +10 -0
  13. package/dist/plugins/combat/projectile.d.ts +115 -0
  14. package/dist/plugins/combat/projectile.js +4 -0
  15. package/dist/plugins/combat/projectile.js.map +10 -0
  16. package/dist/plugins/{diagnostics.d.ts → debug/diagnostics.d.ts} +1 -3
  17. package/dist/plugins/debug/diagnostics.js +5 -0
  18. package/dist/plugins/debug/diagnostics.js.map +10 -0
  19. package/dist/plugins/{input.d.ts → input/input.d.ts} +11 -3
  20. package/dist/plugins/input/input.js +4 -0
  21. package/dist/plugins/input/input.js.map +10 -0
  22. package/dist/plugins/input/selection.d.ts +114 -0
  23. package/dist/plugins/input/selection.js +4 -0
  24. package/dist/plugins/input/selection.js.map +11 -0
  25. package/dist/plugins/isometric/depth-sort.d.ts +44 -0
  26. package/dist/plugins/isometric/depth-sort.js +4 -0
  27. package/dist/plugins/isometric/depth-sort.js.map +10 -0
  28. package/dist/plugins/isometric/projection.d.ts +83 -0
  29. package/dist/plugins/isometric/projection.js +4 -0
  30. package/dist/plugins/isometric/projection.js.map +10 -0
  31. package/dist/plugins/{collision.d.ts → physics/collision.d.ts} +10 -9
  32. package/dist/plugins/physics/collision.js +4 -0
  33. package/dist/plugins/physics/collision.js.map +11 -0
  34. package/dist/plugins/{physics2D.d.ts → physics/physics2D.d.ts} +9 -6
  35. package/dist/plugins/physics/physics2D.js +4 -0
  36. package/dist/plugins/physics/physics2D.js.map +11 -0
  37. package/dist/plugins/physics/steering.d.ts +102 -0
  38. package/dist/plugins/physics/steering.js +4 -0
  39. package/dist/plugins/physics/steering.js.map +10 -0
  40. package/dist/plugins/{particles.d.ts → rendering/particles.d.ts} +4 -4
  41. package/dist/plugins/{particles.js → rendering/particles.js} +2 -2
  42. package/dist/plugins/rendering/particles.js.map +10 -0
  43. package/dist/plugins/{renderers → rendering}/renderer2D.d.ts +45 -14
  44. package/dist/plugins/rendering/renderer2D.js +4 -0
  45. package/dist/plugins/rendering/renderer2D.js.map +10 -0
  46. package/dist/plugins/{sprite-animation.d.ts → rendering/sprite-animation.d.ts} +2 -3
  47. package/dist/plugins/{sprite-animation.js → rendering/sprite-animation.js} +2 -2
  48. package/dist/plugins/rendering/sprite-animation.js.map +10 -0
  49. package/dist/plugins/{coroutine.d.ts → scripting/coroutine.d.ts} +2 -3
  50. package/dist/plugins/{coroutine.js → scripting/coroutine.js} +2 -2
  51. package/dist/plugins/scripting/coroutine.js.map +10 -0
  52. package/dist/plugins/{state-machine.d.ts → scripting/state-machine.d.ts} +2 -3
  53. package/dist/plugins/{state-machine.js → scripting/state-machine.js} +2 -2
  54. package/dist/plugins/scripting/state-machine.js.map +10 -0
  55. package/dist/plugins/{timers.d.ts → scripting/timers.d.ts} +2 -3
  56. package/dist/plugins/scripting/timers.js +4 -0
  57. package/dist/plugins/scripting/timers.js.map +10 -0
  58. package/dist/plugins/{tween.d.ts → scripting/tween.d.ts} +3 -4
  59. package/dist/plugins/{tween.js → scripting/tween.js} +2 -2
  60. package/dist/plugins/scripting/tween.js.map +11 -0
  61. package/dist/plugins/{bounds.d.ts → spatial/bounds.d.ts} +2 -3
  62. package/dist/plugins/spatial/bounds.js +4 -0
  63. package/dist/plugins/spatial/bounds.js.map +10 -0
  64. package/dist/plugins/{camera.d.ts → spatial/camera.d.ts} +43 -13
  65. package/dist/plugins/spatial/camera.js +4 -0
  66. package/dist/plugins/spatial/camera.js.map +10 -0
  67. package/dist/plugins/{spatial-index.d.ts → spatial/spatial-index.d.ts} +3 -6
  68. package/dist/plugins/spatial/spatial-index.js +4 -0
  69. package/dist/plugins/spatial/spatial-index.js.map +11 -0
  70. package/dist/plugins/{transform.d.ts → spatial/transform.d.ts} +3 -3
  71. package/dist/plugins/spatial/transform.js +4 -0
  72. package/dist/plugins/spatial/transform.js.map +10 -0
  73. package/dist/utils/narrowphase.d.ts +60 -19
  74. package/dist/utils/spatial-hash.d.ts +11 -1
  75. package/package.json +80 -49
  76. package/dist/plugins/audio.js.map +0 -10
  77. package/dist/plugins/bounds.js +0 -4
  78. package/dist/plugins/bounds.js.map +0 -10
  79. package/dist/plugins/camera.js +0 -4
  80. package/dist/plugins/camera.js.map +0 -10
  81. package/dist/plugins/collision.js +0 -4
  82. package/dist/plugins/collision.js.map +0 -11
  83. package/dist/plugins/coroutine.js.map +0 -10
  84. package/dist/plugins/diagnostics.js +0 -5
  85. package/dist/plugins/diagnostics.js.map +0 -10
  86. package/dist/plugins/input.js +0 -4
  87. package/dist/plugins/input.js.map +0 -10
  88. package/dist/plugins/particles.js.map +0 -10
  89. package/dist/plugins/physics2D.js +0 -4
  90. package/dist/plugins/physics2D.js.map +0 -11
  91. package/dist/plugins/renderers/renderer2D.js +0 -4
  92. package/dist/plugins/renderers/renderer2D.js.map +0 -10
  93. package/dist/plugins/spatial-index.js +0 -4
  94. package/dist/plugins/spatial-index.js.map +0 -11
  95. package/dist/plugins/sprite-animation.js.map +0 -10
  96. package/dist/plugins/state-machine.js.map +0 -10
  97. package/dist/plugins/timers.js +0 -4
  98. package/dist/plugins/timers.js.map +0 -10
  99. package/dist/plugins/transform.js +0 -4
  100. package/dist/plugins/transform.js.map +0 -10
  101. package/dist/plugins/tween.js.map +0 -11
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- var l=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:($,G)=>(typeof require<"u"?require:$)[G]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});class M{parentMap=new Map;childrenMap=new Map;setParent(j,$){if(j===$)throw Error(`Cannot set entity ${j} as its own parent`);if(this.wouldCreateCycle(j,$))throw Error("Cannot set parent: would create circular reference");let G=this.parentMap.get(j);if(G!==void 0){let X=this.childrenMap.get(G);if(X){let Y=X.indexOf(j);if(Y!==-1)X.splice(Y,1)}}this.parentMap.set(j,$);let J=this.childrenMap.get($);if(J)J.push(j);else this.childrenMap.set($,[j]);return this}removeParent(j){let $=this.parentMap.get(j);if($===void 0)return!1;let G=this.childrenMap.get($);if(G){let J=G.indexOf(j);if(J!==-1)G.splice(J,1)}return this.parentMap.delete(j),!0}getParent(j){return this.parentMap.get(j)??null}getChildren(j){let $=this.childrenMap.get(j);return $?[...$]:[]}getChildAt(j,$){if($<0)return null;let G=this.childrenMap.get(j);if(!G||$>=G.length)return null;return G[$]??null}getChildIndex(j,$){let G=this.childrenMap.get(j);if(!G)return-1;return G.indexOf($)}removeEntity(j){let $=this.parentMap.get(j)??null;if($!==null){let X=this.childrenMap.get($);if(X){let Y=X.indexOf(j);if(Y!==-1)X.splice(Y,1)}}this.parentMap.delete(j);let G=this.childrenMap.get(j)??[],J=[...G];for(let X of G)this.parentMap.delete(X);return this.childrenMap.delete(j),{oldParent:$,orphanedChildren:J}}getAncestors(j){let $=[],G=this.parentMap.get(j);while(G!==void 0)$.push(G),G=this.parentMap.get(G);return $}getDescendants(j){let $=[],G=this.childrenMap.get(j);if(!G)return $;let J=G.slice().reverse();while(J.length>0){let X=J.pop();$.push(X);let Y=this.childrenMap.get(X);if(Y)for(let Z=Y.length-1;Z>=0;Z--)J.push(Y[Z])}return $}getRoot(j){let $=j,G=this.parentMap.get($);while(G!==void 0)$=G,G=this.parentMap.get($);return $}getSiblings(j){let $=this.parentMap.get(j);if($===void 0)return[];let G=this.childrenMap.get($);if(!G)return[];return G.filter((J)=>J!==j)}isDescendantOf(j,$){if(j===$)return!1;let G=this.parentMap.get(j);while(G!==void 0){if(G===$)return!0;G=this.parentMap.get(G)}return!1}isAncestorOf(j,$){return this.isDescendantOf($,j)}getRootEntities(){let j=[];for(let $ of this.childrenMap.keys())if(!this.parentMap.has($))j.push($);return j}wouldCreateCycle(j,$){let G=$;while(G!==void 0){if(G===j)return!0;G=this.parentMap.get(G)}return!1}forEachInHierarchy(j,$){let G=$?.roots??this.getRootEntities(),J=[];for(let X of G)J.push({entityId:X,parentId:null,depth:0});for(let X of J){j(X.entityId,X.parentId,X.depth);let Y=this.childrenMap.get(X.entityId);if(Y)for(let Z of Y)J.push({entityId:Z,parentId:X.entityId,depth:X.depth+1})}}*hierarchyIterator(j){let $=j?.roots??this.getRootEntities(),G=[];for(let J of $)G.push({entityId:J,parentId:null,depth:0});for(let J of G){yield J;let X=this.childrenMap.get(J.entityId);if(X)for(let Y of X)G.push({entityId:Y,parentId:J.entityId,depth:J.depth+1})}}}class T{callbacks=[];_iterDepth=0;_pendingRemovals=[];add(j){this.callbacks.push(j)}remove(j){if(this._iterDepth>0){this._pendingRemovals.push(j);return}let $=this.callbacks.indexOf(j);if($!==-1)this.callbacks.splice($,1)}invoke(j){this._iterDepth++;let $=this.callbacks.length;for(let G=0;G<$;G++){let J=this.callbacks[G];if(J)J(j)}if(this._iterDepth--,this._iterDepth===0&&this._pendingRemovals.length>0){for(let G of this._pendingRemovals){let J=this.callbacks.indexOf(G);if(J!==-1)this.callbacks.splice(J,1)}this._pendingRemovals.length=0}}}class H{nextId=1;entities=new Map;componentIndices=new Map;addedCallbacks=new Map;removedCallbacks=new Map;hierarchyManager=new M;disposeCallbacks=new Map;changeSeqs=new Map;_changeSeq=0;_afterComponentAddedHooks=[];_afterEntityMutatedHooks=[];_afterComponentRemovedHooks=[];_beforeEntityRemovedHooks=[];_afterParentChangedHooks=[];_batchingDepth=0;_batchedEntityIds=new Set;_pendingBatchKeys=null;get entityCount(){return this.entities.size}createEntity(){let j=this.nextId++,$={id:j,components:{}};return this.entities.set(j,$),$}registerDispose(j,$){this.disposeCallbacks.set(j,$)}getDisposeCallbacks(){return this.disposeCallbacks}invokeDispose(j,$,G){let J=this.disposeCallbacks.get(j);if(!J)return;try{J({value:$,entityId:G})}catch(X){console.warn(`Component dispose callback for '${String(j)}' threw:`,X)}}addComponent(j,$,G){let J=this.entities.get(j);if(!J)throw Error(`Cannot add component '${String($)}': Entity with ID ${j} does not exist`);let X=J.components[$];if(X!==void 0)this.invokeDispose($,X,J.id);if(J.components[$]=G,!this.componentIndices.has($))this.componentIndices.set($,new Set);this.componentIndices.get($)?.add(J.id);let Y=this.addedCallbacks.get($);if(Y)Y.invoke({value:G,entity:J});this._batchingDepth++;for(let Z of this._afterComponentAddedHooks)Z(J.id,$);if(this._batchedEntityIds.add(J.id),this._batchingDepth--,this._batchingDepth===0){for(let Z of this._batchedEntityIds)for(let B of this._afterEntityMutatedHooks)B(Z);this._batchedEntityIds.clear()}return this}addComponents(j,$){let G=this.entities.get(j);if(!G)throw Error(`Cannot add components: Entity with ID ${j} does not exist`);let J=this._pendingBatchKeys;this._pendingBatchKeys=new Set(Object.keys($)),this._batchingDepth++;for(let X in $)this.addComponent(G.id,X,$[X]);if(this._batchingDepth--,this._pendingBatchKeys=J,this._batchingDepth===0){for(let X of this._batchedEntityIds)for(let Y of this._afterEntityMutatedHooks)Y(X);this._batchedEntityIds.clear()}return this}removeComponent(j,$){let G=this.entities.get(j);if(!G)throw Error(`Cannot remove component '${String($)}': Entity with ID ${j} does not exist`);let J=G.components[$];if(J!==void 0)this.invokeDispose($,J,G.id);delete G.components[$];let X=this.removedCallbacks.get($);if(X&&J!==void 0)X.invoke({value:J,entity:G});if(this.componentIndices.get($)?.delete(G.id),J!==void 0)for(let Y of this._afterComponentRemovedHooks)Y(G.id,$);return this}getComponent(j,$){let G=this.entities.get(j);if(!G)throw Error(`Cannot get component '${String($)}': Entity with ID ${j} does not exist`);return G.components[$]}getEntitiesWithQuery(j=[],$=[],G,J,X){return this.getEntitiesWithQueryInto([],j,$,G,J,X)}getEntitiesWithQueryInto(j,$=[],G=[],J,X,Y){j.length=0;let Z=J!==void 0&&J.length>0&&X!==void 0,B=Y!==void 0&&Y.length>0;if($.length===0){if(G.length===0&&!Z&&!B){for(let D of this.entities.values())j.push(D);return j}for(let D of this.entities.values()){if(G.length>0&&!G.every((L)=>!(L in D.components)))continue;if(Z){let L=this.changeSeqs.get(D.id);if(!L)continue;if(!J.some((_)=>(L.get(_)??-1)>X))continue}if(B&&!this.parentHasComponents(D.id,Y))continue;j.push(D)}return j}let A=$[0];if(A===void 0)return j;let U=$.reduce((D,L)=>{let _=this.componentIndices.get(L)?.size??0,Q=this.componentIndices.get(D)?.size??1/0;return _<Q?L:D},A),W=this.componentIndices.get(U);if(!W||W.size===0)return j;let F=G.length>0;for(let D of W){let L=this.entities.get(D);if(L&&$.every((_)=>(_ in L.components))&&(!F||G.every((_)=>!(_ in L.components)))){if(Z){let _=this.changeSeqs.get(D);if(!_||!J.some((Q)=>(_.get(Q)??-1)>X))continue}if(B&&!this.parentHasComponents(D,Y))continue;j.push(L)}}return j}parentHasComponents(j,$){let G=this.hierarchyManager.getParent(j);if(G===null)return!1;let J=this.entities.get(G);if(!J)return!1;for(let X of $)if(!(X in J.components))return!1;return!0}removeEntity(j,$){let G=this.entities.get(j);if(!G)return!1;if($?.cascade??!0){let X=this.hierarchyManager.getDescendants(G.id);for(let Y=X.length-1;Y>=0;Y--){let Z=X[Y];if(Z===void 0)continue;for(let B of this._beforeEntityRemovedHooks)B(Z)}for(let Y of this._beforeEntityRemovedHooks)Y(G.id);for(let Y=X.length-1;Y>=0;Y--){let Z=X[Y];if(Z===void 0)continue;this.removeEntityInternal(Z)}}else for(let X of this._beforeEntityRemovedHooks)X(G.id);return this.removeEntityInternal(G.id)}removeEntityInternal(j){let $=this.entities.get(j);if(!$)return!1;this.hierarchyManager.removeEntity(j);for(let G of Object.keys($.components)){let J=$.components[G];if(J!==void 0){this.invokeDispose(G,J,$.id);let X=this.removedCallbacks.get(G);if(X)X.invoke({value:J,entity:$})}this.componentIndices.get(G)?.delete($.id)}return this.changeSeqs.delete($.id),this.entities.delete($.id)}getEntity(j){return this.entities.get(j)}onComponentAdded(j,$){let G=$,J=this.addedCallbacks.get(j);if(!J)J=new T,this.addedCallbacks.set(j,J);return J.add(G),()=>{this.addedCallbacks.get(j)?.remove(G)}}onComponentRemoved(j,$){let G=$,J=this.removedCallbacks.get(j);if(!J)J=new T,this.removedCallbacks.set(j,J);return J.add(G),()=>{this.removedCallbacks.get(j)?.remove(G)}}onAfterComponentAdded(j){return this._afterComponentAddedHooks.push(j),()=>{let $=this._afterComponentAddedHooks.indexOf(j);if($!==-1)this._afterComponentAddedHooks.splice($,1)}}onAfterEntityMutated(j){return this._afterEntityMutatedHooks.push(j),()=>{let $=this._afterEntityMutatedHooks.indexOf(j);if($!==-1)this._afterEntityMutatedHooks.splice($,1)}}onAfterComponentRemoved(j){return this._afterComponentRemovedHooks.push(j),()=>{let $=this._afterComponentRemovedHooks.indexOf(j);if($!==-1)this._afterComponentRemovedHooks.splice($,1)}}onBeforeEntityRemoved(j){return this._beforeEntityRemovedHooks.push(j),()=>{let $=this._beforeEntityRemovedHooks.indexOf(j);if($!==-1)this._beforeEntityRemovedHooks.splice($,1)}}onAfterParentChanged(j){return this._afterParentChangedHooks.push(j),()=>{let $=this._afterParentChangedHooks.indexOf(j);if($!==-1)this._afterParentChangedHooks.splice($,1)}}get changeSeq(){return this._changeSeq}markChanged(j,$){let G=++this._changeSeq,J=this.changeSeqs.get(j);if(!J)J=new Map,this.changeSeqs.set(j,J);J.set($,G)}getChangeSeq(j,$){return this.changeSeqs.get(j)?.get($)??-1}spawnChild(j,$){let G=this.createEntity();return this.addComponents(G.id,$),this.setParent(G.id,j),G}setParent(j,$){this.hierarchyManager.setParent(j,$);for(let G of this._afterParentChangedHooks)G(j);return this}removeParent(j){let $=this.hierarchyManager.removeParent(j);if($)for(let G of this._afterParentChangedHooks)G(j);return $}getParent(j){return this.hierarchyManager.getParent(j)}getChildren(j){return this.hierarchyManager.getChildren(j)}getChildAt(j,$){return this.hierarchyManager.getChildAt(j,$)}getChildIndex(j,$){return this.hierarchyManager.getChildIndex(j,$)}getAncestors(j){return this.hierarchyManager.getAncestors(j)}getDescendants(j){return this.hierarchyManager.getDescendants(j)}getRoot(j){return this.hierarchyManager.getRoot(j)}getSiblings(j){return this.hierarchyManager.getSiblings(j)}isDescendantOf(j,$){return this.hierarchyManager.isDescendantOf(j,$)}isAncestorOf(j,$){return this.hierarchyManager.isAncestorOf(j,$)}getRootEntities(){return this.hierarchyManager.getRootEntities()}forEachInHierarchy(j,$){this.hierarchyManager.forEachInHierarchy(j,$)}hierarchyIterator(j){return this.hierarchyManager.hierarchyIterator(j)}}class V{handlers=new Map;subscribe(j,$){return this.addHandler(j,$,!1)}once(j,$){return this.addHandler(j,$,!0)}unsubscribe(j,$){let G=this.handlers.get(j);if(!G)return!1;let J=G.findIndex((X)=>X.callback===$);if(J===-1)return!1;return G.splice(J,1),!0}addHandler(j,$,G){let J=this.handlers.get(j);if(!J)J=[],this.handlers.set(j,J);let X={callback:$,once:G};return J.push(X),()=>{let Y=this.handlers.get(j);if(Y){let Z=Y.indexOf(X);if(Z!==-1)Y.splice(Z,1)}}}publish(...[j,$]){let G=this.handlers.get(j);if(!G||G.length===0)return;let J=!1,X=G.length;for(let Y=0;Y<X&&Y<G.length;Y++){let Z=G[Y];if(!Z)continue;if(Z.callback($),Z.once)J=!0}if(J){for(let Y=G.length-1;Y>=0;Y--)if(G[Y]?.once)G.splice(Y,1)}}clear(){this.handlers.clear()}clearEvent(j){this.handlers.delete(j)}}var x=Symbol("resource-direct");function I(j){return{[x]:j}}function p(j){return typeof j==="object"&&j!==null&&"factory"in j&&typeof j.factory==="function"}function u(j){return typeof j==="object"&&j!==null&&x in j}function f(j,$){let G=[],J=new Set,X=new Set;function Y(Z,B=[]){if(J.has(Z))return;if(X.has(Z))throw Error(`Circular resource dependency: ${[...B,Z].join(" -> ")}`);X.add(Z);for(let A of $(Z)){let U=j.find((W)=>W===A);if(U)Y(U,[...B,Z])}X.delete(Z),J.add(Z),G.push(Z)}for(let Z of j)Y(Z);return G}class E{resources=new Map;resourceFactories=new Map;resourceDependencies=new Map;resourceDisposers=new Map;initializedResourceKeys=new Set;_changeSubscribers=new Map;add(j,$){let G=(J)=>{this.resources.set(j,J),this.initializedResourceKeys.add(j),this.resourceDependencies.set(j,[])};if(p($)){if(this.resourceFactories.set(j,$.factory),this.resourceDependencies.set(j,$.dependsOn??[]),$.onDispose)this.resourceDisposers.set(j,$.onDispose)}else if(u($))G($[x]);else if(typeof $==="function")this.resourceFactories.set(j,$),this.resourceDependencies.set(j,[]);else G($);return this}tryGet(j,...$){if(!this.has(j))return;return this.get(j,...$)}get(j,...$){let G=this.resources.get(j);if(G!==void 0)return G;let J=this.resourceFactories.get(j);if(J===void 0)throw Error(`Resource ${String(j)} not found`);let X=$[0],Y=J(X);if(!(Y instanceof Promise))this.resources.set(j,Y),this.initializedResourceKeys.add(j);return Y}has(j){return this.resources.has(j)||this.resourceFactories.has(j)}remove(j){let $=this.resources.delete(j),G=this.resourceFactories.delete(j);return this.resourceDependencies.delete(j),this.resourceDisposers.delete(j),this.initializedResourceKeys.delete(j),$||G}getKeys(){let j=new Set([...this.resources.keys(),...this.resourceFactories.keys()]);return Array.from(j)}needsInitialization(j){return this.resourceFactories.has(j)&&!this.initializedResourceKeys.has(j)}getPendingInitializationKeys(){return Array.from(this.resourceFactories.keys()).filter((j)=>!this.initializedResourceKeys.has(j))}async initializeResource(j,...$){if(!this.resourceFactories.has(j)||this.initializedResourceKeys.has(j))return;let G=this.resourceFactories.get(j);if(!G)return;let J=$[0],X=await G(J);this.resources.set(j,X),this.initializedResourceKeys.add(j),this.resourceFactories.delete(j)}async initializeResources(...j){let $=j.slice(1),G=$.length===0?this.getPendingInitializationKeys():$;if(G.length===0)return;let J=f(G,(X)=>[...this.resourceDependencies.get(X)??[]]);for(let X of J)await this.initializeResource(X,...j.slice(0,1))}getDependencies(j){return this.resourceDependencies.get(j)??[]}async disposeResource(j,...$){if(!this.resources.has(j)&&!this.resourceFactories.has(j))return!1;if(this.initializedResourceKeys.has(j)){let G=this.resourceDisposers.get(j),J=this.resources.get(j);if(G&&J!==void 0){let X=$[0];await G(J,X)}}return this.resources.delete(j),this.resourceFactories.delete(j),this.resourceDependencies.delete(j),this.resourceDisposers.delete(j),this.initializedResourceKeys.delete(j),this._changeSubscribers.delete(j),!0}onResourceChange(j,$){let G=this._changeSubscribers.get(j),J=G??new Set;if(!G)this._changeSubscribers.set(j,J);let X=$;return J.add(X),()=>{if(J.delete(X),J.size===0)this._changeSubscribers.delete(j)}}notifyChange(j,$,G){if(Object.is($,G))return;let J=this._changeSubscribers.get(j);if(!J||J.size===0)return;let X=[...J];for(let Y of X)Y($,G)}async disposeResources(...j){let $=Array.from(this.initializedResourceKeys);if($.length===0)return;let G=f($,(J)=>[...this.resourceDependencies.get(J)??[]]).reverse();for(let J of G)await this.disposeResource(J,...j)}}class R{queries=new Map;entityManager;_hasParentHasQueries=!1;constructor(j){this.entityManager=j}get hasParentHasQueries(){return this._hasParentHasQueries}addQuery(j,$){let G={definition:$,matchingEntities:new Set};if(this.queries.set(j,G),$.parentHas?.length)this._hasParentHasQueries=!0;let J=this.entityManager.getEntitiesWithQuery($.with,$.without??[]);for(let X of J)if(this.entityMatchesQuery(X,G.definition))G.matchingEntities.add(X.id),G.definition.onEnter?.(X)}removeQuery(j){let $=this.queries.delete(j);if($)this._recalcParentHasFlag();return $}entityMatchesQuery(j,$){for(let G of $.with)if(!(G in j.components))return!1;if($.without){for(let G of $.without)if(G in j.components)return!1}if($.parentHas?.length){let G=this.entityManager.getParent(j.id);if(G===null)return!1;let J=this.entityManager.getEntity(G);if(!J)return!1;for(let X of $.parentHas)if(!(X in J.components))return!1}return!0}_applyQueryTransition(j,$){let G=$.matchingEntities.has(j.id),J=this.entityMatchesQuery(j,$.definition);if(!G&&J)$.matchingEntities.add(j.id),$.definition.onEnter?.(j);else if(G&&!J)$.matchingEntities.delete(j.id),$.definition.onExit?.(j.id)}onComponentAdded(j,$){for(let[,G]of this.queries)this._applyQueryTransition(j,G);if(this._hasParentHasQueries)this._recheckChildren(j.id)}onComponentRemoved(j,$){for(let[,G]of this.queries)this._applyQueryTransition(j,G);if(this._hasParentHasQueries)this._recheckChildren(j.id)}onEntityRemoved(j){for(let[$,G]of this.queries)if(G.matchingEntities.has(j))G.matchingEntities.delete(j),G.definition.onExit?.(j)}recheckEntity(j){for(let[,$]of this.queries)this._applyQueryTransition(j,$)}recheckEntityAndChildren(j){if(this.recheckEntity(j),this._hasParentHasQueries)this._recheckChildren(j.id)}_recheckChildren(j){let $=this.entityManager.getChildren(j);for(let G of $){let J=this.entityManager.getEntity(G);if(J)this.recheckEntity(J)}}_recalcParentHasFlag(){this._hasParentHasQueries=!1;for(let[,j]of this.queries)if(j.definition.parentHas?.length){this._hasParentHasQueries=!0;return}}}class w{commands=[];removeEntity(j,$){this.commands.push((G)=>{G.removeEntity(j,$)})}addComponent(j,$,G){this.commands.push((J)=>{J.addComponent(j,$,G)})}removeComponent(j,$){this.commands.push((G)=>{G.removeComponent(j,$)})}spawn(j){this.commands.push(($)=>{$.spawn(j)})}spawnChild(j,$){this.commands.push((G)=>{G.spawnChild(j,$)})}addComponents(j,$){this.commands.push((G)=>{G.addComponents(j,$)})}setParent(j,$){this.commands.push((G)=>{G.setParent(j,$)})}mutateComponent(j,$,G){this.commands.push((J)=>{J.mutateComponent(j,$,G)})}markChanged(j,$){this.commands.push((G)=>{G.markChanged(j,$)})}removeParent(j){this.commands.push(($)=>{$.removeParent(j)})}playback(j){for(let $ of this.commands)try{$(j)}catch(G){console.warn("CommandBuffer: Command failed during playback:",G)}this.commands.length=0}clear(){this.commands.length=0}get length(){return this.commands.length}}function z(j){return j}class S{_label;queries={};processFunction;detachFunction;initializeFunction;eventHandlers;_priority=0;_phase="update";_groups=[];_inScreens;_excludeScreens;_requiredAssets;_runWhenEmpty=!1;_entityEnterHandlers={};_resourceKeys;constructor(j){this._label=j}get label(){return this._label}_createSystemObject(){let j={label:this._label,entityQueries:this.queries,priority:this._priority,phase:this._phase};if(this.processFunction)j.process=this.processFunction;if(this.detachFunction)j.onDetach=this.detachFunction;if(this.initializeFunction)j.onInitialize=this.initializeFunction;if(this.eventHandlers)j.eventHandlers=this.eventHandlers;if(this._groups.length>0)j.groups=[...this._groups];if(this._inScreens)j.inScreens=this._inScreens;if(this._excludeScreens)j.excludeScreens=this._excludeScreens;if(this._requiredAssets)j.requiredAssets=this._requiredAssets;if(this._runWhenEmpty)j.runWhenEmpty=!0;if(Object.keys(this._entityEnterHandlers).length>0)j.onEntityEnter={...this._entityEnterHandlers};return j}setPriority(j){return this._priority=j,this}inPhase(j){return this._phase=j,this}inGroup(j){if(!this._groups.includes(j))this._groups.push(j);return this}inScreens(j){return this._inScreens=[...j],this}excludeScreens(j){return this._excludeScreens=[...j],this}requiresAssets(j){return this._requiredAssets=[...j],this}runWhenEmpty(){return this._runWhenEmpty=!0,this}withResources(j){return this._resourceKeys=[...j],this}addQuery(j,$){let G=this;return G.queries={...this.queries,[j]:$},G}setProcess(j){if(this._resourceKeys?.length){let $=this._resourceKeys,G;this.processFunction=(J)=>{if(!G){G={};for(let X of $)G[X]=J.ecs.getResource(X)}J.resources=G,j(J)}}else this.processFunction=j;return this}setOnEntityEnter(j,$){return this._entityEnterHandlers[j]=$,this}setOnDetach(j){return this.detachFunction=j,this}setOnInitialize(j){return this.initializeFunction=j,this}setEventHandlers(j){return this.eventHandlers=j,this}}function N(j,$,G){let J=new Set,X=[$];while(X.length>0){let Y=X.pop();if(Y===void 0)break;if(Y===j)throw Error(`Circular required component dependency: '${String(j)}' -> '${String($)}' -> ... -> '${String(j)}'`);if(J.has(Y))continue;J.add(Y);let Z=G(Y);if(Z)for(let B of Z)X.push(B.component)}}var b="0.12.9";class K{assets=new Map;groups=new Map;eventBus=null;setEventBus(j){this.eventBus=j}register(j,$){if(this.assets.set(j,{definition:$,status:"pending"}),$.group){let G=this.groups.get($.group)??new Set;G.add(j),this.groups.set($.group,G)}}async loadEagerAssets(){let j=[];for(let[$,G]of this.assets)if(G.definition.eager&&G.status==="pending")j.push($);await Promise.all(j.map(($)=>this.loadAsset($)))}async loadAsset(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);if($.status==="loaded"&&$.value!==void 0)return $.value;if($.status==="loading"&&$.loadPromise)return $.loadPromise;if($.status==="failed")$.status="pending";$.status="loading",$.loadPromise=$.definition.loader();try{let G=await $.loadPromise;return $.value=G,$.status="loaded",$.loadPromise=void 0,this.eventBus?.publish("assetLoaded",{key:j}),this.checkGroupProgress($.definition.group),G}catch(G){let J=G instanceof Error?G:Error(String(G));throw $.status="failed",$.error=J,$.loadPromise=void 0,this.eventBus?.publish("assetFailed",{key:j,error:J}),J}}async loadAssetGroup(j){let $=this.groups.get(j);if(!$||$.size===0)throw Error(`Asset group '${j}' not found or empty`);await Promise.all(Array.from($).map((G)=>this.loadAsset(G)))}get(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);if($.status!=="loaded"||$.value===void 0)throw Error(`Asset '${String(j)}' is not loaded (status: ${$.status})`);return $.value}tryGet(j){let $=this.assets.get(j);if(!$||$.status!=="loaded")return;return $.value}getHandle(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);let G=this;return{get status(){return $.status},get isLoaded(){return $.status==="loaded"},get(){return G.get(j)},tryGet(){return G.tryGet(j)}}}getStatus(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);return $.status}isLoaded(j){return this.assets.get(j)?.status==="loaded"}isGroupLoaded(j){let $=this.groups.get(j);if(!$||$.size===0)return!1;for(let G of $){let J=this.assets.get(G);if(!J||J.status!=="loaded")return!1}return!0}getGroupProgress(j){return this.getGroupProgressDetails(j).progress}getGroupProgressDetails(j){let $=this.groups.get(j);if(!$||$.size===0)return{loaded:0,total:0,progress:0};let G=0;for(let X of $)if(this.assets.get(X)?.status==="loaded")G++;let J=$.size;return{loaded:G,total:J,progress:G/J}}checkGroupProgress(j){if(!j||!this.eventBus)return;let $=j,G=this.getGroupProgressDetails($);if(this.eventBus.publish("assetGroupProgress",{group:$,...G}),G.loaded===G.total)this.eventBus.publish("assetGroupLoaded",{group:$})}createResource(){let j=this;return{getStatus($){return j.getStatus($)},isLoaded($){return j.isLoaded($)},isGroupLoaded($){return j.isGroupLoaded($)},getGroupProgress($){return j.getGroupProgress($)},get($){return j.get($)},tryGet($){return j.tryGet($)},getHandle($){return j.getHandle($)}}}getKeys(){return Array.from(this.assets.keys())}getGroupNames(){return Array.from(this.groups.keys())}getGroupKeys(j){let $=this.groups.get(j);return $?Array.from($):[]}}class g{manager;constructor(j){this.manager=j}add(j,$){return this.manager.register(j,{loader:$,eager:!0}),this}addWithConfig(j,$){return this.manager.register(j,$),this}addGroup(j,$){for(let[G,J]of Object.entries($))this.manager.register(G,{loader:J,eager:!1,group:j});return this}getManager(){return this.manager}}function C(j){return new g(j??new K)}class O{screens=new Map;currentScreen=null;screenStack=[];eventBus=null;assetManager=null;ecs=null;setDependencies(j,$,G){this.eventBus=j,this.assetManager=$,this.ecs=G}requireEcs(){if(!this.ecs)throw Error("ScreenManager: dependencies not set. Call setDependencies() first.");return this.ecs}register(j,$){this.screens.set(j,{definition:$})}async setScreen(j,$){let G=this.screens.get(j);if(!G)throw Error(`Screen '${String(j)}' not found`);await this.verifyRequiredAssets(G.definition.requiredAssets,G.definition.requiredAssetGroups);while(this.screenStack.length>0){let X=this.screenStack.pop();if(X)await this.exitScreen(X.name)}if(this.currentScreen)await this.exitScreen(this.currentScreen.name);let J=G.definition.initialState($);this.currentScreen={name:j,config:$,state:J},await G.definition.onEnter?.({config:$,ecs:this.requireEcs()}),this.eventBus?.publish("screenEnter",{screen:j,config:$})}async pushScreen(j,$){let G=this.screens.get(j);if(!G)throw Error(`Screen '${String(j)}' not found`);if(await this.verifyRequiredAssets(G.definition.requiredAssets,G.definition.requiredAssetGroups),this.currentScreen)this.screenStack.push(this.currentScreen);let J=G.definition.initialState($);this.currentScreen={name:j,config:$,state:J},await G.definition.onEnter?.({config:$,ecs:this.requireEcs()}),this.eventBus?.publish("screenPush",{screen:j,config:$})}async popScreen(){if(this.screenStack.length===0)throw Error("Cannot pop screen: stack is empty");if(this.currentScreen)await this.exitScreen(this.currentScreen.name),this.eventBus?.publish("screenPop",{screen:this.currentScreen.name});this.currentScreen=this.screenStack.pop()??null}async exitScreen(j){let $=this.screens.get(j);if($?.definition.onExit)await $.definition.onExit(this.requireEcs());this.eventBus?.publish("screenExit",{screen:j})}async verifyRequiredAssets(j,$){if(!this.assetManager)return;if(j){for(let G of j)if(!this.assetManager.isLoaded(G))await this.assetManager.loadAsset(G)}if($){for(let G of $)if(!this.assetManager.isGroupLoaded(G))await this.assetManager.loadAssetGroup(G)}}getCurrentScreen(){return this.currentScreen?.name??null}getConfig(j){if(!this.currentScreen)throw Error("No current screen");if(j!==void 0&&this.currentScreen.name!==j)throw Error(`Expected current screen '${String(j)}', but current is '${String(this.currentScreen.name)}'`);return this.currentScreen.config}tryGetConfig(j){if(!this.currentScreen)return;if(j!==void 0&&this.currentScreen.name!==j)return;return this.currentScreen.config}getState(j){if(!this.currentScreen)throw Error("No current screen");if(j!==void 0&&this.currentScreen.name!==j)throw Error(`Expected current screen '${String(j)}', but current is '${String(this.currentScreen.name)}'`);return this.currentScreen.state}tryGetState(j){if(!this.currentScreen)return;if(j!==void 0&&this.currentScreen.name!==j)return;return this.currentScreen.state}updateState(j,$){if(!this.currentScreen)throw Error("No current screen");if($!==void 0&&this.currentScreen.name!==$)throw Error(`Expected current screen '${String($)}', but current is '${String(this.currentScreen.name)}'`);let G=typeof j==="function"?j(this.currentScreen.state):j;this.currentScreen.state={...this.currentScreen.state,...G}}getStackDepth(){return this.screenStack.length}isOverlay(){return this.screenStack.length>0}isActive(j){if(this.currentScreen?.name===j)return!0;return this.screenStack.some(($)=>$.name===j)}isCurrent(j){return this.currentScreen?.name===j}createResource(){let j=this;return{get current(){return j.getCurrentScreen()},get config(){return j.tryGetConfig()??null},get state(){return j.tryGetState()??null},set state($){if(j.currentScreen&&$!==null)j.currentScreen.state=$},get stack(){return j.screenStack},get isOverlay(){return j.isOverlay()},get stackDepth(){return j.getStackDepth()},isActive($){return j.isActive($)},isCurrent($){return j.isCurrent($)}}}getScreenNames(){return Array.from(this.screens.keys())}hasScreen(j){return this.screens.has(j)}}class k{manager;constructor(j){this.manager=j}add(j,$){return this.manager.register(j,$),this}getManager(){return this.manager}}function q(j){return new k(j??new O)}class v{assetConfigurator=null;screenConfigurator=null;pendingResources=[];pendingDisposeCallbacks=[];pendingRequiredComponents=[];pendingPlugins=[];_fixedDt=null;constructor(){}withPlugin(j){return this.pendingPlugins.push(j),this}withComponentTypes(){return this}withEventTypes(){return this}withResourceTypes(){return this}withResource(j,$){return this.pendingResources.push({key:j,value:$}),this}withDispose(j,$){return this.pendingDisposeCallbacks.push({key:j,callback:$}),this}withRequired(j,$,G){return this.pendingRequiredComponents.push({trigger:j,required:$,factory:G}),this}withAssets(j){let $=C();return j($),this.assetConfigurator=$,this}withScreens(j){let $=q();return j($),this.screenConfigurator=$,this}withFixedTimestep(j){return this._fixedDt=j,this}withReactiveQueryNames(){return this}pluginFactory(){return z}build(){let j=new P;for(let $ of this.pendingPlugins)j.installPlugin($);for(let{key:$,value:G}of this.pendingResources)j.addResource($,G);for(let{key:$,callback:G}of this.pendingDisposeCallbacks)j.registerDispose($,G);for(let{trigger:$,required:G,factory:J}of this.pendingRequiredComponents)j.registerRequired($,G,J);if(this.assetConfigurator)j._setAssetManager(this.assetConfigurator.getManager());else if(j._hasPendingPluginAssets())j._setAssetManager(new K);if(this.screenConfigurator)j._setScreenManager(this.screenConfigurator.getManager());else if(j._hasPendingPluginScreens())j._setScreenManager(new O);if(this._fixedDt!==null)j._setFixedDt(this._fixedDt);return j}}var h=["preUpdate","fixedUpdate","update","postUpdate","render"];class P{static VERSION=b;_entityManager;_eventBus;_resourceManager;_commandBuffer;_systems=[];_phaseSystems={preUpdate:[],fixedUpdate:[],update:[],postUpdate:[],render:[]};_installedPlugins=new Set;_disabledGroups=new Set;_assetManager=null;_screenManager=null;_reactiveQueryManager;_postUpdateHooks=[];_currentTick=0;_systemLastSeqs=new Map;_changeThreshold=0;_fixedDt=0.016666666666666666;_fixedAccumulator=0;_interpolationAlpha=0;_maxFixedSteps=8;_requiredComponents=new Map;_pendingPluginAssets=[];_pendingPluginScreens=[];_diagnosticsEnabled=!1;_systemTimings=new Map;_phaseTimings={preUpdate:0,fixedUpdate:0,update:0,postUpdate:0,render:0};_entityEnterTracking=new Map;_entityEnterFrameSet=new Set;_systemContexts=new WeakMap;_pendingFinalizers=[];_batchingRegistrations=!1;constructor(){this._entityManager=new H,this._eventBus=new V,this._resourceManager=new E,this._reactiveQueryManager=new R(this._entityManager),this._commandBuffer=new w,this._subscribeLifecycleHooks()}_subscribeLifecycleHooks(){this._entityManager.onAfterComponentAdded((j,$)=>{this._entityManager.markChanged(j,$);let G=this._requiredComponents.get($);if(G){let J=this._entityManager.getEntity(j);if(J){let X=J.components[$];for(let{component:Y,factory:Z}of G){if(this._entityManager._pendingBatchKeys?.has(Y))continue;if(!(Y in J.components))this._entityManager.addComponent(j,Y,Z(X))}}}}),this._entityManager.onAfterEntityMutated((j)=>{let $=this._entityManager.getEntity(j);if($)this._reactiveQueryManager.recheckEntityAndChildren($)}),this._entityManager.onAfterComponentRemoved((j,$)=>{let G=this._entityManager.getEntity(j);if(G)this._reactiveQueryManager.onComponentRemoved(G,$)}),this._entityManager.onBeforeEntityRemoved((j)=>{this._reactiveQueryManager.onEntityRemoved(j)}),this._entityManager.onAfterParentChanged((j)=>{if(this._reactiveQueryManager.hasParentHasQueries){let $=this._entityManager.getEntity(j);if($)this._reactiveQueryManager.recheckEntity($)}})}static create(){return new v}addSystem(j){let $=new S(j);return this._pendingFinalizers.push(()=>{this._registerSystem($._createSystemObject())}),$}_finalizePendingBuilders(){if(this._pendingFinalizers.length===0)return;this._batchingRegistrations=!0;while(this._pendingFinalizers.length>0){let j=this._pendingFinalizers;this._pendingFinalizers=[];for(let $ of j)$()}this._batchingRegistrations=!1,this._rebuildPhaseSystems()}update(j){this._finalizePendingBuilders();let $=this._screenManager?.getCurrentScreen()??null,G=this._diagnosticsEnabled;this._runPhase("preUpdate",j,$,G);let J=G?performance.now():0;this._fixedAccumulator+=j;let X=0;while(this._fixedAccumulator>=this._fixedDt&&X<this._maxFixedSteps)this._executePhase(this._phaseSystems.fixedUpdate,this._fixedDt,$),this._commandBuffer.playback(this),this._fixedAccumulator-=this._fixedDt,X++;if(this._fixedAccumulator>=this._fixedDt)this._fixedAccumulator=0;if(G)this._phaseTimings.fixedUpdate=performance.now()-J;this._interpolationAlpha=this._fixedAccumulator/this._fixedDt,this._runPhase("update",j,$,G),this._runPhase("postUpdate",j,$,G);for(let Y of this._postUpdateHooks)Y({ecs:this,dt:j});this._runPhase("render",j,$,G),this._changeThreshold=this._entityManager.changeSeq,this._currentTick++}_executePhase(j,$,G){for(let J of j){if(!J.process&&!J.onEntityEnter)continue;if(J.groups?.length){let W=!1;for(let F of J.groups)if(this._disabledGroups.has(F)){W=!0;break}if(W)continue}if(J.inScreens?.length){if(G===null||!J.inScreens.includes(G))continue}if(J.excludeScreens?.length){if(G!==null&&J.excludeScreens.includes(G))continue}if(J.requiredAssets?.length&&this._assetManager){let W=!0;for(let F of J.requiredAssets)if(!this._assetManager.isLoaded(F)){W=!1;break}if(!W)continue}let X=this._systemLastSeqs.get(J)??0;this._changeThreshold=X;let Y=this._systemContexts.get(J);if(!Y)Y={queries:{},dt:0,ecs:this},this._systemContexts.set(J,Y);Y.dt=$;let Z=Y.queries,B=!1,A=!1;if(J.entityQueries)for(let W in J.entityQueries){A=!0;let F=J.entityQueries[W];if(F){let L=Z[W]??(Z[W]=[]);if(this._entityManager.getEntitiesWithQueryInto(L,F.with,F.without||[],F.changed,F.changed?this._changeThreshold:void 0,F.parentHas),L.length)B=!0}}let U=this._entityEnterTracking.get(J);if(U&&J.onEntityEnter)for(let W in J.onEntityEnter){let F=Z[W],D=U.get(W);if(!F||!D)continue;let L=J.onEntityEnter[W];if(!L)continue;let _=this._entityEnterFrameSet;_.clear();for(let Q of F)if(_.add(Q.id),!D.has(Q.id))D.add(Q.id),L({entity:Q,ecs:this});for(let Q of D)if(!_.has(Q))D.delete(Q)}if(J.process){if(this._diagnosticsEnabled){let W=performance.now();if(B||J.runWhenEmpty)J.process(Y);else if(!A)J.process(Y);this._systemTimings.set(J.label,performance.now()-W)}else if(B||J.runWhenEmpty)J.process(Y);else if(!A)J.process(Y)}this._systemLastSeqs.set(J,this._entityManager.changeSeq)}}_runPhase(j,$,G,J){if(J){let X=performance.now();this._executePhase(this._phaseSystems[j],$,G),this._phaseTimings[j]=performance.now()-X}else this._executePhase(this._phaseSystems[j],$,G);this._commandBuffer.playback(this)}async initialize(){if(this._finalizePendingBuilders(),await this.initializeResources(),this._assetManager)this._assetManager.setEventBus(this._eventBus),await this._assetManager.loadEagerAssets(),this._resourceManager.add("$assets",this._assetManager.createResource());if(this._screenManager)this._screenManager.setDependencies(this._eventBus,this._assetManager,this),this._resourceManager.add("$screen",this._screenManager.createResource());for(let j of this._systems)await j.onInitialize?.(this)}async initializeResources(...j){await this._resourceManager.initializeResources(this,...j)}_rebuildPhaseSystems(){for(let j of h)this._phaseSystems[j]=[];for(let j of this._systems){let $=j.phase??"update";this._phaseSystems[$].push(j)}for(let j of h)this._phaseSystems[j].sort(($,G)=>{let J=$.priority??0;return(G.priority??0)-J})}updateSystemPriority(j,$){this._finalizePendingBuilders();let G=this._systems.find((J)=>J.label===j);if(!G)return!1;return G.priority=$,this._rebuildPhaseSystems(),!0}updateSystemPhase(j,$){this._finalizePendingBuilders();let G=this._systems.find((J)=>J.label===j);if(!G)return!1;return G.phase=$,this._rebuildPhaseSystems(),!0}get interpolationAlpha(){return this._interpolationAlpha}get fixedDt(){return this._fixedDt}disableSystemGroup(j){this._disabledGroups.add(j)}enableSystemGroup(j){this._disabledGroups.delete(j)}isSystemGroupEnabled(j){return!this._disabledGroups.has(j)}getSystemsInGroup(j){return this._finalizePendingBuilders(),this._systems.filter(($)=>$.groups?.includes(j)).map(($)=>$.label)}removeSystem(j){this._finalizePendingBuilders();let $=this._systems.findIndex((J)=>J.label===j);if($===-1)return!1;let G=this._systems[$];if(!G)return!1;if(G.onDetach)G.onDetach(this);return this._systems.splice($,1),this._systemLastSeqs.delete(G),this._entityEnterTracking.delete(G),this._rebuildPhaseSystems(),!0}_registerSystem(j){if(this._systems.push(j),this._systemLastSeqs.set(j,this._changeThreshold),!this._batchingRegistrations)this._rebuildPhaseSystems();if(j.onEntityEnter){let $=new Map;for(let G in j.onEntityEnter)$.set(G,new Set);this._entityEnterTracking.set(j,$)}if(!j.eventHandlers)return;for(let $ in j.eventHandlers){let G=j.eventHandlers[$];if(G)this._eventBus.subscribe($,(J)=>{G({data:J,ecs:this})})}}hasResource(j){return this._resourceManager.has(j)}getResource(j){if(!this._resourceManager.has(j))throw Error(`Resource '${String(j)}' not found. Available resources: [${this.getResourceKeys().map(($)=>String($)).join(", ")}]`);return this._resourceManager.get(j,this)}tryGetResource(j){let $=j;if(!this._resourceManager.has($))return;return this._resourceManager.get($,this)}addResource(j,$){return this._resourceManager.add(j,$),this}removeResource(j){return this._resourceManager.remove(j)}async disposeResource(j){return this._resourceManager.disposeResource(j,this)}async disposeResources(){return this._resourceManager.disposeResources(this)}updateResource(j,$){let G=this.getResource(j),J=$(G);return this._resourceManager.add(j,J),this._resourceManager.notifyChange(j,J,G),this}setResource(j,$){let G=this.tryGetResource(j);if(this._resourceManager.add(j,$),G!==void 0)this._resourceManager.notifyChange(j,$,G);return this}onResourceChange(j,$){return this._resourceManager.onResourceChange(j,$)}getResourceKeys(){return this._resourceManager.getKeys()}resourceNeedsInitialization(j){return this._resourceManager.needsInitialization(j)}getEntity(j){return this._entityManager.getEntity(j)}getComponent(j,$){return this._entityManager.getComponent(j,$)}addComponent(j,$,G){this._entityManager.addComponent(j,$,G)}addComponents(j,$){this._entityManager.addComponents(j,$)}removeComponent(j,$){this._entityManager.removeComponent(j,$)}hasComponent(j,$){return this._entityManager.getComponent(j,$)!==void 0}spawn(j){let $=this._entityManager.createEntity();return this._entityManager.addComponents($.id,j),$}getEntitiesWithQuery(j,$=[],G,J){return this._entityManager.getEntitiesWithQuery(j,$,G,G?this._changeThreshold:void 0,J)}getSingleton(j,$=[]){let G=this._entityManager.getEntitiesWithQuery(j,$);if(G.length===0)throw Error(`getSingleton: no entity matches query with=[${String(j)}] without=[${String($)}]`);if(G.length>1)throw Error(`getSingleton: expected 1 entity but found ${G.length} matching query with=[${String(j)}] without=[${String($)}]`);let J=G[0];if(!J)throw Error("getSingleton: unexpected empty result");return J}tryGetSingleton(j,$=[]){let G=this._entityManager.getEntitiesWithQuery(j,$);if(G.length===0)return;if(G.length>1)throw Error(`tryGetSingleton: expected 0 or 1 entity but found ${G.length} matching query with=[${String(j)}] without=[${String($)}]`);return G[0]}removeEntity(j,$){return this._entityManager.removeEntity(j,$)}spawnChild(j,$){let G=this._entityManager.spawnChild(j,$);return this._emitHierarchyChanged(G.id,null,j),G}setParent(j,$){let G=this._entityManager.getParent(j);return this._entityManager.setParent(j,$),this._emitHierarchyChanged(j,G,$),this}removeParent(j){let $=this._entityManager.getParent(j),G=this._entityManager.removeParent(j);if(G)this._emitHierarchyChanged(j,$,null);return G}getParent(j){return this._entityManager.getParent(j)}getChildren(j){return this._entityManager.getChildren(j)}getChildAt(j,$){return this._entityManager.getChildAt(j,$)}getChildIndex(j,$){return this._entityManager.getChildIndex(j,$)}getAncestors(j){return this._entityManager.getAncestors(j)}getDescendants(j){return this._entityManager.getDescendants(j)}getRoot(j){return this._entityManager.getRoot(j)}getSiblings(j){return this._entityManager.getSiblings(j)}isDescendantOf(j,$){return this._entityManager.isDescendantOf(j,$)}isAncestorOf(j,$){return this._entityManager.isAncestorOf(j,$)}getRootEntities(){return this._entityManager.getRootEntities()}forEachInHierarchy(j,$){this._entityManager.forEachInHierarchy(j,$)}hierarchyIterator(j){return this._entityManager.hierarchyIterator(j)}_emitHierarchyChanged(j,$,G){this._eventBus.publish("hierarchyChanged",{entityId:j,oldParent:$,newParent:G})}get installedPlugins(){return Array.from(this._installedPlugins)}get entityManager(){return this._entityManager}get eventBus(){return this._finalizePendingBuilders(),this._eventBus}get commands(){return this._commandBuffer}get currentTick(){return this._currentTick}get changeThreshold(){return this._changeThreshold}enableDiagnostics(j){if(this._diagnosticsEnabled=j,!j)this._systemTimings.clear(),this._phaseTimings={preUpdate:0,fixedUpdate:0,update:0,postUpdate:0,render:0}}get diagnosticsEnabled(){return this._diagnosticsEnabled}get systemTimings(){return this._systemTimings}get phaseTimings(){return this._phaseTimings}get entityCount(){return this._entityManager.entityCount}mutateComponent(j,$,G){let J=this._entityManager.getComponent(j,$);if(J===void 0)throw Error(`Entity ${j} does not have component "${String($)}"`);return G(J),this._entityManager.markChanged(j,$),J}markChanged(j,$){this._entityManager.markChanged(j,$)}registerDispose(j,$){this._entityManager.registerDispose(j,$)}registerRequired(j,$,G){if(String(j)===String($))throw Error(`Cannot require a component to depend on itself: '${String(j)}'`);let J=this._requiredComponents.get(j)??[];if(J.some((X)=>X.component===$))throw Error(`Required component '${String($)}' already registered for trigger '${String(j)}'`);this._checkRequiredCycle(j,$),J.push({component:$,factory:G}),this._requiredComponents.set(j,J)}_checkRequiredCycle(j,$){N(j,$,(G)=>this._requiredComponents.get(G))}onComponentAdded(j,$){return this._entityManager.onComponentAdded(j,$)}onComponentRemoved(j,$){return this._entityManager.onComponentRemoved(j,$)}addReactiveQuery(j,$){this._reactiveQueryManager.addQuery(j,$)}removeReactiveQuery(j){return this._reactiveQueryManager.removeQuery(j)}on(j,$){return this._eventBus.subscribe(j,$)}off(j,$){return this._eventBus.unsubscribe(j,$)}onPostUpdate(j){return this._postUpdateHooks.push(j),()=>{let $=this._postUpdateHooks.indexOf(j);if($!==-1)this._postUpdateHooks.splice($,1)}}requireAssetManager(){if(!this._assetManager)throw Error("Asset manager not configured. Use withAssets() in builder.");return this._assetManager}getAsset(j){return this.requireAssetManager().get(j)}tryGetAsset(j){return this._assetManager?.tryGet(j)}getAssetHandle(j){return this.requireAssetManager().getHandle(j)}isAssetLoaded(j){return this._assetManager?.isLoaded(j)??!1}async loadAsset(j){return this.requireAssetManager().loadAsset(j)}async loadAssetGroup(j){return this.requireAssetManager().loadAssetGroup(j)}isAssetGroupLoaded(j){return this._assetManager?.isGroupLoaded(j)??!1}getAssetGroupProgress(j){return this._assetManager?.getGroupProgress(j)??0}requireScreenManager(){if(!this._screenManager)throw Error("Screen manager not configured. Use withScreens() in builder.");return this._screenManager}async setScreen(j,$){return this.requireScreenManager().setScreen(j,$)}async pushScreen(j,$){return this.requireScreenManager().pushScreen(j,$)}async popScreen(){return this.requireScreenManager().popScreen()}getCurrentScreen(){return this._screenManager?.getCurrentScreen()??null}getScreenConfig(j){return this.requireScreenManager().getConfig(j)}tryGetScreenConfig(j){return this._screenManager?.tryGetConfig(j)??void 0}getScreenState(j){return this.requireScreenManager().getState(j)}tryGetScreenState(j){return this._screenManager?.tryGetState(j)??void 0}updateScreenState(j,$){if(typeof j==="string")this.requireScreenManager().updateState($,j);else this.requireScreenManager().updateState(j)}isCurrentScreen(j){return this._screenManager?.isCurrent(j)??!1}isScreenActive(j){return this._screenManager?.isActive(j)??!1}getScreenStackDepth(){return this._screenManager?.getStackDepth()??0}_setAssetManager(j){this._assetManager=j;for(let[$,G]of this._pendingPluginAssets)this._assetManager.register($,G);this._pendingPluginAssets=[]}_setScreenManager(j){this._screenManager=j;for(let[$,G]of this._pendingPluginScreens)this._screenManager.register($,G);this._pendingPluginScreens=[]}_hasPendingPluginAssets(){return this._pendingPluginAssets.length>0}_hasPendingPluginScreens(){return this._pendingPluginScreens.length>0}_setFixedDt(j){this._fixedDt=j}_registerAsset(j,$){this._pendingPluginAssets.push([j,$])}_registerScreen(j,$){this._pendingPluginScreens.push([j,$])}installPlugin(j){if(this._installedPlugins.has(j.id))return this;return this._installedPlugins.add(j.id),j.install(this),this}pluginFactory(){return z}getHelpers(j){return j(this)}}function Hj(j){return j}function Ej(j,$){return{x:j,y:$}}function Rj(){return{x:0,y:0}}function wj(j,$){return{x:j.x+$.x,y:j.y+$.y}}function Sj(j,$){return{x:j.x-$.x,y:j.y-$.y}}function Tj(j,$){return{x:j.x*$,y:j.y*$}}function xj(j){return{x:-j.x,y:-j.y}}function Cj(j,$){return j.x*$.x+j.y*$.y}function qj(j,$){return j.x*$.y-j.y*$.x}function vj(j){return j.x*j.x+j.y*j.y}function fj(j){return Math.sqrt(j.x*j.x+j.y*j.y)}function Nj(j){let $=Math.sqrt(j.x*j.x+j.y*j.y);if($===0)return{x:0,y:0};return{x:j.x/$,y:j.y/$}}function bj(j,$){let G=j.x-$.x,J=j.y-$.y;return G*G+J*J}function gj(j,$){let G=j.x-$.x,J=j.y-$.y;return Math.sqrt(G*G+J*J)}function kj(j,$,G=0.0000000001){return Math.abs(j.x-$.x)<=G&&Math.abs(j.y-$.y)<=G}var rj=P;export{Rj as vec2Zero,Sj as vec2Sub,Tj as vec2Scale,Nj as vec2Normalize,xj as vec2Negate,vj as vec2LengthSq,fj as vec2Length,kj as vec2Equals,Cj as vec2Dot,bj as vec2DistanceSq,gj as vec2Distance,qj as vec2Cross,wj as vec2Add,Ej as vec2,I as directValue,z as definePlugin,rj as default,q as createScreenConfigurator,Hj as createQueryDefinition,C as createAssetConfigurator,S as SystemBuilder,O as ScreenManager,K as AssetManager};
1
+ var l=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:($,G)=>(typeof require<"u"?require:$)[G]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});class M{parentMap=new Map;childrenMap=new Map;setParent(j,$){if(j===$)throw Error(`Cannot set entity ${j} as its own parent`);if(this.wouldCreateCycle(j,$))throw Error("Cannot set parent: would create circular reference");let G=this.parentMap.get(j);if(G!==void 0){let X=this.childrenMap.get(G);if(X){let Y=X.indexOf(j);if(Y!==-1)X.splice(Y,1)}}this.parentMap.set(j,$);let J=this.childrenMap.get($);if(J)J.push(j);else this.childrenMap.set($,[j]);return this}removeParent(j){let $=this.parentMap.get(j);if($===void 0)return!1;let G=this.childrenMap.get($);if(G){let J=G.indexOf(j);if(J!==-1)G.splice(J,1)}return this.parentMap.delete(j),!0}getParent(j){return this.parentMap.get(j)??null}getChildren(j){let $=this.childrenMap.get(j);return $?[...$]:[]}getChildAt(j,$){if($<0)return null;let G=this.childrenMap.get(j);if(!G||$>=G.length)return null;return G[$]??null}getChildIndex(j,$){let G=this.childrenMap.get(j);if(!G)return-1;return G.indexOf($)}removeEntity(j){let $=this.parentMap.get(j)??null;if($!==null){let X=this.childrenMap.get($);if(X){let Y=X.indexOf(j);if(Y!==-1)X.splice(Y,1)}}this.parentMap.delete(j);let G=this.childrenMap.get(j)??[],J=[...G];for(let X of G)this.parentMap.delete(X);return this.childrenMap.delete(j),{oldParent:$,orphanedChildren:J}}getAncestors(j){let $=[],G=this.parentMap.get(j);while(G!==void 0)$.push(G),G=this.parentMap.get(G);return $}getDescendants(j){let $=[],G=this.childrenMap.get(j);if(!G)return $;let J=G.slice().reverse();while(J.length>0){let X=J.pop();$.push(X);let Y=this.childrenMap.get(X);if(Y)for(let Z=Y.length-1;Z>=0;Z--)J.push(Y[Z])}return $}getRoot(j){let $=j,G=this.parentMap.get($);while(G!==void 0)$=G,G=this.parentMap.get($);return $}getSiblings(j){let $=this.parentMap.get(j);if($===void 0)return[];let G=this.childrenMap.get($);if(!G)return[];return G.filter((J)=>J!==j)}isDescendantOf(j,$){if(j===$)return!1;let G=this.parentMap.get(j);while(G!==void 0){if(G===$)return!0;G=this.parentMap.get(G)}return!1}isAncestorOf(j,$){return this.isDescendantOf($,j)}getRootEntities(){let j=[];for(let $ of this.childrenMap.keys())if(!this.parentMap.has($))j.push($);return j}wouldCreateCycle(j,$){let G=$;while(G!==void 0){if(G===j)return!0;G=this.parentMap.get(G)}return!1}forEachInHierarchy(j,$){let G=$?.roots??this.getRootEntities(),J=[];for(let X of G)J.push({entityId:X,parentId:null,depth:0});for(let X of J){j(X.entityId,X.parentId,X.depth);let Y=this.childrenMap.get(X.entityId);if(Y)for(let Z of Y)J.push({entityId:Z,parentId:X.entityId,depth:X.depth+1})}}*hierarchyIterator(j){let $=j?.roots??this.getRootEntities(),G=[];for(let J of $)G.push({entityId:J,parentId:null,depth:0});for(let J of G){yield J;let X=this.childrenMap.get(J.entityId);if(X)for(let Y of X)G.push({entityId:Y,parentId:J.entityId,depth:J.depth+1})}}}class T{callbacks=[];_iterDepth=0;_pendingRemovals=[];add(j){this.callbacks.push(j)}remove(j){if(this._iterDepth>0){this._pendingRemovals.push(j);return}let $=this.callbacks.indexOf(j);if($!==-1)this.callbacks.splice($,1)}invoke(j){this._iterDepth++;let $=this.callbacks.length;for(let G=0;G<$;G++){let J=this.callbacks[G];if(J)J(j)}if(this._iterDepth--,this._iterDepth===0&&this._pendingRemovals.length>0){for(let G of this._pendingRemovals){let J=this.callbacks.indexOf(G);if(J!==-1)this.callbacks.splice(J,1)}this._pendingRemovals.length=0}}}class H{nextId=1;entities=new Map;componentIndices=new Map;addedCallbacks=new Map;removedCallbacks=new Map;hierarchyManager=new M;disposeCallbacks=new Map;changeSeqs=new Map;_changeSeq=0;_afterComponentAddedHooks=[];_afterEntityMutatedHooks=[];_afterComponentRemovedHooks=[];_beforeEntityRemovedHooks=[];_afterParentChangedHooks=[];_batchingDepth=0;_batchedEntityIds=new Set;_pendingBatchKeys=null;get entityCount(){return this.entities.size}createEntity(){let j=this.nextId++,$={id:j,components:{}};return this.entities.set(j,$),$}registerDispose(j,$){this.disposeCallbacks.set(j,$)}getDisposeCallbacks(){return this.disposeCallbacks}invokeDispose(j,$,G){let J=this.disposeCallbacks.get(j);if(!J)return;try{J({value:$,entityId:G})}catch(X){console.warn(`Component dispose callback for '${String(j)}' threw:`,X)}}addComponent(j,$,G){let J=this.entities.get(j);if(!J)throw Error(`Cannot add component '${String($)}': Entity with ID ${j} does not exist`);let X=J.components[$];if(X!==void 0)this.invokeDispose($,X,J.id);if(J.components[$]=G,!this.componentIndices.has($))this.componentIndices.set($,new Set);this.componentIndices.get($)?.add(J.id);let Y=this.addedCallbacks.get($);if(Y)Y.invoke({value:G,entity:J});this._batchingDepth++;for(let Z of this._afterComponentAddedHooks)Z(J.id,$);if(this._batchedEntityIds.add(J.id),this._batchingDepth--,this._batchingDepth===0){for(let Z of this._batchedEntityIds)for(let F of this._afterEntityMutatedHooks)F(Z);this._batchedEntityIds.clear()}return this}addComponents(j,$){let G=this.entities.get(j);if(!G)throw Error(`Cannot add components: Entity with ID ${j} does not exist`);let J=this._pendingBatchKeys;this._pendingBatchKeys=new Set(Object.keys($)),this._batchingDepth++;for(let X in $)this.addComponent(G.id,X,$[X]);if(this._batchingDepth--,this._pendingBatchKeys=J,this._batchingDepth===0){for(let X of this._batchedEntityIds)for(let Y of this._afterEntityMutatedHooks)Y(X);this._batchedEntityIds.clear()}return this}removeComponent(j,$){let G=this.entities.get(j);if(!G)throw Error(`Cannot remove component '${String($)}': Entity with ID ${j} does not exist`);let J=G.components[$];if(J!==void 0)this.invokeDispose($,J,G.id);delete G.components[$];let X=this.removedCallbacks.get($);if(X&&J!==void 0)X.invoke({value:J,entity:G});if(this.componentIndices.get($)?.delete(G.id),J!==void 0)for(let Y of this._afterComponentRemovedHooks)Y(G.id,$);return this}getComponent(j,$){let G=this.entities.get(j);if(!G)throw Error(`Cannot get component '${String($)}': Entity with ID ${j} does not exist`);return G.components[$]}getEntitiesWithQuery(j=[],$=[],G,J,X){return this.getEntitiesWithQueryInto([],j,$,G,J,X)}getEntitiesWithQueryInto(j,$=[],G=[],J,X,Y){j.length=0;let Z=J!==void 0&&J.length>0&&X!==void 0,F=Y!==void 0&&Y.length>0;if($.length===0){if(G.length===0&&!Z&&!F){for(let D of this.entities.values())j.push(D);return j}for(let D of this.entities.values()){if(G.length>0&&!G.every((L)=>!(L in D.components)))continue;if(Z){let L=this.changeSeqs.get(D.id);if(!L)continue;if(!J.some((B)=>(L.get(B)??-1)>X))continue}if(F&&!this.parentHasComponents(D.id,Y))continue;j.push(D)}return j}let A=$[0];if(A===void 0)return j;let O=$.reduce((D,L)=>{let B=this.componentIndices.get(L)?.size??0,Q=this.componentIndices.get(D)?.size??1/0;return B<Q?L:D},A),W=this.componentIndices.get(O);if(!W||W.size===0)return j;let _=G.length>0;for(let D of W){let L=this.entities.get(D);if(L&&$.every((B)=>(B in L.components))&&(!_||G.every((B)=>!(B in L.components)))){if(Z){let B=this.changeSeqs.get(D);if(!B||!J.some((Q)=>(B.get(Q)??-1)>X))continue}if(F&&!this.parentHasComponents(D,Y))continue;j.push(L)}}return j}parentHasComponents(j,$){let G=this.hierarchyManager.getParent(j);if(G===null)return!1;let J=this.entities.get(G);if(!J)return!1;for(let X of $)if(!(X in J.components))return!1;return!0}removeEntity(j,$){let G=this.entities.get(j);if(!G)return!1;if($?.cascade??!0){let X=this.hierarchyManager.getDescendants(G.id);for(let Y=X.length-1;Y>=0;Y--){let Z=X[Y];if(Z===void 0)continue;for(let F of this._beforeEntityRemovedHooks)F(Z)}for(let Y of this._beforeEntityRemovedHooks)Y(G.id);for(let Y=X.length-1;Y>=0;Y--){let Z=X[Y];if(Z===void 0)continue;this.removeEntityInternal(Z)}}else for(let X of this._beforeEntityRemovedHooks)X(G.id);return this.removeEntityInternal(G.id)}removeEntityInternal(j){let $=this.entities.get(j);if(!$)return!1;this.hierarchyManager.removeEntity(j);for(let G of Object.keys($.components)){let J=$.components[G];if(J!==void 0){this.invokeDispose(G,J,$.id);let X=this.removedCallbacks.get(G);if(X)X.invoke({value:J,entity:$})}this.componentIndices.get(G)?.delete($.id)}return this.changeSeqs.delete($.id),this.entities.delete($.id)}getEntity(j){return this.entities.get(j)}onComponentAdded(j,$){let G=$,J=this.addedCallbacks.get(j);if(!J)J=new T,this.addedCallbacks.set(j,J);return J.add(G),()=>{this.addedCallbacks.get(j)?.remove(G)}}onComponentRemoved(j,$){let G=$,J=this.removedCallbacks.get(j);if(!J)J=new T,this.removedCallbacks.set(j,J);return J.add(G),()=>{this.removedCallbacks.get(j)?.remove(G)}}onAfterComponentAdded(j){return this._afterComponentAddedHooks.push(j),()=>{let $=this._afterComponentAddedHooks.indexOf(j);if($!==-1)this._afterComponentAddedHooks.splice($,1)}}onAfterEntityMutated(j){return this._afterEntityMutatedHooks.push(j),()=>{let $=this._afterEntityMutatedHooks.indexOf(j);if($!==-1)this._afterEntityMutatedHooks.splice($,1)}}onAfterComponentRemoved(j){return this._afterComponentRemovedHooks.push(j),()=>{let $=this._afterComponentRemovedHooks.indexOf(j);if($!==-1)this._afterComponentRemovedHooks.splice($,1)}}onBeforeEntityRemoved(j){return this._beforeEntityRemovedHooks.push(j),()=>{let $=this._beforeEntityRemovedHooks.indexOf(j);if($!==-1)this._beforeEntityRemovedHooks.splice($,1)}}onAfterParentChanged(j){return this._afterParentChangedHooks.push(j),()=>{let $=this._afterParentChangedHooks.indexOf(j);if($!==-1)this._afterParentChangedHooks.splice($,1)}}get changeSeq(){return this._changeSeq}markChanged(j,$){let G=++this._changeSeq,J=this.changeSeqs.get(j);if(!J)J=new Map,this.changeSeqs.set(j,J);J.set($,G)}getChangeSeq(j,$){return this.changeSeqs.get(j)?.get($)??-1}spawnChild(j,$){let G=this.createEntity();return this.addComponents(G.id,$),this.setParent(G.id,j),G}setParent(j,$){this.hierarchyManager.setParent(j,$);for(let G of this._afterParentChangedHooks)G(j);return this}removeParent(j){let $=this.hierarchyManager.removeParent(j);if($)for(let G of this._afterParentChangedHooks)G(j);return $}getParent(j){return this.hierarchyManager.getParent(j)}getChildren(j){return this.hierarchyManager.getChildren(j)}getChildAt(j,$){return this.hierarchyManager.getChildAt(j,$)}getChildIndex(j,$){return this.hierarchyManager.getChildIndex(j,$)}getAncestors(j){return this.hierarchyManager.getAncestors(j)}getDescendants(j){return this.hierarchyManager.getDescendants(j)}getRoot(j){return this.hierarchyManager.getRoot(j)}getSiblings(j){return this.hierarchyManager.getSiblings(j)}isDescendantOf(j,$){return this.hierarchyManager.isDescendantOf(j,$)}isAncestorOf(j,$){return this.hierarchyManager.isAncestorOf(j,$)}getRootEntities(){return this.hierarchyManager.getRootEntities()}forEachInHierarchy(j,$){this.hierarchyManager.forEachInHierarchy(j,$)}hierarchyIterator(j){return this.hierarchyManager.hierarchyIterator(j)}}class V{handlers=new Map;subscribe(j,$){return this.addHandler(j,$,!1)}once(j,$){return this.addHandler(j,$,!0)}unsubscribe(j,$){let G=this.handlers.get(j);if(!G)return!1;let J=G.findIndex((X)=>X.callback===$);if(J===-1)return!1;return G.splice(J,1),!0}addHandler(j,$,G){let J=this.handlers.get(j);if(!J)J=[],this.handlers.set(j,J);let X={callback:$,once:G};return J.push(X),()=>{let Y=this.handlers.get(j);if(Y){let Z=Y.indexOf(X);if(Z!==-1)Y.splice(Z,1)}}}publish(...[j,$]){let G=this.handlers.get(j);if(!G||G.length===0)return;let J=!1,X=G.length;for(let Y=0;Y<X&&Y<G.length;Y++){let Z=G[Y];if(!Z)continue;if(Z.callback($),Z.once)J=!0}if(J){for(let Y=G.length-1;Y>=0;Y--)if(G[Y]?.once)G.splice(Y,1)}}clear(){this.handlers.clear()}clearEvent(j){this.handlers.delete(j)}}var x=Symbol("resource-direct");function I(j){return{[x]:j}}function u(j){return typeof j==="object"&&j!==null&&"factory"in j&&typeof j.factory==="function"}function m(j){return typeof j==="object"&&j!==null&&x in j}function f(j,$){let G=[],J=new Set,X=new Set;function Y(Z,F=[]){if(J.has(Z))return;if(X.has(Z))throw Error(`Circular resource dependency: ${[...F,Z].join(" -> ")}`);X.add(Z);for(let A of $(Z)){let O=j.find((W)=>W===A);if(O)Y(O,[...F,Z])}X.delete(Z),J.add(Z),G.push(Z)}for(let Z of j)Y(Z);return G}class E{resources=new Map;resourceFactories=new Map;resourceDependencies=new Map;resourceDisposers=new Map;initializedResourceKeys=new Set;_changeSubscribers=new Map;add(j,$){let G=(J)=>{this.resources.set(j,J),this.initializedResourceKeys.add(j),this.resourceDependencies.set(j,[])};if(u($)){if(this.resourceFactories.set(j,$.factory),this.resourceDependencies.set(j,$.dependsOn??[]),$.onDispose)this.resourceDisposers.set(j,$.onDispose)}else if(m($))G($[x]);else if(typeof $==="function")this.resourceFactories.set(j,$),this.resourceDependencies.set(j,[]);else G($);return this}tryGet(j,...$){if(!this.has(j))return;return this.get(j,...$)}get(j,...$){let G=this.resources.get(j);if(G!==void 0)return G;let J=this.resourceFactories.get(j);if(J===void 0)throw Error(`Resource ${String(j)} not found`);let X=$[0],Y=J(X);if(!(Y instanceof Promise))this.resources.set(j,Y),this.initializedResourceKeys.add(j);return Y}has(j){return this.resources.has(j)||this.resourceFactories.has(j)}remove(j){let $=this.resources.delete(j),G=this.resourceFactories.delete(j);return this.resourceDependencies.delete(j),this.resourceDisposers.delete(j),this.initializedResourceKeys.delete(j),$||G}getKeys(){let j=new Set([...this.resources.keys(),...this.resourceFactories.keys()]);return Array.from(j)}needsInitialization(j){return this.resourceFactories.has(j)&&!this.initializedResourceKeys.has(j)}getPendingInitializationKeys(){return Array.from(this.resourceFactories.keys()).filter((j)=>!this.initializedResourceKeys.has(j))}async initializeResource(j,...$){if(!this.resourceFactories.has(j)||this.initializedResourceKeys.has(j))return;let G=this.resourceFactories.get(j);if(!G)return;let J=$[0],X=await G(J);this.resources.set(j,X),this.initializedResourceKeys.add(j),this.resourceFactories.delete(j)}async initializeResources(...j){let $=j.slice(1),G=$.length===0?this.getPendingInitializationKeys():$;if(G.length===0)return;let J=f(G,(X)=>[...this.resourceDependencies.get(X)??[]]);for(let X of J)await this.initializeResource(X,...j.slice(0,1))}getDependencies(j){return this.resourceDependencies.get(j)??[]}async disposeResource(j,...$){if(!this.resources.has(j)&&!this.resourceFactories.has(j))return!1;if(this.initializedResourceKeys.has(j)){let G=this.resourceDisposers.get(j),J=this.resources.get(j);if(G&&J!==void 0){let X=$[0];await G(J,X)}}return this.resources.delete(j),this.resourceFactories.delete(j),this.resourceDependencies.delete(j),this.resourceDisposers.delete(j),this.initializedResourceKeys.delete(j),this._changeSubscribers.delete(j),!0}onResourceChange(j,$){let G=this._changeSubscribers.get(j),J=G??new Set;if(!G)this._changeSubscribers.set(j,J);let X=$;return J.add(X),()=>{if(J.delete(X),J.size===0)this._changeSubscribers.delete(j)}}notifyChange(j,$,G){if(Object.is($,G))return;let J=this._changeSubscribers.get(j);if(!J||J.size===0)return;let X=[...J];for(let Y of X)Y($,G)}async disposeResources(...j){let $=Array.from(this.initializedResourceKeys);if($.length===0)return;let G=f($,(J)=>[...this.resourceDependencies.get(J)??[]]).reverse();for(let J of G)await this.disposeResource(J,...j)}}class R{queries=new Map;entityManager;_hasParentHasQueries=!1;constructor(j){this.entityManager=j}get hasParentHasQueries(){return this._hasParentHasQueries}addQuery(j,$){let G={definition:$,matchingEntities:new Set};if(this.queries.set(j,G),$.parentHas?.length)this._hasParentHasQueries=!0;let J=this.entityManager.getEntitiesWithQuery($.with,$.without??[]);for(let X of J)if(this.entityMatchesQuery(X,G.definition))G.matchingEntities.add(X.id),G.definition.onEnter?.(X)}removeQuery(j){let $=this.queries.delete(j);if($)this._recalcParentHasFlag();return $}entityMatchesQuery(j,$){for(let G of $.with)if(!(G in j.components))return!1;if($.without){for(let G of $.without)if(G in j.components)return!1}if($.parentHas?.length){let G=this.entityManager.getParent(j.id);if(G===null)return!1;let J=this.entityManager.getEntity(G);if(!J)return!1;for(let X of $.parentHas)if(!(X in J.components))return!1}return!0}_applyQueryTransition(j,$){let G=$.matchingEntities.has(j.id),J=this.entityMatchesQuery(j,$.definition);if(!G&&J)$.matchingEntities.add(j.id),$.definition.onEnter?.(j);else if(G&&!J)$.matchingEntities.delete(j.id),$.definition.onExit?.(j.id)}onComponentAdded(j,$){for(let[,G]of this.queries)this._applyQueryTransition(j,G);if(this._hasParentHasQueries)this._recheckChildren(j.id)}onComponentRemoved(j,$){for(let[,G]of this.queries)this._applyQueryTransition(j,G);if(this._hasParentHasQueries)this._recheckChildren(j.id)}onEntityRemoved(j){for(let[$,G]of this.queries)if(G.matchingEntities.has(j))G.matchingEntities.delete(j),G.definition.onExit?.(j)}recheckEntity(j){for(let[,$]of this.queries)this._applyQueryTransition(j,$)}recheckEntityAndChildren(j){if(this.recheckEntity(j),this._hasParentHasQueries)this._recheckChildren(j.id)}_recheckChildren(j){let $=this.entityManager.getChildren(j);for(let G of $){let J=this.entityManager.getEntity(G);if(J)this.recheckEntity(J)}}_recalcParentHasFlag(){this._hasParentHasQueries=!1;for(let[,j]of this.queries)if(j.definition.parentHas?.length){this._hasParentHasQueries=!0;return}}}class w{commands=[];removeEntity(j,$){this.commands.push((G)=>{G.removeEntity(j,$)})}addComponent(j,$,G){this.commands.push((J)=>{J.addComponent(j,$,G)})}removeComponent(j,$){this.commands.push((G)=>{G.removeComponent(j,$)})}spawn(j){this.commands.push(($)=>{$.spawn(j)})}spawnChild(j,$){this.commands.push((G)=>{G.spawnChild(j,$)})}addComponents(j,$){this.commands.push((G)=>{G.addComponents(j,$)})}setParent(j,$){this.commands.push((G)=>{G.setParent(j,$)})}mutateComponent(j,$,G){this.commands.push((J)=>{J.mutateComponent(j,$,G)})}markChanged(j,$){this.commands.push((G)=>{G.markChanged(j,$)})}removeParent(j){this.commands.push(($)=>{$.removeParent(j)})}playback(j){for(let $ of this.commands)try{$(j)}catch(G){console.warn("CommandBuffer: Command failed during playback:",G)}this.commands.length=0}clear(){this.commands.length=0}get length(){return this.commands.length}}class b{_id;constructor(j){this._id=j}withComponentTypes(){return this}withEventTypes(){return this}withResourceTypes(){return this}withAssetTypes(){return this}withScreenTypes(){return this}withLabels(){return this}withGroups(){return this}withAssetGroupNames(){return this}withReactiveQueryNames(){return this}requires(){return this}install(j){return{id:this._id,install:j}}}function P(j){return new b(j)}class S{_label;queries={};processFunction;detachFunction;initializeFunction;eventHandlers;_priority=0;_phase="update";_groups=[];_inScreens;_excludeScreens;_requiredAssets;_runWhenEmpty=!1;_entityEnterHandlers={};_resourceKeys;constructor(j){this._label=j}get label(){return this._label}_createSystemObject(){let j={label:this._label,entityQueries:this.queries,priority:this._priority,phase:this._phase};if(this.processFunction)j.process=this.processFunction;if(this.detachFunction)j.onDetach=this.detachFunction;if(this.initializeFunction)j.onInitialize=this.initializeFunction;if(this.eventHandlers)j.eventHandlers=this.eventHandlers;if(this._groups.length>0)j.groups=[...this._groups];if(this._inScreens)j.inScreens=this._inScreens;if(this._excludeScreens)j.excludeScreens=this._excludeScreens;if(this._requiredAssets)j.requiredAssets=this._requiredAssets;if(this._runWhenEmpty)j.runWhenEmpty=!0;if(Object.keys(this._entityEnterHandlers).length>0)j.onEntityEnter={...this._entityEnterHandlers};return j}setPriority(j){return this._priority=j,this}inPhase(j){return this._phase=j,this}inGroup(j){if(!this._groups.includes(j))this._groups.push(j);return this}inScreens(j){return this._inScreens=[...j],this}excludeScreens(j){return this._excludeScreens=[...j],this}requiresAssets(j){return this._requiredAssets=[...j],this}runWhenEmpty(){return this._runWhenEmpty=!0,this}withResources(j){return this._resourceKeys=[...j],this}addQuery(j,$){let G=this;return G.queries={...this.queries,[j]:$},G}setProcess(j){if(this._resourceKeys?.length){let $=this._resourceKeys,G;this.processFunction=(J)=>{if(!G){G={};for(let X of $)G[X]=J.ecs.getResource(X)}J.resources=G,j(J)}}else this.processFunction=j;return this}setOnEntityEnter(j,$){return this._entityEnterHandlers[j]=$,this}setOnDetach(j){return this.detachFunction=j,this}setOnInitialize(j){return this.initializeFunction=j,this}setEventHandlers(j){return this.eventHandlers=j,this}}function g(j,$,G){let J=new Set,X=[$];while(X.length>0){let Y=X.pop();if(Y===void 0)break;if(Y===j)throw Error(`Circular required component dependency: '${String(j)}' -> '${String($)}' -> ... -> '${String(j)}'`);if(J.has(Y))continue;J.add(Y);let Z=G(Y);if(Z)for(let F of Z)X.push(F.component)}}var N="0.13.1";class U{assets=new Map;groups=new Map;eventBus=null;setEventBus(j){this.eventBus=j}register(j,$){if(this.assets.set(j,{definition:$,status:"pending"}),$.group){let G=this.groups.get($.group)??new Set;G.add(j),this.groups.set($.group,G)}}async loadEagerAssets(){let j=[];for(let[$,G]of this.assets)if(G.definition.eager&&G.status==="pending")j.push($);await Promise.all(j.map(($)=>this.loadAsset($)))}async loadAsset(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);if($.status==="loaded"&&$.value!==void 0)return $.value;if($.status==="loading"&&$.loadPromise)return $.loadPromise;if($.status==="failed")$.status="pending";$.status="loading",$.loadPromise=$.definition.loader();try{let G=await $.loadPromise;return $.value=G,$.status="loaded",$.loadPromise=void 0,this.eventBus?.publish("assetLoaded",{key:j}),this.checkGroupProgress($.definition.group),G}catch(G){let J=G instanceof Error?G:Error(String(G));throw $.status="failed",$.error=J,$.loadPromise=void 0,this.eventBus?.publish("assetFailed",{key:j,error:J}),J}}async loadAssetGroup(j){let $=this.groups.get(j);if(!$||$.size===0)throw Error(`Asset group '${j}' not found or empty`);await Promise.all(Array.from($).map((G)=>this.loadAsset(G)))}get(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);if($.status!=="loaded"||$.value===void 0)throw Error(`Asset '${String(j)}' is not loaded (status: ${$.status})`);return $.value}tryGet(j){let $=this.assets.get(j);if(!$||$.status!=="loaded")return;return $.value}getHandle(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);let G=this;return{get status(){return $.status},get isLoaded(){return $.status==="loaded"},get(){return G.get(j)},tryGet(){return G.tryGet(j)}}}getStatus(j){let $=this.assets.get(j);if(!$)throw Error(`Asset '${String(j)}' not found`);return $.status}isLoaded(j){return this.assets.get(j)?.status==="loaded"}isGroupLoaded(j){let $=this.groups.get(j);if(!$||$.size===0)return!1;for(let G of $){let J=this.assets.get(G);if(!J||J.status!=="loaded")return!1}return!0}getGroupProgress(j){return this.getGroupProgressDetails(j).progress}getGroupProgressDetails(j){let $=this.groups.get(j);if(!$||$.size===0)return{loaded:0,total:0,progress:0};let G=0;for(let X of $)if(this.assets.get(X)?.status==="loaded")G++;let J=$.size;return{loaded:G,total:J,progress:G/J}}checkGroupProgress(j){if(!j||!this.eventBus)return;let $=j,G=this.getGroupProgressDetails($);if(this.eventBus.publish("assetGroupProgress",{group:$,...G}),G.loaded===G.total)this.eventBus.publish("assetGroupLoaded",{group:$})}createResource(){let j=this;return{getStatus($){return j.getStatus($)},isLoaded($){return j.isLoaded($)},isGroupLoaded($){return j.isGroupLoaded($)},getGroupProgress($){return j.getGroupProgress($)},get($){return j.get($)},tryGet($){return j.tryGet($)},getHandle($){return j.getHandle($)}}}getKeys(){return Array.from(this.assets.keys())}getGroupNames(){return Array.from(this.groups.keys())}getGroupKeys(j){let $=this.groups.get(j);return $?Array.from($):[]}}class h{manager;constructor(j){this.manager=j}add(j,$){return this.manager.register(j,{loader:$,eager:!0}),this}addWithConfig(j,$){return this.manager.register(j,$),this}addGroup(j,$){for(let[G,J]of Object.entries($))this.manager.register(G,{loader:J,eager:!1,group:j});return this}getManager(){return this.manager}}function C(j){return new h(j??new U)}class K{screens=new Map;currentScreen=null;screenStack=[];eventBus=null;assetManager=null;ecs=null;setDependencies(j,$,G){this.eventBus=j,this.assetManager=$,this.ecs=G}requireEcs(){if(!this.ecs)throw Error("ScreenManager: dependencies not set. Call setDependencies() first.");return this.ecs}register(j,$){this.screens.set(j,{definition:$})}async setScreen(j,$){let G=this.screens.get(j);if(!G)throw Error(`Screen '${String(j)}' not found`);await this.verifyRequiredAssets(G.definition.requiredAssets,G.definition.requiredAssetGroups);while(this.screenStack.length>0){let X=this.screenStack.pop();if(X)await this.exitScreen(X.name)}if(this.currentScreen)await this.exitScreen(this.currentScreen.name);let J=G.definition.initialState($);this.currentScreen={name:j,config:$,state:J},await G.definition.onEnter?.({config:$,ecs:this.requireEcs()}),this.eventBus?.publish("screenEnter",{screen:j,config:$})}async pushScreen(j,$){let G=this.screens.get(j);if(!G)throw Error(`Screen '${String(j)}' not found`);if(await this.verifyRequiredAssets(G.definition.requiredAssets,G.definition.requiredAssetGroups),this.currentScreen)this.screenStack.push(this.currentScreen);let J=G.definition.initialState($);this.currentScreen={name:j,config:$,state:J},await G.definition.onEnter?.({config:$,ecs:this.requireEcs()}),this.eventBus?.publish("screenPush",{screen:j,config:$})}async popScreen(){if(this.screenStack.length===0)throw Error("Cannot pop screen: stack is empty");if(this.currentScreen)await this.exitScreen(this.currentScreen.name),this.eventBus?.publish("screenPop",{screen:this.currentScreen.name});this.currentScreen=this.screenStack.pop()??null}async exitScreen(j){let $=this.screens.get(j);if($?.definition.onExit)await $.definition.onExit(this.requireEcs());this.eventBus?.publish("screenExit",{screen:j})}async verifyRequiredAssets(j,$){if(!this.assetManager)return;if(j){for(let G of j)if(!this.assetManager.isLoaded(G))await this.assetManager.loadAsset(G)}if($){for(let G of $)if(!this.assetManager.isGroupLoaded(G))await this.assetManager.loadAssetGroup(G)}}getCurrentScreen(){return this.currentScreen?.name??null}getConfig(j){if(!this.currentScreen)throw Error("No current screen");if(j!==void 0&&this.currentScreen.name!==j)throw Error(`Expected current screen '${String(j)}', but current is '${String(this.currentScreen.name)}'`);return this.currentScreen.config}tryGetConfig(j){if(!this.currentScreen)return;if(j!==void 0&&this.currentScreen.name!==j)return;return this.currentScreen.config}getState(j){if(!this.currentScreen)throw Error("No current screen");if(j!==void 0&&this.currentScreen.name!==j)throw Error(`Expected current screen '${String(j)}', but current is '${String(this.currentScreen.name)}'`);return this.currentScreen.state}tryGetState(j){if(!this.currentScreen)return;if(j!==void 0&&this.currentScreen.name!==j)return;return this.currentScreen.state}updateState(j,$){if(!this.currentScreen)throw Error("No current screen");if($!==void 0&&this.currentScreen.name!==$)throw Error(`Expected current screen '${String($)}', but current is '${String(this.currentScreen.name)}'`);let G=typeof j==="function"?j(this.currentScreen.state):j;this.currentScreen.state={...this.currentScreen.state,...G}}getStackDepth(){return this.screenStack.length}isOverlay(){return this.screenStack.length>0}isActive(j){if(this.currentScreen?.name===j)return!0;return this.screenStack.some(($)=>$.name===j)}isCurrent(j){return this.currentScreen?.name===j}createResource(){let j=this;return{get current(){return j.getCurrentScreen()},get config(){return j.tryGetConfig()??null},get state(){return j.tryGetState()??null},set state($){if(j.currentScreen&&$!==null)j.currentScreen.state=$},get stack(){return j.screenStack},get isOverlay(){return j.isOverlay()},get stackDepth(){return j.getStackDepth()},isActive($){return j.isActive($)},isCurrent($){return j.isCurrent($)}}}getScreenNames(){return Array.from(this.screens.keys())}hasScreen(j){return this.screens.has(j)}}class k{manager;constructor(j){this.manager=j}add(j,$){return this.manager.register(j,$),this}getManager(){return this.manager}}function q(j){return new k(j??new K)}class v{assetConfigurator=null;screenConfigurator=null;pendingResources=[];pendingDisposeCallbacks=[];pendingRequiredComponents=[];pendingPlugins=[];_fixedDt=null;constructor(){}withPlugin(j){return this.pendingPlugins.push(j),this}withComponentTypes(){return this}withEventTypes(){return this}withResourceTypes(){return this}withResource(j,$){return this.pendingResources.push({key:j,value:$}),this}withDispose(j,$){return this.pendingDisposeCallbacks.push({key:j,callback:$}),this}withRequired(j,$,G){return this.pendingRequiredComponents.push({trigger:j,required:$,factory:G}),this}withAssets(j){let $=C();return j($),this.assetConfigurator=$,this}withScreens(j){let $=q();return j($),this.screenConfigurator=$,this}withFixedTimestep(j){return this._fixedDt=j,this}withReactiveQueryNames(){return this}pluginFactory(){return(j)=>P(j.id).install(j.install)}build(){let j=new z;for(let $ of this.pendingPlugins)j.installPlugin($);for(let{key:$,value:G}of this.pendingResources)j.addResource($,G);for(let{key:$,callback:G}of this.pendingDisposeCallbacks)j.registerDispose($,G);for(let{trigger:$,required:G,factory:J}of this.pendingRequiredComponents)j.registerRequired($,G,J);if(this.assetConfigurator)j._setAssetManager(this.assetConfigurator.getManager());else if(j._hasPendingPluginAssets())j._setAssetManager(new U);if(this.screenConfigurator)j._setScreenManager(this.screenConfigurator.getManager());else if(j._hasPendingPluginScreens())j._setScreenManager(new K);if(this._fixedDt!==null)j._setFixedDt(this._fixedDt);return j}}var p=["preUpdate","fixedUpdate","update","postUpdate","render"];class z{static VERSION=N;_entityManager;_eventBus;_resourceManager;_commandBuffer;_systems=[];_phaseSystems={preUpdate:[],fixedUpdate:[],update:[],postUpdate:[],render:[]};_installedPlugins=new Set;_disabledGroups=new Set;_assetManager=null;_screenManager=null;_reactiveQueryManager;_postUpdateHooks=[];_currentTick=0;_systemLastSeqs=new Map;_changeThreshold=0;_fixedDt=0.016666666666666666;_fixedAccumulator=0;_interpolationAlpha=0;_maxFixedSteps=8;_requiredComponents=new Map;_pendingPluginAssets=[];_pendingPluginScreens=[];_diagnosticsEnabled=!1;_systemTimings=new Map;_phaseTimings={preUpdate:0,fixedUpdate:0,update:0,postUpdate:0,render:0};_entityEnterTracking=new Map;_entityEnterFrameSet=new Set;_systemContexts=new WeakMap;_pendingFinalizers=[];_batchingRegistrations=!1;constructor(){this._entityManager=new H,this._eventBus=new V,this._resourceManager=new E,this._reactiveQueryManager=new R(this._entityManager),this._commandBuffer=new w,this._subscribeLifecycleHooks()}_subscribeLifecycleHooks(){this._entityManager.onAfterComponentAdded((j,$)=>{this._entityManager.markChanged(j,$);let G=this._requiredComponents.get($);if(G){let J=this._entityManager.getEntity(j);if(J){let X=J.components[$];for(let{component:Y,factory:Z}of G){if(this._entityManager._pendingBatchKeys?.has(Y))continue;if(!(Y in J.components))this._entityManager.addComponent(j,Y,Z(X))}}}}),this._entityManager.onAfterEntityMutated((j)=>{let $=this._entityManager.getEntity(j);if($)this._reactiveQueryManager.recheckEntityAndChildren($)}),this._entityManager.onAfterComponentRemoved((j,$)=>{let G=this._entityManager.getEntity(j);if(G)this._reactiveQueryManager.onComponentRemoved(G,$)}),this._entityManager.onBeforeEntityRemoved((j)=>{this._reactiveQueryManager.onEntityRemoved(j)}),this._entityManager.onAfterParentChanged((j)=>{if(this._reactiveQueryManager.hasParentHasQueries){let $=this._entityManager.getEntity(j);if($)this._reactiveQueryManager.recheckEntity($)}})}static create(){return new v}addSystem(j){let $=new S(j);return this._pendingFinalizers.push(()=>{this._registerSystem($._createSystemObject())}),$}_finalizePendingBuilders(){if(this._pendingFinalizers.length===0)return;this._batchingRegistrations=!0;while(this._pendingFinalizers.length>0){let j=this._pendingFinalizers;this._pendingFinalizers=[];for(let $ of j)$()}this._batchingRegistrations=!1,this._rebuildPhaseSystems()}update(j){this._finalizePendingBuilders();let $=this._screenManager?.getCurrentScreen()??null,G=this._diagnosticsEnabled;this._runPhase("preUpdate",j,$,G);let J=G?performance.now():0;this._fixedAccumulator+=j;let X=0;while(this._fixedAccumulator>=this._fixedDt&&X<this._maxFixedSteps)this._executePhase(this._phaseSystems.fixedUpdate,this._fixedDt,$),this._commandBuffer.playback(this),this._fixedAccumulator-=this._fixedDt,X++;if(this._fixedAccumulator>=this._fixedDt)this._fixedAccumulator=0;if(G)this._phaseTimings.fixedUpdate=performance.now()-J;this._interpolationAlpha=this._fixedAccumulator/this._fixedDt,this._runPhase("update",j,$,G),this._runPhase("postUpdate",j,$,G);for(let Y of this._postUpdateHooks)Y({ecs:this,dt:j});this._runPhase("render",j,$,G),this._changeThreshold=this._entityManager.changeSeq,this._currentTick++}_executePhase(j,$,G){for(let J of j){if(!J.process&&!J.onEntityEnter)continue;if(J.groups?.length){let W=!1;for(let _ of J.groups)if(this._disabledGroups.has(_)){W=!0;break}if(W)continue}if(J.inScreens?.length){if(G===null||!J.inScreens.includes(G))continue}if(J.excludeScreens?.length){if(G!==null&&J.excludeScreens.includes(G))continue}if(J.requiredAssets?.length&&this._assetManager){let W=!0;for(let _ of J.requiredAssets)if(!this._assetManager.isLoaded(_)){W=!1;break}if(!W)continue}let X=this._systemLastSeqs.get(J)??0;this._changeThreshold=X;let Y=this._systemContexts.get(J);if(!Y)Y={queries:{},dt:0,ecs:this},this._systemContexts.set(J,Y);Y.dt=$;let Z=Y.queries,F=!1,A=!1;if(J.entityQueries)for(let W in J.entityQueries){A=!0;let _=J.entityQueries[W];if(_){let L=Z[W]??(Z[W]=[]);if(this._entityManager.getEntitiesWithQueryInto(L,_.with,_.without||[],_.changed,_.changed?this._changeThreshold:void 0,_.parentHas),L.length)F=!0}}let O=this._entityEnterTracking.get(J);if(O&&J.onEntityEnter)for(let W in J.onEntityEnter){let _=Z[W],D=O.get(W);if(!_||!D)continue;let L=J.onEntityEnter[W];if(!L)continue;let B=this._entityEnterFrameSet;B.clear();for(let Q of _)if(B.add(Q.id),!D.has(Q.id))D.add(Q.id),L({entity:Q,ecs:this});for(let Q of D)if(!B.has(Q))D.delete(Q)}if(J.process){if(this._diagnosticsEnabled){let W=performance.now();if(F||J.runWhenEmpty)J.process(Y);else if(!A)J.process(Y);this._systemTimings.set(J.label,performance.now()-W)}else if(F||J.runWhenEmpty)J.process(Y);else if(!A)J.process(Y)}this._systemLastSeqs.set(J,this._entityManager.changeSeq)}}_runPhase(j,$,G,J){if(J){let X=performance.now();this._executePhase(this._phaseSystems[j],$,G),this._phaseTimings[j]=performance.now()-X}else this._executePhase(this._phaseSystems[j],$,G);this._commandBuffer.playback(this)}async initialize(){if(this._finalizePendingBuilders(),await this.initializeResources(),this._assetManager)this._assetManager.setEventBus(this._eventBus),await this._assetManager.loadEagerAssets(),this._resourceManager.add("$assets",this._assetManager.createResource());if(this._screenManager)this._screenManager.setDependencies(this._eventBus,this._assetManager,this),this._resourceManager.add("$screen",this._screenManager.createResource());for(let j of this._systems)await j.onInitialize?.(this)}async initializeResources(...j){await this._resourceManager.initializeResources(this,...j)}_rebuildPhaseSystems(){for(let j of p)this._phaseSystems[j]=[];for(let j of this._systems){let $=j.phase??"update";this._phaseSystems[$].push(j)}for(let j of p)this._phaseSystems[j].sort(($,G)=>{let J=$.priority??0;return(G.priority??0)-J})}updateSystemPriority(j,$){this._finalizePendingBuilders();let G=this._systems.find((J)=>J.label===j);if(!G)return!1;return G.priority=$,this._rebuildPhaseSystems(),!0}updateSystemPhase(j,$){this._finalizePendingBuilders();let G=this._systems.find((J)=>J.label===j);if(!G)return!1;return G.phase=$,this._rebuildPhaseSystems(),!0}get interpolationAlpha(){return this._interpolationAlpha}get fixedDt(){return this._fixedDt}disableSystemGroup(j){this._disabledGroups.add(j)}enableSystemGroup(j){this._disabledGroups.delete(j)}isSystemGroupEnabled(j){return!this._disabledGroups.has(j)}getSystemsInGroup(j){return this._finalizePendingBuilders(),this._systems.filter(($)=>$.groups?.includes(j)).map(($)=>$.label)}removeSystem(j){this._finalizePendingBuilders();let $=this._systems.findIndex((J)=>J.label===j);if($===-1)return!1;let G=this._systems[$];if(!G)return!1;if(G.onDetach)G.onDetach(this);return this._systems.splice($,1),this._systemLastSeqs.delete(G),this._entityEnterTracking.delete(G),this._rebuildPhaseSystems(),!0}_registerSystem(j){if(this._systems.push(j),this._systemLastSeqs.set(j,this._changeThreshold),!this._batchingRegistrations)this._rebuildPhaseSystems();if(j.onEntityEnter){let $=new Map;for(let G in j.onEntityEnter)$.set(G,new Set);this._entityEnterTracking.set(j,$)}if(!j.eventHandlers)return;for(let $ in j.eventHandlers){let G=j.eventHandlers[$];if(G)this._eventBus.subscribe($,(J)=>{G({data:J,ecs:this})})}}hasResource(j){return this._resourceManager.has(j)}getResource(j){if(!this._resourceManager.has(j))throw Error(`Resource '${String(j)}' not found. Available resources: [${this.getResourceKeys().map(($)=>String($)).join(", ")}]`);return this._resourceManager.get(j,this)}tryGetResource(j){let $=j;if(!this._resourceManager.has($))return;return this._resourceManager.get($,this)}addResource(j,$){return this._resourceManager.add(j,$),this}removeResource(j){return this._resourceManager.remove(j)}async disposeResource(j){return this._resourceManager.disposeResource(j,this)}async disposeResources(){return this._resourceManager.disposeResources(this)}updateResource(j,$){let G=this.getResource(j),J=$(G);return this._resourceManager.add(j,J),this._resourceManager.notifyChange(j,J,G),this}setResource(j,$){let G=this.tryGetResource(j);if(this._resourceManager.add(j,$),G!==void 0)this._resourceManager.notifyChange(j,$,G);return this}onResourceChange(j,$){return this._resourceManager.onResourceChange(j,$)}getResourceKeys(){return this._resourceManager.getKeys()}resourceNeedsInitialization(j){return this._resourceManager.needsInitialization(j)}getEntity(j){return this._entityManager.getEntity(j)}getComponent(j,$){return this._entityManager.getComponent(j,$)}addComponent(j,$,G){this._entityManager.addComponent(j,$,G)}addComponents(j,$){this._entityManager.addComponents(j,$)}removeComponent(j,$){this._entityManager.removeComponent(j,$)}hasComponent(j,$){return this._entityManager.getComponent(j,$)!==void 0}spawn(j){let $=this._entityManager.createEntity();return this._entityManager.addComponents($.id,j),$}getEntitiesWithQuery(j,$=[],G,J){return this._entityManager.getEntitiesWithQuery(j,$,G,G?this._changeThreshold:void 0,J)}getSingleton(j,$=[]){let G=this._entityManager.getEntitiesWithQuery(j,$);if(G.length===0)throw Error(`getSingleton: no entity matches query with=[${String(j)}] without=[${String($)}]`);if(G.length>1)throw Error(`getSingleton: expected 1 entity but found ${G.length} matching query with=[${String(j)}] without=[${String($)}]`);let J=G[0];if(!J)throw Error("getSingleton: unexpected empty result");return J}tryGetSingleton(j,$=[]){let G=this._entityManager.getEntitiesWithQuery(j,$);if(G.length===0)return;if(G.length>1)throw Error(`tryGetSingleton: expected 0 or 1 entity but found ${G.length} matching query with=[${String(j)}] without=[${String($)}]`);return G[0]}removeEntity(j,$){return this._entityManager.removeEntity(j,$)}spawnChild(j,$){let G=this._entityManager.spawnChild(j,$);return this._emitHierarchyChanged(G.id,null,j),G}setParent(j,$){let G=this._entityManager.getParent(j);return this._entityManager.setParent(j,$),this._emitHierarchyChanged(j,G,$),this}removeParent(j){let $=this._entityManager.getParent(j),G=this._entityManager.removeParent(j);if(G)this._emitHierarchyChanged(j,$,null);return G}getParent(j){return this._entityManager.getParent(j)}getChildren(j){return this._entityManager.getChildren(j)}getChildAt(j,$){return this._entityManager.getChildAt(j,$)}getChildIndex(j,$){return this._entityManager.getChildIndex(j,$)}getAncestors(j){return this._entityManager.getAncestors(j)}getDescendants(j){return this._entityManager.getDescendants(j)}getRoot(j){return this._entityManager.getRoot(j)}getSiblings(j){return this._entityManager.getSiblings(j)}isDescendantOf(j,$){return this._entityManager.isDescendantOf(j,$)}isAncestorOf(j,$){return this._entityManager.isAncestorOf(j,$)}getRootEntities(){return this._entityManager.getRootEntities()}forEachInHierarchy(j,$){this._entityManager.forEachInHierarchy(j,$)}hierarchyIterator(j){return this._entityManager.hierarchyIterator(j)}_emitHierarchyChanged(j,$,G){this._eventBus.publish("hierarchyChanged",{entityId:j,oldParent:$,newParent:G})}get installedPlugins(){return Array.from(this._installedPlugins)}get entityManager(){return this._entityManager}get eventBus(){return this._finalizePendingBuilders(),this._eventBus}get commands(){return this._commandBuffer}get currentTick(){return this._currentTick}get changeThreshold(){return this._changeThreshold}enableDiagnostics(j){if(this._diagnosticsEnabled=j,!j)this._systemTimings.clear(),this._phaseTimings={preUpdate:0,fixedUpdate:0,update:0,postUpdate:0,render:0}}get diagnosticsEnabled(){return this._diagnosticsEnabled}get systemTimings(){return this._systemTimings}get phaseTimings(){return this._phaseTimings}get entityCount(){return this._entityManager.entityCount}mutateComponent(j,$,G){let J=this._entityManager.getComponent(j,$);if(J===void 0)throw Error(`Entity ${j} does not have component "${String($)}"`);return G(J),this._entityManager.markChanged(j,$),J}markChanged(j,$){this._entityManager.markChanged(j,$)}registerDispose(j,$){this._entityManager.registerDispose(j,$)}registerRequired(j,$,G){if(String(j)===String($))throw Error(`Cannot require a component to depend on itself: '${String(j)}'`);let J=this._requiredComponents.get(j)??[];if(J.some((X)=>X.component===$))throw Error(`Required component '${String($)}' already registered for trigger '${String(j)}'`);this._checkRequiredCycle(j,$),J.push({component:$,factory:G}),this._requiredComponents.set(j,J)}_checkRequiredCycle(j,$){g(j,$,(G)=>this._requiredComponents.get(G))}onComponentAdded(j,$){return this._entityManager.onComponentAdded(j,$)}onComponentRemoved(j,$){return this._entityManager.onComponentRemoved(j,$)}addReactiveQuery(j,$){this._reactiveQueryManager.addQuery(j,$)}removeReactiveQuery(j){return this._reactiveQueryManager.removeQuery(j)}on(j,$){return this._eventBus.subscribe(j,$)}off(j,$){return this._eventBus.unsubscribe(j,$)}onPostUpdate(j){return this._postUpdateHooks.push(j),()=>{let $=this._postUpdateHooks.indexOf(j);if($!==-1)this._postUpdateHooks.splice($,1)}}requireAssetManager(){if(!this._assetManager)throw Error("Asset manager not configured. Use withAssets() in builder.");return this._assetManager}getAsset(j){return this.requireAssetManager().get(j)}tryGetAsset(j){return this._assetManager?.tryGet(j)}getAssetHandle(j){return this.requireAssetManager().getHandle(j)}isAssetLoaded(j){return this._assetManager?.isLoaded(j)??!1}async loadAsset(j){return this.requireAssetManager().loadAsset(j)}async loadAssetGroup(j){return this.requireAssetManager().loadAssetGroup(j)}isAssetGroupLoaded(j){return this._assetManager?.isGroupLoaded(j)??!1}getAssetGroupProgress(j){return this._assetManager?.getGroupProgress(j)??0}requireScreenManager(){if(!this._screenManager)throw Error("Screen manager not configured. Use withScreens() in builder.");return this._screenManager}async setScreen(j,$){return this.requireScreenManager().setScreen(j,$)}async pushScreen(j,$){return this.requireScreenManager().pushScreen(j,$)}async popScreen(){return this.requireScreenManager().popScreen()}getCurrentScreen(){return this._screenManager?.getCurrentScreen()??null}getScreenConfig(j){return this.requireScreenManager().getConfig(j)}tryGetScreenConfig(j){return this._screenManager?.tryGetConfig(j)??void 0}getScreenState(j){return this.requireScreenManager().getState(j)}tryGetScreenState(j){return this._screenManager?.tryGetState(j)??void 0}updateScreenState(j,$){if(typeof j==="string")this.requireScreenManager().updateState($,j);else this.requireScreenManager().updateState(j)}isCurrentScreen(j){return this._screenManager?.isCurrent(j)??!1}isScreenActive(j){return this._screenManager?.isActive(j)??!1}getScreenStackDepth(){return this._screenManager?.getStackDepth()??0}_setAssetManager(j){this._assetManager=j;for(let[$,G]of this._pendingPluginAssets)this._assetManager.register($,G);this._pendingPluginAssets=[]}_setScreenManager(j){this._screenManager=j;for(let[$,G]of this._pendingPluginScreens)this._screenManager.register($,G);this._pendingPluginScreens=[]}_hasPendingPluginAssets(){return this._pendingPluginAssets.length>0}_hasPendingPluginScreens(){return this._pendingPluginScreens.length>0}_setFixedDt(j){this._fixedDt=j}_registerAsset(j,$){this._pendingPluginAssets.push([j,$])}_registerScreen(j,$){this._pendingPluginScreens.push([j,$])}installPlugin(j){if(this._installedPlugins.has(j.id))return this;return this._installedPlugins.add(j.id),j.install(this),this}pluginFactory(){return(j)=>P(j.id).install(j.install)}getHelpers(j){return j(this)}}function Vj(j){return j}function Rj(j,$){return{x:j,y:$}}function wj(){return{x:0,y:0}}function Sj(j,$){return{x:j.x+$.x,y:j.y+$.y}}function Tj(j,$){return{x:j.x-$.x,y:j.y-$.y}}function xj(j,$){return{x:j.x*$,y:j.y*$}}function Cj(j){return{x:-j.x,y:-j.y}}function qj(j,$){return j.x*$.x+j.y*$.y}function vj(j,$){return j.x*$.y-j.y*$.x}function fj(j){return j.x*j.x+j.y*j.y}function bj(j){return Math.sqrt(j.x*j.x+j.y*j.y)}function gj(j){let $=Math.sqrt(j.x*j.x+j.y*j.y);if($===0)return{x:0,y:0};return{x:j.x/$,y:j.y/$}}function Nj(j,$){let G=j.x-$.x,J=j.y-$.y;return G*G+J*J}function hj(j,$){let G=j.x-$.x,J=j.y-$.y;return Math.sqrt(G*G+J*J)}function kj(j,$,G=0.0000000001){return Math.abs(j.x-$.x)<=G&&Math.abs(j.y-$.y)<=G}var aj=z;export{wj as vec2Zero,Tj as vec2Sub,xj as vec2Scale,gj as vec2Normalize,Cj as vec2Negate,fj as vec2LengthSq,bj as vec2Length,kj as vec2Equals,qj as vec2Dot,Nj as vec2DistanceSq,hj as vec2Distance,vj as vec2Cross,Sj as vec2Add,Rj as vec2,I as directValue,P as definePlugin,aj as default,q as createScreenConfigurator,Vj as createQueryDefinition,C as createAssetConfigurator,S as SystemBuilder,K as ScreenManager,U as AssetManager};
2
2
 
3
- //# debugId=2F687826EF1C313A64756E2164756E21
3
+ //# debugId=17547FC76ADAEF0C64756E2164756E21
4
4
  //# sourceMappingURL=index.js.map