@eva/plugin-renderer-tilemap 2.1.0-beta.1 → 2.1.0-beta.10

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.
@@ -1 +1 @@
1
- function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},_extends.apply(null,arguments)}globalThis.EVA=globalThis.EVA||{},globalThis.EVA.plugin=globalThis.EVA.plugin||{},globalThis.EVA.plugin.renderer=globalThis.EVA.plugin.renderer||{};var _EVA_IIFE_tilemap=function(e,t,n,r){"use strict";function o(e,t,n,r){var o,i=arguments.length,s=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(s=(i<3?o(s):i>3?o(t,n,s):o(t,n))||s);return i>3&&s&&Object.defineProperty(t,n,s),s}function i(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,n,r){return new(n||(n=Promise))(function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function l(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(s,l)}a((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class l extends Error{constructor(){super("Symbol keys are not supported yet!"),Object.setPrototypeOf(this,new.target.prototype)}}const a="IDE_PROPERTY_METADATA";function c(e,t,n,r){let o=Reflect.getMetadata("design:type",e,t),i=o===Array;const s=function(e){return e===String?"string":e===Number?"number":e===Boolean?"boolean":"unknown"}(o);if("unknown"!==s&&(o=s),r){const e=r();Array.isArray(e)?(i=!0,o=e[0]):o=e}const l=Reflect.getMetadata(a,e.constructor)||{},c=_extends(_extends(_extends({},l[t]||{}),{type:o,isArray:i}),n);l[t]=c,Reflect.defineMetadata(a,l,e.constructor);!function(e,t,n){const r=e.constructor,o=r.IDEProps||{};o[t]=_extends(_extends({key:t},o[t]),n),r.IDEProps=o}(e,t,function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(c,["isArray"]))}function d(e){return t={type:e},(e,r)=>{if("symbol"==typeof r)throw new l;const{options:o,returnTypeFunc:i}=function(e,t){return"function"==typeof e?{returnTypeFunc:e,options:t||{}}:{options:e||{}}}(t,n);c(e,r,o,i)};var t,n}var u;!function(e){e[e.Edit=2]="Edit",e[e.Game=4]="Game",e[e.All=6]="All"}(u||(u={}));class p extends t.Component{constructor(){super(...arguments),this.tileset="",this.tileWidth=32,this.tileHeight=32,this.tilesetSpacing=0,this.tilesetMargin=0,this.layers=[]}init(e){e&&_extends(this,e)}}p.componentName="Tilemap",o([d("string"),i("design:type",String)],p.prototype,"tileset",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tileWidth",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tileHeight",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tilesetSpacing",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tilesetMargin",void 0);let f=class extends n.Renderer{constructor(){super(...arguments),this.name="Tilemap",this.records={}}init(){this.renderSystem=this.game.getSystem(n.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){}componentChanged(e){var n,o,i;return s(this,void 0,void 0,function*(){if("Tilemap"!==e.componentName)return;const s=e.component,l=e.gameObject.id;if(e.type===t.OBSERVER_TYPE.ADD){const n=this.increaseAsyncId(l);let o=null;if(s.tileset){const{instance:r}=yield t.resource.getResource(s.tileset);if(!this.validateAsyncId(l,n))return;if(!r)return void console.error(`GameObject:${e.gameObject.name}'s Tilemap tileset load error`);o=r}if(!o)return;const i=new r.Container,a=this.containerManager.getContainer(l);a&&a.addChildAt(i,0);const c={root:i,layerContainers:[],frameTextures:[],baseTexture:o};this.records[l]=c,this.buildLayers(c,s)}else if(e.type===t.OBSERVER_TYPE.CHANGE){const r=this.records[l];if(!r)return;if("tileset"===(null===(o=null===(n=e.prop)||void 0===n?void 0:n.prop)||void 0===o?void 0:o[0])){const e=this.increaseAsyncId(l),{instance:n}=yield t.resource.getResource(s.tileset);if(!this.validateAsyncId(l,e))return;if(!n)return;this.tearDownChildren(r),r.baseTexture=n,this.buildLayers(r,s)}}else if(e.type===t.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(l);const e=this.records[l];if(!e)return;this.tearDownChildren(e);const t=null===(i=this.containerManager)||void 0===i?void 0:i.getContainer(l);t&&t.removeChild(e.root),e.root.destroy({children:!0}),delete this.records[l]}})}tearDownChildren(e){for(const t of e.layerContainers)e.root.removeChild(t),t.destroy({children:!0});e.layerContainers=[];for(const t of e.frameTextures)try{t.destroy(!1)}catch(e){}e.frameTextures=[]}buildLayers(e,t){var n,o,i,s,l,a,c,d,u,p;const f=e.baseTexture;if(!f)return;const h=t.tileWidth||32,y=t.tileHeight||32,g=null!==(n=t.renderTileWidth)&&void 0!==n?n:h,m=null!==(o=t.renderTileHeight)&&void 0!==o?o:y,v=t.tilesetMargin||0,b=t.tilesetSpacing||0,E=null!==(u=null!==(c=null!==(a=null!==(s=null===(i=f.orig)||void 0===i?void 0:i.width)&&void 0!==s?s:null===(l=f.source)||void 0===l?void 0:l.width)&&void 0!==a?a:f.width)&&void 0!==c?c:null===(d=f.frame)||void 0===d?void 0:d.width)&&void 0!==u?u:0,T=E>0?Math.max(1,Math.floor((E-v+b)/(h+b))):1,A=t.tilesetColumns&&t.tilesetColumns>0?t.tilesetColumns:T,w=new Map,x=t=>{var n,o;if(t<=0)return null;const i=t-1,s=w.get(i);if(s)return s;const l=i%A,a=Math.floor(i/A),c=v+l*(h+b),d=v+a*(y+b);try{const t=null!==(o=null!==(n=f.source)&&void 0!==n?n:f.baseTexture)&&void 0!==o?o:f,s=new r.Texture({source:t,frame:new r.Rectangle(c,d,h,y)});return w.set(i,s),e.frameTextures.push(s),s}catch(e){return"undefined"!=typeof console&&console.warn("[Tilemap] Texture slice failed, falling back to base",e),f}};for(const n of t.layers||[]){const t=new r.Container;t.label=null!==(p=n.name)&&void 0!==p?p:"tilemap-layer";const o=n.offsetX||0,i=n.offsetY||0;null!=n.alpha&&(t.alpha=n.alpha),!1===n.visible&&(t.visible=!1);const s=n.data||[],l=n.tint;for(let e=0;e<s.length;e++){const n=s[e];if(n)for(let s=0;s<n.length;s++){const a=n[s];if(!a||a<=0)continue;const c=x(a);if(!c)continue;const d=new r.Sprite(c);d.x=o+s*h,d.y=i+e*y,d.width=g,d.height=m,null!=l&&(d.tint=l),t.addChild(d)}}e.root.addChild(t),e.layerContainers.push(t)}}destroy(){var e;for(const t in this.records){const n=parseInt(t),r=this.records[n];this.tearDownChildren(r);const o=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(n);o&&o.removeChild(r.root),r.root.destroy({children:!0}),delete this.records[n]}}};f.systemName="Tilemap",f=o([t.decorators.componentObserver({Tilemap:[{prop:["tileset"],deep:!1}]})],f);var h=f;return e.Tilemap=p,e.TilemapSystem=h,Object.defineProperty(e,"__esModule",{value:!0}),e}({},EVA,EVA.plugin.renderer,PIXI);globalThis.EVA.plugin.renderer.tilemap=globalThis.EVA.plugin.renderer.tilemap||_EVA_IIFE_tilemap;
1
+ function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},_extends.apply(null,arguments)}globalThis.EVA=globalThis.EVA||{},globalThis.EVA.plugin=globalThis.EVA.plugin||{},globalThis.EVA.plugin.renderer=globalThis.EVA.plugin.renderer||{};var _EVA_IIFE_tilemap=function(e,t,n,i){"use strict";function o(e,t,n,i){var o,r=arguments.length,s=r<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,i);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(s=(r<3?o(s):r>3?o(t,n,s):o(t,n))||s);return r>3&&s&&Object.defineProperty(t,n,s),s}function r(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,n,i){return new(n||(n=Promise))(function(o,r){function s(e){try{a(i.next(e))}catch(e){r(e)}}function l(e){try{a(i.throw(e))}catch(e){r(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(s,l)}a((i=i.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;class l extends Error{constructor(){super("Symbol keys are not supported yet!"),Object.setPrototypeOf(this,new.target.prototype)}}const a="IDE_PROPERTY_METADATA";function c(e,t,n,i){let o=Reflect.getMetadata("design:type",e,t),r=o===Array;const s=function(e){return e===String?"string":e===Number?"number":e===Boolean?"boolean":"unknown"}(o);if("unknown"!==s&&(o=s),i){const e=i();Array.isArray(e)?(r=!0,o=e[0]):o=e}const l=Reflect.getMetadata(a,e.constructor)||{},c=_extends(_extends(_extends({},l[t]||{}),{type:o,isArray:r}),n);l[t]=c,Reflect.defineMetadata(a,l,e.constructor);!function(e,t,n){const i=e.constructor,o=i.IDEProps||{};o[t]=_extends(_extends({key:t},o[t]),n),i.IDEProps=o}(e,t,function(e,t){var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++)t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}return n}(c,["isArray"]))}function d(e){return t={type:e},(e,i)=>{if("symbol"==typeof i)throw new l;const{options:o,returnTypeFunc:r}=function(e,t){return"function"==typeof e?{returnTypeFunc:e,options:t||{}}:{options:e||{}}}(t,n);c(e,i,o,r)};var t,n}var u;!function(e){e[e.Edit=2]="Edit",e[e.Game=4]="Game",e[e.All=6]="All"}(u||(u={}));class h extends t.Component{constructor(){super(...arguments),this.tileset="",this.tileWidth=32,this.tileHeight=32,this.tilesetSpacing=0,this.tilesetMargin=0,this.layers=[],this.tilemapRef="",this.layersV2=void 0}init(e){e&&_extends(this,e)}}h.componentName="Tilemap",o([d("string"),r("design:type",String)],h.prototype,"tileset",void 0),o([d("number"),r("design:type",Number)],h.prototype,"tileWidth",void 0),o([d("number"),r("design:type",Number)],h.prototype,"tileHeight",void 0),o([d("number"),r("design:type",Number)],h.prototype,"tilesetSpacing",void 0),o([d("number"),r("design:type",Number)],h.prototype,"tilesetMargin",void 0),o([d("string"),r("design:type",String)],h.prototype,"tilemapRef",void 0);const f=16;function p(e){return{sourceSlot:255&e,col:e>>>8&255,row:e>>>16&255,altIdx:e>>>24&31,flipH:1==(e>>>29&1),flipV:1==(e>>>30&1),transpose:1==(e>>>31&1)}}function m(e){return!(255&e)}function y(e){const t=function(e){if("function"==typeof atob){const t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}const t=globalThis.Buffer;if(t)return new Uint8Array(t.from(e,"base64"));const n=e.replace(/[^A-Za-z0-9+/]/g,""),i=e.endsWith("==")?2:e.endsWith("=")?1:0,o=Math.floor(3*n.length/4)-i,r=new Uint8Array(o);let s=0;for(let e=0;e<n.length;e+=4){const t=v.indexOf(n[e])<<18|v.indexOf(n[e+1])<<12|(n[e+2]?v.indexOf(n[e+2]):0)<<6|(n[e+3]?v.indexOf(n[e+3]):0);s<o&&(r[s++]=t>>16&255),s<o&&(r[s++]=t>>8&255),s<o&&(r[s++]=255&t)}return r}(e.blob);if(1024!==t.byteLength)throw new Error(`decodeChunk: expected 1024 bytes, got ${t.byteLength}`);const n=new Uint8Array(t.byteLength);return n.set(t),new Int32Array(n.buffer)}const v="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function g(e){var t,n,i,o,r,s,l,a,c,d,u,h,f,p,m,y;const v=new Map,g=[],T=new Map;for(let C=0;C<e.sources.length;C++){const b=e.sources[C];if(g.push(b),v.set(b.id,C+1),"atlas"===b.kind&&b.textureAsset){const e=T.get(b.textureAsset);if(e){const v=e.regionSize.width===b.regionSize.width&&e.regionSize.height===b.regionSize.height,g=(null!==(n=null===(t=e.margins)||void 0===t?void 0:t.x)&&void 0!==n?n:0)===(null!==(o=null===(i=b.margins)||void 0===i?void 0:i.x)&&void 0!==o?o:0)&&(null!==(s=null===(r=e.margins)||void 0===r?void 0:r.y)&&void 0!==s?s:0)===(null!==(a=null===(l=b.margins)||void 0===l?void 0:l.y)&&void 0!==a?a:0),T=(null!==(d=null===(c=e.separation)||void 0===c?void 0:c.x)&&void 0!==d?d:0)===(null!==(h=null===(u=b.separation)||void 0===u?void 0:u.x)&&void 0!==h?h:0)&&(null!==(p=null===(f=e.separation)||void 0===f?void 0:f.y)&&void 0!==p?p:0)===(null!==(y=null===(m=b.separation)||void 0===m?void 0:m.y)&&void 0!==y?y:0);v&&g&&T||"undefined"!=typeof console&&console.warn(`[Tilemap] Atlas sources '${e.sourceId}' and '${b.id}' share textureAsset='${b.textureAsset}' but differ in regionSize/margins/separation. Frame texture cache may collide and render wrong frames. Use distinct textureAssets per atlas source.`)}else T.set(b.textureAsset,{regionSize:b.regionSize,margins:b.margins,separation:b.separation,sourceId:b.id})}}return{raw:e,sourcesBySlot:g,slotByIdMap:v,tileWidth:e.tileSize.width,tileHeight:e.tileSize.height}}class T{constructor(){this.animations=[],this.lastFrameIdxByKey=new Map}loadFromTileset(e){var t;this.animations=[],this.lastFrameIdxByKey.clear();for(let n=0;n<e.sources.length;n++){const i=e.sources[n];if("atlas"!==i.kind)continue;const o=n+1;for(const e of i.tiles){const n=e.animation;if(!n||!n.frames||0===n.frames.length)continue;const i=n.frames.map(e=>{var t;return{col:e.atlasCoords.col,row:e.atlasCoords.row,durationMs:n.stepMs*(null!==(t=e.durationFactor)&&void 0!==t?t:1)}}),r=i.reduce((e,t)=>e+t.durationMs,0);if(r<=0)continue;const s="randomStart"===n.phase?C(o,e.atlasCoords.col,e.atlasCoords.row,r):0;this.animations.push({sourceSlot:o,col:e.atlasCoords.col,row:e.atlasCoords.row,frames:i,totalDurationMs:r,phase:null!==(t=n.phase)&&void 0!==t?t:"sync",phaseOffsetMs:s})}}}get animationCount(){return this.animations.length}isAnimatedSource(e,t,n){for(const i of this.animations)if(i.sourceSlot===e&&i.col===t&&i.row===n)return!0;return!1}advance(e){const t=new Map,n=new Set;for(const i of this.animations){const o=`${i.sourceSlot},${i.col},${i.row}`,r=((e+i.phaseOffsetMs)%i.totalDurationMs+i.totalDurationMs)%i.totalDurationMs;let s=0,l=0;for(;l<i.frames.length&&(s+=i.frames[l].durationMs,!(r<s));l++);l>=i.frames.length&&(l=i.frames.length-1);const a=i.frames[l];t.set(o,{col:a.col,row:a.row});this.lastFrameIdxByKey.get(o)!==l&&(n.add(o),this.lastFrameIdxByKey.set(o,l))}return{currentFrames:t,dirtyKeys:n}}reset(){this.lastFrameIdxByKey.clear()}}function C(e,t,n,i){let o=73856093*e^19349663*t^83492791*n;o=1274126177*(o^o>>>13),o^=o>>>16;const r=(o>>>0)/4294967295;return Math.floor(r*i)}const b={DIRTY_REBUILD_MS:"tilemap.dirtyRebuild.ms",CULL_CHECK_MS:"tilemap.cullCheck.ms",ANIM_TICK_MS:"tilemap.animTick.ms",DRAWCALLS_COUNT:"tilemap.drawcalls.count",DRAWCALLS_BY_ATLAS:"tilemap.drawcalls.byAtlas",PHYSICS_REBAKE_MS:"tilemap.physicsRebake.ms",GPU_UPLOAD_MS:"tilemap.gpuUpload.ms",GPU_UPLOAD_BYTES:"tilemap.gpuUpload.bytes",AUTOTILE_MS:"tilemap.autotile.ms",PATCH_APPLY_MS:"tilemap.patchApply.ms",DIRTY_PENDING:"tilemap.dirty.pending",DIRTY_FRAMES_BEHIND:"tilemap.dirty.framesBehind",BODIES_TOTAL:"tilemap.bodies.total",BODIES_CREATED:"tilemap.bodies.created",BODIES_DESTROYED:"tilemap.bodies.destroyed",STRATEGY_SPRITE_COUNT:"tilemap.strategy.sprite.count",STRATEGY_MESH_COUNT:"tilemap.strategy.mesh.count",STRATEGY_MESH_FALLBACK_COUNT:"tilemap.strategy.meshFallback.count",SCENECOLLECTION_SKIPPED_COUNT:"tilemap.sceneCollection.skipped.count",CULL_HITS_COUNT:"tilemap.cull.hits",MODE_SWITCH_FAILED_COUNT:"tilemap.modeSwitch.failed",ANIM_TICK_ERROR_COUNT:"tilemap.animTick.error.count",CONTEXT_LOST_COUNT:"tilemap.context.lost"};function x(){return"undefined"!=typeof performance&&performance.now?performance.now():0}function w(e){var t,n,i,o;const r=null==e?void 0:e.perfProbes;return r?{beginTiming:null!==(t=r.beginTiming)&&void 0!==t?t:()=>{},endTiming:null!==(n=r.endTiming)&&void 0!==n?n:()=>{},count:null!==(i=r.count)&&void 0!==i?i:()=>{},gauge:null!==(o=r.gauge)&&void 0!==o?o:()=>{},histogram:r.histogram}:null}function S(e){return e.preference&&"auto"!==e.preference?e.preference:e.atlasesInChunk>1?"sprite":e.nonEmptyCellsInChunk>=192?"mesh":"sprite"}let E=class extends n.Renderer{constructor(){super(...arguments),this.name="Tilemap",this.records={},this.animationDrivers=new Map,this.probes=null,this.isHidden=!1,this.contextLost=!1,this.canvasWithCtxListeners=null}init(){this.renderSystem=this.game.getSystem(n.RendererSystem),this.renderSystem.rendererManager.register(this),this.probes=w(this.game),this.installVisibilityHandlers()}installVisibilityHandlers(){var e,t,n;if("undefined"!=typeof document){try{this.visibilityListener=()=>{this.isHidden=!!document.hidden},document.addEventListener("visibilitychange",this.visibilityListener)}catch(e){}try{const i=null!==(n=null===(t=null===(e=this.renderSystem)||void 0===e?void 0:e.application)||void 0===t?void 0:t.canvas)&&void 0!==n?n:null;i&&"function"==typeof i.addEventListener&&(this.contextLostListener=()=>{this.contextLost=!0,this.probeCount(b.CONTEXT_LOST_COUNT,1),"undefined"!=typeof console&&console.warn("[Tilemap] WebGL context lost — pausing animation tick")},this.contextRestoredListener=()=>{this.contextLost=!1,"undefined"!=typeof console&&console.info("[Tilemap] WebGL context restored — resuming animation tick")},i.addEventListener("webglcontextlost",this.contextLostListener),i.addEventListener("webglcontextrestored",this.contextRestoredListener),this.canvasWithCtxListeners=i)}catch(e){}}}uninstallVisibilityHandlers(){if("undefined"!=typeof document&&this.visibilityListener){try{document.removeEventListener("visibilitychange",this.visibilityListener)}catch(e){}this.visibilityListener=void 0}if(this.canvasWithCtxListeners){try{this.contextLostListener&&this.canvasWithCtxListeners.removeEventListener("webglcontextlost",this.contextLostListener),this.contextRestoredListener&&this.canvasWithCtxListeners.removeEventListener("webglcontextrestored",this.contextRestoredListener)}catch(e){}this.contextLostListener=void 0,this.contextRestoredListener=void 0,this.canvasWithCtxListeners=null}}attachPerfProbes(e){this.probes=e}setCullingBoundsHint(e,t){const n=this.records[e];n&&(n.cullingBoundsHint=null===t?void 0:t)}invalidateChunks(e,t,n){const i=this.records[e];if(!i||0===n.length)return;let o=i.dirtyChunkKeys.get(t);o||(o=new Set,i.dirtyChunkKeys.set(t,o));for(const e of n)o.add(e);let r=0;for(const e of i.dirtyChunkKeys.values())r+=e.size;if(this.probeGauge(b.DIRTY_PENDING,r),!i.flushScheduled){i.flushScheduled=!0;("undefined"!=typeof requestAnimationFrame?requestAnimationFrame:e=>setTimeout(()=>e(0),16))(()=>this.flushDirtyChunks(e))}}flushDirtyChunks(e){var t,n,o,r,s,l,a,c,d,u,h,f,p,m;const y=this.records[e];if(!y)return;if(y.flushScheduled=!1,0===y.dirtyChunkKeys.size)return;if("v2"!==y.mode||!y.loadedTileset)return y.dirtyChunkKeys.clear(),void this.probeGauge(b.DIRTY_PENDING,0);this.probeCount(b.DIRTY_FRAMES_BEHIND,1);const v=y.loadedTileset,g=null!==(t=this.animationDrivers.get(e))&&void 0!==t?t:null,T=this.game;let C=null;if(null==T?void 0:T.gameObjects)for(const t of T.gameObjects){if(t.id!==e)continue;const i=null===(n=t.components)||void 0===n?void 0:n.find(e=>"Tilemap"===(null==e?void 0:e.name));if(i){C=i;break}}const x=null!==(r=null===(o=null==C?void 0:C.cellSize)||void 0===o?void 0:o.width)&&void 0!==r?r:v.tileWidth,w=null!==(l=null===(s=null==C?void 0:C.cellSize)||void 0===s?void 0:s.height)&&void 0!==l?l:v.tileHeight,S=null!==(c=null===(a=null==C?void 0:C.mapOrigin)||void 0===a?void 0:a.x)&&void 0!==c?c:0,E=null!==(u=null===(d=null==C?void 0:C.mapOrigin)||void 0===d?void 0:d.y)&&void 0!==u?u:0;this.probeBegin(b.DIRTY_REBUILD_MS);try{for(const[e,t]of y.dirtyChunkKeys){const n=y.layerContainersV2.get(e);if(!n)continue;const o=null!==(f=null===(h=null==C?void 0:C.layersV2)||void 0===h?void 0:h.find(t=>t.id===e))&&void 0!==f?f:null;if(o)for(const e of t){const t=n.chunkContainers.get(e);if(t){n.container.removeChild(t);try{t.destroy({children:!0})}catch(e){}n.chunkContainers.delete(e)}const r=null===(m=null===(p=o.cellData)||void 0===p?void 0:p.chunks)||void 0===m?void 0:m[e];if(!r||r.nonEmpty<=0)continue;const s=new i.Container;s.label=`chunk-${e}`,this.populateChunkSprites(s,e,r,y,v,x,w,S,E,o,g),n.container.addChild(s),n.chunkContainers.set(e,s)}}}finally{this.probeEnd(b.DIRTY_REBUILD_MS)}y.dirtyChunkKeys.clear(),this.probeGauge(b.DIRTY_PENDING,0),this.requestRedraw()}probeBegin(e){var t;null===(t=this.probes)||void 0===t||t.beginTiming(e)}probeEnd(e){var t;null===(t=this.probes)||void 0===t||t.endTiming(e)}probeCount(e,t=1){var n;null===(n=this.probes)||void 0===n||n.count(e,t)}probeGauge(e,t){var n;null===(n=this.probes)||void 0===n||n.gauge(e,t)}rendererUpdate(e){}update(e){if(this.isHidden||this.contextLost)return;if(0===this.animationDrivers.size)return;this.probeBegin(b.ANIM_TICK_MS);let t=0;try{const e="undefined"!=typeof performance&&performance.now?performance.now():0;for(const[n,i]of this.animationDrivers){const o=this.records[n];if(o&&"v2"===o.mode)try{const n=i.advance(e);if(0===n.dirtyKeys.size)continue;const r=o.loadedTileset;if(!r)continue;let s=!1;for(const e of n.dirtyKeys){const i=o.animatedSpritesByAnimKey.get(e);if(!i||0===i.length)continue;const l=n.currentFrames.get(e);if(!l)continue;const a=e.split(","),c=Number.parseInt(a[0],10),d=this.getAtlasFrameTexture(o,r,{sourceSlot:c,col:l.col,row:l.row});if(d)for(const e of i)e.texture=d,s=!0,t++}s&&this.requestRedraw()}catch(e){this.probeCount(b.ANIM_TICK_ERROR_COUNT,1),"undefined"!=typeof console&&console.error(`[Tilemap] animation tick failed for entity ${n}`,e)}}}finally{this.probeEnd(b.ANIM_TICK_MS),t>0&&this.probeCount("tilemap.anim.spritesSwapped",t)}}componentChanged(e){return s(this,void 0,void 0,function*(){if("Tilemap"!==e.componentName)return;const n=e.component,i=e.gameObject.id;e.type===t.OBSERVER_TYPE.ADD?yield this.handleAdd(i,e.gameObject,n):e.type===t.OBSERVER_TYPE.CHANGE?yield this.handleChange(i,n,e):e.type===t.OBSERVER_TYPE.REMOVE&&this.handleRemove(i)})}handleAdd(e,t,n){return s(this,void 0,void 0,function*(){this.probeBegin(b.DIRTY_REBUILD_MS);try{const o=this.increaseAsyncId(e),r=this.detectMode(n),s=new i.Container,l=this.containerManager.getContainer(e);l&&l.addChildAt(s,0);const a={root:s,layerContainers:[],layerContainersV2:new Map,frameTextures:[],frameTexturesV2:new Map,baseTexture:null,atlasTextures:new Map,loadedTileset:null,mode:r,animatedSpritesByAnimKey:new Map,sceneCollectionWarnedSourceIds:new Set,dirtyChunkKeys:new Map,flushScheduled:!1};this.records[e]=a,"v1"===r?yield this.ensureV1Built(a,e,t,n,o):"v2"===r&&(yield this.ensureV2Built(a,e,t,n,o))}finally{this.probeEnd(b.DIRTY_REBUILD_MS)}})}ensureV1Built(e,t,n,i,o){return s(this,void 0,void 0,function*(){const r=yield this.loadV1Texture(i);this.validateAsyncId(t,o)&&(r?(e.baseTexture=r,this.buildLayersV1(e,i),this.requestRedraw()):"undefined"!=typeof console&&console.error(`GameObject:${n.name}'s Tilemap tileset load error`))})}ensureV2Built(e,t,n,i,o){return s(this,void 0,void 0,function*(){const r=yield this.loadV2Tileset(i);if(!this.validateAsyncId(t,o))return;if(!r)return void("undefined"!=typeof console&&console.error(`GameObject:${n.name}'s Tilemap tilemapRef load error`));if(e.loadedTileset=r,yield this.loadAtlasTextures(e,r),!this.validateAsyncId(t,o))return;const s=new T;s.loadFromTileset(r.raw);const l=s.animationCount>0?s:null;l&&this.animationDrivers.set(t,l),this.buildLayersV2(e,i,l),this.requestRedraw()})}requestRedraw(){var e;try{const t=null===(e=this.renderSystem)||void 0===e?void 0:e.application;(null==t?void 0:t.renderer)&&t.stage&&t.renderer.render(t.stage)}catch(e){"undefined"!=typeof console&&console.warn("[Tilemap] requestRedraw failed",e)}}handleChange(e,t,n){var i,o;return s(this,void 0,void 0,function*(){const r=this.records[e];if(!r)return;const s=null===(o=null===(i=n.prop)||void 0===i?void 0:i.prop)||void 0===o?void 0:o[0];this.probeBegin(b.PATCH_APPLY_MS);try{const i=this.detectMode(t);if(i!==r.mode){const o=this.increaseAsyncId(e);try{if("v1"===r.mode?this.tearDownChildrenV1(r):"v2"===r.mode&&(this.tearDownChildrenV2(r),this.animationDrivers.delete(e)),r.baseTexture=null,r.loadedTileset=null,"v1"===i){const i=yield this.loadV1Texture(t);if(!this.validateAsyncId(e,o))return;if(!i)return r.mode="unknown",this.probeCount(b.MODE_SWITCH_FAILED_COUNT,1),void("undefined"!=typeof console&&console.error(`GameObject:${n.gameObject.name}'s Tilemap mode-switch v→v1 failed (tileset load returned null)`));r.mode="v1",r.baseTexture=i,this.buildLayersV1(r,t),this.requestRedraw()}else if("v2"===i){const i=yield this.loadV2Tileset(t);if(!this.validateAsyncId(e,o))return;if(!i)return r.mode="unknown",this.probeCount(b.MODE_SWITCH_FAILED_COUNT,1),void("undefined"!=typeof console&&console.error(`GameObject:${n.gameObject.name}'s Tilemap mode-switch v→v2 failed (tilemapRef load returned null)`));if(yield this.loadAtlasTextures(r,i),!this.validateAsyncId(e,o))return;r.mode="v2",r.loadedTileset=i;const s=new T;s.loadFromTileset(i.raw);const l=s.animationCount>0?s:null;l&&this.animationDrivers.set(e,l),this.buildLayersV2(r,t,l),this.requestRedraw()}else r.mode="unknown"}catch(e){r.mode="unknown",this.probeCount(b.MODE_SWITCH_FAILED_COUNT,1),"undefined"!=typeof console&&console.error("[Tilemap] mode switch threw",e)}return}if("tileset"===s&&"v1"===r.mode){const n=this.increaseAsyncId(e),i=yield this.loadV1Texture(t);if(!this.validateAsyncId(e,n))return;if(!i)return;this.tearDownChildrenV1(r),r.baseTexture=i,this.buildLayersV1(r,t),this.requestRedraw()}else if("tilemapRef"===s&&"v2"===r.mode){const n=this.increaseAsyncId(e),i=yield this.loadV2Tileset(t);if(!this.validateAsyncId(e,n))return;if(!i)return;if(this.tearDownChildrenV2(r),r.loadedTileset=i,yield this.loadAtlasTextures(r,i),!this.validateAsyncId(e,n))return;this.animationDrivers.delete(e);const o=new T;o.loadFromTileset(i.raw);const s=o.animationCount>0?o:null;s&&this.animationDrivers.set(e,s),this.buildLayersV2(r,t,s),this.requestRedraw()}else if("layersV2"===s&&"v2"===r.mode){this.tearDownChildrenV2(r),this.animationDrivers.delete(e),r.dirtyChunkKeys.clear();const n=r.loadedTileset;let i=null;if(n){const t=new T;t.loadFromTileset(n.raw),t.animationCount>0&&(i=t,this.animationDrivers.set(e,t))}this.buildLayersV2(r,t,i),this.requestRedraw()}}finally{this.probeEnd(b.PATCH_APPLY_MS)}})}handleRemove(e){var t;this.increaseAsyncId(e);const n=this.records[e];if(!n)return;this.tearDownChildrenV1(n),this.tearDownChildrenV2(n);const i=null===(t=this.containerManager)||void 0===t?void 0:t.getContainer(e);i&&i.removeChild(n.root),n.root.destroy({children:!0}),delete this.records[e],this.animationDrivers.delete(e),this.probeGauge("tilemap.records.alive",Object.keys(this.records).length)}detectMode(e){return e.tilemapRef?"v2":e.tileset?"v1":"unknown"}loadV1Texture(e){var n;return s(this,void 0,void 0,function*(){if(!e.tileset)return null;const{instance:i}=yield t.resource.getResource(e.tileset);return null!==(n=i)&&void 0!==n?n:null})}tearDownChildrenV1(e){for(const t of e.layerContainers)e.root.removeChild(t),t.destroy({children:!0});e.layerContainers=[];for(const t of e.frameTextures)try{t.destroy(!1)}catch(e){}e.frameTextures=[]}buildLayersV1(e,t){var n,o,r,s,l,a,c,d,u,h;const f=e.baseTexture;if(!f)return;const p=t.tileWidth||32,m=t.tileHeight||32,y=null!==(n=t.renderTileWidth)&&void 0!==n?n:p,v=null!==(o=t.renderTileHeight)&&void 0!==o?o:m,g=t.tilesetMargin||0,T=t.tilesetSpacing||0,C=null!==(u=null!==(c=null!==(a=null!==(s=null===(r=f.orig)||void 0===r?void 0:r.width)&&void 0!==s?s:null===(l=f.source)||void 0===l?void 0:l.width)&&void 0!==a?a:f.width)&&void 0!==c?c:null===(d=f.frame)||void 0===d?void 0:d.width)&&void 0!==u?u:0,b=C>0?Math.max(1,Math.floor((C-g+T)/(p+T))):1,x=t.tilesetColumns&&t.tilesetColumns>0?t.tilesetColumns:b,w=new Map,S=t=>{var n,o;if(t<=0)return null;const r=t-1,s=w.get(r);if(s)return s;const l=r%x,a=Math.floor(r/x),c=g+l*(p+T),d=g+a*(m+T);try{const t=null!==(o=null!==(n=f.source)&&void 0!==n?n:f.baseTexture)&&void 0!==o?o:f,s=new i.Texture({source:t,frame:new i.Rectangle(c,d,p,m)});return w.set(r,s),e.frameTextures.push(s),s}catch(e){return"undefined"!=typeof console&&console.warn("[Tilemap] Texture slice failed, falling back to base",e),f}};for(const n of t.layers||[]){const t=new i.Container;t.label=null!==(h=n.name)&&void 0!==h?h:"tilemap-layer";const o=n.offsetX||0,r=n.offsetY||0;null!=n.alpha&&(t.alpha=n.alpha),!1===n.visible&&(t.visible=!1);const s=n.data||[],l=n.tint;for(let e=0;e<s.length;e++){const n=s[e];if(n)for(let s=0;s<n.length;s++){const a=n[s];if(!a||a<=0)continue;const c=S(a);if(!c)continue;const d=new i.Sprite(c);d.x=o+s*p,d.y=r+e*m,d.width=y,d.height=v,null!=l&&(d.tint=l),t.addChild(d)}}e.root.addChild(t),e.layerContainers.push(t)}}loadV2Tileset(e){var n,i;return s(this,void 0,void 0,function*(){if(!e.tilemapRef)return null;const o=yield t.resource.getResource(e.tilemapRef);if(!o)return null;const r=null!==(n=o.instance)&&void 0!==n?n:null===(i=o.data)||void 0===i?void 0:i.json;return r&&"tileset"===r.kind?g(r):("undefined"!=typeof console&&console.error(`[Tilemap] resource '${e.tilemapRef}' is not a TileSet document`),null)})}loadAtlasTextures(e,n){return s(this,void 0,void 0,function*(){const i=new Set;for(const e of n.sourcesBySlot)"atlas"===e.kind&&e.textureAsset&&i.add(e.textureAsset);yield Promise.all(Array.from(i).map(n=>s(this,void 0,void 0,function*(){var i,o,r;try{const s=yield t.resource.getResource(n),l=null!==(r=null!==(i=null==s?void 0:s.instance)&&void 0!==i?i:null===(o=null==s?void 0:s.data)||void 0===o?void 0:o.image)&&void 0!==r?r:null;l&&e.atlasTextures.set(n,l)}catch(e){"undefined"!=typeof console&&console.warn(`[Tilemap] failed to load atlas asset '${n}'`,e)}})))})}tearDownChildrenV2(e){for(const{container:t}of e.layerContainersV2.values())e.root.removeChild(t),t.destroy({children:!0});e.layerContainersV2.clear();for(const t of e.frameTexturesV2.values())try{t.destroy(!1)}catch(e){}e.frameTexturesV2.clear(),e.animatedSpritesByAnimKey.clear()}buildLayersV2(e,t,n){var o,r,s,l,a,c,d,u,h,f,p;const m=e.loadedTileset;if(!m)return;const y=null!==(r=null===(o=t.cellSize)||void 0===o?void 0:o.width)&&void 0!==r?r:m.tileWidth,v=null!==(l=null===(s=t.cellSize)||void 0===s?void 0:s.height)&&void 0!==l?l:m.tileHeight,g=null!==(c=null===(a=t.mapOrigin)||void 0===a?void 0:a.x)&&void 0!==c?c:0,T=null!==(u=null===(d=t.mapOrigin)||void 0===d?void 0:d.y)&&void 0!==u?u:0,C=(null!==(h=t.layersV2)&&void 0!==h?h:[]).map((e,t)=>({layer:e,idx:t})).sort((e,t)=>{var n,i;return(null!==(n=e.layer.zIndex)&&void 0!==n?n:0)-(null!==(i=t.layer.zIndex)&&void 0!==i?i:0)});for(const{layer:t}of C){if(!1===t.enabled)continue;const o=new i.Container;o.label=null!==(f=t.name)&&void 0!==f?f:t.id,o.alpha=null!==(p=t.opacity)&&void 0!==p?p:1,o.visible=!1!==t.visible;const r=new Map;this.buildChunksForLayer(e,o,r,t,m,y,v,g,T,n),e.root.addChild(o),e.layerContainersV2.set(t.id,{container:o,chunkContainers:r})}}buildChunksForLayer(e,t,n,o,r,s,l,a,c,d){var u;const h=o.cellData;if(!h||!h.chunks)return;const p=Object.entries(h.chunks).filter(([,e])=>e&&e.nonEmpty>0),v=p.length,g=null!==(u=e.cullingBoundsHint)&&void 0!==u?u:null;let T=0;this.probeBegin(b.CULL_CHECK_MS);for(const[u,h]of p){if(g){const e=u.split(","),t=Number.parseInt(e[0],10),n=Number.parseInt(e[1],10),i=a+t*f*s,o=c+n*f*l,r=o+f*l;if(!(i+f*s>g.minX&&i<g.maxX&&r>g.minY&&o<g.maxY)){T++;continue}}const p=new i.Container;p.label=`chunk-${u}`;let C=1,x=null;try{x=y(h);const e=new Set;for(let t=0;t<x.length;t++){const n=x[t];if(m(n))continue;const i=255&n,o=r.sourcesBySlot[i-1];o&&"atlas"===o.kind&&e.add(i)}C=Math.max(1,e.size)}catch(e){C=1,x=null}let w="sprite";"mesh"===S({nonEmptyCellsInChunk:h.nonEmpty,atlasesInChunk:C,totalChunksVisible:v})?this.tryBuildMeshChunk(p,u,h,r,s,l,a,c,o)?w="mesh":(w="meshFallback",this.probeCount(b.STRATEGY_MESH_FALLBACK_COUNT,1),this.populateChunkSprites(p,u,h,e,r,s,l,a,c,o,d,x)):this.populateChunkSprites(p,u,h,e,r,s,l,a,c,o,d,x),"sprite"===w?this.probeCount(b.STRATEGY_SPRITE_COUNT,1):"mesh"===w&&this.probeCount(b.STRATEGY_MESH_COUNT,1),t.addChild(p),n.set(u,p)}this.probeEnd(b.CULL_CHECK_MS),T>0&&this.probeCount(b.CULL_HITS_COUNT,T)}tryBuildMeshChunk(e,t,n,i,o,r,s,l,a){return!1}populateChunkSprites(e,t,n,o,r,s,l,a,c,d,u,h){let v;if(h)v=h;else try{v=y(n)}catch(e){return void("undefined"!=typeof console&&console.warn(`[Tilemap] chunk decode failed for ${t}`,e))}const g=t.split(","),T=Number.parseInt(g[0],10),C=Number.parseInt(g[1],10),x=T*f,w=C*f,S=function(e){if(!e)return;const t=/^#?([0-9a-fA-F]{6})(?:[0-9a-fA-F]{2})?$/.exec(e);return t?parseInt(t[1],16):void 0}(d.modulate);let E=0;for(let t=0;t<f;t++)for(let n=0;n<f;n++){const d=v[t*f+n];if(m(d))continue;const h=p(d),y=this.getAtlasFrameTexture(o,r,h);if(!y)continue;const g=new i.Sprite(y);if(E++,g.x=a+(x+n)*s,g.y=c+(w+t)*l,g.width=s,g.height=l,h.flipH&&(g.scale.x=-Math.abs(g.scale.x||1)),h.flipV&&(g.scale.y=-Math.abs(g.scale.y||1)),h.transpose&&(g.rotation=Math.PI/2),h.flipH&&(g.x+=s),h.flipV&&(g.y+=l),null!=S&&(g.tint=S),e.addChild(g),u&&u.isAnimatedSource(h.sourceSlot,h.col,h.row)){const e=`${h.sourceSlot},${h.col},${h.row}`;let t=o.animatedSpritesByAnimKey.get(e);t||(t=[],o.animatedSpritesByAnimKey.set(e,t)),t.push(g)}}E>0&&this.probeCount(b.DRAWCALLS_COUNT,E)}getAtlasFrameTexture(e,t,n){var o,r,s,l;const a=t.sourcesBySlot[n.sourceSlot-1];if(!a)return null;if("atlas"!==a.kind)return"sceneCollection"===a.kind&&(this.probeCount(b.SCENECOLLECTION_SKIPPED_COUNT,1),e.sceneCollectionWarnedSourceIds.has(a.id)||(e.sceneCollectionWarnedSourceIds.add(a.id),"undefined"!=typeof console&&console.warn(`[Tilemap] sceneCollection source '${a.id}' cells are not yet rendered (prefab instantiation pending). Skipped cell at slot=${n.sourceSlot} col=${n.col} row=${n.row}.`))),null;const c=e.atlasTextures.get(a.textureAsset);if(!c)return null;const d=null!==(o=a.margins)&&void 0!==o?o:{x:0,y:0},u=null!==(r=a.separation)&&void 0!==r?r:{x:0,y:0},h=a.regionSize.width,f=a.regionSize.height,p=`${a.textureAsset}#${h}x${f}|${d.x},${d.y}|${u.x},${u.y}#${n.col},${n.row}`,m=e.frameTexturesV2.get(p);if(m)return m;const y=d.x+n.col*(h+u.x),v=d.y+n.row*(f+u.y);try{const t=null!==(l=null!==(s=c.source)&&void 0!==s?s:c.baseTexture)&&void 0!==l?l:c,n=new i.Texture({source:t,frame:new i.Rectangle(y,v,h,f)});return e.frameTexturesV2.set(p,n),n}catch(e){return"undefined"!=typeof console&&console.warn(`[Tilemap] frame slice failed for ${p}`,e),null}}destroy(){var e;for(const t in this.records){const n=parseInt(t),i=this.records[n];this.tearDownChildrenV1(i),this.tearDownChildrenV2(i);const o=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(n);o&&o.removeChild(i.root),i.root.destroy({children:!0}),delete this.records[n]}this.uninstallVisibilityHandlers()}};E.systemName="Tilemap",E=o([t.decorators.componentObserver({Tilemap:[{prop:["tileset"],deep:!1},{prop:["tilemapRef"],deep:!1},{prop:["layersV2"],deep:!1}]})],E);var _=E;const I=["topLeft","top","topRight","right","bottomRight","bottom","bottomLeft","left"];function A(e){var t;let n=0;for(let i=0;i<8;i++){n|=(15&(null!==(t=e[i])&&void 0!==t?t:15))<<4*i}return n>>>0}function R(e,t){return e>>>4*t&15}const M="#version 300 es\nprecision highp float;\n\nin vec2 aPosition;\nin vec2 aTexCoord;\nin vec4 aFlags;\n\nuniform mat3 uProjectionMatrix;\nuniform mat3 uWorldTransformMatrix;\n\nout vec2 vTexCoord;\n\nvoid main() {\n vec3 worldPos = uWorldTransformMatrix * vec3(aPosition, 1.0);\n gl_Position = vec4((uProjectionMatrix * worldPos).xy, 0.0, 1.0);\n\n vec2 uv = aTexCoord;\n if (aFlags.x > 0.5) uv.x = 1.0 - uv.x;\n if (aFlags.y > 0.5) uv.y = 1.0 - uv.y;\n if (aFlags.z > 0.5) { float t = uv.x; uv.x = uv.y; uv.y = t; }\n vTexCoord = uv;\n}\n",O="#version 300 es\nprecision highp float;\n\nin vec2 vTexCoord;\nout vec4 fragColor;\n\nuniform sampler2D uTexture;\nuniform vec4 uModulate;\n\nvoid main() {\n vec4 sampled = texture(uTexture, vTexCoord);\n fragColor = sampled * uModulate;\n if (fragColor.a < 0.01) discard;\n}\n",L={vertex:M,fragment:O},k=16;function D(e,t){const n=new Map,i=new Map;for(const t of e.sources)n.set(t.id,t);for(const e of t.sources)i.set(e.id,e);const o=[],r=[],s=[];for(const e of i.keys())n.has(e)?JSON.stringify(n.get(e))!==JSON.stringify(i.get(e))&&s.push(e):o.push(i.get(e));for(const e of n.keys())i.has(e)||r.push(e);const l=new Set,a=new Set,c=new Set,d=e=>{const t=new Map;if("atlas"!==e.kind)return t;for(const n of e.tiles)t.set(`${e.id}/${n.atlasCoords.col},${n.atlasCoords.row}`,n);return t};for(const[e,t]of i){const i=n.get(e);if(!i){const e=d(t);for(const t of e.keys())l.add(t);continue}const o=d(i),r=d(t);for(const e of r.keys())o.has(e)?JSON.stringify(o.get(e))!==JSON.stringify(r.get(e))&&c.add(e):l.add(e);for(const e of o.keys())r.has(e)||a.add(e)}for(const e of r){const t=d(n.get(e));for(const e of t.keys())a.add(e)}return{addedSources:o,removedSourceIds:r,changedSourceIds:s,addedTilesByKey:l,removedTilesByKey:a,changedTilesByKey:c}}const N=D;function B(e){return`${e.sourceSlot},${e.col},${e.row},${e.altIdx},${e.layerId}`}class V{constructor(e=1e3){this.cache=new Map,this.accessOrder=new Map,this.accessCounter=0,this.maxEntries=e}get(e){const t=B(e),n=this.cache.get(t);return n&&this.accessOrder.set(t,++this.accessCounter),n}set(e,t){const n=B(e);this.cache.set(n,t),this.accessOrder.set(n,++this.accessCounter),this.cache.size>this.maxEntries&&this.evictLRU()}has(e){return this.cache.has(B(e))}clear(){this.cache.clear(),this.accessOrder.clear(),this.accessCounter=0}get size(){return this.cache.size}evictLRU(){let e,t=1/0;for(const[n,i]of this.accessOrder)i<t&&(t=i,e=n);e&&(this.cache.delete(e),this.accessOrder.delete(e))}}function P(e,t,n){return e.map(e=>({vertices:e.vertices.map(e=>({x:e.x+t,y:e.y+n}))}))}function $(e){if(e.length<3)return[];if(!function(e){if(e.length<3)return!0;let t=0;const n=e.length;for(let i=0;i<n;i++){const o=e[i],r=e[(i+1)%n],s=e[(i+2)%n],l=r.x-o.x,a=r.y-o.y,c=s.x-r.x,d=l*(s.y-r.y)-a*c;if(0!==d)if(0===t)t=d>0?1:-1;else if((d>0?1:-1)!==t)return!1}return!0}(e)){const t=new Error(`decomposePolygon: input polygon (${e.length} vertices) is non-convex. This is a placeholder implementation that assumes convex input — concave polygons silently produce invalid Matter.js bodies. Real concave decomposition requires the 'poly-decomp' npm dependency (see ADR-0019 Phase 1). If you need concave physics tiles right now, split into convex parts manually.`);throw"undefined"!=typeof console&&console.error("[Tilemap]",t.message),t}return[{vertices:e.slice()}]}function H(e,t,n,i){return((255&e)<<24|(63&i)<<18|(511&n)<<9|511&t)>>>0}V.prototype.getNum=function(e){return this.get({sourceSlot:e>>>24&255,altIdx:e>>>18&63,row:e>>>9&511,col:511&e,layerId:"__num__"})},V.prototype.setNum=function(e,t){this.set({sourceSlot:e>>>24&255,altIdx:e>>>18&63,row:e>>>9&511,col:511&e,layerId:"__num__"},t)};const K=new class{constructor(){this.builders=new Map}register(e,t){this.builders.set(e,t)}unregister(e){this.builders.delete(e)}has(e){return this.builders.has(e)}build(e,t){const n=this.builders.get(e);return n?n(t):null}listRegisteredNames(){return Array.from(this.builders.keys())}clear(){this.builders.clear()}};function U(e){return 255&e}function F(e){return e>>>8&255}function W(e){return e>>>16&255}function Y(e){return e>>>24&31}function G(e,t){var n;const i=[];for(const[o,r]of Object.entries(e.chunksByKey)){const[s,l]=o.split(","),a=Number.parseInt(s,10),c=Number.parseInt(l,10);if(Number.isFinite(a)&&Number.isFinite(c))for(let s=0;s<16;s++)for(let l=0;l<16;l++){const d=r[16*s+l],u=U(d);if(0===u)continue;const h=F(d),f=W(d),p=Y(d),m=`${u},${h},${f},${p}`,y=e.physicsByCellKey.get(m);if(!y||0===y.length)continue;const v=e.mapOriginX+(16*a+l)*e.cellWidth+t.worldX,g=e.mapOriginY+(16*c+s)*e.cellHeight+t.worldY;for(const t of y)for(const r of t.polygons){const a=H(u,h,f,p);let c=null===(n=e.cache.getNum(a))||void 0===n?void 0:n.parts;c||(c=$(r.points),e.cache.setNum(a,{parts:c}));const d=P(c,v,g);for(let n=0;n<d.length;n++)i.push({id:`tile-${o}-${l}-${s}-${t.layerId}-${n}`,vertices:d[n].vertices,centerX:v+e.cellWidth/2,centerY:g+e.cellHeight/2,isStatic:!0,oneWay:t.oneWay,friction:t.friction,restitution:t.restitution,metadata:{layerId:t.layerId,chunkKey:o,cellLocal:[l,s]}})}}}return i}try{t.resource.registerResourceType("TILESET")}catch(e){}try{t.resource.registerInstance("TILESET",e=>{var t,n,i,o;const r=null===(t=e.data)||void 0===t?void 0:t.json,s=null===(i=null===(n=e.src)||void 0===n?void 0:n.json)||void 0===i?void 0:i.data;return null!==(o=null!=r?r:s)&&void 0!==o?o:null})}catch(e){}return e.ADR_0019_NATIVE_REGISTRY_AVAILABLE=!1,e.CHUNK_SIZE=f,e.DecompositionCache=V,e.MAX_TERRAIN_ID=14,e.NEIGHBOR_DIRECTIONS=I,e.NO_NEIGHBOR=15,e.PeeringBitIndex=class{constructor(e){this.terrainSetIndex=e,this.exact=new Map,this.wildcards=[]}add(e,t){var n;const i=[],o=[];for(let e=0;e<8;e++){const n=null==t?void 0:t[I[e]];void 0===n?(i.push(15),o.push(0)):(i.push(15&n),o.push(15))}const r=A(i),s=A(o);if(o.every(e=>15===e)){const t=null!==(n=this.exact.get(r))&&void 0!==n?n:[];t.push(e),this.exact.set(r,t)}else this.wildcards.push({key:r,mask:s,candidate:e})}match(e){const t=A(e),n=this.exact.get(t);if(n&&n.length>0)return{candidates:n.slice(),exactMatch:!0,bestHammingDistance:0};let i=1/0,o=[];for(const{key:e,mask:n,candidate:r}of this.wildcards){let s=0,l=!0;for(let o=0;o<8;o++){if(0===R(n,o))continue;if(R(t,o)!==R(e,o)&&(s++,s>i)){l=!1;break}}l&&(s<i?(i=s,o=[r]):s===i&&o.push(r))}return{candidates:o,exactMatch:!1,bestHammingDistance:i===1/0?-1:i}}get exactBucketCount(){return this.exact.size}get wildcardCount(){return this.wildcards.length}},e.TILEMAP_BODY_SOURCE_REGISTRY=K,e.TILEMAP_PROBE_NAMES=b,e.TILE_FRAG_SHADER=O,e.TILE_SHADER_SOURCES=L,e.TILE_VERT_SHADER=M,e.TileAnimationDriver=T,e.Tilemap=h,e.TilemapSystem=_,e.adaptGamePerfProbes=w,e.buildChunkGeometry=function(e){const{cells:t,chunkWorldX:n,chunkWorldY:i,cellWidth:o,cellHeight:r,atlasInfo:s}=e;if(256!==t.length)throw new Error(`buildChunkGeometry: cells length ${t.length} != 256`);let l=0;for(let e=0;e<t.length;e++)m(t[e])||l++;const a=new Float32Array(4*l*8),c=new Uint16Array(6*l);let d=0,u=0,h=0;for(let e=0;e<k;e++)for(let l=0;l<k;l++){const f=t[e*k+l];if(m(f))continue;const y=p(f),v=n+l*o,g=i+e*r,T=v+o,C=g+r,b=(s.margins.x+y.col*(s.regionWidth+s.separation.x))/s.textureWidth,x=b+s.regionWidth/s.textureWidth,w=(s.margins.y+y.row*(s.regionHeight+s.separation.y))/s.textureHeight,S=w+s.regionHeight/s.textureHeight,E=y.flipH?1:0,_=y.flipV?1:0,I=y.transpose?1:0,A=0,R=(e,t,n,i)=>{a[d++]=e,a[d++]=t,a[d++]=n,a[d++]=i,a[d++]=E,a[d++]=_,a[d++]=I,a[d++]=A};R(v,g,b,w),R(T,g,x,w),R(T,C,x,S),R(v,C,b,S);const M=4*h;c[u++]=M,c[u++]=M+1,c[u++]=M+2,c[u++]=M,c[u++]=M+2,c[u++]=M+3,h++}return{vertexData:a,indices:c,quadCount:h}},e.buildChunkMesh=function(e){const t=new i.Container;return t.label=`chunk-mesh-stub-${e.chunkKey}`,t},e.buildTileMapStaticBodies=G,e.buildTileMapStaticBodyDefinitions=function(e,t,n){var i,o;let r;null==n||n.beginTiming(b.PHYSICS_REBAKE_MS);try{r=G(e,t)}finally{null==n||n.endTiming(b.PHYSICS_REBAKE_MS)}const s=new Map;for(const e of r){const t=null!==(o=null===(i=e.metadata)||void 0===i?void 0:i.layerId)&&void 0!==o?o:"<unlabeled>";let n=s.get(t);n||(n=[],s.set(t,n)),n.push(e)}const l=r.length;return null==n||n.gauge(b.BODIES_TOTAL,l),l>0&&(null==n||n.count(b.BODIES_CREATED,l)),{bodies:r,totalBodyCount:l,bodiesByLayerId:s}},e.cacheKeyOf=B,e.cacheKeyOfNum=H,e.computeAffectedChunks=function(e,t,n){const i=new Set,o=(e,i,o)=>{const r=t.get(e);if(!r)return!1;const s=`${r}/${i},${o}`;return n.changedTilesByKey.has(s)||n.removedTilesByKey.has(s)||n.addedTilesByKey.has(s)};for(const[t,n]of Object.entries(e))for(let e=0;e<n.length;e++){const r=n[e],s=255&r;if(0===s)continue;if(o(s,r>>>8&255,r>>>16&255)){i.add(t);break}}return i},e.createInMemoryProbeRegistry=function(){const e=new Map,t=new Map,n=new Map,i=new Map;return{beginTiming(t){e.set(t,x())},endTiming(n){const i=e.get(n);if(void 0===i)return;e.delete(n);const o=x()-i;t.has(n)||t.set(n,[]),t.get(n).push(o)},count(e,t=1){var i;n.set(e,(null!==(i=n.get(e))&&void 0!==i?i:0)+t)},gauge(e,t){i.set(e,t)},histogram(e,t){this.count(`${e}.histN`),this.count(`${e}.histSum`,t)},timings:t,counts:n,gauges:i}},e.decodeChunk=y,e.decomposePolygon=$,e.diffTilesetDocuments=N,e.diffTilesetForChunkRebuild=D,e.estimateChunkMeshMemoryKB=function(e){return Math.ceil(11)},e.estimateSpriteNodes=function(e){return 1+e},e.expandSceneCollectionSource=function(e){return"sceneCollection"!==e.kind?[]:e.prefabRefs.map((t,n)=>({sourceId:e.id,prefabName:t,index:n}))},e.extractPhysicsByCellKey=function(e){const t=new Map;for(let n=0;n<e.sources.length;n++){const i=e.sources[n];if(!i||"atlas"!==i.kind||!i.tiles)continue;const o=n+1;for(const e of i.tiles){const{col:n,row:i}=e.atlasCoords;for(const r of e.alternatives){if(!r.physics||0===r.physics.length)continue;const e=`${o},${n},${i},${31&r.altId}`;t.set(e,r.physics)}}}return t},e.getRequiredTilemapProbes=function(){return Object.values(b)},e.isEmptyCellValue=m,e.isMeshPathAvailable=function(){return!1},e.makeLoadedTileset=g,e.pickAutotileCandidate=function(e,t){if(0===e.length)return null;if(1===e.length)return e[0];const n=e.reduce((e,t)=>e+(t.probability>0?t.probability:1),0),i=t()*n;let o=0;for(const t of e)if(o+=t.probability>0?t.probability:1,i<o)return t;return e[e.length-1]},e.resolveCellPrefabName=function(e,t){var n;const i=255&e;if(0===i)return null;const o=t[i-1];if(!o||"sceneCollection"!==o.kind)return null;const r=e>>>8&255;return null!==(n=o.prefabRefs[r])&&void 0!==n?n:null},e.resolveChunkRenderStrategy=S,e.translateConvexParts=P,e.unpackCell=p,Object.defineProperty(e,"__esModule",{value:!0}),e}({},EVA,EVA.plugin.renderer,PIXI);globalThis.EVA.plugin.renderer.tilemap=globalThis.EVA.plugin.renderer.tilemap||_EVA_IIFE_tilemap;