@esengine/ecs-framework 2.1.6 → 2.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var se=Object.defineProperty,ae=Object.defineProperties;var he=Object.getOwnPropertyDescriptors;var te=Object.getOwnPropertySymbols;var ce=Object.prototype.hasOwnProperty,le=Object.prototype.propertyIsEnumerable;var ee=(a,t,e)=>t in a?se(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,g=(a,t)=>{for(var e in t||(t={}))ce.call(t,e)&&ee(a,e,t[e]);if(te)for(var e of te(t))le.call(t,e)&&ee(a,e,t[e]);return a},M=(a,t)=>ae(a,he(t)),r=(a,t)=>se(a,"name",{value:t,configurable:!0}),p=(a=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(a,{get:(t,e)=>(typeof require!="undefined"?require:t)[e]}):a)(function(a){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+a+'" is not supported')});var lt=class lt{constructor(t,e){this.func=t,this.context=e}};r(lt,"FuncPack");var H=lt,ut=class ut{constructor(){this._messageTable=new Map}addObserver(t,e,s){let i=this._messageTable.get(t);i||(i=[],this._messageTable.set(t,i)),this.hasObserver(t,e)||i.push(new H(e,s))}removeObserver(t,e){let s=this._messageTable.get(t);if(s){let i=s.findIndex(n=>n.func==e);i!=-1&&s.splice(i,1)}}emit(t,...e){let s=this._messageTable.get(t);if(s)for(let i of s)i.func.call(i.context,...e)}hasObserver(t,e){let s=this._messageTable.get(t);return s?s.some(i=>i.func===e):!1}};r(ut,"Emitter");var U=ut;var z;(function(a){a[a.sceneChanged=0]="sceneChanged",a[a.frameUpdated=1]="frameUpdated",a[a.renderChanged=2]="renderChanged"})(z||(z={}));var mt=class mt{constructor(){this._enabled=!1}get enabled(){return this._enabled}set enabled(t){this.setEnabled(t)}setEnabled(t){this._enabled!=t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled())}onEnabled(){}onDisabled(){}update(){}};r(mt,"GlobalManager");var D=mt;var dt=class dt{static update(t=-1){if(t===-1&&(t=Date.now()),this._isFirstUpdate){this._lastTime=t,this._isFirstUpdate=!1;return}this.unscaledDeltaTime=(t-this._lastTime)/1e3,this.deltaTime=this.unscaledDeltaTime*this.timeScale,this.unscaledTotalTime+=this.unscaledDeltaTime,this.totalTime+=this.deltaTime,this.frameCount++,this._lastTime=t}static sceneChanged(){this._isFirstUpdate=!0}static checkEvery(t,e){return this.totalTime-e>=t}};r(dt,"Time");var l=dt;l.deltaTime=0;l.unscaledDeltaTime=0;l.totalTime=0;l.unscaledTotalTime=0;l.timeScale=1;l.frameCount=0;l._lastTime=0;l._isFirstUpdate=!0;var pt=class pt{constructor(){this._timeInSeconds=0,this._repeats=!1,this._isDone=!1,this._elapsedTime=0}getContext(){return this.context}reset(){this._elapsedTime=0}stop(){this._isDone=!0}tick(){return!this._isDone&&this._elapsedTime>this._timeInSeconds&&(this._elapsedTime-=this._timeInSeconds,this._onTime(this),!this._isDone&&!this._repeats&&(this._isDone=!0)),this._elapsedTime+=l.deltaTime,this._isDone}initialize(t,e,s,i){this._timeInSeconds=t,this._repeats=e,this.context=s,this._onTime=i.bind(s)}unload(){this.context=null,this._onTime=null}};r(pt,"Timer");var q=pt;var ft=class ft extends D{constructor(){super(...arguments),this._timers=[]}update(){for(let t=this._timers.length-1;t>=0;t--)this._timers[t].tick()&&(this._timers[t].unload(),this._timers.splice(t,1))}schedule(t,e,s,i){let n=new q;return n.initialize(t,e,s,i),this._timers.push(n),n}};r(ft,"TimerManager");var $=ft;var ie;(function(a){a.HIGH_EXECUTION_TIME="high_execution_time",a.HIGH_MEMORY_USAGE="high_memory_usage",a.HIGH_CPU_USAGE="high_cpu_usage",a.FREQUENT_GC="frequent_gc",a.LOW_FPS="low_fps",a.HIGH_ENTITY_COUNT="high_entity_count"})(ie||(ie={}));var b=class b{static get instance(){return b._instance||(b._instance=new b),b._instance}constructor(){this._systemData=new Map,this._systemStats=new Map,this._warnings=[],this._isEnabled=!1,this._maxRecentSamples=60,this._maxWarnings=100,this._thresholds={executionTime:{warning:16.67,critical:33.33},memoryUsage:{warning:100,critical:200},cpuUsage:{warning:70,critical:90},fps:{warning:45,critical:30},entityCount:{warning:1e3,critical:5e3}},this._fpsHistory=[],this._lastFrameTime=0,this._frameCount=0,this._fpsUpdateInterval=1e3,this._lastFpsUpdate=0,this._currentFps=60,this._memoryCheckInterval=5e3,this._lastMemoryCheck=0,this._memoryHistory=[],this._gcCount=0,this._lastGcCheck=0,this._gcCheckInterval=1e3}enable(){this._isEnabled=!0}disable(){this._isEnabled=!1}get isEnabled(){return this._isEnabled}startMonitoring(t){return this._isEnabled?performance.now():0}endMonitoring(t,e,s=0){if(!this._isEnabled||e===0)return;let i=performance.now(),n=i-e,o=s>0?n/s:0,h={name:t,executionTime:n,entityCount:s,averageTimePerEntity:o,lastUpdateTime:i};this._systemData.set(t,h),this.updateStats(t,n)}updateStats(t,e){let s=this._systemStats.get(t);s||(s={totalTime:0,averageTime:0,minTime:Number.MAX_VALUE,maxTime:0,executionCount:0,recentTimes:[],standardDeviation:0,percentile95:0,percentile99:0},this._systemStats.set(t,s)),s.totalTime+=e,s.executionCount++,s.averageTime=s.totalTime/s.executionCount,s.minTime=Math.min(s.minTime,e),s.maxTime=Math.max(s.maxTime,e),s.recentTimes.push(e),s.recentTimes.length>this._maxRecentSamples&&s.recentTimes.shift(),this.calculateAdvancedStats(s)}calculateAdvancedStats(t){if(t.recentTimes.length===0)return;let e=t.recentTimes.reduce((o,h)=>o+h,0)/t.recentTimes.length,s=t.recentTimes.reduce((o,h)=>o+Math.pow(h-e,2),0)/t.recentTimes.length;t.standardDeviation=Math.sqrt(s);let i=[...t.recentTimes].sort((o,h)=>o-h),n=i.length;t.percentile95=i[Math.floor(n*.95)]||0,t.percentile99=i[Math.floor(n*.99)]||0}getSystemData(t){return this._systemData.get(t)}getSystemStats(t){return this._systemStats.get(t)}getAllSystemData(){return new Map(this._systemData)}getAllSystemStats(){return new Map(this._systemStats)}getPerformanceReport(){if(!this._isEnabled)return"Performance monitoring is disabled.";let t=[];t.push("=== ECS Performance Report ==="),t.push("");let e=Array.from(this._systemStats.entries()).sort((i,n)=>n[1].averageTime-i[1].averageTime);for(let[i,n]of e){let o=this._systemData.get(i);t.push(`System: ${i}`),t.push(` Current: ${o==null?void 0:o.executionTime.toFixed(2)}ms (${o==null?void 0:o.entityCount} entities)`),t.push(` Average: ${n.averageTime.toFixed(2)}ms`),t.push(` Min/Max: ${n.minTime.toFixed(2)}ms / ${n.maxTime.toFixed(2)}ms`),t.push(` Total: ${n.totalTime.toFixed(2)}ms (${n.executionCount} calls)`),o!=null&&o.averageTimePerEntity&&o.averageTimePerEntity>0&&t.push(` Per Entity: ${o.averageTimePerEntity.toFixed(4)}ms`),t.push("")}let s=Array.from(this._systemData.values()).reduce((i,n)=>i+n.executionTime,0);return t.push(`Total Frame Time: ${s.toFixed(2)}ms`),t.push(`Systems Count: ${this._systemData.size}`),t.join(`
2
- `)}reset(){this._systemData.clear(),this._systemStats.clear()}resetSystem(t){this._systemData.delete(t),this._systemStats.delete(t)}getPerformanceWarnings(t=16.67){let e=[];for(let[s,i]of this._systemData.entries())i.executionTime>t&&e.push(`${s}: ${i.executionTime.toFixed(2)}ms (>${t}ms)`);return e}setMaxRecentSamples(t){this._maxRecentSamples=t;for(let e of this._systemStats.values())for(;e.recentTimes.length>t;)e.recentTimes.shift()}};r(b,"PerformanceMonitor");var B=b;var G=class G{constructor(t,e=100,s=1024){this._objects=[],this._createFn=t,this._maxSize=e,this._objectSize=s,this._stats={size:0,maxSize:e,totalCreated:0,totalObtained:0,totalReleased:0,hitRate:0,estimatedMemoryUsage:0}}static getPool(t,e=100,s=1024){let i=this._pools.get(t);return i||(i=new G(()=>new t,e,s),this._pools.set(t,i)),i}obtain(){if(this._stats.totalObtained++,this._objects.length>0){let e=this._objects.pop();return this._stats.size--,this._updateHitRate(),this._updateMemoryUsage(),e}let t=this._createFn();return this._stats.totalCreated++,this._updateHitRate(),t}free(t){this._objects.length<this._maxSize&&(t.reset(),this._objects.push(t),this._stats.size++,this._stats.totalReleased++,this._updateMemoryUsage())}warmUp(t){let e=Math.min(t,this._maxSize);for(;this._objects.length<e;){let s=this._createFn();this._stats.totalCreated++,this._objects.push(s),this._stats.size++}this._updateMemoryUsage()}clear(){this._objects.length=0,this._stats.size=0,this._updateMemoryUsage()}get size(){return this._objects.length}get maxSize(){return this._maxSize}set maxSize(t){for(this._maxSize=t,this._stats.maxSize=t;this._objects.length>this._maxSize;)this._objects.pop(),this._stats.size--;this._updateMemoryUsage()}getStats(){return g({},this._stats)}resetStats(){this._stats.totalCreated=0,this._stats.totalObtained=0,this._stats.totalReleased=0,this._stats.hitRate=0}_updateHitRate(){if(this._stats.totalObtained>0){let t=this._stats.totalObtained-this._stats.totalCreated;this._stats.hitRate=t/this._stats.totalObtained}}_updateMemoryUsage(){this._stats.estimatedMemoryUsage=this._stats.size*this._objectSize}static obtain(t){return this.getPool(t).obtain()}static free(t,e){this.getPool(t).free(e)}static warmUp(t,e){this.getPool(t).warmUp(e)}static clearPool(t){let e=this._pools.get(t);e&&e.clear()}static clearAllPools(){for(let t of this._pools.values())t.clear();this._pools.clear()}static getStats(){let t={};for(let[e,s]of this._pools.entries()){let i=e.name||"Unknown";t[i]=s.getStats()}return t}static getTotalMemoryUsage(){let t=0;for(let e of this._pools.values())t+=e.getStats().estimatedMemoryUsage;return t}static getPerformanceReport(){let t=this.getStats(),e=[];e.push("=== Object Pool Performance Report ==="),e.push(`Total Memory Usage: ${(this.getTotalMemoryUsage()/1024/1024).toFixed(2)} MB`),e.push("");for(let[s,i]of Object.entries(t))e.push(`${s}:`),e.push(` Size: ${i.size}/${i.maxSize}`),e.push(` Hit Rate: ${(i.hitRate*100).toFixed(1)}%`),e.push(` Total Created: ${i.totalCreated}`),e.push(` Total Obtained: ${i.totalObtained}`),e.push(` Memory: ${(i.estimatedMemoryUsage/1024).toFixed(1)} KB`),e.push("");return e.join(`
3
- `)}};r(G,"Pool");var E=G;E._pools=new Map;var gt=class gt{constructor(t,e,s=[10,50,200],i=1024){this.pools=[],this.totalObtained=0,this.totalReleased=0,this.createFn=t,this.resetFn=e,this.tierSizes=s;for(let n of s)this.pools.push(new E(t,n,i))}obtain(){this.totalObtained++;for(let t of this.pools)if(t.size>0)return t.obtain();return this.createFn()}release(t){this.totalReleased++,this.resetFn(t);for(let e of this.pools)if(e.size<e.maxSize){e.free(t);return}}warmUp(t){let e=t;for(let s of this.pools){let i=Math.min(e,s.maxSize);if(s.warmUp(i),e-=i,e<=0)break}}clear(){for(let t of this.pools)t.clear()}getStats(){let t=0,e=0,s=0,i=[];for(let o of this.pools){let h=o.getStats();i.push(h),t+=h.size,e+=h.maxSize,s+=h.estimatedMemoryUsage}let n=this.totalObtained>0?(this.totalObtained-this.getTotalCreated())/this.totalObtained:0;return{totalSize:t,totalMaxSize:e,totalMemoryUsage:s,tierStats:i,hitRate:n}}getTotalCreated(){return this.pools.reduce((t,e)=>t+e.getStats().totalCreated,0)}};r(gt,"TieredObjectPool");var L=gt,T=class T{constructor(){this.pools=new Map,this.autoCompactInterval=6e4,this.lastCompactTime=0}static getInstance(){return T.instance||(T.instance=new T),T.instance}registerPool(t,e){this.pools.set(t,e)}getPool(t){return this.pools.get(t)||null}update(){let t=Date.now();t-this.lastCompactTime>this.autoCompactInterval&&(this.compactAllPools(),this.lastCompactTime=t)}compactAllPools(){for(let t of this.pools.values())t instanceof E&&t.resetStats()}getAllStats(){let t=new Map;for(let[e,s]of this.pools.entries())(s instanceof E||s instanceof L)&&t.set(e,s.getStats());return t}generateReport(){let t=[];t.push("=== Pool Manager Report ===");let e=0;for(let[s,i]of this.pools.entries())if(t.push(`
4
- ${s}:`),i instanceof E){let n=i.getStats();t.push(" Type: Standard Pool"),t.push(` Size: ${n.size}/${n.maxSize}`),t.push(` Hit Rate: ${(n.hitRate*100).toFixed(1)}%`),t.push(` Memory: ${(n.estimatedMemoryUsage/1024).toFixed(1)} KB`),e+=n.estimatedMemoryUsage}else if(i instanceof L){let n=i.getStats();t.push(" Type: Tiered Pool"),t.push(` Total Size: ${n.totalSize}/${n.totalMaxSize}`),t.push(` Hit Rate: ${(n.hitRate*100).toFixed(1)}%`),t.push(` Memory: ${(n.totalMemoryUsage/1024).toFixed(1)} KB`),e+=n.totalMemoryUsage}return t.push(`
1
+ var Ne=Object.defineProperty;var a=(o,t)=>Ne(o,"name",{value:t,configurable:!0});var At=class At{constructor(t,e){this.func=t,this.context=e}};a(At,"FuncPack");var ht=At,Dt=class Dt{constructor(){this._messageTable=new Map}addObserver(t,e,s){let i=this._messageTable.get(t);i||(i=[],this._messageTable.set(t,i)),this.hasObserver(t,e)||i.push(new ht(e,s))}removeObserver(t,e){let s=this._messageTable.get(t);if(s){let i=s.findIndex(n=>n.func==e);i!=-1&&s.splice(i,1)}}emit(t,...e){let s=this._messageTable.get(t);if(s)for(let i of s)i.func.call(i.context,...e)}hasObserver(t,e){let s=this._messageTable.get(t);return s?s.some(i=>i.func===e):!1}};a(Dt,"Emitter");var G=Dt;var b;(function(o){o[o.sceneChanged=0]="sceneChanged",o[o.frameUpdated=1]="frameUpdated",o[o.renderChanged=2]="renderChanged"})(b||(b={}));var l;(function(o){o.ENTITY_CREATED="entity:created",o.ENTITY_DESTROYED="entity:destroyed",o.ENTITY_ENABLED="entity:enabled",o.ENTITY_DISABLED="entity:disabled",o.ENTITY_TAG_ADDED="entity:tag:added",o.ENTITY_TAG_REMOVED="entity:tag:removed",o.ENTITY_NAME_CHANGED="entity:name:changed",o.COMPONENT_ADDED="component:added",o.COMPONENT_REMOVED="component:removed",o.COMPONENT_MODIFIED="component:modified",o.COMPONENT_ENABLED="component:enabled",o.COMPONENT_DISABLED="component:disabled",o.SYSTEM_ADDED="system:added",o.SYSTEM_REMOVED="system:removed",o.SYSTEM_ENABLED="system:enabled",o.SYSTEM_DISABLED="system:disabled",o.SYSTEM_PROCESSING_START="system:processing:start",o.SYSTEM_PROCESSING_END="system:processing:end",o.SYSTEM_ERROR="system:error",o.SCENE_CREATED="scene:created",o.SCENE_DESTROYED="scene:destroyed",o.SCENE_ACTIVATED="scene:activated",o.SCENE_DEACTIVATED="scene:deactivated",o.SCENE_PAUSED="scene:paused",o.SCENE_RESUMED="scene:resumed",o.QUERY_EXECUTED="query:executed",o.QUERY_CACHE_HIT="query:cache:hit",o.QUERY_CACHE_MISS="query:cache:miss",o.QUERY_OPTIMIZED="query:optimized",o.PERFORMANCE_WARNING="performance:warning",o.PERFORMANCE_CRITICAL="performance:critical",o.MEMORY_USAGE_HIGH="memory:usage:high",o.FRAME_RATE_DROP="frame:rate:drop",o.INDEX_CREATED="index:created",o.INDEX_UPDATED="index:updated",o.INDEX_OPTIMIZED="index:optimized",o.ARCHETYPE_CREATED="archetype:created",o.ARCHETYPE_ENTITY_ADDED="archetype:entity:added",o.ARCHETYPE_ENTITY_REMOVED="archetype:entity:removed",o.DIRTY_MARK_ADDED="dirty:mark:added",o.DIRTY_BATCH_PROCESSED="dirty:batch:processed",o.ERROR_OCCURRED="error:occurred",o.WARNING_ISSUED="warning:issued",o.FRAMEWORK_INITIALIZED="framework:initialized",o.FRAMEWORK_SHUTDOWN="framework:shutdown",o.DEBUG_INFO="debug:info",o.DEBUG_STATS_UPDATED="debug:stats:updated"})(l||(l={}));var V;(function(o){o[o.LOWEST=0]="LOWEST",o[o.LOW=25]="LOW",o[o.NORMAL=50]="NORMAL",o[o.HIGH=75]="HIGH",o[o.HIGHEST=100]="HIGHEST",o[o.CRITICAL=200]="CRITICAL"})(V||(V={}));var C={CORE:{SCENE_CHANGED:"core:scene:changed",FRAME_UPDATED:"core:frame:updated",RENDER_CHANGED:"core:render:changed"},ENTITY:{CREATED:l.ENTITY_CREATED,DESTROYED:l.ENTITY_DESTROYED,ENABLED:l.ENTITY_ENABLED,DISABLED:l.ENTITY_DISABLED,TAG_ADDED:l.ENTITY_TAG_ADDED,TAG_REMOVED:l.ENTITY_TAG_REMOVED,NAME_CHANGED:l.ENTITY_NAME_CHANGED},COMPONENT:{ADDED:l.COMPONENT_ADDED,REMOVED:l.COMPONENT_REMOVED,MODIFIED:l.COMPONENT_MODIFIED,ENABLED:l.COMPONENT_ENABLED,DISABLED:l.COMPONENT_DISABLED},SYSTEM:{ADDED:l.SYSTEM_ADDED,REMOVED:l.SYSTEM_REMOVED,ENABLED:l.SYSTEM_ENABLED,DISABLED:l.SYSTEM_DISABLED,PROCESSING_START:l.SYSTEM_PROCESSING_START,PROCESSING_END:l.SYSTEM_PROCESSING_END,ERROR:l.SYSTEM_ERROR},PERFORMANCE:{WARNING:l.PERFORMANCE_WARNING,CRITICAL:l.PERFORMANCE_CRITICAL,MEMORY_HIGH:l.MEMORY_USAGE_HIGH,FRAME_DROP:l.FRAME_RATE_DROP}},Ot=class Ot{static isValid(t){return this.validTypes.has(t)}static getAllValidTypes(){return Array.from(this.validTypes)}static addCustomType(t){this.validTypes.add(t)}static removeCustomType(t){this.validTypes.delete(t)}};a(Ot,"EventTypeValidator");var I=Ot;I.validTypes=new Set([...Object.values(b).map(o=>o.toString()),...Object.values(l),...Object.values(C.CORE),...Object.values(C.ENTITY),...Object.values(C.COMPONENT),...Object.values(C.SYSTEM),...Object.values(C.PERFORMANCE)]);var vt=class vt{constructor(){this._enabled=!1}get enabled(){return this._enabled}set enabled(t){this.setEnabled(t)}setEnabled(t){this._enabled!=t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled())}onEnabled(){}onDisabled(){}update(){}};a(vt,"GlobalManager");var K=vt;var Nt=class Nt{static update(t=-1){if(t===-1&&(t=Date.now()),this._isFirstUpdate){this._lastTime=t,this._isFirstUpdate=!1;return}this.unscaledDeltaTime=(t-this._lastTime)/1e3,this.deltaTime=this.unscaledDeltaTime*this.timeScale,this.unscaledTotalTime+=this.unscaledDeltaTime,this.totalTime+=this.deltaTime,this.frameCount++,this._lastTime=t}static sceneChanged(){this._isFirstUpdate=!0}static checkEvery(t,e){return this.totalTime-e>=t}};a(Nt,"Time");var f=Nt;f.deltaTime=0;f.unscaledDeltaTime=0;f.totalTime=0;f.unscaledTotalTime=0;f.timeScale=1;f.frameCount=0;f._lastTime=0;f._isFirstUpdate=!0;var kt=class kt{constructor(){this._timeInSeconds=0,this._repeats=!1,this._isDone=!1,this._elapsedTime=0}getContext(){return this.context}reset(){this._elapsedTime=0}stop(){this._isDone=!0}tick(){return!this._isDone&&this._elapsedTime>this._timeInSeconds&&(this._elapsedTime-=this._timeInSeconds,this._onTime(this),!this._isDone&&!this._repeats&&(this._isDone=!0)),this._elapsedTime+=f.deltaTime,this._isDone}initialize(t,e,s,i){this._timeInSeconds=t,this._repeats=e,this.context=s,this._onTime=i.bind(s)}unload(){this.context=null,this._onTime=null}};a(kt,"Timer");var Z=kt;var Rt=class Rt extends K{constructor(){super(...arguments),this._timers=[]}update(){for(let t=this._timers.length-1;t>=0;t--)this._timers[t].tick()&&(this._timers[t].unload(),this._timers.splice(t,1))}schedule(t,e,s,i){let n=new Z;return n.initialize(t,e,s,i),this._timers.push(n),n}};a(Rt,"TimerManager");var J=Rt;var Ie;(function(o){o.HIGH_EXECUTION_TIME="high_execution_time",o.HIGH_MEMORY_USAGE="high_memory_usage",o.HIGH_CPU_USAGE="high_cpu_usage",o.FREQUENT_GC="frequent_gc",o.LOW_FPS="low_fps",o.HIGH_ENTITY_COUNT="high_entity_count"})(Ie||(Ie={}));var A=class A{static get instance(){return A._instance||(A._instance=new A),A._instance}constructor(){this._systemData=new Map,this._systemStats=new Map,this._warnings=[],this._isEnabled=!1,this._maxRecentSamples=60,this._maxWarnings=100,this._thresholds={executionTime:{warning:16.67,critical:33.33},memoryUsage:{warning:100,critical:200},cpuUsage:{warning:70,critical:90},fps:{warning:45,critical:30},entityCount:{warning:1e3,critical:5e3}},this._fpsHistory=[],this._lastFrameTime=0,this._frameCount=0,this._fpsUpdateInterval=1e3,this._lastFpsUpdate=0,this._currentFps=60,this._memoryCheckInterval=5e3,this._lastMemoryCheck=0,this._memoryHistory=[],this._gcCount=0,this._lastGcCheck=0,this._gcCheckInterval=1e3}enable(){this._isEnabled=!0}disable(){this._isEnabled=!1}get isEnabled(){return this._isEnabled}startMonitoring(t){return this._isEnabled?performance.now():0}endMonitoring(t,e,s=0){if(!this._isEnabled||e===0)return;let i=performance.now(),n=i-e,r=s>0?n/s:0,h={name:t,executionTime:n,entityCount:s,averageTimePerEntity:r,lastUpdateTime:i};this._systemData.set(t,h),this.updateStats(t,n)}updateStats(t,e){let s=this._systemStats.get(t);s||(s={totalTime:0,averageTime:0,minTime:Number.MAX_VALUE,maxTime:0,executionCount:0,recentTimes:[],standardDeviation:0,percentile95:0,percentile99:0},this._systemStats.set(t,s)),s.totalTime+=e,s.executionCount++,s.averageTime=s.totalTime/s.executionCount,s.minTime=Math.min(s.minTime,e),s.maxTime=Math.max(s.maxTime,e),s.recentTimes.push(e),s.recentTimes.length>this._maxRecentSamples&&s.recentTimes.shift(),this.calculateAdvancedStats(s)}calculateAdvancedStats(t){if(t.recentTimes.length===0)return;let e=t.recentTimes.reduce((r,h)=>r+h,0)/t.recentTimes.length,s=t.recentTimes.reduce((r,h)=>r+Math.pow(h-e,2),0)/t.recentTimes.length;t.standardDeviation=Math.sqrt(s);let i=[...t.recentTimes].sort((r,h)=>r-h),n=i.length;t.percentile95=i[Math.floor(n*.95)]||0,t.percentile99=i[Math.floor(n*.99)]||0}getSystemData(t){return this._systemData.get(t)}getSystemStats(t){return this._systemStats.get(t)}getAllSystemData(){return new Map(this._systemData)}getAllSystemStats(){return new Map(this._systemStats)}getPerformanceReport(){if(!this._isEnabled)return"Performance monitoring is disabled.";let t=[];t.push("=== ECS Performance Report ==="),t.push("");let e=Array.from(this._systemStats.entries()).sort((i,n)=>n[1].averageTime-i[1].averageTime);for(let[i,n]of e){let r=this._systemData.get(i);t.push(`System: ${i}`),t.push(` Current: ${r?.executionTime.toFixed(2)}ms (${r?.entityCount} entities)`),t.push(` Average: ${n.averageTime.toFixed(2)}ms`),t.push(` Min/Max: ${n.minTime.toFixed(2)}ms / ${n.maxTime.toFixed(2)}ms`),t.push(` Total: ${n.totalTime.toFixed(2)}ms (${n.executionCount} calls)`),r?.averageTimePerEntity&&r.averageTimePerEntity>0&&t.push(` Per Entity: ${r.averageTimePerEntity.toFixed(4)}ms`),t.push("")}let s=Array.from(this._systemData.values()).reduce((i,n)=>i+n.executionTime,0);return t.push(`Total Frame Time: ${s.toFixed(2)}ms`),t.push(`Systems Count: ${this._systemData.size}`),t.join(`
2
+ `)}reset(){this._systemData.clear(),this._systemStats.clear()}resetSystem(t){this._systemData.delete(t),this._systemStats.delete(t)}getPerformanceWarnings(t=16.67){let e=[];for(let[s,i]of this._systemData.entries())i.executionTime>t&&e.push(`${s}: ${i.executionTime.toFixed(2)}ms (>${t}ms)`);return e}setMaxRecentSamples(t){this._maxRecentSamples=t;for(let e of this._systemStats.values())for(;e.recentTimes.length>t;)e.recentTimes.shift()}};a(A,"PerformanceMonitor");var H=A;var mt=class mt{constructor(t,e=100,s=1024){this._objects=[],this._createFn=t,this._maxSize=e,this._objectSize=s,this._stats={size:0,maxSize:e,totalCreated:0,totalObtained:0,totalReleased:0,hitRate:0,estimatedMemoryUsage:0}}static getPool(t,e=100,s=1024){let i=this._pools.get(t);return i||(i=new mt(()=>new t,e,s),this._pools.set(t,i)),i}obtain(){if(this._stats.totalObtained++,this._objects.length>0){let e=this._objects.pop();return this._stats.size--,this._updateHitRate(),this._updateMemoryUsage(),e}let t=this._createFn();return this._stats.totalCreated++,this._updateHitRate(),t}free(t){this._objects.length<this._maxSize&&(t.reset(),this._objects.push(t),this._stats.size++,this._stats.totalReleased++,this._updateMemoryUsage())}warmUp(t){let e=Math.min(t,this._maxSize);for(;this._objects.length<e;){let s=this._createFn();this._stats.totalCreated++,this._objects.push(s),this._stats.size++}this._updateMemoryUsage()}clear(){this._objects.length=0,this._stats.size=0,this._updateMemoryUsage()}get size(){return this._objects.length}get maxSize(){return this._maxSize}set maxSize(t){for(this._maxSize=t,this._stats.maxSize=t;this._objects.length>this._maxSize;)this._objects.pop(),this._stats.size--;this._updateMemoryUsage()}getStats(){return{...this._stats}}resetStats(){this._stats.totalCreated=0,this._stats.totalObtained=0,this._stats.totalReleased=0,this._stats.hitRate=0}_updateHitRate(){if(this._stats.totalObtained>0){let t=this._stats.totalObtained-this._stats.totalCreated;this._stats.hitRate=t/this._stats.totalObtained}}_updateMemoryUsage(){this._stats.estimatedMemoryUsage=this._stats.size*this._objectSize}static obtain(t){return this.getPool(t).obtain()}static free(t,e){this.getPool(t).free(e)}static warmUp(t,e){this.getPool(t).warmUp(e)}static clearPool(t){let e=this._pools.get(t);e&&e.clear()}static clearAllPools(){for(let t of this._pools.values())t.clear();this._pools.clear()}static getStats(){let t={};for(let[e,s]of this._pools.entries()){let i=e.name||"Unknown";t[i]=s.getStats()}return t}static getTotalMemoryUsage(){let t=0;for(let e of this._pools.values())t+=e.getStats().estimatedMemoryUsage;return t}static getPerformanceReport(){let t=this.getStats(),e=[];e.push("=== Object Pool Performance Report ==="),e.push(`Total Memory Usage: ${(this.getTotalMemoryUsage()/1024/1024).toFixed(2)} MB`),e.push("");for(let[s,i]of Object.entries(t))e.push(`${s}:`),e.push(` Size: ${i.size}/${i.maxSize}`),e.push(` Hit Rate: ${(i.hitRate*100).toFixed(1)}%`),e.push(` Total Created: ${i.totalCreated}`),e.push(` Total Obtained: ${i.totalObtained}`),e.push(` Memory: ${(i.estimatedMemoryUsage/1024).toFixed(1)} KB`),e.push("");return e.join(`
3
+ `)}};a(mt,"Pool");var O=mt;O._pools=new Map;var Bt=class Bt{constructor(t,e,s=[10,50,200],i=1024){this.pools=[],this.totalObtained=0,this.totalReleased=0,this.createFn=t,this.resetFn=e,this.tierSizes=s;for(let n of s)this.pools.push(new O(t,n,i))}obtain(){this.totalObtained++;for(let t of this.pools)if(t.size>0)return t.obtain();return this.createFn()}release(t){this.totalReleased++,this.resetFn(t);for(let e of this.pools)if(e.size<e.maxSize){e.free(t);return}}warmUp(t){let e=t;for(let s of this.pools){let i=Math.min(e,s.maxSize);if(s.warmUp(i),e-=i,e<=0)break}}clear(){for(let t of this.pools)t.clear()}getStats(){let t=0,e=0,s=0,i=[];for(let r of this.pools){let h=r.getStats();i.push(h),t+=h.size,e+=h.maxSize,s+=h.estimatedMemoryUsage}let n=this.totalObtained>0?(this.totalObtained-this.getTotalCreated())/this.totalObtained:0;return{totalSize:t,totalMaxSize:e,totalMemoryUsage:s,tierStats:i,hitRate:n}}getTotalCreated(){return this.pools.reduce((t,e)=>t+e.getStats().totalCreated,0)}};a(Bt,"TieredObjectPool");var ct=Bt,D=class D{constructor(){this.pools=new Map,this.autoCompactInterval=6e4,this.lastCompactTime=0}static getInstance(){return D.instance||(D.instance=new D),D.instance}registerPool(t,e){this.pools.set(t,e)}getPool(t){return this.pools.get(t)||null}update(){let t=Date.now();t-this.lastCompactTime>this.autoCompactInterval&&(this.compactAllPools(),this.lastCompactTime=t)}compactAllPools(){for(let t of this.pools.values())t instanceof O&&t.resetStats()}getAllStats(){let t=new Map;for(let[e,s]of this.pools.entries())(s instanceof O||s instanceof ct)&&t.set(e,s.getStats());return t}generateReport(){let t=[];t.push("=== Pool Manager Report ===");let e=0;for(let[s,i]of this.pools.entries())if(t.push(`
4
+ ${s}:`),i instanceof O){let n=i.getStats();t.push(" Type: Standard Pool"),t.push(` Size: ${n.size}/${n.maxSize}`),t.push(` Hit Rate: ${(n.hitRate*100).toFixed(1)}%`),t.push(` Memory: ${(n.estimatedMemoryUsage/1024).toFixed(1)} KB`),e+=n.estimatedMemoryUsage}else if(i instanceof ct){let n=i.getStats();t.push(" Type: Tiered Pool"),t.push(` Total Size: ${n.totalSize}/${n.totalMaxSize}`),t.push(` Hit Rate: ${(n.hitRate*100).toFixed(1)}%`),t.push(` Memory: ${(n.totalMemoryUsage/1024).toFixed(1)} KB`),e+=n.totalMemoryUsage}return t.push(`
5
5
  Total Memory Usage: ${(e/1024/1024).toFixed(2)} MB`),t.join(`
6
- `)}};r(T,"PoolManager");var Q=T;var _t=class _t{static register(t){if(this.componentTypes.has(t))return this.componentTypes.get(t);if(this.nextBitIndex>=this.maxComponents)throw new Error(`Maximum number of component types (${this.maxComponents}) exceeded`);let e=this.nextBitIndex++;return this.componentTypes.set(t,e),e}static getBitMask(t){let e=this.componentTypes.get(t);if(e===void 0)throw new Error(`Component type ${t.name} is not registered`);return BigInt(1)<<BigInt(e)}static getBitIndex(t){let e=this.componentTypes.get(t);if(e===void 0)throw new Error(`Component type ${t.name} is not registered`);return e}static isRegistered(t){return this.componentTypes.has(t)}static getAllRegisteredTypes(){return new Map(this.componentTypes)}};r(_t,"ComponentRegistry");var c=_t;c.componentTypes=new Map;c.nextBitIndex=0;c.maxComponents=64;var xt=class xt{constructor(t){this.components=[],this.entityToIndex=new Map,this.indexToEntity=[],this.freeIndices=[],this._size=0,this.componentType=t,c.isRegistered(t)||c.register(t)}addComponent(t,e){if(this.entityToIndex.has(t))throw new Error(`Entity ${t} already has component ${this.componentType.name}`);let s;this.freeIndices.length>0?(s=this.freeIndices.pop(),this.components[s]=e,this.indexToEntity[s]=t):(s=this.components.length,this.components.push(e),this.indexToEntity.push(t)),this.entityToIndex.set(t,s),this._size++}getComponent(t){let e=this.entityToIndex.get(t);return e!==void 0?this.components[e]:null}hasComponent(t){return this.entityToIndex.has(t)}removeComponent(t){let e=this.entityToIndex.get(t);if(e===void 0)return null;let s=this.components[e];return this.entityToIndex.delete(t),this.components[e]=null,this.freeIndices.push(e),this._size--,s}forEach(t){for(let e=0;e<this.components.length;e++){let s=this.components[e];s&&t(s,this.indexToEntity[e],e)}}getDenseArray(){let t=[],e=[];for(let s=0;s<this.components.length;s++){let i=this.components[s];i&&(t.push(i),e.push(this.indexToEntity[s]))}return{components:t,entityIds:e}}clear(){this.components.length=0,this.entityToIndex.clear(),this.indexToEntity.length=0,this.freeIndices.length=0,this._size=0}get size(){return this._size}get type(){return this.componentType}compact(){if(this.freeIndices.length===0)return;let t=[],e=[],s=new Map,i=0;for(let n=0;n<this.components.length;n++){let o=this.components[n];o&&(t[i]=o,e[i]=this.indexToEntity[n],s.set(this.indexToEntity[n],i),i++)}this.components=t,this.indexToEntity=e,this.entityToIndex=s,this.freeIndices.length=0}getStats(){let t=this.components.length,e=this._size,s=this.freeIndices.length,i=t>0?s/t:0;return{totalSlots:t,usedSlots:e,freeSlots:s,fragmentation:i}}};r(xt,"ComponentStorage");var yt=xt,Ct=class Ct{constructor(){this.storages=new Map}getStorage(t){let e=this.storages.get(t);return e||(e=new yt(t),this.storages.set(t,e)),e}addComponent(t,e){let s=e.constructor;this.getStorage(s).addComponent(t,e)}getComponent(t,e){let s=this.storages.get(e);return s?s.getComponent(t):null}hasComponent(t,e){let s=this.storages.get(e);return s?s.hasComponent(t):!1}removeComponent(t,e){let s=this.storages.get(e);return s?s.removeComponent(t):null}removeAllComponents(t){for(let e of this.storages.values())e.removeComponent(t)}getComponentMask(t){let e=BigInt(0);for(let[s,i]of this.storages.entries())i.hasComponent(t)&&(e|=c.getBitMask(s));return e}compactAll(){for(let t of this.storages.values())t.compact()}getAllStats(){let t=new Map;for(let[e,s]of this.storages.entries()){let i=e.name||"Unknown";t.set(i,s.getStats())}return t}clear(){for(let t of this.storages.values())t.clear();this.storages.clear()}};r(Ct,"ComponentStorageManager");var J=Ct;var St=class St{compare(t,e){let s=t.updateOrder-e.updateOrder;return s==0&&(s=t.id-e.id),s}};r(St,"EntityComparer");var Y=St,Mt=class Mt{constructor(t={maxSize:16,ttl:5e3,enableLRU:!0}){this.cache=new Map,this.accessOrder=[],this.config=t}get(t){let e=this.cache.get(t);return e?Date.now()-e.lastAccessed>this.config.ttl?(this.cache.delete(t),this.removeFromAccessOrder(t),null):(e.lastAccessed=Date.now(),e.accessCount++,this.config.enableLRU&&this.updateAccessOrder(t),e.component):null}set(t,e){this.cache.size>=this.config.maxSize&&!this.cache.has(t)&&this.evictLeastRecentlyUsed();let s={component:e,lastAccessed:Date.now(),accessCount:1};this.cache.set(t,s),this.config.enableLRU&&this.updateAccessOrder(t)}delete(t){let e=this.cache.delete(t);return e&&this.removeFromAccessOrder(t),e}clear(){this.cache.clear(),this.accessOrder.length=0}has(t){return this.cache.has(t)}evictLeastRecentlyUsed(){if(this.accessOrder.length>0){let t=this.accessOrder[0];this.cache.delete(t),this.accessOrder.shift()}}updateAccessOrder(t){this.removeFromAccessOrder(t),this.accessOrder.push(t)}removeFromAccessOrder(t){let e=this.accessOrder.indexOf(t);e!==-1&&this.accessOrder.splice(e,1)}getStats(){let t=0,e=0;for(let s of this.cache.values())t+=s.accessCount,e++;return{size:this.cache.size,maxSize:this.config.maxSize,hitRate:t>0?e/t:0,averageAccessCount:this.cache.size>0?t/this.cache.size:0}}};r(Mt,"ComponentCache");var wt=Mt,bt=class bt{constructor(t,e){this.components=[],this.updateInterval=1,this._isDestroyed=!1,this._parent=null,this._children=[],this._active=!0,this._tag=0,this._enabled=!0,this._updateOrder=0,this._componentMask=BigInt(0),this._componentTypeToIndex=new Map,this._componentAccessStats=new Map,this.name=t,this.id=e,this._componentCache=new wt}get isDestroyed(){return this._isDestroyed}get parent(){return this._parent}get children(){return[...this._children]}get childCount(){return this._children.length}get active(){return this._active}set active(t){this._active!==t&&(this._active=t,this.onActiveChanged())}get activeInHierarchy(){return this._active?this._parent?this._parent.activeInHierarchy:!0:!1}get tag(){return this._tag}set tag(t){this._tag=t}get enabled(){return this._enabled}set enabled(t){this._enabled=t}get updateOrder(){return this._updateOrder}set updateOrder(t){this._updateOrder=t}get componentMask(){return this._componentMask}createComponent(t,...e){let s=new t(...e);return this.addComponent(s)}addComponentInternal(t){let e=t.constructor;c.isRegistered(e)||c.register(e),t.entity=this;let s=this.components.length;return this.components.push(t),this._componentTypeToIndex.set(e,s),this._componentMask|=c.getBitMask(e),this._componentCache.set(e,t),this._componentAccessStats.set(e,{accessCount:0,lastAccessed:Date.now(),cacheHits:0,cacheMisses:0}),t}addComponent(t){let e=t.constructor;if(this.hasComponent(e))throw new Error(`Entity ${this.name} already has component ${e.name}`);if(this.addComponentInternal(t),this.scene&&this.scene.componentStorageManager&&this.scene.componentStorageManager.addComponent(this.id,t),t.onAddedToEntity(),this.scene&&this.scene.entityProcessors)for(let s of this.scene.entityProcessors.processors)s.onChanged(this);return t}getComponent(t){if(this.updateComponentAccessStats(t),!c.isRegistered(t))return this.recordCacheMiss(t),null;let e=c.getBitMask(t);if((this._componentMask&e)===BigInt(0))return this.recordCacheMiss(t),null;let s=this._componentCache.get(t);if(s)return this.recordCacheHit(t),s;let i=this._componentTypeToIndex.get(t);if(i!==void 0&&i<this.components.length){let n=this.components[i];if(n&&n.constructor===t)return this._componentCache.set(t,n),this.recordCacheHit(t),n}if(this.scene&&this.scene.componentStorageManager){let n=this.scene.componentStorageManager.getComponent(this.id,t);if(n)return this._componentCache.set(t,n),this.rebuildComponentIndex(),this.recordCacheHit(t),n}for(let n=0;n<this.components.length;n++){let o=this.components[n];if(o instanceof t)return this._componentTypeToIndex.set(t,n),this._componentCache.set(t,o),this.recordCacheHit(t),o}return this.recordCacheMiss(t),null}updateComponentAccessStats(t){let e=this._componentAccessStats.get(t);e||(e={accessCount:0,lastAccessed:Date.now(),cacheHits:0,cacheMisses:0},this._componentAccessStats.set(t,e)),e.accessCount++,e.lastAccessed=Date.now()}recordCacheHit(t){let e=this._componentAccessStats.get(t);e&&e.cacheHits++}recordCacheMiss(t){let e=this._componentAccessStats.get(t);e&&e.cacheMisses++}rebuildComponentIndex(){this._componentTypeToIndex.clear();for(let t=0;t<this.components.length;t++){let s=this.components[t].constructor;this._componentTypeToIndex.set(s,t)}}hasComponent(t){if(!c.isRegistered(t))return!1;let e=c.getBitMask(t);return(this._componentMask&e)!==BigInt(0)}getOrCreateComponent(t,...e){let s=this.getComponent(t);return s||(s=this.createComponent(t,...e)),s}removeComponent(t){let e=t.constructor,s=this.components.indexOf(t);if(s!==-1&&(this.components.splice(s,1),this.rebuildComponentIndex()),this._componentCache.delete(e),this._componentAccessStats.delete(e),c.isRegistered(e)&&(this._componentMask&=~c.getBitMask(e)),this.scene&&this.scene.componentStorageManager&&this.scene.componentStorageManager.removeComponent(this.id,e),t.onRemovedFromEntity(),t.entity=null,this.scene&&this.scene.entityProcessors)for(let i of this.scene.entityProcessors.processors)i.onChanged(this)}removeComponentByType(t){let e=this.getComponent(t);return e?(this.removeComponent(e),e):null}removeAllComponents(){let t=[...this.components];this._componentCache.clear(),this._componentTypeToIndex.clear(),this._componentAccessStats.clear(),this._componentMask=BigInt(0);for(let e of t){let s=e.constructor;this.scene&&this.scene.componentStorageManager&&this.scene.componentStorageManager.removeComponent(this.id,s),e.onRemovedFromEntity(),e.entity=null}if(this.components.length=0,this.scene&&this.scene.entityProcessors)for(let e of this.scene.entityProcessors.processors)e.onChanged(this)}addComponents(t){let e=[];for(let s of t)try{e.push(this.addComponent(s))}catch(i){console.warn(`Failed to add component ${s.constructor.name}:`,i)}return e}removeComponentsByTypes(t){let e=[];for(let s of t)e.push(this.removeComponentByType(s));return e}getComponentCacheStats(){let t=new Map;for(let[e,s]of this._componentAccessStats){let i=s.cacheHits+s.cacheMisses;t.set(e.name,M(g({},s),{hitRate:i>0?s.cacheHits/i:0}))}return{cacheStats:this._componentCache.getStats(),accessStats:t,indexMappingSize:this._componentTypeToIndex.size,totalComponents:this.components.length}}warmUpComponentCache(){for(let t=0;t<this.components.length;t++){let e=this.components[t],s=e.constructor;this._componentTypeToIndex.set(s,t),this._componentCache.set(s,e)}}cleanupComponentCache(){let t=Date.now(),e=3e4;for(let[s,i]of this._componentAccessStats)t-i.lastAccessed>e&&i.accessCount<5&&this._componentCache.delete(s)}getComponents(t){let e=[];for(let s of this.components)s instanceof t&&e.push(s);return e}addChild(t){if(t===this)throw new Error("Entity cannot be its own child");return t._parent===this||(t._parent&&t._parent.removeChild(t),t._parent=this,this._children.push(t),!t.scene&&this.scene&&(t.scene=this.scene,this.scene.addEntity(t))),t}removeChild(t){let e=this._children.indexOf(t);return e===-1?!1:(this._children.splice(e,1),t._parent=null,!0)}removeAllChildren(){let t=[...this._children];for(let e of t)this.removeChild(e)}findChild(t,e=!1){for(let s of this._children)if(s.name===t)return s;if(e)for(let s of this._children){let i=s.findChild(t,!0);if(i)return i}return null}findChildrenByTag(t,e=!1){let s=[];for(let i of this._children)i.tag===t&&s.push(i);if(e)for(let i of this._children)s.push(...i.findChildrenByTag(t,!0));return s}getRoot(){let t=this;for(;t._parent;)t=t._parent;return t}isAncestorOf(t){let e=t._parent;for(;e;){if(e===this)return!0;e=e._parent}return!1}isDescendantOf(t){return t.isAncestorOf(this)}getDepth(){let t=0,e=this._parent;for(;e;)t++,e=e._parent;return t}forEachChild(t,e=!1){this._children.forEach((s,i)=>{t(s,i),e&&s.forEachChild(t,!0)})}onActiveChanged(){for(let t of this.components)"onActiveChanged"in t&&typeof t.onActiveChanged=="function"&&t.onActiveChanged();this.scene&&this.scene.eventSystem&&this.scene.eventSystem.emitSync("entity:activeChanged",{entity:this,active:this._active,activeInHierarchy:this.activeInHierarchy})}update(){if(!(!this.activeInHierarchy||this._isDestroyed)){for(let t of this.components)t.enabled&&t.update();for(let t of this._children)t.update()}}destroy(){if(this._isDestroyed)return;this._isDestroyed=!0;let t=[...this._children];for(let e of t)e.destroy();this._parent&&this._parent.removeChild(this),this.removeAllComponents(),this.scene&&this.scene.entities&&this.scene.entities.remove(this)}compareTo(t){return Y.prototype.compare(this,t)}toString(){return`Entity[${this.name}:${this.id}]`}getDebugInfo(){var s;let t=this.getComponentCacheStats(),e=Array.from(t.accessStats.entries()).map(([i,n])=>({componentType:i,accessCount:n.accessCount,cacheHits:n.cacheHits,cacheMisses:n.cacheMisses,hitRate:n.hitRate,lastAccessed:new Date(n.lastAccessed).toISOString()}));return{name:this.name,id:this.id,enabled:this._enabled,active:this._active,activeInHierarchy:this.activeInHierarchy,destroyed:this._isDestroyed,componentCount:this.components.length,componentTypes:this.components.map(i=>i.constructor.name),componentMask:this._componentMask.toString(2),parentId:((s=this._parent)==null?void 0:s.id)||null,childCount:this._children.length,childIds:this._children.map(i=>i.id),depth:this.getDepth(),componentCache:t.cacheStats,componentAccessStats:e,indexMappingSize:t.indexMappingSize}}};r(bt,"Entity");var y=bt;y.entityComparer=new Y;var Tt=class Tt{get count(){return this.buffer.length}constructor(t){this.buffer=[],this._idToEntity=new Map,this._nameToEntities=new Map,this._entitiesToAdd=[],this._entitiesToRemove=[],this._isUpdating=!1,this._scene=t}add(t){this._isUpdating?this._entitiesToAdd.push(t):this.addImmediate(t)}addImmediate(t){this._idToEntity.has(t.id)||(this.buffer.push(t),this._idToEntity.set(t.id,t),this.updateNameIndex(t,!0))}remove(t){this._isUpdating?this._entitiesToRemove.push(t):this.removeImmediate(t)}removeImmediate(t){let e=this.buffer.indexOf(t);e!==-1&&(this.buffer.splice(e,1),this._idToEntity.delete(t.id),this.updateNameIndex(t,!1))}removeAllEntities(){for(let t=this.buffer.length-1;t>=0;t--)this.buffer[t].destroy();this.buffer.length=0,this._idToEntity.clear(),this._nameToEntities.clear(),this._entitiesToAdd.length=0,this._entitiesToRemove.length=0}updateLists(){if(this._entitiesToAdd.length>0){for(let t of this._entitiesToAdd)this.addImmediate(t);this._entitiesToAdd.length=0}if(this._entitiesToRemove.length>0){for(let t of this._entitiesToRemove)this.removeImmediate(t);this._entitiesToRemove.length=0}}update(){this._isUpdating=!0;try{for(let t=0;t<this.buffer.length;t++){let e=this.buffer[t];e.enabled&&!e.isDestroyed&&e.update()}}finally{this._isUpdating=!1}this.updateLists()}findEntity(t){let e=this._nameToEntities.get(t);return e&&e.length>0?e[0]:null}findEntitiesByName(t){return this._nameToEntities.get(t)||[]}findEntityById(t){return this._idToEntity.get(t)||null}findEntitiesByTag(t){let e=[];for(let s of this.buffer)s.tag===t&&e.push(s);return e}findEntitiesWithComponent(t){let e=[];for(let s of this.buffer)s.hasComponent(t)&&e.push(s);return e}forEach(t){for(let e of this.buffer)t(e)}forEachWhere(t,e){for(let s of this.buffer)t(s)&&e(s)}updateNameIndex(t,e){if(t.name)if(e){let s=this._nameToEntities.get(t.name);s||(s=[],this._nameToEntities.set(t.name,s)),s.push(t)}else{let s=this._nameToEntities.get(t.name);if(s){let i=s.indexOf(t);i!==-1&&(s.splice(i,1),s.length===0&&this._nameToEntities.delete(t.name))}}}getStats(){let t=0;for(let e of this.buffer)e.enabled&&!e.isDestroyed&&t++;return{totalEntities:this.buffer.length,activeEntities:t,pendingAdd:this._entitiesToAdd.length,pendingRemove:this._entitiesToRemove.length,nameIndexSize:this._nameToEntities.size}}};r(Tt,"EntityList");var P=Tt;var Et=class Et{constructor(){this._processors=[],this._isDirty=!1}setDirty(){this._isDirty=!0}add(t){this._processors.push(t),this.setDirty()}remove(t){let e=this._processors.indexOf(t);e!==-1&&this._processors.splice(e,1)}getProcessor(t){for(let e of this._processors)if(e instanceof t)return e;return null}begin(){this.sortProcessors();for(let t of this._processors)t.initialize()}end(){}update(){this.sortProcessors();for(let t of this._processors)t.update()}lateUpdate(){for(let t of this._processors)t.lateUpdate()}sortProcessors(){this._isDirty&&(this._processors.sort((t,e)=>t.updateOrder-e.updateOrder),this._isDirty=!1)}get processors(){return this._processors}get count(){return this._processors.length}};r(Et,"EntityProcessorList");var R=Et;var It=class It{constructor(){this._nextAvailableId=0,this._ids=[]}checkOut(){return this._ids.length>0?this._ids.pop():this._nextAvailableId++}checkIn(t){this._ids.push(t)}};r(It,"IdentifierPool");var F=It;var At=class At{constructor(){this.wasmCore=null,this.wasmModule=null,this.initialized=!1,this.usingWasm=!1,this.silent=!1,this.jsEntityMasks=new Map,this.jsNextEntityId=1,this.jsQueryCount=0,this.jsUpdateCount=0}setSilent(t){this.silent=t}async initialize(){if(this.initialized)return!0;this.silent||console.log("\u{1F504} \u521D\u59CB\u5316ECS\u6838\u5FC3...");try{let t="../../bin/wasm/ecs_wasm_core";if(!this.silent&&(console.log(`\u{1F50D} \u5C1D\u8BD5\u52A0\u8F7DWASM\u6A21\u5757: ${t}`),console.log(`\u{1F4C1} \u5F53\u524D\u6587\u4EF6\u4F4D\u7F6E: ${typeof __filename!="undefined"?__filename:"unknown"}`),console.log(`\u{1F4C2} \u5DE5\u4F5C\u76EE\u5F55: ${typeof process!="undefined"?process.cwd():"unknown"}`),typeof __filename!="undefined"&&typeof p!="undefined")){let e=p("path"),s=p("fs"),i=e.dirname(__filename),n=e.resolve(i,t);console.log(`\u{1F4CD} \u8BA1\u7B97\u7684\u7EDD\u5BF9\u8DEF\u5F84: ${n}`);let o=n+".js",h=e.resolve(i,"../../bin/wasm/ecs_wasm_core_bg.wasm");console.log(`\u{1F4C4} \u68C0\u67E5JS\u6587\u4EF6: ${o} - ${s.existsSync(o)?"\u5B58\u5728":"\u4E0D\u5B58\u5728"}`),console.log(`\u{1F4C4} \u68C0\u67E5WASM\u6587\u4EF6: ${h} - ${s.existsSync(h)?"\u5B58\u5728":"\u4E0D\u5B58\u5728"}`)}if(this.wasmModule=await import(t),this.silent||console.log("\u2705 WASM\u6A21\u5757\u5BFC\u5165\u6210\u529F\uFF0C\u6B63\u5728\u521D\u59CB\u5316..."),this.wasmModule){if(!this.silent&&(console.log("\u{1F50D} WASM\u6A21\u5757\u5C06\u5C1D\u8BD5\u52A0\u8F7D .wasm \u6587\u4EF6..."),typeof __filename!="undefined"&&typeof p!="undefined")){let e=p("path"),{pathToFileURL:s}=p("url"),i=e.dirname(__filename),n=e.resolve(i,"../../bin/wasm/ecs_wasm_core.js"),o=e.resolve(i,"../../bin/wasm/ecs_wasm_core_bg.wasm"),h=s(n).href,m=new URL("ecs_wasm_core_bg.wasm",h).href;console.log(`\u{1F4CD} WASM JS\u6587\u4EF6URL: ${h}`),console.log(`\u{1F4CD} \u9884\u671F\u7684.wasm\u6587\u4EF6URL: ${m}`),console.log(`\u{1F4CD} \u5B9E\u9645.wasm\u6587\u4EF6\u8DEF\u5F84: ${o}`);let f=p("fs");console.log(`\u{1F4C4} .wasm\u6587\u4EF6\u662F\u5426\u5B58\u5728: ${f.existsSync(o)?"\u5B58\u5728":"\u4E0D\u5B58\u5728"}`)}if(typeof p!="undefined"){let e=p("fs"),s=p("path"),i=s.dirname(__filename),n=s.resolve(i,"../../bin/wasm/ecs_wasm_core_bg.wasm");if(this.silent||console.log(`\u{1F527} \u5728Node.js\u73AF\u5883\u4E2D\u624B\u52A8\u52A0\u8F7DWASM\u6587\u4EF6: ${n}`),e.existsSync(n)){let o=e.readFileSync(n);this.wasmModule.initSync?this.wasmModule.initSync(o):await this.wasmModule.default({module_or_path:o})}else throw new Error(`WASM\u6587\u4EF6\u4E0D\u5B58\u5728: ${n}`)}else await this.wasmModule.default();this.wasmCore=new this.wasmModule.EcsCore}this.usingWasm=!0,this.silent||console.log("\u2705 WASM\u6A21\u5757\u52A0\u8F7D\u6210\u529F")}catch(t){this.silent||(console.warn("\u26A0\uFE0F WASM\u52A0\u8F7D\u5931\u8D25\uFF0C\u4F7F\u7528JavaScript\u5B9E\u73B0"),console.warn(`\u274C \u9519\u8BEF\u8BE6\u60C5: ${t}`)),this.usingWasm=!1}return this.initialized=!0,this.silent||console.log(`\u{1F3AE} ECS\u6838\u5FC3\u521D\u59CB\u5316\u5B8C\u6210 (${this.usingWasm?"WASM":"JavaScript"})`),!0}createEntity(){if(this.ensureInitialized(),this.usingWasm&&this.wasmCore)return this.wasmCore.create_entity();{let t=this.jsNextEntityId++;return this.jsEntityMasks.set(t,BigInt(0)),t}}destroyEntity(t){return this.ensureInitialized(),this.usingWasm&&this.wasmCore?this.wasmCore.destroy_entity(t):this.jsEntityMasks.delete(t)}updateEntityMask(t,e){this.ensureInitialized(),this.usingWasm&&this.wasmCore?this.wasmCore.update_entity_mask(t,e):(this.jsEntityMasks.set(t,e),this.jsUpdateCount++)}batchUpdateMasks(t,e){if(this.ensureInitialized(),t.length!==e.length)throw new Error("\u5B9E\u4F53ID\u548C\u63A9\u7801\u6570\u7EC4\u957F\u5EA6\u5FC5\u987B\u76F8\u540C");if(this.usingWasm&&this.wasmCore){let s=new Uint32Array(t),i=new BigUint64Array(e);this.wasmCore.batch_update_masks(s,i)}else{for(let s=0;s<t.length;s++)this.jsEntityMasks.set(t[s],e[s]);this.jsUpdateCount+=t.length}}queryEntities(t,e=1e4){var i;if(this.ensureInitialized(),this.usingWasm&&this.wasmCore)try{let n=this.wasmCore.query_entities(t,e),o=this.wasmCore.get_query_result_count();if(n&&o>0&&((i=this.wasmModule)!=null&&i.memory)){let h=new Uint32Array(this.wasmModule.memory.buffer,n,o);return{entities:new Uint32Array(h),count:o}}else return{entities:new Uint32Array(0),count:0}}catch(n){this.silent||console.warn("WASM\u67E5\u8BE2\u5931\u8D25\uFF0C\u56DE\u9000\u5230JavaScript\u5B9E\u73B0:",n)}this.jsQueryCount++;let s=[];for(let[n,o]of this.jsEntityMasks)if((o&t)===t&&(s.push(n),s.length>=e))break;return{entities:new Uint32Array(s),count:s.length}}queryCached(t){var e;if(this.ensureInitialized(),this.usingWasm&&this.wasmCore)try{let s=this.wasmCore.query_cached(t),i=this.wasmCore.get_cached_query_count(t);if(s&&i>0&&((e=this.wasmModule)!=null&&e.memory)){let n=new Uint32Array(this.wasmModule.memory.buffer,s,i);return{entities:new Uint32Array(n),count:i}}return{entities:new Uint32Array(0),count:0}}catch(s){return this.silent||console.warn("WASM\u7F13\u5B58\u67E5\u8BE2\u5931\u8D25\uFF0C\u56DE\u9000\u5230\u901A\u7528\u67E5\u8BE2:",s),this.queryEntities(t)}return this.queryEntities(t)}queryMultipleComponents(t,e=1e4){var i;if(this.ensureInitialized(),this.usingWasm&&this.wasmCore)try{let n=new BigUint64Array(t);return this.wasmCore.query_multiple_components(n,e)&&((i=this.wasmModule)!=null&&i.memory)?{entities:new Uint32Array(0),count:0}:{entities:new Uint32Array(0),count:0}}catch(n){this.silent||console.warn("WASM\u591A\u7EC4\u4EF6\u67E5\u8BE2\u5931\u8D25\uFF0C\u56DE\u9000\u5230JavaScript\u5B9E\u73B0:",n)}this.jsQueryCount++;let s=[];for(let[n,o]of this.jsEntityMasks){let h=!0;for(let m of t)if((o&m)!==m){h=!1;break}if(h&&(s.push(n),s.length>=e))break}return{entities:new Uint32Array(s),count:s.length}}queryWithExclusion(t,e,s=1e4){var n;if(this.ensureInitialized(),this.usingWasm&&this.wasmCore)try{return this.wasmCore.query_with_exclusion(t,e,s)&&((n=this.wasmModule)!=null&&n.memory)?{entities:new Uint32Array(0),count:0}:{entities:new Uint32Array(0),count:0}}catch(o){this.silent||console.warn("WASM\u6392\u9664\u67E5\u8BE2\u5931\u8D25\uFF0C\u56DE\u9000\u5230JavaScript\u5B9E\u73B0:",o)}this.jsQueryCount++;let i=[];for(let[o,h]of this.jsEntityMasks)if((h&t)===t&&(h&e)===BigInt(0)&&(i.push(o),i.length>=s))break;return{entities:new Uint32Array(i),count:i.length}}getEntityMask(t){return this.ensureInitialized(),this.usingWasm&&this.wasmCore?this.wasmCore.get_entity_mask(t)||null:this.jsEntityMasks.get(t)||null}entityExists(t){return this.ensureInitialized(),this.usingWasm&&this.wasmCore?this.wasmCore.entity_exists(t):this.jsEntityMasks.has(t)}createComponentMask(t){if(this.usingWasm&&this.wasmModule)return this.wasmModule.create_component_mask(new Uint32Array(t));{let e=BigInt(0);for(let s of t)s<64&&(e|=BigInt(1)<<BigInt(s));return e}}maskContainsComponent(t,e){return this.usingWasm&&this.wasmModule?this.wasmModule.mask_contains_component(t,e):e>=64?!1:(t&BigInt(1)<<BigInt(e))!==BigInt(0)}getPerformanceStats(){if(this.ensureInitialized(),this.usingWasm&&this.wasmCore){let t=Array.from(this.wasmCore.get_performance_stats());return{entityCount:t[0],indexCount:t[1],queryCount:t[2],updateCount:t[3],wasmEnabled:!0}}else return{entityCount:this.jsEntityMasks.size,indexCount:0,queryCount:this.jsQueryCount,updateCount:this.jsUpdateCount,wasmEnabled:!1}}clear(){this.ensureInitialized(),this.usingWasm&&this.wasmCore?this.wasmCore.clear():(this.jsEntityMasks.clear(),this.jsNextEntityId=1,this.jsQueryCount=0,this.jsUpdateCount=0)}isUsingWasm(){return this.usingWasm}isInitialized(){return this.initialized}ensureInitialized(){if(!this.initialized)throw new Error("ECS\u6838\u5FC3\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize()")}cleanup(){var t,e;if(this.usingWasm&&this.wasmCore)try{(e=(t=this.wasmCore).free)==null||e.call(t)}catch(s){this.silent||console.warn("\u26A0\uFE0F \u6E05\u7406WASM\u8D44\u6E90\u65F6\u51FA\u9519:",s)}this.wasmCore=null,this.wasmModule=null,this.jsEntityMasks.clear(),this.initialized=!1,this.usingWasm=!1}};r(At,"WasmEcsCore");var K=At,d=new K;async function ue(a=!1){return d.setSilent(a),d.initialize()}r(ue,"initializeEcs");var me={withComponent:r((a,t)=>{let e=d.createComponentMask([a]);return d.queryEntities(e,t)},"withComponent"),withComponents:r((a,t)=>{let e=a.map(s=>d.createComponentMask([s]));return d.queryMultipleComponents(e,t)},"withComponents"),withExclusion:r((a,t,e)=>{let s=d.createComponentMask(a),i=d.createComponentMask(t);return d.queryWithExclusion(s,i,e)},"withExclusion")};var kt=class kt{constructor(t,e,s=1e3){this.pool=[],this.createFn=t,this.resetFn=e,this.maxSize=s}acquire(){return this.pool.length>0?this.pool.pop():this.createFn()}release(t){this.pool.length<this.maxSize&&(this.resetFn&&this.resetFn(t),this.pool.push(t))}prewarm(t){for(let e=0;e<t&&this.pool.length<this.maxSize;e++)this.pool.push(this.createFn())}clear(){this.pool.length=0}getAvailableCount(){return this.pool.length}getMaxSize(){return this.maxSize}};r(kt,"ComponentPool");var vt=kt,I=class I{constructor(){this.pools=new Map}static getInstance(){return I.instance||(I.instance=new I),I.instance}registerPool(t,e,s,i){this.pools.set(t,new vt(e,s,i))}acquireComponent(t){let e=this.pools.get(t);return e?e.acquire():null}releaseComponent(t,e){let s=this.pools.get(t);s&&s.release(e)}prewarmAll(t=100){for(let e of this.pools.values())e.prewarm(t)}clearAll(){for(let t of this.pools.values())t.clear()}getPoolStats(){let t=new Map;for(let[e,s]of this.pools)t.set(e,{available:s.getAvailableCount(),maxSize:s.getMaxSize()});return t}};r(I,"ComponentPoolManager");var Z=I;var A=class A{constructor(){this.maskCache=new Map,this.componentTypeMap=new Map,this.nextComponentId=0}static getInstance(){return A.instance||(A.instance=new A),A.instance}registerComponentType(t){return this.componentTypeMap.has(t)||this.componentTypeMap.set(t,this.nextComponentId++),this.componentTypeMap.get(t)}getComponentTypeId(t){return this.componentTypeMap.get(t)}createSingleComponentMask(t){let e=`single:${t}`;if(this.maskCache.has(e))return this.maskCache.get(e);let s=this.getComponentTypeId(t);if(s===void 0)throw new Error(`Component type not registered: ${t}`);let i=BigInt(1)<<BigInt(s);return this.maskCache.set(e,i),i}createCombinedMask(t){let s=`combined:${[...t].sort().join(",")}`;if(this.maskCache.has(s))return this.maskCache.get(s);let i=BigInt(0);for(let n of t){let o=this.getComponentTypeId(n);if(o===void 0)throw new Error(`Component type not registered: ${n}`);i|=BigInt(1)<<BigInt(o)}return this.maskCache.set(s,i),i}maskContainsComponent(t,e){let s=this.createSingleComponentMask(e);return(t&s)!==BigInt(0)}maskContainsAllComponents(t,e){let s=this.createCombinedMask(e);return(t&s)===s}maskContainsAnyComponent(t,e){let s=this.createCombinedMask(e);return(t&s)!==BigInt(0)}addComponentToMask(t,e){let s=this.createSingleComponentMask(e);return t|s}removeComponentFromMask(t,e){let s=this.createSingleComponentMask(e);return t&~s}precomputeCommonMasks(t){for(let e of t)this.createCombinedMask(e)}getCacheStats(){return{size:this.maskCache.size,componentTypes:this.componentTypeMap.size}}clearCache(){this.maskCache.clear()}reset(){this.maskCache.clear(),this.componentTypeMap.clear(),this.nextComponentId=0}maskToComponentNames(t){let e=[];for(let[s,i]of this.componentTypeMap){let n=BigInt(1)<<BigInt(i);(t&n)!==BigInt(0)&&e.push(s)}return e}getComponentCount(t){let e=0,s=t;for(;s!==BigInt(0);)(s&BigInt(1))!==BigInt(0)&&e++,s>>=BigInt(1);return e}};r(A,"BitMaskOptimizer");var V=A;var w;(function(a){a.ADD_ENTITY="add_entity",a.REMOVE_ENTITY="remove_entity",a.UPDATE_ENTITY="update_entity"})(w||(w={}));var zt=class zt{constructor(){this.pendingOperations=[],this.isProcessing=!1,this.batchSize=1e3,this.flushTimeout=null,this.flushDelay=16}addOperation(t){this.pendingOperations.push(t),this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}addEntities(t){for(let e of t)this.pendingOperations.push({type:w.ADD_ENTITY,entity:e});this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}removeEntities(t){for(let e of t)this.pendingOperations.push({type:w.REMOVE_ENTITY,entity:e});this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}updateEntities(t){for(let e of t)this.pendingOperations.push({type:w.UPDATE_ENTITY,entity:e.entity,oldMask:e.oldMask,newMask:e.newMask});this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}scheduleFlush(){this.flushTimeout||(this.flushTimeout=setTimeout(()=>{this.flush()},this.flushDelay))}flush(){if(!(this.isProcessing||this.pendingOperations.length===0)){this.isProcessing=!0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null);try{this.processBatch()}finally{this.isProcessing=!1}}}processBatch(){let t=this.pendingOperations;this.pendingOperations=[];let e=[],s=[],i=[];for(let n of t)switch(n.type){case w.ADD_ENTITY:e.push(n.entity);break;case w.REMOVE_ENTITY:s.push(n.entity);break;case w.UPDATE_ENTITY:n.oldMask!==void 0&&n.newMask!==void 0&&i.push({entity:n.entity,oldMask:n.oldMask,newMask:n.newMask});break}e.length>0&&this.processBatchAdd(e),s.length>0&&this.processBatchRemove(s),i.length>0&&this.processBatchUpdate(i)}processBatchAdd(t){this.onBatchAdd&&this.onBatchAdd(t)}processBatchRemove(t){this.onBatchRemove&&this.onBatchRemove(t)}processBatchUpdate(t){this.onBatchUpdate&&this.onBatchUpdate(t)}setBatchSize(t){this.batchSize=Math.max(1,t)}setFlushDelay(t){this.flushDelay=Math.max(0,t)}getPendingCount(){return this.pendingOperations.length}clear(){this.pendingOperations.length=0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null)}hasPendingOperations(){return this.pendingOperations.length>0}};r(zt,"IndexUpdateBatcher");var X=zt;var S;(function(a){a.ALL="all",a.ANY="any",a.NONE="none"})(S||(S={}));var Bt=class Bt{constructor(){this.entities=[],this.wasmAvailable=!1,this.indexDirty=!0,this.queryCache=new Map,this.cacheMaxSize=1e3,this.cacheTimeout=5e3,this.queryStats={totalQueries:0,cacheHits:0,indexHits:0,linearScans:0},this.entityIndex={byMask:new Map,byComponentType:new Map,byTag:new Map,byName:new Map},this.componentPoolManager=Z.getInstance(),this.bitMaskOptimizer=V.getInstance(),this.indexUpdateBatcher=new X,this.indexUpdateBatcher.onBatchAdd=t=>{for(let e of t)this.addEntityToIndexes(e)},this.indexUpdateBatcher.onBatchRemove=t=>{for(let e of t)this.removeEntityFromIndexes(e)},this.indexUpdateBatcher.onBatchUpdate=t=>{for(let e of t)this.removeEntityFromIndexes(e.entity),this.addEntityToIndexes(e.entity)},this.initializeWasm()}async initializeWasm(){try{let t=await d.initialize();this.wasmAvailable=t&&d.isUsingWasm(),this.wasmAvailable?console.log("QuerySystem: WebAssembly\u8BA1\u7B97\u52A0\u901F\u5DF2\u542F\u7528"):console.log("QuerySystem: \u4F7F\u7528JavaScript\u5B9E\u73B0")}catch(t){console.warn("QuerySystem: WebAssembly\u521D\u59CB\u5316\u5931\u8D25\uFF0C\u4F7F\u7528JavaScript\u5B9E\u73B0:",t),this.wasmAvailable=!1}}setEntities(t){this.entities=t,this.clearQueryCache(),this.rebuildIndexes()}addEntity(t,e=!1){this.entities.includes(t)||(this.entities.push(t),this.addEntityToIndexes(t),e||this.clearQueryCache())}addEntities(t){if(t.length===0)return;let e=new Set(this.entities.map(i=>i.id)),s=0;for(let i of t)e.has(i.id)||(this.entities.push(i),this.addEntityToIndexes(i),e.add(i.id),s++);s>0&&this.clearQueryCache()}addEntitiesUnchecked(t){if(t.length!==0){for(let e of t)this.entities.push(e);for(let e of t)this.addEntityToIndexes(e);this.clearQueryCache()}}removeEntity(t){let e=this.entities.indexOf(t);e!==-1&&(this.entities.splice(e,1),this.removeEntityFromIndexes(t),this.clearQueryCache())}addEntityToIndexes(t){let e=t.componentMask,s=this.entityIndex.byMask.get(e);s||(s=new Set,this.entityIndex.byMask.set(e,s)),s.add(t);let i=t.components;for(let h=0;h<i.length;h++){let m=i[h].constructor,f=this.entityIndex.byComponentType.get(m);f||(f=new Set,this.entityIndex.byComponentType.set(m,f)),f.add(t)}let n=t.tag;if(n!==void 0){let h=this.entityIndex.byTag.get(n);h||(h=new Set,this.entityIndex.byTag.set(n,h)),h.add(t)}let o=t.name;if(o){let h=this.entityIndex.byName.get(o);h||(h=new Set,this.entityIndex.byName.set(o,h)),h.add(t)}}removeEntityFromIndexes(t){let e=t.componentMask,s=this.entityIndex.byMask.get(e);s&&(s.delete(t),s.size===0&&this.entityIndex.byMask.delete(e));for(let i of t.components){let n=i.constructor,o=this.entityIndex.byComponentType.get(n);o&&(o.delete(t),o.size===0&&this.entityIndex.byComponentType.delete(n))}if(t.tag!==void 0){let i=this.entityIndex.byTag.get(t.tag);i&&(i.delete(t),i.size===0&&this.entityIndex.byTag.delete(t.tag))}if(t.name){let i=this.entityIndex.byName.get(t.name);i&&(i.delete(t),i.size===0&&this.entityIndex.byName.delete(t.name))}}rebuildIndexes(){this.entityIndex.byMask.clear(),this.entityIndex.byComponentType.clear(),this.entityIndex.byTag.clear(),this.entityIndex.byName.clear();for(let t of this.entities)this.addEntityToIndexes(t);this.indexDirty=!1}queryAll(...t){let e=performance.now();this.queryStats.totalQueries++;let s=`all:${t.map(o=>o.name).sort().join(",")}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};let n;return t.length===1?(this.queryStats.indexHits++,n=Array.from(this.entityIndex.byComponentType.get(t[0])||[])):n=this.queryMultipleComponents(t),this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}queryMultipleComponents(t){let e=null,s=1/0;for(let o of t){let h=this.entityIndex.byComponentType.get(o);if(!h||h.size===0)return[];h.size<s&&(s=h.size,e=h)}if(!e)return this.queryStats.linearScans++,this.queryByLinearScan(t);let i=this.createComponentMask(t),n=[];for(let o of e)(o.componentMask&i)===i&&n.push(o);return n}queryByLinearScan(t){let e=this.createComponentMask(t);return this.entities.filter(s=>(s.componentMask&e)===e)}queryAny(...t){let e=performance.now();this.queryStats.totalQueries++;let s=`any:${t.map(h=>h.name).sort().join(",")}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};let n=new Set;for(let h of t){let m=this.entityIndex.byComponentType.get(h);if(m)for(let f of m)n.add(f)}let o=Array.from(n);return this.addToCache(s,o),{entities:o,count:o.length,executionTime:performance.now()-e,fromCache:!1}}queryNone(...t){let e=performance.now();this.queryStats.totalQueries++;let s=`none:${t.map(h=>h.name).sort().join(",")}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};let n=this.createComponentMask(t),o=this.entities.filter(h=>(h.componentMask&n)===BigInt(0));return this.addToCache(s,o),{entities:o,count:o.length,executionTime:performance.now()-e,fromCache:!1}}queryByTag(t){let e=performance.now();this.queryStats.totalQueries++;let s=`tag:${t}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};this.queryStats.indexHits++;let n=Array.from(this.entityIndex.byTag.get(t)||[]);return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}queryByName(t){let e=performance.now();this.queryStats.totalQueries++;let s=`name:${t}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};this.queryStats.indexHits++;let n=Array.from(this.entityIndex.byName.get(t)||[]);return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}queryByComponent(t){let e=performance.now();this.queryStats.totalQueries++;let s=`component:${t.name}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};this.queryStats.indexHits++;let n=Array.from(this.entityIndex.byComponentType.get(t)||[]);return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}getFromCache(t){let e=this.queryCache.get(t);return e?Date.now()-e.timestamp>this.cacheTimeout?(this.queryCache.delete(t),null):(e.hitCount++,e.entities):null}addToCache(t,e){this.queryCache.size>=this.cacheMaxSize&&this.cleanupCache(),this.queryCache.set(t,{entities:[...e],timestamp:Date.now(),hitCount:0})}cleanupCache(){let t=Date.now();for(let[e,s]of this.queryCache.entries())t-s.timestamp>this.cacheTimeout&&this.queryCache.delete(e);if(this.queryCache.size>=this.cacheMaxSize){let e=Array.from(this.queryCache.entries());e.sort((i,n)=>i[1].hitCount-n[1].hitCount);let s=Math.floor(this.cacheMaxSize*.2);for(let i=0;i<s&&i<e.length;i++)this.queryCache.delete(e[i][0])}}clearQueryCache(){this.queryCache.clear()}clearCache(){this.clearQueryCache()}batchUpdateComponents(t){if(this.wasmAvailable&&t.length>100)try{let e=t.map(i=>i.entityId),s=t.map(i=>i.componentMask);d.batchUpdateMasks(e,s),console.log(`WebAssembly\u52A0\u901F\u6279\u91CF\u66F4\u65B0 ${t.length} \u4E2A\u5B9E\u4F53`)}catch(e){console.warn("WebAssembly\u6279\u91CF\u66F4\u65B0\u5931\u8D25\uFF0C\u56DE\u9000\u5230JavaScript\u5B9E\u73B0:",e),this.batchUpdateComponentsJS(t)}else this.batchUpdateComponentsJS(t);this.clearQueryCache()}batchUpdateComponentsJS(t){for(let e of t)this.entities.find(i=>i.id===e.entityId)&&console.log(`\u66F4\u65B0\u5B9E\u4F53 ${e.entityId} \u7684\u7EC4\u4EF6\u63A9\u7801: ${e.componentMask}`);this.rebuildIndexes()}getAccelerationStatus(){return{wasmEnabled:this.wasmAvailable,currentProvider:this.wasmAvailable?"hybrid":"javascript",availableProviders:["javascript","hybrid"],performanceInfo:{entityCount:this.entities.length,wasmEnabled:this.wasmAvailable,cacheStats:{size:this.queryCache.size,hitRate:this.queryStats.totalQueries>0?(this.queryStats.cacheHits/this.queryStats.totalQueries*100).toFixed(2)+"%":"0%"}}}}async switchAccelerationProvider(t){return!0}createComponentMask(t){let e=t.map(s=>s.name);for(let s of e)this.bitMaskOptimizer.registerComponentType(s);return this.bitMaskOptimizer.createCombinedMask(e)}getStats(){return{entityCount:this.entities.length,indexStats:{maskIndexSize:this.entityIndex.byMask.size,componentIndexSize:this.entityIndex.byComponentType.size,tagIndexSize:this.entityIndex.byTag.size,nameIndexSize:this.entityIndex.byName.size},accelerationStatus:this.getAccelerationStatus(),queryStats:M(g({},this.queryStats),{cacheHitRate:this.queryStats.totalQueries>0?(this.queryStats.cacheHits/this.queryStats.totalQueries*100).toFixed(2)+"%":"0%"})}}};r(Bt,"QuerySystem");var tt=Bt,Ot=class Ot{constructor(t){this.conditions=[],this.querySystem=t}withAll(...t){return this.conditions.push({type:S.ALL,componentTypes:t,mask:this.createComponentMask(t)}),this}withAny(...t){return this.conditions.push({type:S.ANY,componentTypes:t,mask:this.createComponentMask(t)}),this}without(...t){return this.conditions.push({type:S.NONE,componentTypes:t,mask:this.createComponentMask(t)}),this}execute(){let t=performance.now();if(this.conditions.length===1){let e=this.conditions[0];switch(e.type){case S.ALL:return this.querySystem.queryAll(...e.componentTypes);case S.ANY:return this.querySystem.queryAny(...e.componentTypes);case S.NONE:return this.querySystem.queryNone(...e.componentTypes)}}return{entities:[],count:0,executionTime:performance.now()-t,fromCache:!1}}createComponentMask(t){let e=BigInt(0);for(let s of t)try{let i=c.getBitMask(s);e|=i}catch(i){console.warn(`\u7EC4\u4EF6\u7C7B\u578B ${s.name} \u672A\u6CE8\u518C\uFF0C\u8DF3\u8FC7`)}return e}reset(){return this.conditions=[],this}};r(Ot,"QueryBuilder");var et=Ot;var Ut=class Ut{constructor(){this.listeners=new Map,this.stats=new Map,this.batchQueue=new Map,this.batchTimers=new Map,this.batchConfigs=new Map,this.nextListenerId=0,this.isEnabled=!0,this.maxListeners=100}on(t,e,s={}){return this.addListener(t,e,s)}once(t,e,s={}){return this.addListener(t,e,M(g({},s),{once:!0}))}onAsync(t,e,s={}){return this.addListener(t,e,M(g({},s),{async:!0}))}off(t,e){let s=this.listeners.get(t);if(!s)return!1;let i=s.findIndex(n=>n.id===e);return i===-1?!1:(s.splice(i,1),s.length===0&&(this.listeners.delete(t),this.stats.delete(t)),!0)}offAll(t){this.listeners.delete(t),this.stats.delete(t),this.clearBatch(t)}async emit(t,e){if(!this.isEnabled)return;let s=this.batchConfigs.get(t);if(s!=null&&s.enabled){this.addToBatch(t,e);return}await this.executeEvent(t,e)}emitSync(t,e){if(!this.isEnabled)return;let s=this.listeners.get(t);if(!s||s.length===0)return;let i=performance.now(),n=[],o=this.sortListenersByPriority(s);for(let h of o)if(!h.config.async)try{h.config.context?h.handler.call(h.config.context,e):h.handler(e),h.config.once&&n.push(h.id)}catch(m){console.error(`Error in event handler for ${t}:`,m)}this.removeListeners(t,n),this.updateStats(t,performance.now()-i)}setBatchConfig(t,e){this.batchConfigs.set(t,e)}flushBatch(t){let e=this.batchQueue.get(t);if(!e||e.length===0)return;let s=this.batchTimers.get(t);s&&(clearTimeout(s),this.batchTimers.delete(t)),this.processBatch(t,e),this.batchQueue.delete(t)}getStats(t){return t?this.stats.get(t)||this.createEmptyStats(t):new Map(this.stats)}resetStats(t){t?this.stats.delete(t):this.stats.clear()}setEnabled(t){this.isEnabled=t}hasListeners(t){let e=this.listeners.get(t);return e?e.length>0:!1}getListenerCount(t){let e=this.listeners.get(t);return e?e.length:0}clear(){this.listeners.clear(),this.stats.clear(),this.clearAllBatches()}setMaxListeners(t){this.maxListeners=t}addListener(t,e,s){let i=this.listeners.get(t);if(i||(i=[],this.listeners.set(t,i)),i.length>=this.maxListeners)return console.warn(`Maximum listeners (${this.maxListeners}) exceeded for event type: ${t}`),"";let n=`listener_${this.nextListenerId++}`,o={handler:e,config:g({priority:0},s),id:n};return i.push(o),this.stats.has(t)||this.stats.set(t,this.createEmptyStats(t)),n}async executeEvent(t,e){let s=this.listeners.get(t);if(!s||s.length===0)return;let i=performance.now(),n=[],o=this.sortListenersByPriority(s),h=o.filter(u=>!u.config.async),m=o.filter(u=>u.config.async);for(let u of h)try{u.config.context?u.handler.call(u.config.context,e):u.handler(e),u.config.once&&n.push(u.id)}catch(ct){console.error(`Error in sync event handler for ${t}:`,ct)}let f=m.map(async u=>{try{u.config.context?await u.handler.call(u.config.context,e):await u.handler(e),u.config.once&&n.push(u.id)}catch(ct){console.error(`Error in async event handler for ${t}:`,ct)}});await Promise.all(f),this.removeListeners(t,n),this.updateStats(t,performance.now()-i)}sortListenersByPriority(t){return t.slice().sort((e,s)=>(s.config.priority||0)-(e.config.priority||0))}removeListeners(t,e){if(e.length===0)return;let s=this.listeners.get(t);if(s){for(let i of e){let n=s.findIndex(o=>o.id===i);n!==-1&&s.splice(n,1)}s.length===0&&(this.listeners.delete(t),this.stats.delete(t))}}addToBatch(t,e){let s=this.batchQueue.get(t);s||(s=[],this.batchQueue.set(t,s)),s.push(e);let i=this.batchConfigs.get(t);if(s.length>=i.batchSize){this.flushBatch(t);return}if(!this.batchTimers.has(t)){let n=setTimeout(()=>{this.flushBatch(t)},i.delay);this.batchTimers.set(t,n)}}async processBatch(t,e){let s={type:t,events:e,count:e.length,timestamp:Date.now()};await this.executeEvent(`${t}:batch`,s)}clearBatch(t){this.batchQueue.delete(t);let e=this.batchTimers.get(t);e&&(clearTimeout(e),this.batchTimers.delete(t))}clearAllBatches(){this.batchQueue.clear();for(let t of this.batchTimers.values())clearTimeout(t);this.batchTimers.clear(),this.batchConfigs.clear()}updateStats(t,e){let s=this.stats.get(t);s||(s=this.createEmptyStats(t),this.stats.set(t,s)),s.triggerCount++,s.totalExecutionTime+=e,s.averageExecutionTime=s.totalExecutionTime/s.triggerCount,s.lastTriggerTime=Date.now(),s.listenerCount=this.getListenerCount(t)}createEmptyStats(t){return{eventType:t,listenerCount:0,triggerCount:0,totalExecutionTime:0,averageExecutionTime:0,lastTriggerTime:0}}};r(Ut,"TypeSafeEventSystem");var N=Ut,ns=new N;var Dt=class Dt{get systems(){return this.entityProcessors.processors}constructor(t=!0){this.name="",this._didSceneBegin=!1,this.entities=new P(this),this.entityProcessors=new R,this.identifierPool=new F,this.componentStorageManager=new J,this.querySystem=new tt,this.eventSystem=new N,this.initialize()}initialize(){}onStart(){}unload(){}begin(){this.entityProcessors!=null&&this.entityProcessors.begin(),this._didSceneBegin=!0,this.onStart()}end(){this._didSceneBegin=!1,this.entities.removeAllEntities(),this.componentStorageManager.clear(),this.entityProcessors&&this.entityProcessors.end(),this.unload()}update(){this.entities.updateLists(),this.entityProcessors!=null&&this.entityProcessors.update(),this.entities.update(),this.entityProcessors!=null&&this.entityProcessors.lateUpdate()}createEntity(t){let e=new y(t,this.identifierPool.checkOut());return this.addEntity(e)}addEntity(t,e=!1){return this.entities.add(t),t.scene=this,this.querySystem.addEntity(t,e),this.eventSystem.emitSync("entity:added",{entity:t,scene:this}),t}createEntities(t,e="Entity"){let s=[];for(let i=0;i<t;i++){let n=new y(`${e}_${i}`,this.identifierPool.checkOut());n.scene=this,s.push(n)}for(let i of s)this.entities.add(i);return this.querySystem.addEntitiesUnchecked(s),this.eventSystem.emitSync("entities:batch_added",{entities:s,scene:this,count:t}),s}createEntitiesOld(t,e="Entity"){let s=[];for(let i=0;i<t;i++){let n=new y(`${e}_${i}`,this.identifierPool.checkOut());s.push(n),this.addEntity(n,!0)}return this.querySystem.clearCache(),s}destroyAllEntities(){for(let t=0;t<this.entities.count;t++)this.entities.buffer[t].destroy()}findEntity(t){return this.entities.findEntity(t)}findEntityById(t){return this.entities.findEntityById(t)}findEntitiesByTag(t){let e=[];for(let s of this.entities.buffer)s.tag===t&&e.push(s);return e}getEntityByName(t){return this.findEntity(t)}getEntitiesByTag(t){return this.findEntitiesByTag(t)}addEntityProcessor(t){return t.scene=this,this.entityProcessors.add(t),t.setUpdateOrder(this.entityProcessors.count-1),t}addSystem(t){return this.addEntityProcessor(t)}removeEntityProcessor(t){this.entityProcessors.remove(t)}getEntityProcessor(t){return this.entityProcessors.getProcessor(t)}getStats(){return{entityCount:this.entities.count,processorCount:this.entityProcessors.count,componentStorageStats:this.componentStorageManager.getAllStats()}}compactComponentStorage(){this.componentStorageManager.compactAll()}getDebugInfo(){return{name:this.constructor.name,entityCount:this.entities.count,processorCount:this.entityProcessors.count,isRunning:this._didSceneBegin,entities:this.entities.buffer.map(t=>({name:t.name,id:t.id,componentCount:t.components.length,componentTypes:t.components.map(e=>e.constructor.name)})),processors:this.entityProcessors.processors.map(t=>{var e;return{name:t.constructor.name,updateOrder:t.updateOrder,entityCount:((e=t._entities)==null?void 0:e.length)||0}}),componentStats:this.componentStorageManager.getAllStats()}}};r(Dt,"Scene");var W=Dt;var it=class it{constructor(t,e){this.scene=t,this.storageManager=e,this.entity=new y("",t.identifierPool.checkOut())}named(t){return this.entity.name=t,this}tagged(t){return this.entity.tag=t,this}with(t){return this.entity.addComponent(t),this}withComponents(...t){for(let e of t)this.entity.addComponent(e);return this}withIf(t,e){return t&&this.entity.addComponent(e),this}withFactory(t){let e=t();return this.entity.addComponent(e),this}configure(t,e){let s=this.entity.getComponent(t);return s&&e(s),this}enabled(t=!0){return this.entity.enabled=t,this}active(t=!0){return this.entity.active=t,this}withChild(t){let e=t.build();return this.entity.addChild(e),this}withChildren(...t){for(let e of t){let s=e.build();this.entity.addChild(s)}return this}withChildFactory(t){let s=t(this.entity).build();return this.entity.addChild(s),this}withChildIf(t,e){if(t){let s=e.build();this.entity.addChild(s)}return this}build(){return this.entity}spawn(){return this.scene.addEntity(this.entity),this.entity}clone(){let t=new it(this.scene,this.storageManager);return t.entity=this.entity,t}};r(it,"EntityBuilder");var st=it,Ft=class Ft{constructor(){this.scene=new W}named(t){return this.scene.name=t,this}withEntity(t){return this.scene.addEntity(t),this}withEntityBuilder(t){let e=new st(this.scene,this.scene.componentStorageManager),i=t(e).build();return this.scene.addEntity(i),this}withEntities(...t){for(let e of t)this.scene.addEntity(e);return this}withSystem(t){return this.scene.addSystem(t),this}withSystems(...t){for(let e of t)this.scene.addSystem(e);return this}build(){return this.scene}};r(Ft,"SceneBuilder");var qt=Ft,Nt=class Nt{constructor(t,...e){this.component=new t(...e)}set(t,e){return this.component[t]=e,this}configure(t){return t(this.component),this}setIf(t,e,s){return t&&(this.component[e]=s),this}build(){return this.component}};r(Nt,"ComponentBuilder");var $t=Nt,Wt=class Wt{constructor(t,e,s){this.scene=t,this.querySystem=e,this.eventSystem=s}createEntity(){return new st(this.scene,this.scene.componentStorageManager)}createScene(){return new qt}createComponent(t,...e){return new $t(t,...e)}query(){return new et(this.querySystem)}find(...t){return this.querySystem.queryAll(...t).entities}findFirst(...t){let e=this.querySystem.queryAll(...t);return e.entities.length>0?e.entities[0]:null}findByName(t){return this.scene.getEntityByName(t)}findByTag(t){return this.scene.getEntitiesByTag(t)}emit(t,e){this.eventSystem.emitSync(t,e)}async emitAsync(t,e){await this.eventSystem.emit(t,e)}on(t,e){return this.eventSystem.on(t,e)}once(t,e){return this.eventSystem.once(t,e)}off(t,e){this.eventSystem.off(t,e)}batch(t){return new Rt(t)}getStats(){return{entityCount:this.scene.entities.count,systemCount:this.scene.systems.length,componentStats:this.scene.componentStorageManager.getAllStats(),queryStats:this.querySystem.getStats(),eventStats:this.eventSystem.getStats()}}};r(Wt,"ECSFluentAPI");var Pt=Wt,nt=class nt{constructor(t){this.entities=t}addComponent(t){for(let e of this.entities)e.addComponent(t);return this}removeComponent(t){for(let e of this.entities)e.removeComponentByType(t);return this}setActive(t){for(let e of this.entities)e.active=t;return this}setTag(t){for(let e of this.entities)e.tag=t;return this}forEach(t){return this.entities.forEach(t),this}filter(t){return new nt(this.entities.filter(t))}toArray(){return this.entities.slice()}count(){return this.entities.length}};r(nt,"EntityBatchOperator");var Rt=nt;function ne(a,t,e){return new Pt(a,t,e)}r(ne,"createECSAPI");var _=class _{constructor(t=!0,e=!0){this._nextScene=null,this._globalManagers=[],_._instance=this,_.emitter=new U,_.emitter.addObserver(z.frameUpdated,this.update,this),this._timerManager=new $,_.registerGlobalManager(this._timerManager),this._performanceMonitor=B.instance,this._poolManager=Q.getInstance(),_.entitySystemsEnabled=e,this.debug=t,this.initialize()}static get Instance(){return this._instance}static get scene(){return this._instance&&this._instance._scene||null}static set scene(t){if(t){if(!t)throw new Error("\u573A\u666F\u4E0D\u80FD\u4E3A\u7A7A");this._instance._scene==null?(this._instance._scene=t,this._instance.onSceneChanged(),this._instance._scene.begin()):this._instance._nextScene=t}}static create(t=!0){return this._instance==null&&(this._instance=new _(t)),this._instance}static registerGlobalManager(t){this._instance._globalManagers.push(t),t.enabled=!0}static unregisterGlobalManager(t){this._instance._globalManagers.splice(this._instance._globalManagers.indexOf(t),1),t.enabled=!1}static getGlobalManager(t){for(let e of this._instance._globalManagers)if(e instanceof t)return e;return null}static schedule(t,e=!1,s=null,i){return this._instance._timerManager.schedule(t,e,s,i)}static get ecsAPI(){var t;return((t=this._instance)==null?void 0:t._ecsAPI)||null}onSceneChanged(){if(l.sceneChanged(),this._scene&&typeof this._scene.querySystem!="undefined"){let t=this._scene;this._ecsAPI=ne(t,t.querySystem,t.eventSystem)}}initialize(){}update(t=-1){var i;if(_.paused)return;let e=this._performanceMonitor.startMonitoring("Core.update");l.update(t),typeof this._performanceMonitor.updateFPS=="function"&&this._performanceMonitor.updateFPS(l.deltaTime);let s=this._performanceMonitor.startMonitoring("GlobalManagers.update");for(let n of this._globalManagers)n.enabled&&n.update();if(this._performanceMonitor.endMonitoring("GlobalManagers.update",s,this._globalManagers.length),this._poolManager.update(),this._nextScene!=null&&(this._scene!=null&&this._scene.end(),this._scene=this._nextScene,this._nextScene=null,this.onSceneChanged(),this._scene.begin()),this._scene!=null&&this._scene.update){let n=this._performanceMonitor.startMonitoring("Scene.update");this._scene.update();let o=((i=this._scene.entities)==null?void 0:i.count)||0;this._performanceMonitor.endMonitoring("Scene.update",n,o)}this._performanceMonitor.endMonitoring("Core.update",e)}};r(_,"Core");var ot=_;ot.paused=!1;var at=class at{constructor(){this._enabled=!0,this._updateOrder=0,this.id=at._idGenerator++}get enabled(){return this.entity?this.entity.enabled&&this._enabled:this._enabled}set enabled(t){this._enabled!==t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled())}get updateOrder(){return this._updateOrder}set updateOrder(t){this._updateOrder=t}onAddedToEntity(){}onRemovedFromEntity(){}onEnabled(){}onDisabled(){}update(){}};r(at,"Component");var rt=at;rt._idGenerator=0;var x=class x{constructor(){this._words=[],this._words=[]}set(t){let e=Math.floor(t/x.WORD_SIZE),s=t%x.WORD_SIZE;for(;this._words.length<=e;)this._words.push(0);this._words[e]|=1<<s}clear(t){let e=Math.floor(t/x.WORD_SIZE),s=t%x.WORD_SIZE;e<this._words.length&&(this._words[e]&=~(1<<s))}get(t){let e=Math.floor(t/x.WORD_SIZE),s=t%x.WORD_SIZE;return e>=this._words.length?!1:(this._words[e]&1<<s)!==0}containsAll(t){let e=Math.max(this._words.length,t._words.length);for(let s=0;s<e;s++){let i=s<this._words.length?this._words[s]:0,n=s<t._words.length?t._words[s]:0;if((i&n)!==n)return!1}return!0}intersects(t){let e=Math.min(this._words.length,t._words.length);for(let s=0;s<e;s++)if((this._words[s]&t._words[s])!==0)return!0;return!1}excludes(t){return!this.intersects(t)}clearAll(){this._words.length=0}isEmpty(){for(let t of this._words)if(t!==0)return!1;return!0}cardinality(){let t=0;for(let e of this._words)t+=this.popCount(e);return t}popCount(t){return t=t-(t>>>1&1431655765),t=(t&858993459)+(t>>>2&858993459),(t+(t>>>4)&252645135)*16843009>>>24}copyFrom(t){this._words=[...t._words]}clone(){let t=new x;return t.copyFrom(this),t}};r(x,"Bits");var v=x;v.WORD_SIZE=32;var k=class k{static get instance(){return k._instance||(k._instance=new k),k._instance}constructor(){this._componentTypes=new Map,this._typeNames=new Map,this._nextTypeId=0}getTypeId(t){let e=this._componentTypes.get(t);return e===void 0&&(e=this._nextTypeId++,this._componentTypes.set(t,e),this._typeNames.set(e,t.name)),e}getTypeName(t){return this._typeNames.get(t)||"Unknown"}createBits(...t){let e=new v;for(let s of t){let i=this.getTypeId(s);e.set(i)}return e}getEntityBits(t){let e=new v;for(let s of t){let i=this.getTypeId(s.constructor);e.set(i)}return e}reset(){this._componentTypes.clear(),this._typeNames.clear(),this._nextTypeId=0}get registeredTypeCount(){return this._componentTypes.size}};r(k,"ComponentTypeManager");var O=k;var ht=class ht{constructor(){this.allSet=[],this.exclusionSet=[],this.oneSet=[],this._isDirty=!0}static empty(){return new ht}getAllSet(){return this.allSet}getExclusionSet(){return this.exclusionSet}getOneSet(){return this.oneSet}isInterestedEntity(t){let e=this.getEntityBits(t);return this.isInterested(e)}isInterested(t){return this.updateBitsIfDirty(),!(this._allBits&&!t.containsAll(this._allBits)||this._exclusionBits&&t.intersects(this._exclusionBits)||this._oneBits&&!t.intersects(this._oneBits))}all(...t){return this.allSet.push(...t),this._isDirty=!0,this}exclude(...t){return this.exclusionSet.push(...t),this._isDirty=!0,this}one(...t){return this.oneSet.push(...t),this._isDirty=!0,this}getEntityBits(t){let e=t.components;return O.instance.getEntityBits(e)}updateBitsIfDirty(){if(!this._isDirty)return;let t=O.instance;this.allSet.length>0?this._allBits=t.createBits(...this.allSet):this._allBits=void 0,this.exclusionSet.length>0?this._exclusionBits=t.createBits(...this.exclusionSet):this._exclusionBits=void 0,this.oneSet.length>0?this._oneBits=t.createBits(...this.oneSet):this._oneBits=void 0,this._isDirty=!1}toString(){let t=[];return this.allSet.length>0&&t.push(`all: [${this.allSet.map(e=>e.name).join(", ")}]`),this.exclusionSet.length>0&&t.push(`exclude: [${this.exclusionSet.map(e=>e.name).join(", ")}]`),this.oneSet.length>0&&t.push(`one: [${this.oneSet.map(e=>e.name).join(", ")}]`),`Matcher(${t.join(", ")})`}};r(ht,"Matcher");var j=ht;var jt=class jt{get entities(){return this._entities}get updateOrder(){return this._updateOrder}set updateOrder(t){this.setUpdateOrder(t)}get enabled(){return this._enabled}set enabled(t){this._enabled=t}get systemName(){return this._systemName}constructor(t){this._entities=[],this._updateOrder=0,this._enabled=!0,this._performanceMonitor=B.instance,this._matcher=t||j.empty(),this._systemName=this.constructor.name,this.initialize()}get scene(){return this._scene}set scene(t){this._scene=t,this._entities=[]}get matcher(){return this._matcher}setUpdateOrder(t){this._updateOrder=t,this.scene.entityProcessors.setDirty()}initialize(){}onChanged(t){let e=this._entities.includes(t),s=this._matcher.isInterestedEntity(t);s&&!e?this.add(t):!s&&e&&this.remove(t)}add(t){this._entities.includes(t)||(this._entities.push(t),this.onAdded(t))}onAdded(t){}remove(t){let e=this._entities.indexOf(t);e!==-1&&(this._entities.splice(e,1),this.onRemoved(t))}onRemoved(t){}update(){if(!this._enabled||!this.checkProcessing())return;let t=this._performanceMonitor.startMonitoring(this._systemName);try{this.begin(),this.process(this._entities)}finally{this._performanceMonitor.endMonitoring(this._systemName,t,this._entities.length)}}lateUpdate(){if(!this._enabled||!this.checkProcessing())return;let t=this._performanceMonitor.startMonitoring(`${this._systemName}_Late`);try{this.lateProcess(this._entities),this.end()}finally{this._performanceMonitor.endMonitoring(`${this._systemName}_Late`,t,this._entities.length)}}begin(){}process(t){}lateProcess(t){}end(){}checkProcessing(){return!0}getPerformanceData(){return this._performanceMonitor.getSystemData(this._systemName)}getPerformanceStats(){return this._performanceMonitor.getSystemStats(this._systemName)}resetPerformanceData(){this._performanceMonitor.resetSystem(this._systemName)}toString(){let t=this._entities.length,e=this.getPerformanceData(),s=e?` (${e.executionTime.toFixed(2)}ms)`:"";return`${this._systemName}[${t} entities]${s}`}};r(jt,"EntitySystem");var C=jt;var Lt=class Lt extends C{onChanged(t){}process(t){this.processSystem()}};r(Lt,"ProcessingSystem");var Ht=Lt;var Gt=class Gt extends C{onChanged(t){}process(t){}};r(Gt,"PassiveSystem");var Qt=Gt;var Yt=class Yt extends C{constructor(t,e){super(t),this.acc=0,this.intervalRemainder=0,this.interval=e}checkProcessing(){return this.acc+=l.deltaTime,this.acc>=this.interval?(this.intervalRemainder=this.acc-this.interval,this.acc=0,!0):!1}getIntervalDelta(){return this.interval+this.intervalRemainder}};r(Yt,"IntervalSystem");var Jt=Yt;var Zt=class Zt{static getType(t){return t.constructor}};r(Zt,"TypeUtils");var Kt=Zt;var Xt=class Xt{static toNumber(t){return t==null?0:Number(t)}};r(Xt,"NumberExtension");var Vt=Xt;var oe;(function(a){a[a.Error=0]="Error",a[a.Assert=1]="Assert",a[a.Warning=2]="Warning",a[a.Log=3]="Log",a[a.Exception=4]="Exception"})(oe||(oe={}));var re;(function(a){a[a.Position=1]="Position",a[a.Scale=2]="Scale",a[a.Rotation=4]="Rotation"})(re||(re={}));export{v as Bits,rt as Component,re as ComponentTransform,O as ComponentTypeManager,ot as Core,z as CoreEvents,U as Emitter,y as Entity,P as EntityList,R as EntityProcessorList,C as EntitySystem,H as FuncPack,D as GlobalManager,F as IdentifierPool,Jt as IntervalSystem,oe as LogType,j as Matcher,Vt as NumberExtension,Qt as PassiveSystem,B as PerformanceMonitor,ie as PerformanceWarningType,E as Pool,Q as PoolManager,Ht as ProcessingSystem,me as Query,W as Scene,L as TieredObjectPool,l as Time,q as Timer,$ as TimerManager,Kt as TypeUtils,K as WasmEcsCore,d as ecsCore,ue as initializeEcs};
6
+ `)}};a(D,"PoolManager");var lt=D;var zt=class zt{static register(t){if(this.componentTypes.has(t))return this.componentTypes.get(t);if(this.nextBitIndex>=this.maxComponents)throw new Error(`Maximum number of component types (${this.maxComponents}) exceeded`);let e=this.nextBitIndex++;return this.componentTypes.set(t,e),e}static getBitMask(t){let e=this.componentTypes.get(t);if(e===void 0)throw new Error(`Component type ${t.name} is not registered`);return BigInt(1)<<BigInt(e)}static getBitIndex(t){let e=this.componentTypes.get(t);if(e===void 0)throw new Error(`Component type ${t.name} is not registered`);return e}static isRegistered(t){return this.componentTypes.has(t)}static getAllRegisteredTypes(){return new Map(this.componentTypes)}};a(zt,"ComponentRegistry");var p=zt;p.componentTypes=new Map;p.nextBitIndex=0;p.maxComponents=64;var Pt=class Pt{constructor(t){this.components=[],this.entityToIndex=new Map,this.indexToEntity=[],this.freeIndices=[],this._size=0,this.componentType=t,p.isRegistered(t)||p.register(t)}addComponent(t,e){if(this.entityToIndex.has(t))throw new Error(`Entity ${t} already has component ${this.componentType.name}`);let s;this.freeIndices.length>0?(s=this.freeIndices.pop(),this.components[s]=e,this.indexToEntity[s]=t):(s=this.components.length,this.components.push(e),this.indexToEntity.push(t)),this.entityToIndex.set(t,s),this._size++}getComponent(t){let e=this.entityToIndex.get(t);return e!==void 0?this.components[e]:null}hasComponent(t){return this.entityToIndex.has(t)}removeComponent(t){let e=this.entityToIndex.get(t);if(e===void 0)return null;let s=this.components[e];return this.entityToIndex.delete(t),this.components[e]=null,this.freeIndices.push(e),this._size--,s}forEach(t){for(let e=0;e<this.components.length;e++){let s=this.components[e];s&&t(s,this.indexToEntity[e],e)}}getDenseArray(){let t=[],e=[];for(let s=0;s<this.components.length;s++){let i=this.components[s];i&&(t.push(i),e.push(this.indexToEntity[s]))}return{components:t,entityIds:e}}clear(){this.components.length=0,this.entityToIndex.clear(),this.indexToEntity.length=0,this.freeIndices.length=0,this._size=0}get size(){return this._size}get type(){return this.componentType}compact(){if(this.freeIndices.length===0)return;let t=[],e=[],s=new Map,i=0;for(let n=0;n<this.components.length;n++){let r=this.components[n];r&&(t[i]=r,e[i]=this.indexToEntity[n],s.set(this.indexToEntity[n],i),i++)}this.components=t,this.indexToEntity=e,this.entityToIndex=s,this.freeIndices.length=0}getStats(){let t=this.components.length,e=this._size,s=this.freeIndices.length,i=t>0?s/t:0;return{totalSlots:t,usedSlots:e,freeSlots:s,fragmentation:i}}};a(Pt,"ComponentStorage");var ut=Pt,Ut=class Ut{constructor(){this.storages=new Map}getStorage(t){let e=this.storages.get(t);return e||(e=new ut(t),this.storages.set(t,e)),e}addComponent(t,e){let s=e.constructor;this.getStorage(s).addComponent(t,e)}getComponent(t,e){let s=this.storages.get(e);return s?s.getComponent(t):null}hasComponent(t,e){let s=this.storages.get(e);return s?s.hasComponent(t):!1}removeComponent(t,e){let s=this.storages.get(e);return s?s.removeComponent(t):null}removeAllComponents(t){for(let e of this.storages.values())e.removeComponent(t)}getComponentMask(t){let e=BigInt(0);for(let[s,i]of this.storages.entries())i.hasComponent(t)&&(e|=p.getBitMask(s));return e}compactAll(){for(let t of this.storages.values())t.compact()}getAllStats(){let t=new Map;for(let[e,s]of this.storages.entries()){let i=e.name||"Unknown";t.set(i,s.getStats())}return t}clear(){for(let t of this.storages.values())t.clear();this.storages.clear()}};a(Ut,"ComponentStorageManager");var dt=Ut;var Lt=class Lt{compare(t,e){let s=t.updateOrder-e.updateOrder;return s==0&&(s=t.id-e.id),s}};a(Lt,"EntityComparer");var pt=Lt,Ft=class Ft{constructor(t={maxSize:16,ttl:5e3,enableLRU:!0}){this.cache=new Map,this.accessOrder=[],this.config=t}get(t){let e=this.cache.get(t);return e?Date.now()-e.lastAccessed>this.config.ttl?(this.cache.delete(t),this.removeFromAccessOrder(t),null):(e.lastAccessed=Date.now(),e.accessCount++,this.config.enableLRU&&this.updateAccessOrder(t),e.component):null}set(t,e){this.cache.size>=this.config.maxSize&&!this.cache.has(t)&&this.evictLeastRecentlyUsed();let s={component:e,lastAccessed:Date.now(),accessCount:1};this.cache.set(t,s),this.config.enableLRU&&this.updateAccessOrder(t)}delete(t){let e=this.cache.delete(t);return e&&this.removeFromAccessOrder(t),e}clear(){this.cache.clear(),this.accessOrder.length=0}has(t){return this.cache.has(t)}evictLeastRecentlyUsed(){if(this.accessOrder.length>0){let t=this.accessOrder[0];this.cache.delete(t),this.accessOrder.shift()}}updateAccessOrder(t){this.removeFromAccessOrder(t),this.accessOrder.push(t)}removeFromAccessOrder(t){let e=this.accessOrder.indexOf(t);e!==-1&&this.accessOrder.splice(e,1)}getStats(){let t=0,e=0;for(let s of this.cache.values())t+=s.accessCount,e++;return{size:this.cache.size,maxSize:this.config.maxSize,hitRate:t>0?e/t:0,averageAccessCount:this.cache.size>0?t/this.cache.size:0}}};a(Ft,"ComponentCache");var qt=Ft,v=class v{constructor(t,e){this.components=[],this.updateInterval=1,this._isDestroyed=!1,this._parent=null,this._children=[],this._active=!0,this._tag=0,this._enabled=!0,this._updateOrder=0,this._componentMask=BigInt(0),this._componentTypeToIndex=new Map,this._componentAccessStats=new Map,this.name=t,this.id=e,this._componentCache=new qt}get isDestroyed(){return this._isDestroyed}get parent(){return this._parent}get children(){return[...this._children]}get childCount(){return this._children.length}get active(){return this._active}set active(t){this._active!==t&&(this._active=t,this.onActiveChanged())}get activeInHierarchy(){return this._active?this._parent?this._parent.activeInHierarchy:!0:!1}get tag(){return this._tag}set tag(t){this._tag=t}get enabled(){return this._enabled}set enabled(t){this._enabled=t}get updateOrder(){return this._updateOrder}set updateOrder(t){this._updateOrder=t}get componentMask(){return this._componentMask}createComponent(t,...e){let s=new t(...e);return this.addComponent(s)}addComponentInternal(t){let e=t.constructor;p.isRegistered(e)||p.register(e),t.entity=this;let s=this.components.length;return this.components.push(t),this._componentTypeToIndex.set(e,s),this._componentMask|=p.getBitMask(e),this._componentCache.set(e,t),this._componentAccessStats.set(e,{accessCount:0,lastAccessed:Date.now(),cacheHits:0,cacheMisses:0}),t}addComponent(t){let e=t.constructor;if(this.hasComponent(e))throw new Error(`Entity ${this.name} already has component ${e.name}`);if(this.addComponentInternal(t),this.scene&&this.scene.componentStorageManager&&this.scene.componentStorageManager.addComponent(this.id,t),t.onAddedToEntity(),v.eventBus&&v.eventBus.emitComponentAdded({timestamp:Date.now(),source:"Entity",entityId:this.id,entityName:this.name,entityTag:this.tag?.toString(),componentType:e.name,component:t}),this.scene&&this.scene.entityProcessors)for(let s of this.scene.entityProcessors.processors)s.onChanged(this);return t}getComponent(t){if(this.updateComponentAccessStats(t),!p.isRegistered(t))return this.recordCacheMiss(t),null;let e=p.getBitMask(t);if((this._componentMask&e)===BigInt(0))return this.recordCacheMiss(t),null;let s=this._componentCache.get(t);if(s)return this.recordCacheHit(t),s;let i=this._componentTypeToIndex.get(t);if(i!==void 0&&i<this.components.length){let n=this.components[i];if(n&&n.constructor===t)return this._componentCache.set(t,n),this.recordCacheHit(t),n}if(this.scene&&this.scene.componentStorageManager){let n=this.scene.componentStorageManager.getComponent(this.id,t);if(n)return this._componentCache.set(t,n),this.rebuildComponentIndex(),this.recordCacheHit(t),n}for(let n=0;n<this.components.length;n++){let r=this.components[n];if(r instanceof t)return this._componentTypeToIndex.set(t,n),this._componentCache.set(t,r),this.recordCacheHit(t),r}return this.recordCacheMiss(t),null}updateComponentAccessStats(t){let e=this._componentAccessStats.get(t);e||(e={accessCount:0,lastAccessed:Date.now(),cacheHits:0,cacheMisses:0},this._componentAccessStats.set(t,e)),e.accessCount++,e.lastAccessed=Date.now()}recordCacheHit(t){let e=this._componentAccessStats.get(t);e&&e.cacheHits++}recordCacheMiss(t){let e=this._componentAccessStats.get(t);e&&e.cacheMisses++}rebuildComponentIndex(){this._componentTypeToIndex.clear();for(let t=0;t<this.components.length;t++){let s=this.components[t].constructor;this._componentTypeToIndex.set(s,t)}}hasComponent(t){if(!p.isRegistered(t))return!1;let e=p.getBitMask(t);return(this._componentMask&e)!==BigInt(0)}getOrCreateComponent(t,...e){let s=this.getComponent(t);return s||(s=this.createComponent(t,...e)),s}removeComponent(t){let e=t.constructor,s=this.components.indexOf(t);if(s!==-1&&(this.components.splice(s,1),this.rebuildComponentIndex()),this._componentCache.delete(e),this._componentAccessStats.delete(e),p.isRegistered(e)&&(this._componentMask&=~p.getBitMask(e)),this.scene&&this.scene.componentStorageManager&&this.scene.componentStorageManager.removeComponent(this.id,e),t.onRemovedFromEntity(),v.eventBus&&v.eventBus.emitComponentRemoved({timestamp:Date.now(),source:"Entity",entityId:this.id,entityName:this.name,entityTag:this.tag?.toString(),componentType:e.name,component:t}),t.entity=null,this.scene&&this.scene.entityProcessors)for(let i of this.scene.entityProcessors.processors)i.onChanged(this)}removeComponentByType(t){let e=this.getComponent(t);return e?(this.removeComponent(e),e):null}removeAllComponents(){let t=[...this.components];this._componentCache.clear(),this._componentTypeToIndex.clear(),this._componentAccessStats.clear(),this._componentMask=BigInt(0);for(let e of t){let s=e.constructor;this.scene&&this.scene.componentStorageManager&&this.scene.componentStorageManager.removeComponent(this.id,s),e.onRemovedFromEntity(),e.entity=null}if(this.components.length=0,this.scene&&this.scene.entityProcessors)for(let e of this.scene.entityProcessors.processors)e.onChanged(this)}addComponents(t){let e=[];for(let s of t)try{e.push(this.addComponent(s))}catch(i){console.warn(`Failed to add component ${s.constructor.name}:`,i)}return e}removeComponentsByTypes(t){let e=[];for(let s of t)e.push(this.removeComponentByType(s));return e}getComponentCacheStats(){let t=new Map;for(let[e,s]of this._componentAccessStats){let i=s.cacheHits+s.cacheMisses;t.set(e.name,{...s,hitRate:i>0?s.cacheHits/i:0})}return{cacheStats:this._componentCache.getStats(),accessStats:t,indexMappingSize:this._componentTypeToIndex.size,totalComponents:this.components.length}}warmUpComponentCache(){for(let t=0;t<this.components.length;t++){let e=this.components[t],s=e.constructor;this._componentTypeToIndex.set(s,t),this._componentCache.set(s,e)}}cleanupComponentCache(){let t=Date.now(),e=3e4;for(let[s,i]of this._componentAccessStats)t-i.lastAccessed>e&&i.accessCount<5&&this._componentCache.delete(s)}getComponents(t){let e=[];for(let s of this.components)s instanceof t&&e.push(s);return e}addChild(t){if(t===this)throw new Error("Entity cannot be its own child");return t._parent===this||(t._parent&&t._parent.removeChild(t),t._parent=this,this._children.push(t),!t.scene&&this.scene&&(t.scene=this.scene,this.scene.addEntity(t))),t}removeChild(t){let e=this._children.indexOf(t);return e===-1?!1:(this._children.splice(e,1),t._parent=null,!0)}removeAllChildren(){let t=[...this._children];for(let e of t)this.removeChild(e)}findChild(t,e=!1){for(let s of this._children)if(s.name===t)return s;if(e)for(let s of this._children){let i=s.findChild(t,!0);if(i)return i}return null}findChildrenByTag(t,e=!1){let s=[];for(let i of this._children)i.tag===t&&s.push(i);if(e)for(let i of this._children)s.push(...i.findChildrenByTag(t,!0));return s}getRoot(){let t=this;for(;t._parent;)t=t._parent;return t}isAncestorOf(t){let e=t._parent;for(;e;){if(e===this)return!0;e=e._parent}return!1}isDescendantOf(t){return t.isAncestorOf(this)}getDepth(){let t=0,e=this._parent;for(;e;)t++,e=e._parent;return t}forEachChild(t,e=!1){this._children.forEach((s,i)=>{t(s,i),e&&s.forEachChild(t,!0)})}onActiveChanged(){for(let t of this.components)"onActiveChanged"in t&&typeof t.onActiveChanged=="function"&&t.onActiveChanged();this.scene&&this.scene.eventSystem&&this.scene.eventSystem.emitSync("entity:activeChanged",{entity:this,active:this._active,activeInHierarchy:this.activeInHierarchy})}update(){if(!(!this.activeInHierarchy||this._isDestroyed)){for(let t of this.components)t.enabled&&t.update();for(let t of this._children)t.update()}}destroy(){if(this._isDestroyed)return;this._isDestroyed=!0;let t=[...this._children];for(let e of t)e.destroy();this._parent&&this._parent.removeChild(this),this.removeAllComponents(),this.scene&&this.scene.entities&&this.scene.entities.remove(this)}compareTo(t){return pt.prototype.compare(this,t)}toString(){return`Entity[${this.name}:${this.id}]`}getDebugInfo(){let t=this.getComponentCacheStats(),e=Array.from(t.accessStats.entries()).map(([s,i])=>({componentType:s,accessCount:i.accessCount,cacheHits:i.cacheHits,cacheMisses:i.cacheMisses,hitRate:i.hitRate,lastAccessed:new Date(i.lastAccessed).toISOString()}));return{name:this.name,id:this.id,enabled:this._enabled,active:this._active,activeInHierarchy:this.activeInHierarchy,destroyed:this._isDestroyed,componentCount:this.components.length,componentTypes:this.components.map(s=>s.constructor.name),componentMask:this._componentMask.toString(2),parentId:this._parent?.id||null,childCount:this._children.length,childIds:this._children.map(s=>s.id),depth:this.getDepth(),componentCache:t.cacheStats,componentAccessStats:e,indexMappingSize:t.indexMappingSize}}};a(v,"Entity");var g=v;g.entityComparer=new pt;g.eventBus=null;var Ht=class Ht{get count(){return this.buffer.length}constructor(t){this.buffer=[],this._idToEntity=new Map,this._nameToEntities=new Map,this._entitiesToAdd=[],this._entitiesToRemove=[],this._isUpdating=!1,this._scene=t}add(t){this._isUpdating?this._entitiesToAdd.push(t):this.addImmediate(t)}addImmediate(t){this._idToEntity.has(t.id)||(this.buffer.push(t),this._idToEntity.set(t.id,t),this.updateNameIndex(t,!0))}remove(t){this._isUpdating?this._entitiesToRemove.push(t):this.removeImmediate(t)}removeImmediate(t){let e=this.buffer.indexOf(t);e!==-1&&(this.buffer.splice(e,1),this._idToEntity.delete(t.id),this.updateNameIndex(t,!1))}removeAllEntities(){for(let t=this.buffer.length-1;t>=0;t--)this.buffer[t].destroy();this.buffer.length=0,this._idToEntity.clear(),this._nameToEntities.clear(),this._entitiesToAdd.length=0,this._entitiesToRemove.length=0}updateLists(){if(this._entitiesToAdd.length>0){for(let t of this._entitiesToAdd)this.addImmediate(t);this._entitiesToAdd.length=0}if(this._entitiesToRemove.length>0){for(let t of this._entitiesToRemove)this.removeImmediate(t);this._entitiesToRemove.length=0}}update(){this._isUpdating=!0;try{for(let t=0;t<this.buffer.length;t++){let e=this.buffer[t];e.enabled&&!e.isDestroyed&&e.update()}}finally{this._isUpdating=!1}this.updateLists()}findEntity(t){let e=this._nameToEntities.get(t);return e&&e.length>0?e[0]:null}findEntitiesByName(t){return this._nameToEntities.get(t)||[]}findEntityById(t){return this._idToEntity.get(t)||null}findEntitiesByTag(t){let e=[];for(let s of this.buffer)s.tag===t&&e.push(s);return e}findEntitiesWithComponent(t){let e=[];for(let s of this.buffer)s.hasComponent(t)&&e.push(s);return e}forEach(t){for(let e of this.buffer)t(e)}forEachWhere(t,e){for(let s of this.buffer)t(s)&&e(s)}updateNameIndex(t,e){if(t.name)if(e){let s=this._nameToEntities.get(t.name);s||(s=[],this._nameToEntities.set(t.name,s)),s.push(t)}else{let s=this._nameToEntities.get(t.name);if(s){let i=s.indexOf(t);i!==-1&&(s.splice(i,1),s.length===0&&this._nameToEntities.delete(t.name))}}}getStats(){let t=0;for(let e of this.buffer)e.enabled&&!e.isDestroyed&&t++;return{totalEntities:this.buffer.length,activeEntities:t,pendingAdd:this._entitiesToAdd.length,pendingRemove:this._entitiesToRemove.length,nameIndexSize:this._nameToEntities.size}}};a(Ht,"EntityList");var X=Ht;var $t=class $t{constructor(){this._processors=[],this._isDirty=!1}setDirty(){this._isDirty=!0}add(t){this._processors.push(t),this.setDirty()}remove(t){let e=this._processors.indexOf(t);e!==-1&&this._processors.splice(e,1)}getProcessor(t){for(let e of this._processors)if(e instanceof t)return e;return null}begin(){this.sortProcessors();for(let t of this._processors)t.initialize()}end(){}update(){this.sortProcessors();for(let t of this._processors)t.update()}lateUpdate(){for(let t of this._processors)t.lateUpdate()}sortProcessors(){this._isDirty&&(this._processors.sort((t,e)=>t.updateOrder-e.updateOrder),this._isDirty=!1)}get processors(){return this._processors}get count(){return this._processors.length}};a($t,"EntityProcessorList");var tt=$t;var Wt=class Wt{constructor(){this._nextAvailableId=0,this._ids=[]}checkOut(){return this._ids.length>0?this._ids.pop():this._nextAvailableId++}checkIn(t){this._ids.push(t)}};a(Wt,"IdentifierPool");var N=Wt;var jt=class jt{constructor(){this.wasmModule=null,this.wasmCore=null,this.silent=!1}setSilent(t){this.silent=t}async loadWasmModule(){try{let t="../../bin/wasm/ecs_wasm_core";return this.wasmModule=await import(t),this.wasmModule&&(await this.initializeWasmModule(),this.wasmCore=new this.wasmModule.EcsCore),!0}catch{return this.silent||console.warn("WASM\u52A0\u8F7D\u5931\u8D25\uFF0C\u4F7F\u7528JavaScript\u5B9E\u73B0"),!1}}async initializeWasmModule(){this.wasmModule&&await this.wasmModule.default()}getWasmCore(){return this.wasmCore}getWasmModule(){return this.wasmModule}cleanup(){this.wasmCore&&this.wasmCore.free&&this.wasmCore.free(),this.wasmCore=null,this.wasmModule=null}};a(jt,"WasmLoader");var $=jt;var Yt=class Yt{constructor(){this.jsEntityMasks=new Map,this.jsNextEntityId=1,this.jsQueryCount=0,this.jsUpdateCount=0}createEntity(){let t=this.jsNextEntityId++;return this.jsEntityMasks.set(t,0n),t}destroyEntity(t){return this.jsEntityMasks.delete(t)}updateEntityMask(t,e){this.jsEntityMasks.set(t,e),this.jsUpdateCount++}batchUpdateMasks(t,e){for(let s=0;s<t.length&&s<e.length;s++)this.jsEntityMasks.set(t[s],e[s]);this.jsUpdateCount+=Math.min(t.length,e.length)}queryEntities(t,e=1e4){let s=[];for(let[i,n]of this.jsEntityMasks)if((n&t)===t&&(s.push(i),s.length>=e))break;return this.jsQueryCount++,{entities:new Uint32Array(s),count:s.length}}queryCached(t){return this.queryEntities(t)}queryMultipleComponents(t,e=1e4){let s=[];for(let[i,n]of this.jsEntityMasks){let r=!1;for(let h of t)if((n&h)===h){r=!0;break}if(r&&(s.push(i),s.length>=e))break}return this.jsQueryCount++,{entities:new Uint32Array(s),count:s.length}}queryWithExclusion(t,e,s=1e4){let i=[];for(let[n,r]of this.jsEntityMasks)if((r&t)===t&&(r&e)===0n&&(i.push(n),i.length>=s))break;return this.jsQueryCount++,{entities:new Uint32Array(i),count:i.length}}getEntityMask(t){return this.jsEntityMasks.get(t)||null}entityExists(t){return this.jsEntityMasks.has(t)}createComponentMask(t){let e=0n;for(let s of t)e|=1n<<BigInt(s);return e}maskContainsComponent(t,e){return(t&1n<<BigInt(e))!==0n}getPerformanceStats(){return{entityCount:this.jsEntityMasks.size,indexCount:0,queryCount:this.jsQueryCount,updateCount:this.jsUpdateCount,wasmEnabled:!1}}clear(){this.jsEntityMasks.clear(),this.jsNextEntityId=1,this.jsQueryCount=0,this.jsUpdateCount=0}getEntityCount(){return this.jsEntityMasks.size}};a(Yt,"JavaScriptFallback");var W=Yt;var Qt=class Qt{constructor(){this.initialized=!1,this.usingWasm=!1,this.wasmLoader=new $,this.jsFallback=new W}setSilent(t){this.wasmLoader.setSilent(t)}async initialize(){return this.initialized||(console.log("\u{1F504} \u521D\u59CB\u5316ECS\u6838\u5FC3..."),this.usingWasm=await this.wasmLoader.loadWasmModule(),this.initialized=!0),!0}createEntity(){if(this.ensureInitialized(),this.usingWasm){let t=this.wasmLoader.getWasmCore();return t?t.create_entity():this.jsFallback.createEntity()}return this.jsFallback.createEntity()}destroyEntity(t){if(this.ensureInitialized(),this.usingWasm){let e=this.wasmLoader.getWasmCore();return e?e.destroy_entity(t):this.jsFallback.destroyEntity(t)}return this.jsFallback.destroyEntity(t)}updateEntityMask(t,e){if(this.ensureInitialized(),this.usingWasm){let s=this.wasmLoader.getWasmCore();s?s.update_entity_mask(t,e):this.jsFallback.updateEntityMask(t,e)}else this.jsFallback.updateEntityMask(t,e)}batchUpdateMasks(t,e){if(this.ensureInitialized(),this.usingWasm){let s=this.wasmLoader.getWasmCore();if(s){let i=new Uint32Array(t),n=new BigUint64Array(e);s.batch_update_masks(i,n)}else this.jsFallback.batchUpdateMasks(t,e)}else this.jsFallback.batchUpdateMasks(t,e)}queryEntities(t,e=1e4){if(this.ensureInitialized(),this.usingWasm){let s=this.wasmLoader.getWasmCore();if(s){let i=s.query_entities(t,e),n=s.get_query_result_count(),r=this.wasmLoader.getWasmModule();if(r&&r.memory){let h=new Uint32Array(r.memory.buffer),c=new Uint32Array(n);for(let u=0;u<n;u++)c[u]=h[i/4+u];return{entities:c,count:n}}}}return this.jsFallback.queryEntities(t,e)}queryCached(t){if(this.ensureInitialized(),this.usingWasm){let e=this.wasmLoader.getWasmCore();if(e){let s=e.query_cached(t),i=e.get_cached_query_count(t),n=this.wasmLoader.getWasmModule();if(n&&n.memory){let r=new Uint32Array(n.memory.buffer),h=new Uint32Array(i);for(let c=0;c<i;c++)h[c]=r[s/4+c];return{entities:h,count:i}}}}return this.jsFallback.queryCached(t)}queryMultipleComponents(t,e=1e4){if(this.ensureInitialized(),this.usingWasm){let s=this.wasmLoader.getWasmCore();if(s){let i=new BigUint64Array(t),n=s.query_multiple_components(i,e),r=s.get_query_result_count(),h=this.wasmLoader.getWasmModule();if(h&&h.memory){let c=new Uint32Array(h.memory.buffer),u=new Uint32Array(r);for(let m=0;m<r;m++)u[m]=c[n/4+m];return{entities:u,count:r}}}}return this.jsFallback.queryMultipleComponents(t,e)}queryWithExclusion(t,e,s=1e4){if(this.ensureInitialized(),this.usingWasm){let i=this.wasmLoader.getWasmCore();if(i){let n=i.query_with_exclusion(t,e,s),r=i.get_query_result_count(),h=this.wasmLoader.getWasmModule();if(h&&h.memory){let c=new Uint32Array(h.memory.buffer),u=new Uint32Array(r);for(let m=0;m<r;m++)u[m]=c[n/4+m];return{entities:u,count:r}}}}return this.jsFallback.queryWithExclusion(t,e,s)}getEntityMask(t){if(this.ensureInitialized(),this.usingWasm){let e=this.wasmLoader.getWasmCore();if(e)return e.get_entity_mask(t)}return this.jsFallback.getEntityMask(t)}entityExists(t){if(this.ensureInitialized(),this.usingWasm){let e=this.wasmLoader.getWasmCore();if(e)return e.entity_exists(t)}return this.jsFallback.entityExists(t)}createComponentMask(t){if(this.ensureInitialized(),this.usingWasm){let e=this.wasmLoader.getWasmModule();if(e){let s=new Uint32Array(t);return e.create_component_mask(s)}}return this.jsFallback.createComponentMask(t)}maskContainsComponent(t,e){if(this.ensureInitialized(),this.usingWasm){let s=this.wasmLoader.getWasmModule();if(s)return s.mask_contains_component(t,e)}return this.jsFallback.maskContainsComponent(t,e)}getPerformanceStats(){if(this.ensureInitialized(),this.usingWasm){let t=this.wasmLoader.getWasmCore();if(t){let e=t.get_performance_stats();return{entityCount:e[0]||0,indexCount:e[1]||0,queryCount:e[2]||0,updateCount:e[3]||0,wasmEnabled:!0}}}return this.jsFallback.getPerformanceStats()}clear(){if(this.ensureInitialized(),this.usingWasm){let t=this.wasmLoader.getWasmCore();t&&t.clear()}this.jsFallback.clear()}isUsingWasm(){return this.usingWasm}isInitialized(){return this.initialized}ensureInitialized(){if(!this.initialized)throw new Error("ECS\u6838\u5FC3\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5")}cleanup(){this.wasmLoader.cleanup(),this.jsFallback.clear(),this.initialized=!1,this.usingWasm=!1}};a(Qt,"WasmEcsCore");var j=Qt;var S=new j;async function Ae(o=!1){return S.setSilent(o),await S.initialize()}a(Ae,"initializeEcs");var Gt=class Gt{constructor(t,e,s=1e3){this.pool=[],this.createFn=t,this.resetFn=e,this.maxSize=s}acquire(){return this.pool.length>0?this.pool.pop():this.createFn()}release(t){this.pool.length<this.maxSize&&(this.resetFn&&this.resetFn(t),this.pool.push(t))}prewarm(t){for(let e=0;e<t&&this.pool.length<this.maxSize;e++)this.pool.push(this.createFn())}clear(){this.pool.length=0}getAvailableCount(){return this.pool.length}getMaxSize(){return this.maxSize}};a(Gt,"ComponentPool");var ft=Gt,k=class k{constructor(){this.pools=new Map}static getInstance(){return k.instance||(k.instance=new k),k.instance}registerPool(t,e,s,i){this.pools.set(t,new ft(e,s,i))}acquireComponent(t){let e=this.pools.get(t);return e?e.acquire():null}releaseComponent(t,e){let s=this.pools.get(t);s&&s.release(e)}prewarmAll(t=100){for(let e of this.pools.values())e.prewarm(t)}clearAll(){for(let t of this.pools.values())t.clear()}getPoolStats(){let t=new Map;for(let[e,s]of this.pools)t.set(e,{available:s.getAvailableCount(),maxSize:s.getMaxSize()});return t}};a(k,"ComponentPoolManager");var yt=k;var R=class R{constructor(){this.maskCache=new Map,this.componentTypeMap=new Map,this.nextComponentId=0}static getInstance(){return R.instance||(R.instance=new R),R.instance}registerComponentType(t){return this.componentTypeMap.has(t)||this.componentTypeMap.set(t,this.nextComponentId++),this.componentTypeMap.get(t)}getComponentTypeId(t){return this.componentTypeMap.get(t)}createSingleComponentMask(t){let e=`single:${t}`;if(this.maskCache.has(e))return this.maskCache.get(e);let s=this.getComponentTypeId(t);if(s===void 0)throw new Error(`Component type not registered: ${t}`);let i=1n<<BigInt(s);return this.maskCache.set(e,i),i}createCombinedMask(t){let s=`combined:${[...t].sort().join(",")}`;if(this.maskCache.has(s))return this.maskCache.get(s);let i=0n;for(let n of t){let r=this.getComponentTypeId(n);if(r===void 0)throw new Error(`Component type not registered: ${n}`);i|=1n<<BigInt(r)}return this.maskCache.set(s,i),i}maskContainsComponent(t,e){let s=this.createSingleComponentMask(e);return(t&s)!==0n}maskContainsAllComponents(t,e){let s=this.createCombinedMask(e);return(t&s)===s}maskContainsAnyComponent(t,e){let s=this.createCombinedMask(e);return(t&s)!==0n}addComponentToMask(t,e){let s=this.createSingleComponentMask(e);return t|s}removeComponentFromMask(t,e){let s=this.createSingleComponentMask(e);return t&~s}precomputeCommonMasks(t){for(let e of t)this.createCombinedMask(e)}getCacheStats(){return{size:this.maskCache.size,componentTypes:this.componentTypeMap.size}}clearCache(){this.maskCache.clear()}reset(){this.maskCache.clear(),this.componentTypeMap.clear(),this.nextComponentId=0}maskToComponentNames(t){let e=[];for(let[s,i]of this.componentTypeMap){let n=1n<<BigInt(i);(t&n)!==0n&&e.push(s)}return e}getComponentCount(t){let e=0,s=t;for(;s!==0n;)(s&1n)!==0n&&e++,s>>=1n;return e}};a(R,"BitMaskOptimizer");var et=R;var x;(function(o){o.ADD_ENTITY="add_entity",o.REMOVE_ENTITY="remove_entity",o.UPDATE_ENTITY="update_entity"})(x||(x={}));var Vt=class Vt{constructor(){this.pendingOperations=[],this.isProcessing=!1,this.batchSize=1e3,this.flushTimeout=null,this.flushDelay=16}addOperation(t){this.pendingOperations.push(t),this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}addEntities(t){for(let e of t)this.pendingOperations.push({type:x.ADD_ENTITY,entity:e});this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}removeEntities(t){for(let e of t)this.pendingOperations.push({type:x.REMOVE_ENTITY,entity:e});this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}updateEntities(t){for(let e of t)this.pendingOperations.push({type:x.UPDATE_ENTITY,entity:e.entity,oldMask:e.oldMask,newMask:e.newMask});this.pendingOperations.length>=this.batchSize?this.flush():this.scheduleFlush()}scheduleFlush(){this.flushTimeout||(this.flushTimeout=setTimeout(()=>{this.flush()},this.flushDelay))}flush(){if(!(this.isProcessing||this.pendingOperations.length===0)){this.isProcessing=!0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null);try{this.processBatch()}finally{this.isProcessing=!1}}}processBatch(){let t=this.pendingOperations;this.pendingOperations=[];let e=[],s=[],i=[];for(let n of t)switch(n.type){case x.ADD_ENTITY:e.push(n.entity);break;case x.REMOVE_ENTITY:s.push(n.entity);break;case x.UPDATE_ENTITY:n.oldMask!==void 0&&n.newMask!==void 0&&i.push({entity:n.entity,oldMask:n.oldMask,newMask:n.newMask});break}e.length>0&&this.processBatchAdd(e),s.length>0&&this.processBatchRemove(s),i.length>0&&this.processBatchUpdate(i)}processBatchAdd(t){this.onBatchAdd&&this.onBatchAdd(t)}processBatchRemove(t){this.onBatchRemove&&this.onBatchRemove(t)}processBatchUpdate(t){this.onBatchUpdate&&this.onBatchUpdate(t)}setBatchSize(t){this.batchSize=Math.max(1,t)}setFlushDelay(t){this.flushDelay=Math.max(0,t)}getPendingCount(){return this.pendingOperations.length}clear(){this.pendingOperations.length=0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null)}hasPendingOperations(){return this.pendingOperations.length>0}};a(Vt,"IndexUpdateBatcher");var st=Vt;var d;(function(o){o.HASH="hash",o.BITMAP="bitmap",o.SORTED="sorted"})(d||(d={}));var Kt=class Kt{constructor(){this.type=d.HASH,this._componentToEntities=new Map,this._entityToComponents=new Map,this._queryCount=0,this._totalQueryTime=0,this._lastUpdated=Date.now()}addEntity(t){let e=t.components,s=new Set;for(let i of e){let n=i.constructor;s.add(n);let r=this._componentToEntities.get(n);r||(r=new Set,this._componentToEntities.set(n,r)),r.add(t)}this._entityToComponents.set(t,s),this._lastUpdated=Date.now()}removeEntity(t){let e=this._entityToComponents.get(t);if(e){for(let s of e){let i=this._componentToEntities.get(s);i&&(i.delete(t),i.size===0&&this._componentToEntities.delete(s))}this._entityToComponents.delete(t),this._lastUpdated=Date.now()}}query(t){let e=performance.now(),s=new Set(this._componentToEntities.get(t)||[]);return this._queryCount++,this._totalQueryTime+=performance.now()-e,s}queryMultiple(t,e){let s=performance.now();if(t.length===0)return new Set;if(t.length===1)return this.query(t[0]);let i;if(e==="AND"){let n,r=1/0;for(let h of t){let c=this._componentToEntities.get(h);if(!c||c.size===0)return this._queryCount++,this._totalQueryTime+=performance.now()-s,new Set;c.size<r&&(r=c.size,n=c)}if(i=new Set,n)for(let h of n){let c=!0;for(let u of t){let m=this._componentToEntities.get(u);if(!m||!m.has(h)){c=!1;break}}c&&i.add(h)}}else{i=new Set;for(let n of t){let r=this._componentToEntities.get(n);if(r)for(let h of r)i.add(h)}}return this._queryCount++,this._totalQueryTime+=performance.now()-s,i}clear(){this._componentToEntities.clear(),this._entityToComponents.clear(),this._lastUpdated=Date.now()}getStats(){let t=0;t+=this._componentToEntities.size*64,t+=this._entityToComponents.size*64;for(let e of this._componentToEntities.values())t+=e.size*8;for(let e of this._entityToComponents.values())t+=e.size*8;return{type:this.type,size:this._componentToEntities.size,memoryUsage:t,queryCount:this._queryCount,avgQueryTime:this._queryCount>0?this._totalQueryTime/this._queryCount:0,lastUpdated:this._lastUpdated}}};a(Kt,"HashComponentIndex");var Y=Kt,Zt=class Zt{constructor(){this.type=d.BITMAP,this._componentTypeToBit=new Map,this._entityToBitmap=new Map,this._bitToEntities=new Map,this._nextBit=0,this._queryCount=0,this._totalQueryTime=0,this._lastUpdated=Date.now()}addEntity(t){let e=0;for(let s of t.components){let i=s.constructor,n=this._componentTypeToBit.get(i);n===void 0&&(n=this._nextBit++,this._componentTypeToBit.set(i,n)),e|=1<<n;let r=this._bitToEntities.get(1<<n);r||(r=new Set,this._bitToEntities.set(1<<n,r)),r.add(t)}this._entityToBitmap.set(t,e),this._lastUpdated=Date.now()}removeEntity(t){let e=this._entityToBitmap.get(t);if(e!==void 0){for(let[s,i]of this._bitToEntities)(e&s)!==0&&(i.delete(t),i.size===0&&this._bitToEntities.delete(s));this._entityToBitmap.delete(t),this._lastUpdated=Date.now()}}query(t){let e=performance.now(),s=this._componentTypeToBit.get(t);if(s===void 0)return this._queryCount++,this._totalQueryTime+=performance.now()-e,new Set;let i=new Set(this._bitToEntities.get(1<<s)||[]);return this._queryCount++,this._totalQueryTime+=performance.now()-e,i}queryMultiple(t,e){let s=performance.now();if(t.length===0)return new Set;let i=0,n=[];for(let h of t){let c=this._componentTypeToBit.get(h);c!==void 0&&(i|=1<<c,n.push(1<<c))}let r=new Set;if(e==="AND")for(let[h,c]of this._entityToBitmap)(c&i)===i&&r.add(h);else for(let h of n){let c=this._bitToEntities.get(h);if(c)for(let u of c)r.add(u)}return this._queryCount++,this._totalQueryTime+=performance.now()-s,r}clear(){this._componentTypeToBit.clear(),this._entityToBitmap.clear(),this._bitToEntities.clear(),this._nextBit=0,this._lastUpdated=Date.now()}getStats(){let t=0;t+=this._componentTypeToBit.size*12,t+=this._entityToBitmap.size*12,t+=this._bitToEntities.size*64;for(let e of this._bitToEntities.values())t+=e.size*8;return{type:this.type,size:this._componentTypeToBit.size,memoryUsage:t,queryCount:this._queryCount,avgQueryTime:this._queryCount>0?this._totalQueryTime/this._queryCount:0,lastUpdated:this._lastUpdated}}};a(Zt,"BitmapComponentIndex");var gt=Zt,Jt=class Jt{constructor(t=d.HASH){this._indexHistory=new Map,this._autoOptimize=!0,this._optimizationThreshold=1e3,this._activeIndex=this.createIndex(t)}addEntity(t){this._activeIndex.addEntity(t),this.checkOptimization()}removeEntity(t){this._activeIndex.removeEntity(t)}query(t){return this._activeIndex.query(t)}queryMultiple(t,e){return this._activeIndex.queryMultiple(t,e)}switchIndexType(t){if(t===this._activeIndex.type)return;this._indexHistory.set(this._activeIndex.type,this._activeIndex.getStats());let e=this._activeIndex;this._activeIndex=this.createIndex(t),e.clear()}setAutoOptimize(t){this._autoOptimize=t}getStats(){return this._activeIndex.getStats()}getAllStats(){let t=this._activeIndex.getStats();return new Map([...this._indexHistory,[t.type,t]])}clear(){this._activeIndex.clear()}createIndex(t){switch(t){case d.HASH:return new Y;case d.BITMAP:return new gt;case d.SORTED:return new Y;default:return new Y}}checkOptimization(){if(!this._autoOptimize)return;let t=this._activeIndex.getStats();t.queryCount<this._optimizationThreshold||(t.avgQueryTime>1&&t.type!==d.HASH?this.switchIndexType(d.HASH):t.memoryUsage>10*1024*1024&&t.type!==d.BITMAP&&this.switchIndexType(d.BITMAP))}};a(Jt,"ComponentIndexManager");var B=Jt;var Xt=class Xt{constructor(){this._archetypes=new Map,this._entityToArchetype=new Map,this._componentToArchetypes=new Map,this._queryCache=new Map,this._cacheTimeout=5e3,this._maxCacheSize=100}addEntity(t){let e=this.getEntityComponentTypes(t),s=this.generateArchetypeId(e),i=this._archetypes.get(s);i||(i=this.createArchetype(e)),i.entities.push(t),i.updatedAt=Date.now(),this._entityToArchetype.set(t,i),this.updateComponentIndexes(i,e,!0),this.invalidateQueryCache()}removeEntity(t){let e=this._entityToArchetype.get(t);if(!e)return;let s=e.entities.indexOf(t);s!==-1&&(e.entities.splice(s,1),e.updatedAt=Date.now()),this._entityToArchetype.delete(t),this.invalidateQueryCache()}queryArchetypes(t,e="AND"){let s=performance.now(),i=`${e}:${t.map(u=>u.name).sort().join(",")}`,n=this._queryCache.get(i);if(n&&Date.now()-n.timestamp<this._cacheTimeout)return{...n.result,executionTime:performance.now()-s,fromCache:!0};let r=[],h=0;if(e==="AND")for(let u of this._archetypes.values())this.archetypeContainsAllComponents(u,t)&&(r.push(u),h+=u.entities.length);else{let u=new Set;for(let m of t){let F=this._componentToArchetypes.get(m);if(F)for(let ve of F)u.add(ve)}for(let m of u)r.push(m),h+=m.entities.length}let c={archetypes:r,totalEntities:h,executionTime:performance.now()-s,fromCache:!1};return this._queryCache.set(i,{result:c,timestamp:Date.now()}),c}getEntityArchetype(t){return this._entityToArchetype.get(t)}getAllArchetypes(){return Array.from(this._archetypes.values())}clear(){this._archetypes.clear(),this._entityToArchetype.clear(),this._componentToArchetypes.clear(),this._queryCache.clear()}getEntityComponentTypes(t){return t.components.map(e=>e.constructor)}generateArchetypeId(t){return t.map(e=>e.name).sort().join("|")}createArchetype(t){let e=this.generateArchetypeId(t),s={id:e,componentTypes:[...t],entities:[],createdAt:Date.now(),updatedAt:Date.now()};return this._archetypes.set(e,s),s}archetypeContainsAllComponents(t,e){for(let s of e)if(!t.componentTypes.includes(s))return!1;return!0}updateComponentIndexes(t,e,s){for(let i of e){let n=this._componentToArchetypes.get(i);n||(n=new Set,this._componentToArchetypes.set(i,n)),s?n.add(t):(n.delete(t),n.size===0&&this._componentToArchetypes.delete(i))}}invalidateQueryCache(){this._queryCache.clear()}};a(Xt,"ArchetypeSystem");var z=Xt;var y;(function(o){o[o.COMPONENT_MODIFIED=1]="COMPONENT_MODIFIED",o[o.COMPONENT_ADDED=2]="COMPONENT_ADDED",o[o.COMPONENT_REMOVED=4]="COMPONENT_REMOVED",o[o.TRANSFORM_CHANGED=8]="TRANSFORM_CHANGED",o[o.STATE_CHANGED=16]="STATE_CHANGED",o[o.CUSTOM_1=256]="CUSTOM_1",o[o.CUSTOM_2=512]="CUSTOM_2",o[o.CUSTOM_3=1024]="CUSTOM_3",o[o.ALL=4294967295]="ALL"})(y||(y={}));var te=class te{constructor(){this._dirtyEntities=new Map,this._listeners=[],this._stats={totalMarkings:0,totalCleanups:0,frameCount:0,totalDirtyPerFrame:0},this._currentFrame=0,this._batchSize=100,this._maxProcessingTime=16,this._processingQueue=[],this._isProcessing=!1}markDirty(t,e,s=[]){this._stats.totalMarkings++;let i=this._dirtyEntities.get(t);i||(i={entity:t,flags:0,modifiedComponents:new Set,timestamp:performance.now(),frameNumber:this._currentFrame},this._dirtyEntities.set(t,i)),i.flags|=e,i.timestamp=performance.now(),i.frameNumber=this._currentFrame;for(let n of s)i.modifiedComponents.add(n);this.notifyListeners(i,e)}isDirty(t,e=y.ALL){let s=this._dirtyEntities.get(t);return s?(s.flags&e)!==0:!1}clearDirty(t,e=y.ALL){let s=this._dirtyEntities.get(t);s&&(e===y.ALL?this._dirtyEntities.delete(t):(s.flags&=~e,s.flags===0&&this._dirtyEntities.delete(t)),this._stats.totalCleanups++)}getDirtyEntities(t=y.ALL){let e=[];for(let s of this._dirtyEntities.values())(s.flags&t)!==0&&e.push(s);return e}processDirtyEntities(){if(this._isProcessing)return;this._isProcessing=!0;let t=performance.now();this._processingQueue.length===0&&this._processingQueue.push(...this._dirtyEntities.values());let e=0;for(;this._processingQueue.length>0&&e<this._batchSize&&!(performance.now()-t>this._maxProcessingTime);){let i=this._processingQueue.shift();this.processEntity(i),e++}this._processingQueue.length===0&&(this._isProcessing=!1,this.onFrameEnd())}addListener(t){this._listeners.push(t),this._listeners.sort((e,s)=>(e.priority||100)-(s.priority||100))}removeListener(t){let e=this._listeners.findIndex(s=>s.callback===t);e!==-1&&this._listeners.splice(e,1)}beginFrame(){this._currentFrame++}endFrame(){this._isProcessing||this.processDirtyEntities()}getStats(){return{dirtyEntityCount:this._dirtyEntities.size,totalMarkings:this._stats.totalMarkings,totalCleanups:this._stats.totalCleanups,listenerCount:this._listeners.length,avgDirtyPerFrame:this._stats.frameCount>0?this._stats.totalDirtyPerFrame/this._stats.frameCount:0,estimatedMemoryUsage:this.estimateMemoryUsage()}}clear(){this._dirtyEntities.clear(),this._processingQueue.length=0,this._isProcessing=!1,this._stats={totalMarkings:0,totalCleanups:0,frameCount:0,totalDirtyPerFrame:0}}configureBatchProcessing(t,e){this._batchSize=t,this._maxProcessingTime=e}processEntity(t){for(let e of this._listeners)if((t.flags&e.flags)!==0)try{e.callback(t)}catch(s){console.error("Dirty listener error:",s)}this.clearDirty(t.entity)}notifyListeners(t,e){for(let s of this._listeners)if((e&s.flags)!==0)try{s.callback(t)}catch(i){console.error("Dirty listener notification error:",i)}}onFrameEnd(){this._stats.frameCount++,this._stats.totalDirtyPerFrame+=this._dirtyEntities.size}estimateMemoryUsage(){let t=0;return t+=this._dirtyEntities.size*100,t+=this._listeners.length*50,t+=this._processingQueue.length*8,t}};a(te,"DirtyTrackingSystem");var P=te;var M;(function(o){o.ALL="all",o.ANY="any",o.NONE="none"})(M||(M={}));var ee=class ee{constructor(){this.entities=[],this.wasmAvailable=!1,this.indexDirty=!0,this.queryCache=new Map,this.cacheMaxSize=1e3,this.cacheTimeout=5e3,this.queryStats={totalQueries:0,cacheHits:0,indexHits:0,linearScans:0,archetypeHits:0,dirtyChecks:0},this.entityIndex={byMask:new Map,byComponentType:new Map,byTag:new Map,byName:new Map},this.componentPoolManager=yt.getInstance(),this.bitMaskOptimizer=et.getInstance(),this.indexUpdateBatcher=new st,this.componentIndexManager=new B(d.HASH),this.archetypeSystem=new z,this.dirtyTrackingSystem=new P,this.indexUpdateBatcher.onBatchAdd=t=>{for(let e of t)this.addEntityToIndexes(e)},this.indexUpdateBatcher.onBatchRemove=t=>{for(let e of t)this.removeEntityFromIndexes(e)},this.indexUpdateBatcher.onBatchUpdate=t=>{for(let e of t)this.removeEntityFromIndexes(e.entity),this.addEntityToIndexes(e.entity)},this.initializeWasm()}async initializeWasm(){try{let t=await S.initialize();this.wasmAvailable=t&&S.isUsingWasm(),this.wasmAvailable?console.log("QuerySystem: WebAssembly\u8BA1\u7B97\u52A0\u901F\u5DF2\u542F\u7528"):console.log("QuerySystem: \u4F7F\u7528JavaScript\u5B9E\u73B0")}catch(t){console.warn("QuerySystem: WebAssembly\u521D\u59CB\u5316\u5931\u8D25\uFF0C\u4F7F\u7528JavaScript\u5B9E\u73B0:",t),this.wasmAvailable=!1}}setEntities(t){this.entities=t,this.clearQueryCache(),this.rebuildIndexes()}addEntity(t,e=!1){this.entities.includes(t)||(this.entities.push(t),this.addEntityToIndexes(t),this.componentIndexManager.addEntity(t),this.archetypeSystem.addEntity(t),this.dirtyTrackingSystem.markDirty(t,y.COMPONENT_ADDED),e||this.clearQueryCache())}addEntities(t){if(t.length===0)return;let e=new Set(this.entities.map(i=>i.id)),s=0;for(let i of t)e.has(i.id)||(this.entities.push(i),this.addEntityToIndexes(i),e.add(i.id),s++);s>0&&this.clearQueryCache()}addEntitiesUnchecked(t){if(t.length!==0){for(let e of t)this.entities.push(e);for(let e of t)this.addEntityToIndexes(e);this.clearQueryCache()}}removeEntity(t){let e=this.entities.indexOf(t);e!==-1&&(this.entities.splice(e,1),this.removeEntityFromIndexes(t),this.componentIndexManager.removeEntity(t),this.archetypeSystem.removeEntity(t),this.dirtyTrackingSystem.markDirty(t,y.COMPONENT_REMOVED),this.clearQueryCache())}addEntityToIndexes(t){let e=t.componentMask,s=this.entityIndex.byMask.get(e);s||(s=new Set,this.entityIndex.byMask.set(e,s)),s.add(t);let i=t.components;for(let h=0;h<i.length;h++){let c=i[h].constructor,u=this.entityIndex.byComponentType.get(c);u||(u=new Set,this.entityIndex.byComponentType.set(c,u)),u.add(t)}let n=t.tag;if(n!==void 0){let h=this.entityIndex.byTag.get(n);h||(h=new Set,this.entityIndex.byTag.set(n,h)),h.add(t)}let r=t.name;if(r){let h=this.entityIndex.byName.get(r);h||(h=new Set,this.entityIndex.byName.set(r,h)),h.add(t)}}removeEntityFromIndexes(t){let e=t.componentMask,s=this.entityIndex.byMask.get(e);s&&(s.delete(t),s.size===0&&this.entityIndex.byMask.delete(e));for(let i of t.components){let n=i.constructor,r=this.entityIndex.byComponentType.get(n);r&&(r.delete(t),r.size===0&&this.entityIndex.byComponentType.delete(n))}if(t.tag!==void 0){let i=this.entityIndex.byTag.get(t.tag);i&&(i.delete(t),i.size===0&&this.entityIndex.byTag.delete(t.tag))}if(t.name){let i=this.entityIndex.byName.get(t.name);i&&(i.delete(t),i.size===0&&this.entityIndex.byName.delete(t.name))}}rebuildIndexes(){this.entityIndex.byMask.clear(),this.entityIndex.byComponentType.clear(),this.entityIndex.byTag.clear(),this.entityIndex.byName.clear();for(let t of this.entities)this.addEntityToIndexes(t);this.indexDirty=!1}queryAll(...t){let e=performance.now();this.queryStats.totalQueries++;let s=`all:${t.map(h=>h.name).sort().join(",")}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};let n,r=this.archetypeSystem.queryArchetypes(t,"AND");if(r.archetypes.length>0){this.queryStats.archetypeHits++,n=[];for(let h of r.archetypes)n.push(...h.entities)}else if(t.length===1){this.queryStats.indexHits++;let h=this.componentIndexManager.query(t[0]);n=Array.from(h)}else{let h=this.componentIndexManager.queryMultiple(t,"AND");n=Array.from(h)}return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}queryMultipleComponents(t){let e=null,s=1/0;for(let r of t){let h=this.entityIndex.byComponentType.get(r);if(!h||h.size===0)return[];h.size<s&&(s=h.size,e=h)}if(!e)return this.queryStats.linearScans++,this.queryByLinearScan(t);let i=this.createComponentMask(t),n=[];for(let r of e)(r.componentMask&i)===i&&n.push(r);return n}queryByLinearScan(t){let e=this.createComponentMask(t);return this.entities.filter(s=>(s.componentMask&e)===e)}queryAny(...t){let e=performance.now();this.queryStats.totalQueries++;let s=`any:${t.map(h=>h.name).sort().join(",")}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};let n=this.archetypeSystem.queryArchetypes(t,"OR"),r;if(n.archetypes.length>0){this.queryStats.archetypeHits++,r=[];for(let h of n.archetypes)r.push(...h.entities)}else{let h=this.componentIndexManager.queryMultiple(t,"OR");r=Array.from(h)}return this.addToCache(s,r),{entities:r,count:r.length,executionTime:performance.now()-e,fromCache:!1}}queryNone(...t){let e=performance.now();this.queryStats.totalQueries++;let s=`none:${t.map(h=>h.name).sort().join(",")}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};let n=this.createComponentMask(t),r=this.entities.filter(h=>(h.componentMask&n)===BigInt(0));return this.addToCache(s,r),{entities:r,count:r.length,executionTime:performance.now()-e,fromCache:!1}}queryByTag(t){let e=performance.now();this.queryStats.totalQueries++;let s=`tag:${t}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};this.queryStats.indexHits++;let n=Array.from(this.entityIndex.byTag.get(t)||[]);return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}queryByName(t){let e=performance.now();this.queryStats.totalQueries++;let s=`name:${t}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};this.queryStats.indexHits++;let n=Array.from(this.entityIndex.byName.get(t)||[]);return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}queryByComponent(t){let e=performance.now();this.queryStats.totalQueries++;let s=`component:${t.name}`,i=this.getFromCache(s);if(i)return this.queryStats.cacheHits++,{entities:i,count:i.length,executionTime:performance.now()-e,fromCache:!0};this.queryStats.indexHits++;let n=Array.from(this.entityIndex.byComponentType.get(t)||[]);return this.addToCache(s,n),{entities:n,count:n.length,executionTime:performance.now()-e,fromCache:!1}}getFromCache(t){let e=this.queryCache.get(t);return e?Date.now()-e.timestamp>this.cacheTimeout?(this.queryCache.delete(t),null):(e.hitCount++,e.entities):null}addToCache(t,e){this.queryCache.size>=this.cacheMaxSize&&this.cleanupCache(),this.queryCache.set(t,{entities:[...e],timestamp:Date.now(),hitCount:0})}cleanupCache(){let t=Date.now();for(let[e,s]of this.queryCache.entries())t-s.timestamp>this.cacheTimeout&&this.queryCache.delete(e);if(this.queryCache.size>=this.cacheMaxSize){let e=Array.from(this.queryCache.entries());e.sort((i,n)=>i[1].hitCount-n[1].hitCount);let s=Math.floor(this.cacheMaxSize*.2);for(let i=0;i<s&&i<e.length;i++)this.queryCache.delete(e[i][0])}}clearQueryCache(){this.queryCache.clear()}clearCache(){this.clearQueryCache()}batchUpdateComponents(t){if(this.wasmAvailable&&t.length>100)try{let e=t.map(i=>i.entityId),s=t.map(i=>i.componentMask);S.batchUpdateMasks(e,s),console.log(`WebAssembly\u52A0\u901F\u6279\u91CF\u66F4\u65B0 ${t.length} \u4E2A\u5B9E\u4F53`)}catch(e){console.warn("WebAssembly\u6279\u91CF\u66F4\u65B0\u5931\u8D25\uFF0C\u56DE\u9000\u5230JavaScript\u5B9E\u73B0:",e),this.batchUpdateComponentsJS(t)}else this.batchUpdateComponentsJS(t);this.clearQueryCache()}batchUpdateComponentsJS(t){for(let e of t)this.entities.find(i=>i.id===e.entityId)&&console.log(`\u66F4\u65B0\u5B9E\u4F53 ${e.entityId} \u7684\u7EC4\u4EF6\u63A9\u7801: ${e.componentMask}`);this.rebuildIndexes()}getAccelerationStatus(){return{wasmEnabled:this.wasmAvailable,currentProvider:this.wasmAvailable?"hybrid":"javascript",availableProviders:["javascript","hybrid"],performanceInfo:{entityCount:this.entities.length,wasmEnabled:this.wasmAvailable,cacheStats:{size:this.queryCache.size,hitRate:this.queryStats.totalQueries>0?(this.queryStats.cacheHits/this.queryStats.totalQueries*100).toFixed(2)+"%":"0%"}}}}async switchAccelerationProvider(t){return!0}createComponentMask(t){let e=t.map(s=>s.name);for(let s of e)this.bitMaskOptimizer.registerComponentType(s);return this.bitMaskOptimizer.createCombinedMask(e)}getStats(){return{entityCount:this.entities.length,indexStats:{maskIndexSize:this.entityIndex.byMask.size,componentIndexSize:this.entityIndex.byComponentType.size,tagIndexSize:this.entityIndex.byTag.size,nameIndexSize:this.entityIndex.byName.size},accelerationStatus:this.getAccelerationStatus(),queryStats:{...this.queryStats,cacheHitRate:this.queryStats.totalQueries>0?(this.queryStats.cacheHits/this.queryStats.totalQueries*100).toFixed(2)+"%":"0%"},optimizationStats:{componentIndex:this.componentIndexManager.getStats(),archetypeSystem:this.archetypeSystem.getAllArchetypes().map(t=>({id:t.id,componentTypes:t.componentTypes.map(e=>e.name),entityCount:t.entities.length})),dirtyTracking:this.dirtyTrackingSystem.getStats()}}}switchComponentIndexType(t){this.componentIndexManager.switchIndexType(t)}configureDirtyTracking(t,e){this.dirtyTrackingSystem.configureBatchProcessing(t,e)}optimizePerformance(){this.dirtyTrackingSystem.processDirtyEntities(),this.cleanupCache();let t=this.componentIndexManager.getStats();t.avgQueryTime>2&&t.type!==d.HASH?this.switchComponentIndexType(d.HASH):t.memoryUsage>50*1024*1024&&t.type!==d.BITMAP&&this.switchComponentIndexType(d.BITMAP)}beginFrame(){this.dirtyTrackingSystem.beginFrame()}endFrame(){this.dirtyTrackingSystem.endFrame()}markEntityDirty(t,e){this.queryStats.dirtyChecks++,this.dirtyTrackingSystem.markDirty(t,y.COMPONENT_MODIFIED,e),this.clearQueryCache()}getEntityArchetype(t){return this.archetypeSystem.getEntityArchetype(t)}};a(ee,"QuerySystem");var it=ee,se=class se{constructor(t){this.conditions=[],this.querySystem=t}withAll(...t){return this.conditions.push({type:M.ALL,componentTypes:t,mask:this.createComponentMask(t)}),this}withAny(...t){return this.conditions.push({type:M.ANY,componentTypes:t,mask:this.createComponentMask(t)}),this}without(...t){return this.conditions.push({type:M.NONE,componentTypes:t,mask:this.createComponentMask(t)}),this}execute(){let t=performance.now();if(this.conditions.length===1){let e=this.conditions[0];switch(e.type){case M.ALL:return this.querySystem.queryAll(...e.componentTypes);case M.ANY:return this.querySystem.queryAny(...e.componentTypes);case M.NONE:return this.querySystem.queryNone(...e.componentTypes)}}return{entities:[],count:0,executionTime:performance.now()-t,fromCache:!1}}createComponentMask(t){let e=BigInt(0);for(let s of t)try{let i=p.getBitMask(s);e|=i}catch{console.warn(`\u7EC4\u4EF6\u7C7B\u578B ${s.name} \u672A\u6CE8\u518C\uFF0C\u8DF3\u8FC7`)}return e}reset(){return this.conditions=[],this}};a(se,"QueryBuilder");var _t=se;var ie=class ie{constructor(){this.listeners=new Map,this.stats=new Map,this.batchQueue=new Map,this.batchTimers=new Map,this.batchConfigs=new Map,this.nextListenerId=0,this.isEnabled=!0,this.maxListeners=100}on(t,e,s={}){return this.addListener(t,e,s)}once(t,e,s={}){return this.addListener(t,e,{...s,once:!0})}onAsync(t,e,s={}){return this.addListener(t,e,{...s,async:!0})}off(t,e){let s=this.listeners.get(t);if(!s)return!1;let i=s.findIndex(n=>n.id===e);return i===-1?!1:(s.splice(i,1),s.length===0&&(this.listeners.delete(t),this.stats.delete(t)),!0)}offAll(t){this.listeners.delete(t),this.stats.delete(t),this.clearBatch(t)}async emit(t,e){if(!this.isEnabled)return;if(this.batchConfigs.get(t)?.enabled){this.addToBatch(t,e);return}await this.executeEvent(t,e)}emitSync(t,e){if(!this.isEnabled)return;let s=this.listeners.get(t);if(!s||s.length===0)return;let i=performance.now(),n=[],r=this.sortListenersByPriority(s);for(let h of r)if(!h.config.async)try{h.config.context?h.handler.call(h.config.context,e):h.handler(e),h.config.once&&n.push(h.id)}catch(c){console.error(`Error in event handler for ${t}:`,c)}this.removeListeners(t,n),this.updateStats(t,performance.now()-i)}setBatchConfig(t,e){this.batchConfigs.set(t,e)}flushBatch(t){let e=this.batchQueue.get(t);if(!e||e.length===0)return;let s=this.batchTimers.get(t);s&&(clearTimeout(s),this.batchTimers.delete(t)),this.processBatch(t,e),this.batchQueue.delete(t)}getStats(t){return t?this.stats.get(t)||this.createEmptyStats(t):new Map(this.stats)}resetStats(t){t?this.stats.delete(t):this.stats.clear()}setEnabled(t){this.isEnabled=t}hasListeners(t){let e=this.listeners.get(t);return e?e.length>0:!1}getListenerCount(t){let e=this.listeners.get(t);return e?e.length:0}clear(){this.listeners.clear(),this.stats.clear(),this.clearAllBatches()}setMaxListeners(t){this.maxListeners=t}addListener(t,e,s){let i=this.listeners.get(t);if(i||(i=[],this.listeners.set(t,i)),i.length>=this.maxListeners)return console.warn(`Maximum listeners (${this.maxListeners}) exceeded for event type: ${t}`),"";let n=`listener_${this.nextListenerId++}`,r={handler:e,config:{priority:0,...s},id:n};return i.push(r),this.stats.has(t)||this.stats.set(t,this.createEmptyStats(t)),n}async executeEvent(t,e){let s=this.listeners.get(t);if(!s||s.length===0)return;let i=performance.now(),n=[],r=this.sortListenersByPriority(s),h=r.filter(m=>!m.config.async),c=r.filter(m=>m.config.async);for(let m of h)try{m.config.context?m.handler.call(m.config.context,e):m.handler(e),m.config.once&&n.push(m.id)}catch(F){console.error(`Error in sync event handler for ${t}:`,F)}let u=c.map(async m=>{try{m.config.context?await m.handler.call(m.config.context,e):await m.handler(e),m.config.once&&n.push(m.id)}catch(F){console.error(`Error in async event handler for ${t}:`,F)}});await Promise.all(u),this.removeListeners(t,n),this.updateStats(t,performance.now()-i)}sortListenersByPriority(t){return t.slice().sort((e,s)=>(s.config.priority||0)-(e.config.priority||0))}removeListeners(t,e){if(e.length===0)return;let s=this.listeners.get(t);if(s){for(let i of e){let n=s.findIndex(r=>r.id===i);n!==-1&&s.splice(n,1)}s.length===0&&(this.listeners.delete(t),this.stats.delete(t))}}addToBatch(t,e){let s=this.batchQueue.get(t);s||(s=[],this.batchQueue.set(t,s)),s.push(e);let i=this.batchConfigs.get(t);if(s.length>=i.batchSize){this.flushBatch(t);return}if(!this.batchTimers.has(t)){let n=setTimeout(()=>{this.flushBatch(t)},i.delay);this.batchTimers.set(t,n)}}async processBatch(t,e){let s={type:t,events:e,count:e.length,timestamp:Date.now()};await this.executeEvent(`${t}:batch`,s)}clearBatch(t){this.batchQueue.delete(t);let e=this.batchTimers.get(t);e&&(clearTimeout(e),this.batchTimers.delete(t))}clearAllBatches(){this.batchQueue.clear();for(let t of this.batchTimers.values())clearTimeout(t);this.batchTimers.clear(),this.batchConfigs.clear()}updateStats(t,e){let s=this.stats.get(t);s||(s=this.createEmptyStats(t),this.stats.set(t,s)),s.triggerCount++,s.totalExecutionTime+=e,s.averageExecutionTime=s.totalExecutionTime/s.triggerCount,s.lastTriggerTime=Date.now(),s.listenerCount=this.getListenerCount(t)}createEmptyStats(t){return{eventType:t,listenerCount:0,triggerCount:0,totalExecutionTime:0,averageExecutionTime:0,lastTriggerTime:0}}};a(ie,"TypeSafeEventSystem");var w=ie,Xs=new w;var ne=class ne{get systems(){return this.entityProcessors.processors}constructor(t=!0){this.name="",this._didSceneBegin=!1,this.entities=new X(this),this.entityProcessors=new tt,this.identifierPool=new N,this.componentStorageManager=new dt,this.querySystem=new it,this.eventSystem=new w,this.initialize()}initialize(){}onStart(){}unload(){}begin(){this.entityProcessors!=null&&this.entityProcessors.begin(),this._didSceneBegin=!0,this.onStart()}end(){this._didSceneBegin=!1,this.entities.removeAllEntities(),this.componentStorageManager.clear(),this.entityProcessors&&this.entityProcessors.end(),this.unload()}update(){this.entities.updateLists(),this.entityProcessors!=null&&this.entityProcessors.update(),this.entities.update(),this.entityProcessors!=null&&this.entityProcessors.lateUpdate()}createEntity(t){let e=new g(t,this.identifierPool.checkOut());return this.addEntity(e)}addEntity(t,e=!1){return this.entities.add(t),t.scene=this,this.querySystem.addEntity(t,e),this.eventSystem.emitSync("entity:added",{entity:t,scene:this}),t}createEntities(t,e="Entity"){let s=[];for(let i=0;i<t;i++){let n=new g(`${e}_${i}`,this.identifierPool.checkOut());n.scene=this,s.push(n)}for(let i of s)this.entities.add(i);return this.querySystem.addEntitiesUnchecked(s),this.eventSystem.emitSync("entities:batch_added",{entities:s,scene:this,count:t}),s}createEntitiesOld(t,e="Entity"){let s=[];for(let i=0;i<t;i++){let n=new g(`${e}_${i}`,this.identifierPool.checkOut());s.push(n),this.addEntity(n,!0)}return this.querySystem.clearCache(),s}destroyAllEntities(){for(let t=0;t<this.entities.count;t++)this.entities.buffer[t].destroy()}findEntity(t){return this.entities.findEntity(t)}findEntityById(t){return this.entities.findEntityById(t)}findEntitiesByTag(t){let e=[];for(let s of this.entities.buffer)s.tag===t&&e.push(s);return e}getEntityByName(t){return this.findEntity(t)}getEntitiesByTag(t){return this.findEntitiesByTag(t)}addEntityProcessor(t){return t.scene=this,this.entityProcessors.add(t),t.setUpdateOrder(this.entityProcessors.count-1),t}addSystem(t){return this.addEntityProcessor(t)}removeEntityProcessor(t){this.entityProcessors.remove(t)}getEntityProcessor(t){return this.entityProcessors.getProcessor(t)}getStats(){return{entityCount:this.entities.count,processorCount:this.entityProcessors.count,componentStorageStats:this.componentStorageManager.getAllStats()}}compactComponentStorage(){this.componentStorageManager.compactAll()}getDebugInfo(){return{name:this.constructor.name,entityCount:this.entities.count,processorCount:this.entityProcessors.count,isRunning:this._didSceneBegin,entities:this.entities.buffer.map(t=>({name:t.name,id:t.id,componentCount:t.components.length,componentTypes:t.components.map(e=>e.constructor.name)})),processors:this.entityProcessors.processors.map(t=>({name:t.constructor.name,updateOrder:t.updateOrder,entityCount:t._entities?.length||0})),componentStats:this.componentStorageManager.getAllStats()}}};a(ne,"Scene");var nt=ne;var Ct=class Ct{constructor(t,e){this.scene=t,this.storageManager=e,this.entity=new g("",t.identifierPool.checkOut())}named(t){return this.entity.name=t,this}tagged(t){return this.entity.tag=t,this}with(t){return this.entity.addComponent(t),this}withComponents(...t){for(let e of t)this.entity.addComponent(e);return this}withIf(t,e){return t&&this.entity.addComponent(e),this}withFactory(t){let e=t();return this.entity.addComponent(e),this}configure(t,e){let s=this.entity.getComponent(t);return s&&e(s),this}enabled(t=!0){return this.entity.enabled=t,this}active(t=!0){return this.entity.active=t,this}withChild(t){let e=t.build();return this.entity.addChild(e),this}withChildren(...t){for(let e of t){let s=e.build();this.entity.addChild(s)}return this}withChildFactory(t){let s=t(this.entity).build();return this.entity.addChild(s),this}withChildIf(t,e){if(t){let s=e.build();this.entity.addChild(s)}return this}build(){return this.entity}spawn(){return this.scene.addEntity(this.entity),this.entity}clone(){let t=new Ct(this.scene,this.storageManager);return t.entity=this.entity,t}};a(Ct,"EntityBuilder");var Et=Ct,ce=class ce{constructor(){this.scene=new nt}named(t){return this.scene.name=t,this}withEntity(t){return this.scene.addEntity(t),this}withEntityBuilder(t){let e=new Et(this.scene,this.scene.componentStorageManager),i=t(e).build();return this.scene.addEntity(i),this}withEntities(...t){for(let e of t)this.scene.addEntity(e);return this}withSystem(t){return this.scene.addSystem(t),this}withSystems(...t){for(let e of t)this.scene.addSystem(e);return this}build(){return this.scene}};a(ce,"SceneBuilder");var oe=ce,le=class le{constructor(t,...e){this.component=new t(...e)}set(t,e){return this.component[t]=e,this}configure(t){return t(this.component),this}setIf(t,e,s){return t&&(this.component[e]=s),this}build(){return this.component}};a(le,"ComponentBuilder");var re=le,me=class me{constructor(t,e,s){this.scene=t,this.querySystem=e,this.eventSystem=s}createEntity(){return new Et(this.scene,this.scene.componentStorageManager)}createScene(){return new oe}createComponent(t,...e){return new re(t,...e)}query(){return new _t(this.querySystem)}find(...t){return this.querySystem.queryAll(...t).entities}findFirst(...t){let e=this.querySystem.queryAll(...t);return e.entities.length>0?e.entities[0]:null}findByName(t){return this.scene.getEntityByName(t)}findByTag(t){return this.scene.getEntitiesByTag(t)}emit(t,e){this.eventSystem.emitSync(t,e)}async emitAsync(t,e){await this.eventSystem.emit(t,e)}on(t,e){return this.eventSystem.on(t,e)}once(t,e){return this.eventSystem.once(t,e)}off(t,e){this.eventSystem.off(t,e)}batch(t){return new ae(t)}getStats(){return{entityCount:this.scene.entities.count,systemCount:this.scene.systems.length,componentStats:this.scene.componentStorageManager.getAllStats(),queryStats:this.querySystem.getStats(),eventStats:this.eventSystem.getStats()}}};a(me,"ECSFluentAPI");var Tt=me,St=class St{constructor(t){this.entities=t}addComponent(t){for(let e of this.entities)e.addComponent(t);return this}removeComponent(t){for(let e of this.entities)e.removeComponentByType(t);return this}setActive(t){for(let e of this.entities)e.active=t;return this}setTag(t){for(let e of this.entities)e.tag=t;return this}forEach(t){return this.entities.forEach(t),this}filter(t){return new St(this.entities.filter(t))}toArray(){return this.entities.slice()}count(){return this.entities.length}};a(St,"EntityBatchOperator");var ae=St;function he(o,t,e){return new Tt(o,t,e)}a(he,"createECSAPI");var _=class _{constructor(t=!0,e=!0){this._nextScene=null,this._globalManagers=[],_._instance=this,_.emitter=new G,_.emitter.addObserver(b.frameUpdated,this.update,this),this._timerManager=new J,_.registerGlobalManager(this._timerManager),this._performanceMonitor=H.instance,this._poolManager=lt.getInstance(),_.entitySystemsEnabled=e,this.debug=t,this.initialize()}static get Instance(){return this._instance}static get scene(){return this._instance&&this._instance._scene||null}static set scene(t){if(t){if(!t)throw new Error("\u573A\u666F\u4E0D\u80FD\u4E3A\u7A7A");this._instance._scene==null?(this._instance._scene=t,this._instance.onSceneChanged(),this._instance._scene.begin()):this._instance._nextScene=t}}static create(t=!0){return this._instance==null&&(this._instance=new _(t)),this._instance}static registerGlobalManager(t){this._instance._globalManagers.push(t),t.enabled=!0}static unregisterGlobalManager(t){this._instance._globalManagers.splice(this._instance._globalManagers.indexOf(t),1),t.enabled=!1}static getGlobalManager(t){for(let e of this._instance._globalManagers)if(e instanceof t)return e;return null}static schedule(t,e=!1,s=null,i){return this._instance._timerManager.schedule(t,e,s,i)}static get ecsAPI(){return this._instance?._ecsAPI||null}onSceneChanged(){if(f.sceneChanged(),this._scene&&typeof this._scene.querySystem<"u"){let t=this._scene;this._ecsAPI=he(t,t.querySystem,t.eventSystem)}}initialize(){}update(t=-1){if(_.paused)return;let e=this._performanceMonitor.startMonitoring("Core.update");f.update(t),typeof this._performanceMonitor.updateFPS=="function"&&this._performanceMonitor.updateFPS(f.deltaTime);let s=this._performanceMonitor.startMonitoring("GlobalManagers.update");for(let i of this._globalManagers)i.enabled&&i.update();if(this._performanceMonitor.endMonitoring("GlobalManagers.update",s,this._globalManagers.length),this._poolManager.update(),this._nextScene!=null&&(this._scene!=null&&this._scene.end(),this._scene=this._nextScene,this._nextScene=null,this.onSceneChanged(),this._scene.begin()),this._scene!=null&&this._scene.update){let i=this._performanceMonitor.startMonitoring("Scene.update");this._scene.update();let n=this._scene.entities?.count||0;this._performanceMonitor.endMonitoring("Scene.update",i,n)}this._performanceMonitor.endMonitoring("Core.update",e)}};a(_,"Core");var xt=_;xt.paused=!1;var wt=class wt{constructor(){this._enabled=!0,this._updateOrder=0,this.id=wt._idGenerator++}get enabled(){return this.entity?this.entity.enabled&&this._enabled:this._enabled}set enabled(t){this._enabled!==t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled())}get updateOrder(){return this._updateOrder}set updateOrder(t){this._updateOrder=t}onAddedToEntity(){}onRemovedFromEntity(){}onEnabled(){}onDisabled(){}update(){}};a(wt,"Component");var Mt=wt;Mt._idGenerator=0;var E=class E{constructor(){this._words=[],this._words=[]}set(t){let e=Math.floor(t/E.WORD_SIZE),s=t%E.WORD_SIZE;for(;this._words.length<=e;)this._words.push(0);this._words[e]|=1<<s}clear(t){let e=Math.floor(t/E.WORD_SIZE),s=t%E.WORD_SIZE;e<this._words.length&&(this._words[e]&=~(1<<s))}get(t){let e=Math.floor(t/E.WORD_SIZE),s=t%E.WORD_SIZE;return e>=this._words.length?!1:(this._words[e]&1<<s)!==0}containsAll(t){let e=Math.max(this._words.length,t._words.length);for(let s=0;s<e;s++){let i=s<this._words.length?this._words[s]:0,n=s<t._words.length?t._words[s]:0;if((i&n)!==n)return!1}return!0}intersects(t){let e=Math.min(this._words.length,t._words.length);for(let s=0;s<e;s++)if((this._words[s]&t._words[s])!==0)return!0;return!1}excludes(t){return!this.intersects(t)}clearAll(){this._words.length=0}isEmpty(){for(let t of this._words)if(t!==0)return!1;return!0}cardinality(){let t=0;for(let e of this._words)t+=this.popCount(e);return t}popCount(t){return t=t-(t>>>1&1431655765),t=(t&858993459)+(t>>>2&858993459),(t+(t>>>4)&252645135)*16843009>>>24}copyFrom(t){this._words=[...t._words]}clone(){let t=new E;return t.copyFrom(this),t}};a(E,"Bits");var U=E;U.WORD_SIZE=32;var q=class q{static get instance(){return q._instance||(q._instance=new q),q._instance}constructor(){this._componentTypes=new Map,this._typeNames=new Map,this._nextTypeId=0}getTypeId(t){let e=this._componentTypes.get(t);return e===void 0&&(e=this._nextTypeId++,this._componentTypes.set(t,e),this._typeNames.set(e,t.name)),e}getTypeName(t){return this._typeNames.get(t)||"Unknown"}createBits(...t){let e=new U;for(let s of t){let i=this.getTypeId(s);e.set(i)}return e}getEntityBits(t){let e=new U;for(let s of t){let i=this.getTypeId(s.constructor);e.set(i)}return e}reset(){this._componentTypes.clear(),this._typeNames.clear(),this._nextTypeId=0}get registeredTypeCount(){return this._componentTypes.size}};a(q,"ComponentTypeManager");var Q=q;var bt=class bt{constructor(){this.allSet=[],this.exclusionSet=[],this.oneSet=[],this._isDirty=!0}static empty(){return new bt}getAllSet(){return this.allSet}getExclusionSet(){return this.exclusionSet}getOneSet(){return this.oneSet}isInterestedEntity(t){let e=this.getEntityBits(t);return this.isInterested(e)}isInterested(t){return this.updateBitsIfDirty(),!(this._allBits&&!t.containsAll(this._allBits)||this._exclusionBits&&t.intersects(this._exclusionBits)||this._oneBits&&!t.intersects(this._oneBits))}all(...t){return this.allSet.push(...t),this._isDirty=!0,this}exclude(...t){return this.exclusionSet.push(...t),this._isDirty=!0,this}one(...t){return this.oneSet.push(...t),this._isDirty=!0,this}getEntityBits(t){let e=t.components;return Q.instance.getEntityBits(e)}updateBitsIfDirty(){if(!this._isDirty)return;let t=Q.instance;this.allSet.length>0?this._allBits=t.createBits(...this.allSet):this._allBits=void 0,this.exclusionSet.length>0?this._exclusionBits=t.createBits(...this.exclusionSet):this._exclusionBits=void 0,this.oneSet.length>0?this._oneBits=t.createBits(...this.oneSet):this._oneBits=void 0,this._isDirty=!1}toString(){let t=[];return this.allSet.length>0&&t.push(`all: [${this.allSet.map(e=>e.name).join(", ")}]`),this.exclusionSet.length>0&&t.push(`exclude: [${this.exclusionSet.map(e=>e.name).join(", ")}]`),this.oneSet.length>0&&t.push(`one: [${this.oneSet.map(e=>e.name).join(", ")}]`),`Matcher(${t.join(", ")})`}};a(bt,"Matcher");var ot=bt;var ue=class ue{get entities(){return this._entities}get updateOrder(){return this._updateOrder}set updateOrder(t){this.setUpdateOrder(t)}get enabled(){return this._enabled}set enabled(t){this._enabled=t}get systemName(){return this._systemName}constructor(t){this._entities=[],this._updateOrder=0,this._enabled=!0,this._performanceMonitor=H.instance,this._matcher=t||ot.empty(),this._systemName=this.constructor.name,this.initialize()}get scene(){return this._scene}set scene(t){this._scene=t,this._entities=[]}get matcher(){return this._matcher}setUpdateOrder(t){this._updateOrder=t,this.scene.entityProcessors.setDirty()}initialize(){}onChanged(t){let e=this._entities.includes(t),s=this._matcher.isInterestedEntity(t);s&&!e?this.add(t):!s&&e&&this.remove(t)}add(t){this._entities.includes(t)||(this._entities.push(t),this.onAdded(t))}onAdded(t){}remove(t){let e=this._entities.indexOf(t);e!==-1&&(this._entities.splice(e,1),this.onRemoved(t))}onRemoved(t){}update(){if(!this._enabled||!this.checkProcessing())return;let t=this._performanceMonitor.startMonitoring(this._systemName);try{this.begin(),this.process(this._entities)}finally{this._performanceMonitor.endMonitoring(this._systemName,t,this._entities.length)}}lateUpdate(){if(!this._enabled||!this.checkProcessing())return;let t=this._performanceMonitor.startMonitoring(`${this._systemName}_Late`);try{this.lateProcess(this._entities),this.end()}finally{this._performanceMonitor.endMonitoring(`${this._systemName}_Late`,t,this._entities.length)}}begin(){}process(t){}lateProcess(t){}end(){}checkProcessing(){return!0}getPerformanceData(){return this._performanceMonitor.getSystemData(this._systemName)}getPerformanceStats(){return this._performanceMonitor.getSystemStats(this._systemName)}resetPerformanceData(){this._performanceMonitor.resetSystem(this._systemName)}toString(){let t=this._entities.length,e=this.getPerformanceData(),s=e?` (${e.executionTime.toFixed(2)}ms)`:"";return`${this._systemName}[${t} entities]${s}`}};a(ue,"EntitySystem");var T=ue;var pe=class pe extends T{onChanged(t){}process(t){this.processSystem()}};a(pe,"ProcessingSystem");var de=pe;var ye=class ye extends T{onChanged(t){}process(t){}};a(ye,"PassiveSystem");var fe=ye;var _e=class _e extends T{constructor(t,e){super(t),this.acc=0,this.intervalRemainder=0,this.interval=e}checkProcessing(){return this.acc+=f.deltaTime,this.acc>=this.interval?(this.intervalRemainder=this.acc-this.interval,this.acc=0,!0):!1}getIntervalDelta(){return this.interval+this.intervalRemainder}};a(_e,"IntervalSystem");var ge=_e;var Ee=class Ee{constructor(t=!1){this.eventIdCounter=0,this.isDebugMode=!1,this.eventSystem=new w,this.isDebugMode=t}emit(t,e){this.validateEventType(t);let s=this.enhanceEventData(t,e);this.isDebugMode&&console.log(`[EventBus] Emitting event: ${t}`,s),this.eventSystem.emitSync(t,s)}async emitAsync(t,e){this.validateEventType(t);let s=this.enhanceEventData(t,e);this.isDebugMode&&console.log(`[EventBus] Emitting async event: ${t}`,s),await this.eventSystem.emit(t,s)}on(t,e,s={}){this.validateEventType(t);let i={once:s.once||!1,priority:s.priority||V.NORMAL,async:s.async||!1,context:s.context};return this.isDebugMode&&console.log(`[EventBus] Adding listener for: ${t}`,i),this.eventSystem.on(t,e,i)}once(t,e,s={}){return this.on(t,e,{...s,once:!0})}onAsync(t,e,s={}){return this.on(t,e,{...s,async:!0})}off(t,e){return this.isDebugMode&&console.log(`[EventBus] Removing listener: ${e} for event: ${t}`),this.eventSystem.off(t,e)}offAll(t){this.isDebugMode&&console.log(`[EventBus] Removing all listeners for event: ${t}`),this.eventSystem.offAll(t)}hasListeners(t){return this.eventSystem.hasListeners(t)}getStats(t){let e=this.eventSystem.getStats(t);if(e instanceof Map){let s=new Map;return e.forEach((i,n)=>{s.set(n,this.convertEventStats(i))}),s}else return this.convertEventStats(e)}clear(){this.isDebugMode&&console.log("[EventBus] Clearing all listeners"),this.eventSystem.clear()}setEnabled(t){this.eventSystem.setEnabled(t)}setDebugMode(t){this.isDebugMode=t}setMaxListeners(t){this.eventSystem.setMaxListeners(t)}getListenerCount(t){return this.eventSystem.getListenerCount(t)}setBatchConfig(t,e,s){this.eventSystem.setBatchConfig(t,{batchSize:e,delay:s,enabled:!0})}flushBatch(t){this.eventSystem.flushBatch(t)}resetStats(t){this.eventSystem.resetStats(t)}emitEntityCreated(t){this.emit(l.ENTITY_CREATED,t)}emitEntityDestroyed(t){this.emit(l.ENTITY_DESTROYED,t)}emitComponentAdded(t){this.emit(l.COMPONENT_ADDED,t)}emitComponentRemoved(t){this.emit(l.COMPONENT_REMOVED,t)}emitSystemAdded(t){this.emit(l.SYSTEM_ADDED,t)}emitSystemRemoved(t){this.emit(l.SYSTEM_REMOVED,t)}emitSceneChanged(t){this.emit(C.CORE.SCENE_CHANGED,t)}emitPerformanceWarning(t){this.emit(l.PERFORMANCE_WARNING,t)}onEntityCreated(t,e){return this.on(l.ENTITY_CREATED,t,e)}onComponentAdded(t,e){return this.on(l.COMPONENT_ADDED,t,e)}onSystemError(t,e){return this.on(l.SYSTEM_ERROR,t,e)}onPerformanceWarning(t,e){return this.on(l.PERFORMANCE_WARNING,t,e)}validateEventType(t){I.isValid(t)||(this.isDebugMode&&console.warn(`[EventBus] Unknown event type: ${t}`),this.isDebugMode&&I.addCustomType(t))}enhanceEventData(t,e){let s=e;return s.timestamp||(s.timestamp=Date.now()),s.eventId||(s.eventId=`${t}_${++this.eventIdCounter}`),s.source||(s.source="EventBus"),s}convertEventStats(t){return{eventType:t.eventType,listenerCount:t.listenerCount,triggerCount:t.triggerCount,totalExecutionTime:t.totalExecutionTime,averageExecutionTime:t.averageExecutionTime,lastTriggerTime:t.lastTriggerTime}}};a(Ee,"EventBus");var L=Ee,Te=class Te{static getInstance(t=!1){return this.instance||(this.instance=new L(t)),this.instance}static reset(t=!1){return this.instance&&this.instance.clear(),this.instance=new L(t),this.instance}};a(Te,"GlobalEventBus");var rt=Te;function ke(o,t={}){return function(e,s,i){let n=i.value,r=e.constructor.prototype.initEventListeners||function(){};return e.constructor.prototype.initEventListeners=function(){r.call(this),rt.getInstance().on(o,n.bind(this),t)},i}}a(ke,"EventHandler");function Re(o,t={}){return function(e,s,i){let n=i.value,r=e.constructor.prototype.initEventListeners||function(){};return e.constructor.prototype.initEventListeners=function(){r.call(this),rt.getInstance().onAsync(o,n.bind(this),t)},i}}a(Re,"AsyncEventHandler");var Ce=class Ce{constructor(t){this.entityManager=t,this._allComponents=[],this._anyComponents=[],this._withoutComponents=[],this._withTags=[],this._withoutTags=[],this._activeOnly=!1,this._enabledOnly=!1,this._customPredicates=[]}withAll(...t){return this._allComponents.push(...t),this}withAny(...t){return this._anyComponents.push(...t),this}without(...t){return this._withoutComponents.push(...t),this}withTag(t){return this._withTags.push(t),this}withoutTag(t){return this._withoutTags.push(t),this}active(){return this._activeOnly=!0,this}enabled(){return this._enabledOnly=!0,this}where(t){return this._customPredicates.push(t),this}execute(){let t=[];if(this._allComponents.length>0){let e=this.entityManager.queryWithComponentIndex(this._allComponents,"AND");t=Array.from(e)}else if(this._anyComponents.length>0){let e=this.entityManager.queryWithComponentIndex(this._anyComponents,"OR");t=Array.from(e)}else t=this.entityManager.getAllEntities();return t.filter(e=>this.matchesEntity(e))}first(){return this.entityManager.getAllEntities().find(e=>this.matchesEntity(e))||null}count(){return this.entityManager.getAllEntities().filter(e=>this.matchesEntity(e)).length}forEach(t){this.entityManager.getAllEntities().forEach(s=>{this.matchesEntity(s)&&t(s)})}matchesEntity(t){if(this._activeOnly&&!t.active||this._enabledOnly&&!t.enabled)return!1;if(this._allComponents.length>0){for(let e of this._allComponents)if(!t.hasComponent(e))return!1}if(this._anyComponents.length>0){let e=!1;for(let s of this._anyComponents)if(t.hasComponent(s)){e=!0;break}if(!e)return!1}if(this._withoutComponents.length>0){for(let e of this._withoutComponents)if(t.hasComponent(e))return!1}if(this._withTags.length>0&&!this._withTags.includes(t.tag)||this._withoutTags.length>0&&this._withoutTags.includes(t.tag))return!1;if(this._customPredicates.length>0){for(let e of this._customPredicates)if(!e(t))return!1}return!0}};a(Ce,"EntityQueryBuilder");var at=Ce,Se=class Se{constructor(){this._entities=new Map,this._entitiesByName=new Map,this._entitiesByTag=new Map,this._destroyedEntities=new Set,this._identifierPool=new N,this._componentIndexManager=new B(d.HASH),this._archetypeSystem=new z,this._dirtyTrackingSystem=new P,this._eventBus=new L(!1),g.eventBus=this._eventBus}get entityCount(){return this._entities.size}get activeEntityCount(){let t=0;for(let e of this._entities.values())e.active&&!e.isDestroyed&&t++;return t}createEntity(t=`Entity_${Date.now()}`){let e=this._identifierPool.checkOut(),s=new g(t,e);return this._entities.set(e,s),this.updateNameIndex(s,!0),this.updateTagIndex(s,!0),this._componentIndexManager.addEntity(s),this._archetypeSystem.addEntity(s),this._dirtyTrackingSystem.markDirty(s,y.COMPONENT_ADDED),this._eventBus.emitEntityCreated({timestamp:Date.now(),source:"EntityManager",entityId:s.id,entityName:s.name,entityTag:s.tag?.toString()}),s}destroyEntity(t){let e=null;return typeof t=="string"?e=this.getEntityByName(t):typeof t=="number"?e=this._entities.get(t)||null:e=this._entities.get(t.id)||null,e?(this._destroyedEntities.add(e.id),this.updateNameIndex(e,!1),this.updateTagIndex(e,!1),this._componentIndexManager.removeEntity(e),this._archetypeSystem.removeEntity(e),this._dirtyTrackingSystem.markDirty(e,y.COMPONENT_REMOVED),this._eventBus.emitEntityDestroyed({timestamp:Date.now(),source:"EntityManager",entityId:e.id,entityName:e.name,entityTag:e.tag?.toString()}),e.destroy(),this._entities.delete(e.id),this._identifierPool.checkIn(e.id),!0):!1}getAllEntities(){return Array.from(this._entities.values())}getEntity(t){let e=typeof t=="string"?parseInt(t):t;return this._entities.get(e)||null}getEntityByName(t){let e=this._entitiesByName.get(t);return e&&e.length>0?e[0]:null}getEntitiesByTag(t){return[...this._entitiesByTag.get(t)||[]]}getEntitiesWithComponent(t){let e=this._componentIndexManager.query(t);return Array.from(e)}query(){return new at(this)}queryWithComponentIndex(t,e){return this._componentIndexManager.queryMultiple(t,e)}markEntityDirty(t,e){this._dirtyTrackingSystem.markDirty(t,y.COMPONENT_MODIFIED,e)}getOptimizationStats(){return{componentIndex:this._componentIndexManager.getStats(),archetypeSystem:this._archetypeSystem.getAllArchetypes().map(t=>({id:t.id,componentTypes:t.componentTypes.map(e=>e.name),entityCount:t.entities.length})),dirtyTracking:this._dirtyTrackingSystem.getStats()}}get eventBus(){return this._eventBus}updateNameIndex(t,e){if(t.name)if(e){let s=this._entitiesByName.get(t.name);s||(s=[],this._entitiesByName.set(t.name,s)),s.push(t)}else{let s=this._entitiesByName.get(t.name);if(s){let i=s.indexOf(t);i!==-1&&(s.splice(i,1),s.length===0&&this._entitiesByName.delete(t.name))}}}updateTagIndex(t,e){if(e){let s=this._entitiesByTag.get(t.tag);s||(s=[],this._entitiesByTag.set(t.tag,s)),s.push(t)}else{let s=this._entitiesByTag.get(t.tag);if(s){let i=s.indexOf(t);i!==-1&&(s.splice(i,1),s.length===0&&this._entitiesByTag.delete(t.tag))}}}};a(Se,"EntityManager");var It=Se;var Me=class Me{static getType(t){return t.constructor}};a(Me,"TypeUtils");var xe=Me;var be=class be{static toNumber(t){return t==null?0:Number(t)}};a(be,"NumberExtension");var we=be;var De;(function(o){o[o.Error=0]="Error",o[o.Assert=1]="Assert",o[o.Warning=2]="Warning",o[o.Log=3]="Log",o[o.Exception=4]="Exception"})(De||(De={}));var Oe;(function(o){o[o.Position=1]="Position",o[o.Scale=2]="Scale",o[o.Rotation=4]="Rotation"})(Oe||(Oe={}));export{z as ArchetypeSystem,Re as AsyncEventHandler,et as BitMaskOptimizer,gt as BitmapComponentIndex,U as Bits,Mt as Component,B as ComponentIndexManager,ft as ComponentPool,ut as ComponentStorage,Oe as ComponentTransform,Q as ComponentTypeManager,xt as Core,b as CoreEvents,y as DirtyFlag,P as DirtyTrackingSystem,l as ECSEventType,Tt as ECSFluentAPI,C as EVENT_TYPES,G as Emitter,g as Entity,X as EntityList,It as EntityManager,tt as EntityProcessorList,at as EntityQueryBuilder,T as EntitySystem,L as EventBus,ke as EventHandler,V as EventPriority,I as EventTypeValidator,ht as FuncPack,rt as GlobalEventBus,K as GlobalManager,Y as HashComponentIndex,N as IdentifierPool,d as IndexType,st as IndexUpdateBatcher,ge as IntervalSystem,W as JavaScriptFallback,De as LogType,ot as Matcher,we as NumberExtension,fe as PassiveSystem,H as PerformanceMonitor,Ie as PerformanceWarningType,O as Pool,lt as PoolManager,de as ProcessingSystem,it as QuerySystem,nt as Scene,ct as TieredObjectPool,f as Time,Z as Timer,J as TimerManager,w as TypeSafeEventSystem,xe as TypeUtils,j as WasmEcsCore,$ as WasmLoader,he as createECSAPI,S as ecsCore,Ae as initializeEcs};
7
7
  //# sourceMappingURL=index.js.map