Files
GDevelop/Extensions/TileMap/helper/TileMapHelper.js
T

3 lines
15 KiB
JavaScript

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TileMapHelper={})}(this,(function(t){"use strict";var e=function(t,i){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])},e(t,i)};function i(t,i){if("function"!=typeof i&&null!==i)throw new TypeError("Class extends value "+String(i)+" is not a constructor or null");function n(){this.constructor=t}e(t,i),t.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}function n(t){var e="function"==typeof Symbol&&Symbol.iterator,i=e&&t[e],n=0;if(i)return i.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}var r=function(){function t(t,e,i,n,r){this.tileWidth=t,this.tileHeight=e,this.dimX=i,this.dimY=n,this._tileSet=r,this._layers=[]}return t.prototype.getWidth=function(){return this.tileWidth*this.dimX},t.prototype.getHeight=function(){return this.tileHeight*this.dimY},t.prototype.getTileHeight=function(){return this.tileWidth},t.prototype.getTileWidth=function(){return this.tileHeight},t.prototype.getDimensionX=function(){return this.dimX},t.prototype.getDimensionY=function(){return this.dimY},t.prototype.getTileDefinition=function(t){return this._tileSet.get(t)},t.prototype.getTileDefinitions=function(){return this._tileSet.values()},t.prototype.addTileLayer=function(t){var e=new p(this,t);return this._layers.push(e),e},t.prototype.addObjectLayer=function(t){var e=new o(this,t);return this._layers.push(e),e},t.prototype.getLayers=function(){return this._layers},t.prototype.pointIsInsideTile=function(t,e,i){var r,l,o=Math.floor(t/this.tileWidth),a=Math.floor(e/this.tileHeight);try{for(var s=n(this._layers),p=s.next();!p.done;p=s.next()){var d=p.value;if(d){var u=d.get(o,a);if(void 0===u)return!1;if(this._tileSet.get(u).hasTag(i))return!0}}}catch(t){r={error:t}}finally{try{p&&!p.done&&(l=s.return)&&l.call(s)}finally{if(r)throw r.error}}return!1},t}(),l=function(){function t(t,e){this.visible=!0,this.tileMap=t,this.id=e}return t.prototype.setVisible=function(t){this.visible=t},t.prototype.isVisible=function(){return this.visible},t}(),o=function(t){function e(e,i){var n=t.call(this,e,i)||this;return n.objects=[],n}return i(e,t),e.prototype.add=function(t){this.objects.push(t)},e}(l),a=function(){function t(t,e,i){this.tileId=i,this.x=t,this.y=e}return t.prototype.getTileId=function(){return s.getTileId(this.tileId)},t.prototype.setFlippedHorizontally=function(t){this.tileId=s.setFlippedHorizontally(this.tileId,t)},t.prototype.setFlippedVertically=function(t){this.tileId=s.setFlippedVertically(this.tileId,t)},t.prototype.setFlippedDiagonally=function(t){this.tileId=s.setFlippedDiagonally(this.tileId,t)},t.prototype.isFlippedHorizontally=function(){return s.isFlippedHorizontally(this.tileId)},t.prototype.isFlippedVertically=function(){return s.isFlippedVertically(this.tileId)},t.prototype.isFlippedDiagonally=function(){return s.isFlippedDiagonally(this.tileId)},t}(),s=function(){function t(){}return t.getTileId=function(e){return e&t.tileIdMask},t.setFlippedHorizontally=function(e,i){return e&=~t.flippedHorizontallyFlag,i&&(e|=t.flippedHorizontallyFlag),e},t.setFlippedVertically=function(e,i){return e&=~t.flippedVerticallyFlag,i&&(e|=t.flippedVerticallyFlag),e},t.setFlippedDiagonally=function(e,i){return e&=~t.flippedDiagonallyFlag,i&&(e|=t.flippedDiagonallyFlag),e},t.isFlippedHorizontally=function(e){return 0!=(e&t.flippedHorizontallyFlag)},t.isFlippedVertically=function(e){return 0!=(e&t.flippedVerticallyFlag)},t.isFlippedDiagonally=function(e){return 0!=(e&t.flippedDiagonallyFlag)},t.flippedHorizontallyFlag=2147483648,t.flippedVerticallyFlag=1073741824,t.flippedDiagonallyFlag=536870912,t.tileIdMask=~(t.flippedHorizontallyFlag|t.flippedVerticallyFlag|t.flippedDiagonallyFlag),t}(),p=function(t){function e(e,i){var n=t.call(this,e,i)||this;n._tiles=[],n._tiles.length=n.tileMap.getDimensionY();for(var r=0;r<n._tiles.length;r++)n._tiles[r]=new Int32Array(n.tileMap.getDimensionX());return n}return i(e,t),e.prototype.setTile=function(t,e,i){this.tileMap.getTileDefinition(i)?this._tiles[e][t]=i+1:console.error("Invalid tile definition index: ".concat(i))},e.prototype.removeTile=function(t,e){this._tiles[e][t]=0},e.prototype.setFlippedHorizontally=function(t,e,i){var n=this._tiles[e][t];0!==n&&(this._tiles[e][t]=s.setFlippedHorizontally(n,i))},e.prototype.setFlippedVertically=function(t,e,i){var n=this._tiles[e][t];0!==n&&(this._tiles[e][t]=s.setFlippedVertically(n,i))},e.prototype.setFlippedDiagonally=function(t,e,i){var n=this._tiles[e][t];0!==n&&(this._tiles[e][t]=s.setFlippedDiagonally(n,i))},e.prototype.isFlippedHorizontally=function(t,e){return s.isFlippedHorizontally(this._tiles[e][t])},e.prototype.isFlippedVertically=function(t,e){return s.isFlippedVertically(this._tiles[e][t])},e.prototype.isFlippedDiagonally=function(t,e){return s.isFlippedDiagonally(this._tiles[e][t])},e.prototype.get=function(t,e){var i=this._tiles[e];if(i&&0!==i[t])return s.getTileId(i[t]-1)},e.prototype.getDimensionX=function(){return 0===this._tiles.length?0:this._tiles[0].length},e.prototype.getDimensionY=function(){return this._tiles.length},e.prototype.getWidth=function(){return this.tileMap.getWidth()},e.prototype.getHeight=function(){return this.tileMap.getHeight()},e}(l),d=function(){function t(t){this.taggedHitBoxes=[],this.animationLength=t}return t.prototype.add=function(t,e){var i=this.taggedHitBoxes.find((function(e){return e.tag===t}));i||(i={tag:t,polygons:[]},this.taggedHitBoxes.push(i)),i.polygons.push(e)},t.prototype.hasTag=function(t){return this.taggedHitBoxes.some((function(e){return e.tag===t}))},t.prototype.getHitBoxes=function(t){var e=this.taggedHitBoxes.find((function(e){return e.tag===t}));return e&&e.polygons},t.prototype.getAnimationLength=function(){return this.animationLength},t}(),u=function(){function t(){this._cachedValues=new Map,this._callbacks=new Map}return t.prototype.getOrLoad=function(t,e,i){var r=this,l=this._cachedValues.get(t);if(l)i(l);else{var o=this._callbacks.get(t);o?o.push(i):(this._callbacks.set(t,[i]),e((function(e){var i,l;e&&r._cachedValues.set(t,e);var o=r._callbacks.get(t);r._callbacks.delete(t);try{for(var a=n(o),s=a.next();!s.done;s=a.next()){(0,s.value)(e)}}catch(t){i={error:t}}finally{try{s&&!s.done&&(l=a.return)&&l.call(a)}finally{if(i)throw i.error}}})))}},t}(),c=function(t,e){var i=e.data,n=e.compression;if(!i)return i;var r=4,l=[],o=atob(i).split("").map((function(t){return t.charCodeAt(0)}));try{var a=function(t,e){return t[e]+(t[e+1]<<8)+(t[e+2]<<16)+(t[e+3]<<24)>>>0};if("zlib"===n)for(var s=new Uint8Array(o),p=t.inflate(s);r<=p.length;)l.push(a(p,r-4)),r+=4;else{if("zstd"===n)return console.error("Zstandard compression is not supported for layers in a Tilemap. Use instead zlib compression or no compression."),null;for(;r<=o.length;)l.push(a(o,r-4)),r+=4}return l}catch(t){return console.error("Failed to decompress and unzip base64 layer.data string",t),null}},f=function(t){var e=2147483648,i=1073741824,n=536870912,r=t&e,l=t&i,o=t&n;return{id:h(536870911&t),flippedHorizontally:!!r,flippedVertically:!!l,flippedDiagonally:!!o}},h=function(t){return 0===t?void 0:t-1},y=function(){function t(){}return t.load=function(t,e){var i,l,o,s,p,u,y,g,v,x;if(!e.tiledversion)return console.warn("The loaded Tiled map does not contain a 'tiledversion' key. Are you sure this file has been exported from Tiled (mapeditor.org)?"),null;var F=new Map;try{for(var w=n(e.tilesets),T=w.next();!T.done;T=w.next()){var b=T.value,_=void 0===b.firstgid?1:b.firstgid;if(b.tiles)try{for(var m=(o=void 0,n(b.tiles)),M=m.next();!M.done;M=m.next()){var H=M.value,D=new d(H.animation?H.animation.length:0);if(H.objectgroup){var I=function(t){var e=t.class||H.class;if(!e||0===e.length)return"continue";var i=null;if(t.polygon){var n=t.rotation*Math.PI/180,r=Math.cos(n),l=Math.sin(n);-1!==r&&1!==r||(l=0),-1!==l&&1!==l||(r=0),i=t.polygon.map((function(e){return[t.x+e.x*r-e.y*l,t.y+e.x*l+e.y*r]}))}else void 0!==t.x&&void 0!==t.y&&void 0!==t.width&&void 0!==t.height&&(i=[[t.x,t.y],[t.x,t.y+t.height],[t.x+t.width,t.y+t.height],[t.x+t.width,t.y]]);i&&D.add(e,i)};try{for(var V=(p=void 0,n(H.objectgroup.objects)),z=V.next();!z.done;z=V.next()){I(G=z.value)}}catch(t){p={error:t}}finally{try{z&&!z.done&&(u=V.return)&&u.call(V)}finally{if(p)throw p.error}}}else if(H.class&&H.class.length>0){var L=[[0,0],[0,e.tileheight],[e.tilewidth,e.tileheight],[e.tilewidth,0]];D.add(H.class,L)}F.set(h(_+H.id),D)}}catch(t){o={error:t}}finally{try{M&&!M.done&&(s=m.return)&&s.call(m)}finally{if(o)throw o.error}}for(var j=0;j<b.tilecount;j++){var k=h(_+j);F.has(k)||F.set(k,new d(0))}}}catch(t){i={error:t}}finally{try{T&&!T.done&&(l=w.return)&&l.call(w)}finally{if(i)throw i.error}}var C=new r(e.tilewidth,e.tileheight,e.width,e.height,F);try{for(var O=n(e.layers),X=O.next();!X.done;X=O.next()){var A=X.value;if("objectgroup"===A.type){var P=C.addObjectLayer(A.id);P.setVisible(A.visible);try{for(var W=(v=void 0,n(A.objects)),S=W.next();!S.done;S=W.next()){var Y=S.value;if(Y.visible&&Y.gid){var B=f(Y.gid),G=new a(Y.x,Y.y,B.id);P.add(G),G.setFlippedHorizontally(B.flippedHorizontally),G.setFlippedVertically(B.flippedVertically),G.setFlippedDiagonally(B.flippedDiagonally)}}}catch(t){v={error:t}}finally{try{S&&!S.done&&(x=W.return)&&x.call(W)}finally{if(v)throw v.error}}}else if("tilelayer"===A.type){var E=0,U=null;if("base64"===A.encoding?(U=c(t,A))||console.warn("Failed to uncompress layer.data"):U=A.data,U){var R=C.addTileLayer(A.id);R.setVisible(A.visible);for(var Z=0;Z<A.height;Z++)for(var q=0;q<A.width;q++){var J=U[E],K=f(J);void 0!==K.id&&(R.setTile(q,Z,K.id),R.setFlippedHorizontally(q,Z,K.flippedHorizontally),R.setFlippedVertically(q,Z,K.flippedVertically),R.setFlippedDiagonally(q,Z,K.flippedDiagonally)),E+=1}}}}}catch(t){y={error:t}}finally{try{X&&!X.done&&(g=O.return)&&g.call(O)}finally{if(y)throw y.error}}return C},t}(),g=GlobalPIXIModule.PIXI,v=function(){function t(){this._textures=new Map}return t.prototype.setTexture=function(t,e,i,n,r){var l=this._getGlobalId(t,e,i,n);this._textures.set(l,r)},t.prototype.findTileTexture=function(t,e,i,n){var r=this._getGlobalId(t,e,i,n);if(this._textures.has(r))return this._textures.get(r);var l=this._textures.get(t);if(l){var o=l.frame.clone(),a=l.orig.clone();if(n){var s=a.width;a.width=a.height,a.height=s}var p=a.clone(),d=0;n?(d=10,!e&&i?d=2:e&&!i?d=6:e&&i&&(d=14)):(d=0,!e&&i?d=8:e&&!i?d=12:e&&i&&(d=4));var u=new g.Texture(l.baseTexture,o,a,p,d);return this._textures.set(r,u),u}},t.prototype._getGlobalId=function(e,i,n,r){var l=e;return i&&(l|=t.flippedHorizontallyFlag),n&&(l|=t.flippedVerticallyFlag),r&&(l|=t.flippedDiagonallyFlag),l},t.flippedHorizontallyFlag=2147483648,t.flippedVerticallyFlag=1073741824,t.flippedDiagonallyFlag=536870912,t}(),x=GlobalPIXIModule.PIXI,F=function(){function t(){}return t.parseAtlas=function(t,e,i){if(!t.tiledversion)return console.warn("The loaded Tiled map does not contain a 'tiledversion' key. Are you sure this file has been exported from Tiled (mapeditor.org)?"),null;if(!t.tilesets.length||"source"in t.tilesets[0])return console.warn("The loaded Tiled map seems not to contain any tileset data (nothing in 'tilesets' key)."),null;var n=t.tilesets[0],r=n.tilewidth,l=n.tileheight,o=n.tilecount,a=n.image,s=n.columns,p=n.spacing,d=n.margin,u=void 0===n.firstgid?1:n.firstgid;e||(e=i(a));var c=o/s,f=r*s+p*(s-1)+2*d,y=l*c+p*(c-1)+2*d;if(1!==e.width&&e.width<f||e.width>=f+p+r||1!==e.height&&e.height<y||e.height>=y+p+l)return console.error("It seems the atlas file was resized, which is not supported. "+"It should be ".concat(f,"x").concat(y," px, ")+"but it's ".concat(e.width,"x").concat(e.height," px.")),null;if(1!==e.width&&e.width!==f||1!==e.height&&e.height!==y)return console.warn("It seems the atlas file has unused pixels. "+"It should be ".concat(f,"x").concat(y," px, ")+"but it's ".concat(e.width,"x").concat(e.height," px.")),null;for(var g=new v,F=0;F<o;F++){var w=d+Math.floor(F%s)*(r+p),T=d+Math.floor(F/s)*(l+p),b=h(u+F);try{var _=new x.Rectangle(w,T,r,l),m=new x.Texture(e,_);g.setTexture(b,!1,!1,!1,m)}catch(t){console.error("An error occurred while creating a PIXI.Texture to be used in a TileMap:",t)}}return g},t.updatePixiTileMap=function(t,e,i,r,l){var a,s,d,u,c=t;if(c){c.clear();try{for(var f=n(e.getLayers()),h=f.next();!h.done;h=f.next()){var y=h.value;if("index"===r&&l!==y.id||"visible"===r&&!y.isVisible())return;if(y instanceof o){var g=y;try{for(var v=(d=void 0,n(g.objects)),x=v.next();!x.done;x=v.next()){var F=x.value,w=i.findTileTexture(F.getTileId(),F.isFlippedHorizontally(),F.isFlippedVertically(),F.isFlippedDiagonally());w&&c.addFrame(w,F.x,F.y-g.tileMap.getTileHeight())}}catch(t){d={error:t}}finally{try{x&&!x.done&&(u=v.return)&&u.call(v)}finally{if(d)throw d.error}}}else if(y instanceof p)for(var T=y,b=T.tileMap.getTileWidth(),_=T.tileMap.getTileHeight(),m=T.tileMap.getDimensionX(),M=T.tileMap.getDimensionY(),H=0;H<M;H++)for(var D=0;D<m;D++){var I=b*D,V=_*H,z=T.get(D,H);if(void 0!==z){var L=i.findTileTexture(z,T.isFlippedHorizontally(D,H),T.isFlippedVertically(D,H),T.isFlippedDiagonally(D,H));if(L){var j=c.addFrame(L,I,V),k=T.tileMap.getTileDefinition(z);k&&k.getAnimationLength()>0&&j.tileAnimX(b,k.getAnimationLength())}else console.warn("Unknown tile id: ".concat(z," at (").concat(D,", ").concat(H,")"))}}}}catch(t){a={error:t}}finally{try{h&&!h.done&&(s=f.return)&&s.call(f)}finally{if(a)throw a.error}}}},t.updatePixiCollisionMask=function(t,e,i,r,l,o,a,s){var d,u,c,f;if(t){t.clear(),t.lineStyle(r,l,o),t.drawRect(0,0,e.getWidth(),e.getHeight());try{for(var h=n(e.getLayers()),y=h.next();!y.done;y=h.next()){var g=y.value,v=e.getTileWidth(),x=e.getTileHeight();if(g instanceof p)for(var F=g,w=0;w<F.tileMap.getDimensionY();w++)for(var T=0;T<F.tileMap.getDimensionX();T++){var b=v*T,_=x*w,m=F.get(T,w),M=F.isFlippedHorizontally(T,w),H=F.isFlippedVertically(T,w),D=F.isFlippedDiagonally(T,w),I=F.tileMap.getTileDefinition(m);if(I){var V=I.getHitBoxes(i);if(V)try{for(var z=(c=void 0,n(V)),L=z.next();!L.done;L=z.next()){var j=L.value;if(0!==j.length){t.beginFill(a,s);for(var k=0;k<j.length;k++){var C=j[k][0],O=j[k][1];if(D){var X=C;C=O,O=X}M&&(C=v-C),H&&(O=x-O),0===k?t.moveTo(b+C,_+O):t.lineTo(b+C,_+O)}t.closePath(),t.endFill()}}}catch(t){c={error:t}}finally{try{L&&!L.done&&(f=z.return)&&f.call(z)}finally{if(c)throw c.error}}}}}}catch(t){d={error:t}}finally{try{y&&!y.done&&(u=h.return)&&u.call(h)}finally{if(d)throw d.error}}}},t}(),w=function(){function t(){this._tileMapCache=new u,this._textureCacheCaches=new u}return t.getManager=function(e){return e.tileMapCollisionMaskManager||(e.tileMapCollisionMaskManager=new t),e.tileMapCollisionMaskManager},t.prototype.getOrLoadTileMap=function(t,e,i,n,r){var l=e+"|"+i;this._tileMapCache.getOrLoad(l,(function(r){t(e,i,(function(t){if(t){var e=y.load(n,t);r(e)}else r(null)}))}),r)},t.prototype.getOrLoadTextureCache=function(t,e,i,n,r,l){var o=n+"|"+r+"|"+i;this._textureCacheCaches.getOrLoad(o,(function(l){t(n,r,(function(t){if(t){var n=i?e(i):null,r=F.parseAtlas(t,n,e);l(r)}else l(null)}))}),l)},t}();t.EditableTileMap=r,t.EditableTileMapLayer=p,t.PixiTileMapHelper=F,t.TileDefinition=d,t.TileMapManager=w,t.TileTextureCache=v,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=TileMapHelper.js.map