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

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
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/inspector-decorator"),r=require("@eva/plugin-renderer"),i=require("pixi.js");function n(e,t,r,i){var n,o=arguments.length,s=o<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,i);else for(var l=e.length-1;l>=0;l--)(n=e[l])&&(s=(o<3?n(s):o>3?n(t,r,s):n(t,r))||s);return o>3&&s&&Object.defineProperty(t,r,s),s}function o(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,r,i){return new(r||(r=Promise))(function(n,o){function s(e){try{a(i.next(e))}catch(e){o(e)}}function l(e){try{a(i.throw(e))}catch(e){o(e)}}function a(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,l)}a((i=i.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class l extends e.Component{constructor(){super(...arguments),this.tileset="",this.tileWidth=32,this.tileHeight=32,this.tilesetSpacing=0,this.tilesetMargin=0,this.layers=[]}init(e){e&&Object.assign(this,e)}}l.componentName="Tilemap",n([t.type("string"),o("design:type",String)],l.prototype,"tileset",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tileWidth",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tileHeight",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tilesetSpacing",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tilesetMargin",void 0);let a=class extends r.Renderer{constructor(){super(...arguments),this.name="Tilemap",this.records={}}init(){this.renderSystem=this.game.getSystem(r.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){}componentChanged(t){var r,n,o;return s(this,void 0,void 0,function*(){if("Tilemap"!==t.componentName)return;const s=t.component,l=t.gameObject.id;if(t.type===e.OBSERVER_TYPE.ADD){const r=this.increaseAsyncId(l);let n=null;if(s.tileset){const{instance:i}=yield e.resource.getResource(s.tileset);if(!this.validateAsyncId(l,r))return;if(!i)return void console.error(`GameObject:${t.gameObject.name}'s Tilemap tileset load error`);n=i}if(!n)return;const o=new i.Container,a=this.containerManager.getContainer(l);a&&a.addChildAt(o,0);const c={root:o,layerContainers:[],frameTextures:[],baseTexture:n};this.records[l]=c,this.buildLayers(c,s)}else if(t.type===e.OBSERVER_TYPE.CHANGE){const i=this.records[l];if(!i)return;if("tileset"===(null===(n=null===(r=t.prop)||void 0===r?void 0:r.prop)||void 0===n?void 0:n[0])){const t=this.increaseAsyncId(l),{instance:r}=yield e.resource.getResource(s.tileset);if(!this.validateAsyncId(l,t))return;if(!r)return;this.tearDownChildren(i),i.baseTexture=r,this.buildLayers(i,s)}}else if(t.type===e.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(l);const e=this.records[l];if(!e)return;this.tearDownChildren(e);const t=null===(o=this.containerManager)||void 0===o?void 0:o.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 r,n,o,s,l,a,c,d,u,p;const h=e.baseTexture;if(!h)return;const f=t.tileWidth||32,y=t.tileHeight||32,m=null!==(r=t.renderTileWidth)&&void 0!==r?r:f,v=null!==(n=t.renderTileHeight)&&void 0!==n?n:y,g=t.tilesetMargin||0,b=t.tilesetSpacing||0,C=null!==(u=null!==(c=null!==(a=null!==(s=null===(o=h.orig)||void 0===o?void 0:o.width)&&void 0!==s?s:null===(l=h.source)||void 0===l?void 0:l.width)&&void 0!==a?a:h.width)&&void 0!==c?c:null===(d=h.frame)||void 0===d?void 0:d.width)&&void 0!==u?u:0,T=C>0?Math.max(1,Math.floor((C-g+b)/(f+b))):1,w=t.tilesetColumns&&t.tilesetColumns>0?t.tilesetColumns:T,x=new Map,R=t=>{var r,n;if(t<=0)return null;const o=t-1,s=x.get(o);if(s)return s;const l=o%w,a=Math.floor(o/w),c=g+l*(f+b),d=g+a*(y+b);try{const t=null!==(n=null!==(r=h.source)&&void 0!==r?r:h.baseTexture)&&void 0!==n?n:h,s=new i.Texture({source:t,frame:new i.Rectangle(c,d,f,y)});return x.set(o,s),e.frameTextures.push(s),s}catch(e){return"undefined"!=typeof console&&console.warn("[Tilemap] Texture slice failed, falling back to base",e),h}};for(const r of t.layers||[]){const t=new i.Container;t.label=null!==(p=r.name)&&void 0!==p?p:"tilemap-layer";const n=r.offsetX||0,o=r.offsetY||0;null!=r.alpha&&(t.alpha=r.alpha),!1===r.visible&&(t.visible=!1);const s=r.data||[],l=r.tint;for(let e=0;e<s.length;e++){const r=s[e];if(r)for(let s=0;s<r.length;s++){const a=r[s];if(!a||a<=0)continue;const c=R(a);if(!c)continue;const d=new i.Sprite(c);d.x=n+s*f,d.y=o+e*y,d.width=m,d.height=v,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 r=parseInt(t),i=this.records[r];this.tearDownChildren(i);const n=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(r);n&&n.removeChild(i.root),i.root.destroy({children:!0}),delete this.records[r]}}};a.systemName="Tilemap",a=n([e.decorators.componentObserver({Tilemap:[{prop:["tileset"],deep:!1}]})],a);var c=a;exports.Tilemap=l,exports.TilemapSystem=c;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/inspector-decorator"),n=require("@eva/plugin-renderer"),i=require("pixi.js");function o(e,t,n,i){var o,s=arguments.length,r=s<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,n,i);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(r=(s<3?o(r):s>3?o(t,n,r):o(t,n))||r);return s>3&&r&&Object.defineProperty(t,n,r),r}function s(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function r(e,t,n,i){return new(n||(n=Promise))(function(o,s){function r(e){try{a(i.next(e))}catch(e){s(e)}}function l(e){try{a(i.throw(e))}catch(e){s(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(r,l)}a((i=i.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class l extends e.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&&Object.assign(this,e)}}l.componentName="Tilemap",o([t.type("string"),s("design:type",String)],l.prototype,"tileset",void 0),o([t.type("number"),s("design:type",Number)],l.prototype,"tileWidth",void 0),o([t.type("number"),s("design:type",Number)],l.prototype,"tileHeight",void 0),o([t.type("number"),s("design:type",Number)],l.prototype,"tilesetSpacing",void 0),o([t.type("number"),s("design:type",Number)],l.prototype,"tilesetMargin",void 0),o([t.type("string"),s("design:type",String)],l.prototype,"tilemapRef",void 0);const a=16;function c(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 d(e){return!(255&e)}function u(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,s=new Uint8Array(o);let r=0;for(let e=0;e<n.length;e+=4){const t=h.indexOf(n[e])<<18|h.indexOf(n[e+1])<<12|(n[e+2]?h.indexOf(n[e+2]):0)<<6|(n[e+3]?h.indexOf(n[e+3]):0);r<o&&(s[r++]=t>>16&255),r<o&&(s[r++]=t>>8&255),r<o&&(s[r++]=255&t)}return s}(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 h="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function f(e){var t,n,i,o,s,r,l,a,c,d,u,h,f,p,m,y;const v=new Map,g=[],C=new Map;for(let T=0;T<e.sources.length;T++){const x=e.sources[T];if(g.push(x),v.set(x.id,T+1),"atlas"===x.kind&&x.textureAsset){const e=C.get(x.textureAsset);if(e){const v=e.regionSize.width===x.regionSize.width&&e.regionSize.height===x.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=x.margins)||void 0===i?void 0:i.x)&&void 0!==o?o:0)&&(null!==(r=null===(s=e.margins)||void 0===s?void 0:s.y)&&void 0!==r?r:0)===(null!==(a=null===(l=x.margins)||void 0===l?void 0:l.y)&&void 0!==a?a:0),C=(null!==(d=null===(c=e.separation)||void 0===c?void 0:c.x)&&void 0!==d?d:0)===(null!==(h=null===(u=x.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=x.separation)||void 0===m?void 0:m.y)&&void 0!==y?y:0);v&&g&&C||"undefined"!=typeof console&&console.warn(`[Tilemap] Atlas sources '${e.sourceId}' and '${x.id}' share textureAsset='${x.textureAsset}' but differ in regionSize/margins/separation. Frame texture cache may collide and render wrong frames. Use distinct textureAssets per atlas source.`)}else C.set(x.textureAsset,{regionSize:x.regionSize,margins:x.margins,separation:x.separation,sourceId:x.id})}}return{raw:e,sourcesBySlot:g,slotByIdMap:v,tileWidth:e.tileSize.width,tileHeight:e.tileSize.height}}class p{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)}}),s=i.reduce((e,t)=>e+t.durationMs,0);if(s<=0)continue;const r="randomStart"===n.phase?m(o,e.atlasCoords.col,e.atlasCoords.row,s):0;this.animations.push({sourceSlot:o,col:e.atlasCoords.col,row:e.atlasCoords.row,frames:i,totalDurationMs:s,phase:null!==(t=n.phase)&&void 0!==t?t:"sync",phaseOffsetMs:r})}}}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}`,s=((e+i.phaseOffsetMs)%i.totalDurationMs+i.totalDurationMs)%i.totalDurationMs;let r=0,l=0;for(;l<i.frames.length&&(r+=i.frames[l].durationMs,!(s<r));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 m(e,t,n,i){let o=73856093*e^19349663*t^83492791*n;o=1274126177*(o^o>>>13),o^=o>>>16;const s=(o>>>0)/4294967295;return Math.floor(s*i)}const y={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 v(){return"undefined"!=typeof performance&&performance.now?performance.now():0}function g(e){var t,n,i,o;const s=null==e?void 0:e.perfProbes;return s?{beginTiming:null!==(t=s.beginTiming)&&void 0!==t?t:()=>{},endTiming:null!==(n=s.endTiming)&&void 0!==n?n:()=>{},count:null!==(i=s.count)&&void 0!==i?i:()=>{},gauge:null!==(o=s.gauge)&&void 0!==o?o:()=>{},histogram:s.histogram}:null}function C(e){return e.preference&&"auto"!==e.preference?e.preference:e.atlasesInChunk>1?"sprite":e.nonEmptyCellsInChunk>=192?"mesh":"sprite"}let T=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=g(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(y.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 s=0;for(const e of i.dirtyChunkKeys.values())s+=e.size;if(this.probeGauge(y.DIRTY_PENDING,s),!i.flushScheduled){i.flushScheduled=!0;("undefined"!=typeof requestAnimationFrame?requestAnimationFrame:e=>setTimeout(()=>e(0),16))(()=>this.flushDirtyChunks(e))}}flushDirtyChunks(e){var t,n,o,s,r,l,a,c,d,u,h,f,p,m;const v=this.records[e];if(!v)return;if(v.flushScheduled=!1,0===v.dirtyChunkKeys.size)return;if("v2"!==v.mode||!v.loadedTileset)return v.dirtyChunkKeys.clear(),void this.probeGauge(y.DIRTY_PENDING,0);this.probeCount(y.DIRTY_FRAMES_BEHIND,1);const g=v.loadedTileset,C=null!==(t=this.animationDrivers.get(e))&&void 0!==t?t:null,T=this.game;let x=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){x=i;break}}const b=null!==(s=null===(o=null==x?void 0:x.cellSize)||void 0===o?void 0:o.width)&&void 0!==s?s:g.tileWidth,S=null!==(l=null===(r=null==x?void 0:x.cellSize)||void 0===r?void 0:r.height)&&void 0!==l?l:g.tileHeight,w=null!==(c=null===(a=null==x?void 0:x.mapOrigin)||void 0===a?void 0:a.x)&&void 0!==c?c:0,I=null!==(u=null===(d=null==x?void 0:x.mapOrigin)||void 0===d?void 0:d.y)&&void 0!==u?u:0;this.probeBegin(y.DIRTY_REBUILD_MS);try{for(const[e,t]of v.dirtyChunkKeys){const n=v.layerContainersV2.get(e);if(!n)continue;const o=null!==(f=null===(h=null==x?void 0:x.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 s=null===(m=null===(p=o.cellData)||void 0===p?void 0:p.chunks)||void 0===m?void 0:m[e];if(!s||s.nonEmpty<=0)continue;const r=new i.Container;r.label=`chunk-${e}`,this.populateChunkSprites(r,e,s,v,g,b,S,w,I,o,C),n.container.addChild(r),n.chunkContainers.set(e,r)}}}finally{this.probeEnd(y.DIRTY_REBUILD_MS)}v.dirtyChunkKeys.clear(),this.probeGauge(y.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(y.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 s=o.loadedTileset;if(!s)continue;let r=!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,s,{sourceSlot:c,col:l.col,row:l.row});if(d)for(const e of i)e.texture=d,r=!0,t++}r&&this.requestRedraw()}catch(e){this.probeCount(y.ANIM_TICK_ERROR_COUNT,1),"undefined"!=typeof console&&console.error(`[Tilemap] animation tick failed for entity ${n}`,e)}}}finally{this.probeEnd(y.ANIM_TICK_MS),t>0&&this.probeCount("tilemap.anim.spritesSwapped",t)}}componentChanged(t){return r(this,void 0,void 0,function*(){if("Tilemap"!==t.componentName)return;const n=t.component,i=t.gameObject.id;t.type===e.OBSERVER_TYPE.ADD?yield this.handleAdd(i,t.gameObject,n):t.type===e.OBSERVER_TYPE.CHANGE?yield this.handleChange(i,n,t):t.type===e.OBSERVER_TYPE.REMOVE&&this.handleRemove(i)})}handleAdd(e,t,n){return r(this,void 0,void 0,function*(){this.probeBegin(y.DIRTY_REBUILD_MS);try{const o=this.increaseAsyncId(e),s=this.detectMode(n),r=new i.Container,l=this.containerManager.getContainer(e);l&&l.addChildAt(r,0);const a={root:r,layerContainers:[],layerContainersV2:new Map,frameTextures:[],frameTexturesV2:new Map,baseTexture:null,atlasTextures:new Map,loadedTileset:null,mode:s,animatedSpritesByAnimKey:new Map,sceneCollectionWarnedSourceIds:new Set,dirtyChunkKeys:new Map,flushScheduled:!1};this.records[e]=a,"v1"===s?yield this.ensureV1Built(a,e,t,n,o):"v2"===s&&(yield this.ensureV2Built(a,e,t,n,o))}finally{this.probeEnd(y.DIRTY_REBUILD_MS)}})}ensureV1Built(e,t,n,i,o){return r(this,void 0,void 0,function*(){const s=yield this.loadV1Texture(i);this.validateAsyncId(t,o)&&(s?(e.baseTexture=s,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 r(this,void 0,void 0,function*(){const s=yield this.loadV2Tileset(i);if(!this.validateAsyncId(t,o))return;if(!s)return void("undefined"!=typeof console&&console.error(`GameObject:${n.name}'s Tilemap tilemapRef load error`));if(e.loadedTileset=s,yield this.loadAtlasTextures(e,s),!this.validateAsyncId(t,o))return;const r=new p;r.loadFromTileset(s.raw);const l=r.animationCount>0?r: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 r(this,void 0,void 0,function*(){const s=this.records[e];if(!s)return;const r=null===(o=null===(i=n.prop)||void 0===i?void 0:i.prop)||void 0===o?void 0:o[0];this.probeBegin(y.PATCH_APPLY_MS);try{const i=this.detectMode(t);if(i!==s.mode){const o=this.increaseAsyncId(e);try{if("v1"===s.mode?this.tearDownChildrenV1(s):"v2"===s.mode&&(this.tearDownChildrenV2(s),this.animationDrivers.delete(e)),s.baseTexture=null,s.loadedTileset=null,"v1"===i){const i=yield this.loadV1Texture(t);if(!this.validateAsyncId(e,o))return;if(!i)return s.mode="unknown",this.probeCount(y.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)`));s.mode="v1",s.baseTexture=i,this.buildLayersV1(s,t),this.requestRedraw()}else if("v2"===i){const i=yield this.loadV2Tileset(t);if(!this.validateAsyncId(e,o))return;if(!i)return s.mode="unknown",this.probeCount(y.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(s,i),!this.validateAsyncId(e,o))return;s.mode="v2",s.loadedTileset=i;const r=new p;r.loadFromTileset(i.raw);const l=r.animationCount>0?r:null;l&&this.animationDrivers.set(e,l),this.buildLayersV2(s,t,l),this.requestRedraw()}else s.mode="unknown"}catch(e){s.mode="unknown",this.probeCount(y.MODE_SWITCH_FAILED_COUNT,1),"undefined"!=typeof console&&console.error("[Tilemap] mode switch threw",e)}return}if("tileset"===r&&"v1"===s.mode){const n=this.increaseAsyncId(e),i=yield this.loadV1Texture(t);if(!this.validateAsyncId(e,n))return;if(!i)return;this.tearDownChildrenV1(s),s.baseTexture=i,this.buildLayersV1(s,t),this.requestRedraw()}else if("tilemapRef"===r&&"v2"===s.mode){const n=this.increaseAsyncId(e),i=yield this.loadV2Tileset(t);if(!this.validateAsyncId(e,n))return;if(!i)return;if(this.tearDownChildrenV2(s),s.loadedTileset=i,yield this.loadAtlasTextures(s,i),!this.validateAsyncId(e,n))return;this.animationDrivers.delete(e);const o=new p;o.loadFromTileset(i.raw);const r=o.animationCount>0?o:null;r&&this.animationDrivers.set(e,r),this.buildLayersV2(s,t,r),this.requestRedraw()}else if("layersV2"===r&&"v2"===s.mode){this.tearDownChildrenV2(s),this.animationDrivers.delete(e),s.dirtyChunkKeys.clear();const n=s.loadedTileset;let i=null;if(n){const t=new p;t.loadFromTileset(n.raw),t.animationCount>0&&(i=t,this.animationDrivers.set(e,t))}this.buildLayersV2(s,t,i),this.requestRedraw()}}finally{this.probeEnd(y.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(t){var n;return r(this,void 0,void 0,function*(){if(!t.tileset)return null;const{instance:i}=yield e.resource.getResource(t.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,s,r,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,C=t.tilesetSpacing||0,T=null!==(u=null!==(c=null!==(a=null!==(r=null===(s=f.orig)||void 0===s?void 0:s.width)&&void 0!==r?r: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,x=T>0?Math.max(1,Math.floor((T-g+C)/(p+C))):1,b=t.tilesetColumns&&t.tilesetColumns>0?t.tilesetColumns:x,S=new Map,w=t=>{var n,o;if(t<=0)return null;const s=t-1,r=S.get(s);if(r)return r;const l=s%b,a=Math.floor(s/b),c=g+l*(p+C),d=g+a*(m+C);try{const t=null!==(o=null!==(n=f.source)&&void 0!==n?n:f.baseTexture)&&void 0!==o?o:f,r=new i.Texture({source:t,frame:new i.Rectangle(c,d,p,m)});return S.set(s,r),e.frameTextures.push(r),r}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,s=n.offsetY||0;null!=n.alpha&&(t.alpha=n.alpha),!1===n.visible&&(t.visible=!1);const r=n.data||[],l=n.tint;for(let e=0;e<r.length;e++){const n=r[e];if(n)for(let r=0;r<n.length;r++){const a=n[r];if(!a||a<=0)continue;const c=w(a);if(!c)continue;const d=new i.Sprite(c);d.x=o+r*p,d.y=s+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(t){var n,i;return r(this,void 0,void 0,function*(){if(!t.tilemapRef)return null;const o=yield e.resource.getResource(t.tilemapRef);if(!o)return null;const s=null!==(n=o.instance)&&void 0!==n?n:null===(i=o.data)||void 0===i?void 0:i.json;return s&&"tileset"===s.kind?f(s):("undefined"!=typeof console&&console.error(`[Tilemap] resource '${t.tilemapRef}' is not a TileSet document`),null)})}loadAtlasTextures(t,n){return r(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=>r(this,void 0,void 0,function*(){var i,o,s;try{const r=yield e.resource.getResource(n),l=null!==(s=null!==(i=null==r?void 0:r.instance)&&void 0!==i?i:null===(o=null==r?void 0:r.data)||void 0===o?void 0:o.image)&&void 0!==s?s:null;l&&t.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,s,r,l,a,c,d,u,h,f,p;const m=e.loadedTileset;if(!m)return;const y=null!==(s=null===(o=t.cellSize)||void 0===o?void 0:o.width)&&void 0!==s?s:m.tileWidth,v=null!==(l=null===(r=t.cellSize)||void 0===r?void 0:r.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,C=null!==(u=null===(d=t.mapOrigin)||void 0===d?void 0:d.y)&&void 0!==u?u:0,T=(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 T){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 s=new Map;this.buildChunksForLayer(e,o,s,t,m,y,v,g,C,n),e.root.addChild(o),e.layerContainersV2.set(t.id,{container:o,chunkContainers:s})}}buildChunksForLayer(e,t,n,o,s,r,l,c,h,f){var p;const m=o.cellData;if(!m||!m.chunks)return;const v=Object.entries(m.chunks).filter(([,e])=>e&&e.nonEmpty>0),g=v.length,T=null!==(p=e.cullingBoundsHint)&&void 0!==p?p:null;let x=0;this.probeBegin(y.CULL_CHECK_MS);for(const[p,m]of v){if(T){const e=p.split(","),t=Number.parseInt(e[0],10),n=Number.parseInt(e[1],10),i=c+t*a*r,o=h+n*a*l,s=o+a*l;if(!(i+a*r>T.minX&&i<T.maxX&&s>T.minY&&o<T.maxY)){x++;continue}}const v=new i.Container;v.label=`chunk-${p}`;let b=1,S=null;try{S=u(m);const e=new Set;for(let t=0;t<S.length;t++){const n=S[t];if(d(n))continue;const i=255&n,o=s.sourcesBySlot[i-1];o&&"atlas"===o.kind&&e.add(i)}b=Math.max(1,e.size)}catch(e){b=1,S=null}let w="sprite";"mesh"===C({nonEmptyCellsInChunk:m.nonEmpty,atlasesInChunk:b,totalChunksVisible:g})?this.tryBuildMeshChunk(v,p,m,s,r,l,c,h,o)?w="mesh":(w="meshFallback",this.probeCount(y.STRATEGY_MESH_FALLBACK_COUNT,1),this.populateChunkSprites(v,p,m,e,s,r,l,c,h,o,f,S)):this.populateChunkSprites(v,p,m,e,s,r,l,c,h,o,f,S),"sprite"===w?this.probeCount(y.STRATEGY_SPRITE_COUNT,1):"mesh"===w&&this.probeCount(y.STRATEGY_MESH_COUNT,1),t.addChild(v),n.set(p,v)}this.probeEnd(y.CULL_CHECK_MS),x>0&&this.probeCount(y.CULL_HITS_COUNT,x)}tryBuildMeshChunk(e,t,n,i,o,s,r,l,a){return!1}populateChunkSprites(e,t,n,o,s,r,l,h,f,p,m,v){let g;if(v)g=v;else try{g=u(n)}catch(e){return void("undefined"!=typeof console&&console.warn(`[Tilemap] chunk decode failed for ${t}`,e))}const C=t.split(","),T=Number.parseInt(C[0],10),x=Number.parseInt(C[1],10),b=T*a,S=x*a,w=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}(p.modulate);let I=0;for(let t=0;t<a;t++)for(let n=0;n<a;n++){const u=g[t*a+n];if(d(u))continue;const p=c(u),y=this.getAtlasFrameTexture(o,s,p);if(!y)continue;const v=new i.Sprite(y);if(I++,v.x=h+(b+n)*r,v.y=f+(S+t)*l,v.width=r,v.height=l,p.flipH&&(v.scale.x=-Math.abs(v.scale.x||1)),p.flipV&&(v.scale.y=-Math.abs(v.scale.y||1)),p.transpose&&(v.rotation=Math.PI/2),p.flipH&&(v.x+=r),p.flipV&&(v.y+=l),null!=w&&(v.tint=w),e.addChild(v),m&&m.isAnimatedSource(p.sourceSlot,p.col,p.row)){const e=`${p.sourceSlot},${p.col},${p.row}`;let t=o.animatedSpritesByAnimKey.get(e);t||(t=[],o.animatedSpritesByAnimKey.set(e,t)),t.push(v)}}I>0&&this.probeCount(y.DRAWCALLS_COUNT,I)}getAtlasFrameTexture(e,t,n){var o,s,r,l;const a=t.sourcesBySlot[n.sourceSlot-1];if(!a)return null;if("atlas"!==a.kind)return"sceneCollection"===a.kind&&(this.probeCount(y.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!==(s=a.separation)&&void 0!==s?s:{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 v=d.x+n.col*(h+u.x),g=d.y+n.row*(f+u.y);try{const t=null!==(l=null!==(r=c.source)&&void 0!==r?r:c.baseTexture)&&void 0!==l?l:c,n=new i.Texture({source:t,frame:new i.Rectangle(v,g,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()}};T.systemName="Tilemap",T=o([e.decorators.componentObserver({Tilemap:[{prop:["tileset"],deep:!1},{prop:["tilemapRef"],deep:!1},{prop:["layersV2"],deep:!1}]})],T);var x=T;const b=["topLeft","top","topRight","right","bottomRight","bottom","bottomLeft","left"];function S(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 w(e,t){return e>>>4*t&15}const I="#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",_="#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",E={vertex:I,fragment:_},A=16;function R(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=[],s=[],r=[];for(const e of i.keys())n.has(e)?JSON.stringify(n.get(e))!==JSON.stringify(i.get(e))&&r.push(e):o.push(i.get(e));for(const e of n.keys())i.has(e)||s.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),s=d(t);for(const e of s.keys())o.has(e)?JSON.stringify(o.get(e))!==JSON.stringify(s.get(e))&&c.add(e):l.add(e);for(const e of o.keys())s.has(e)||a.add(e)}for(const e of s){const t=d(n.get(e));for(const e of t.keys())a.add(e)}return{addedSources:o,removedSourceIds:s,changedSourceIds:r,addedTilesByKey:l,removedTilesByKey:a,changedTilesByKey:c}}const M=R;function L(e){return`${e.sourceSlot},${e.col},${e.row},${e.altIdx},${e.layerId}`}class O{constructor(e=1e3){this.cache=new Map,this.accessOrder=new Map,this.accessCounter=0,this.maxEntries=e}get(e){const t=L(e),n=this.cache.get(t);return n&&this.accessOrder.set(t,++this.accessCounter),n}set(e,t){const n=L(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(L(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 k(e,t,n){return e.map(e=>({vertices:e.vertices.map(e=>({x:e.x+t,y:e.y+n}))}))}function D(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],s=e[(i+1)%n],r=e[(i+2)%n],l=s.x-o.x,a=s.y-o.y,c=r.x-s.x,d=l*(r.y-s.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 N(e,t,n,i){return((255&e)<<24|(63&i)<<18|(511&n)<<9|511&t)>>>0}O.prototype.getNum=function(e){return this.get({sourceSlot:e>>>24&255,altIdx:e>>>18&63,row:e>>>9&511,col:511&e,layerId:"__num__"})},O.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 B=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 V(e){return 255&e}function $(e){return e>>>8&255}function P(e){return e>>>16&255}function H(e){return e>>>24&31}function K(e,t){var n;const i=[];for(const[o,s]of Object.entries(e.chunksByKey)){const[r,l]=o.split(","),a=Number.parseInt(r,10),c=Number.parseInt(l,10);if(Number.isFinite(a)&&Number.isFinite(c))for(let r=0;r<16;r++)for(let l=0;l<16;l++){const d=s[16*r+l],u=V(d);if(0===u)continue;const h=$(d),f=P(d),p=H(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+r)*e.cellHeight+t.worldY;for(const t of y)for(const s of t.polygons){const a=N(u,h,f,p);let c=null===(n=e.cache.getNum(a))||void 0===n?void 0:n.parts;c||(c=D(s.points),e.cache.setNum(a,{parts:c}));const d=k(c,v,g);for(let n=0;n<d.length;n++)i.push({id:`tile-${o}-${l}-${r}-${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,r]}})}}}return i}try{e.resource.registerResourceType("TILESET")}catch(e){}try{e.resource.registerInstance("TILESET",e=>{var t,n,i,o;const s=null===(t=e.data)||void 0===t?void 0:t.json,r=null===(i=null===(n=e.src)||void 0===n?void 0:n.json)||void 0===i?void 0:i.data;return null!==(o=null!=s?s:r)&&void 0!==o?o:null})}catch(e){}exports.ADR_0019_NATIVE_REGISTRY_AVAILABLE=!1,exports.CHUNK_SIZE=a,exports.DecompositionCache=O,exports.MAX_TERRAIN_ID=14,exports.NEIGHBOR_DIRECTIONS=b,exports.NO_NEIGHBOR=15,exports.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[b[e]];void 0===n?(i.push(15),o.push(0)):(i.push(15&n),o.push(15))}const s=S(i),r=S(o);if(o.every(e=>15===e)){const t=null!==(n=this.exact.get(s))&&void 0!==n?n:[];t.push(e),this.exact.set(s,t)}else this.wildcards.push({key:s,mask:r,candidate:e})}match(e){const t=S(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:s}of this.wildcards){let r=0,l=!0;for(let o=0;o<8;o++){if(0===w(n,o))continue;if(w(t,o)!==w(e,o)&&(r++,r>i)){l=!1;break}}l&&(r<i?(i=r,o=[s]):r===i&&o.push(s))}return{candidates:o,exactMatch:!1,bestHammingDistance:i===1/0?-1:i}}get exactBucketCount(){return this.exact.size}get wildcardCount(){return this.wildcards.length}},exports.TILEMAP_BODY_SOURCE_REGISTRY=B,exports.TILEMAP_PROBE_NAMES=y,exports.TILE_FRAG_SHADER=_,exports.TILE_SHADER_SOURCES=E,exports.TILE_VERT_SHADER=I,exports.TileAnimationDriver=p,exports.Tilemap=l,exports.TilemapSystem=x,exports.adaptGamePerfProbes=g,exports.buildChunkGeometry=function(e){const{cells:t,chunkWorldX:n,chunkWorldY:i,cellWidth:o,cellHeight:s,atlasInfo:r}=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++)d(t[e])||l++;const a=new Float32Array(4*l*8),u=new Uint16Array(6*l);let h=0,f=0,p=0;for(let e=0;e<A;e++)for(let l=0;l<A;l++){const m=t[e*A+l];if(d(m))continue;const y=c(m),v=n+l*o,g=i+e*s,C=v+o,T=g+s,x=(r.margins.x+y.col*(r.regionWidth+r.separation.x))/r.textureWidth,b=x+r.regionWidth/r.textureWidth,S=(r.margins.y+y.row*(r.regionHeight+r.separation.y))/r.textureHeight,w=S+r.regionHeight/r.textureHeight,I=y.flipH?1:0,_=y.flipV?1:0,E=y.transpose?1:0,R=0,M=(e,t,n,i)=>{a[h++]=e,a[h++]=t,a[h++]=n,a[h++]=i,a[h++]=I,a[h++]=_,a[h++]=E,a[h++]=R};M(v,g,x,S),M(C,g,b,S),M(C,T,b,w),M(v,T,x,w);const L=4*p;u[f++]=L,u[f++]=L+1,u[f++]=L+2,u[f++]=L,u[f++]=L+2,u[f++]=L+3,p++}return{vertexData:a,indices:u,quadCount:p}},exports.buildChunkMesh=function(e){const t=new i.Container;return t.label=`chunk-mesh-stub-${e.chunkKey}`,t},exports.buildTileMapStaticBodies=K,exports.buildTileMapStaticBodyDefinitions=function(e,t,n){var i,o;let s;null==n||n.beginTiming(y.PHYSICS_REBAKE_MS);try{s=K(e,t)}finally{null==n||n.endTiming(y.PHYSICS_REBAKE_MS)}const r=new Map;for(const e of s){const t=null!==(o=null===(i=e.metadata)||void 0===i?void 0:i.layerId)&&void 0!==o?o:"<unlabeled>";let n=r.get(t);n||(n=[],r.set(t,n)),n.push(e)}const l=s.length;return null==n||n.gauge(y.BODIES_TOTAL,l),l>0&&(null==n||n.count(y.BODIES_CREATED,l)),{bodies:s,totalBodyCount:l,bodiesByLayerId:r}},exports.cacheKeyOf=L,exports.cacheKeyOfNum=N,exports.computeAffectedChunks=function(e,t,n){const i=new Set,o=(e,i,o)=>{const s=t.get(e);if(!s)return!1;const r=`${s}/${i},${o}`;return n.changedTilesByKey.has(r)||n.removedTilesByKey.has(r)||n.addedTilesByKey.has(r)};for(const[t,n]of Object.entries(e))for(let e=0;e<n.length;e++){const s=n[e],r=255&s;if(0===r)continue;if(o(r,s>>>8&255,s>>>16&255)){i.add(t);break}}return i},exports.createInMemoryProbeRegistry=function(){const e=new Map,t=new Map,n=new Map,i=new Map;return{beginTiming(t){e.set(t,v())},endTiming(n){const i=e.get(n);if(void 0===i)return;e.delete(n);const o=v()-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}},exports.decodeChunk=u,exports.decomposePolygon=D,exports.diffTilesetDocuments=M,exports.diffTilesetForChunkRebuild=R,exports.estimateChunkMeshMemoryKB=function(e){return Math.ceil(11)},exports.estimateSpriteNodes=function(e){return 1+e},exports.expandSceneCollectionSource=function(e){return"sceneCollection"!==e.kind?[]:e.prefabRefs.map((t,n)=>({sourceId:e.id,prefabName:t,index:n}))},exports.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 s of e.alternatives){if(!s.physics||0===s.physics.length)continue;const e=`${o},${n},${i},${31&s.altId}`;t.set(e,s.physics)}}}return t},exports.getRequiredTilemapProbes=function(){return Object.values(y)},exports.isEmptyCellValue=d,exports.isMeshPathAvailable=function(){return!1},exports.makeLoadedTileset=f,exports.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]},exports.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 s=e>>>8&255;return null!==(n=o.prefabRefs[s])&&void 0!==n?n:null},exports.resolveChunkRenderStrategy=C,exports.translateConvexParts=k,exports.unpackCell=c;