-
Notifications
You must be signed in to change notification settings - Fork 16
/
p5.tiledmap.min.js
1 lines (1 loc) · 13.3 KB
/
p5.tiledmap.min.js
1
!function(t,i){"function"==typeof define&&define.amd?define("p5.tiledmap",["p5"],function(t){i(t)}):"object"==typeof exports?i(require("../p5")):i(t.p5)}(this,function(t){function i(t){"use strict";if(t<TileMaps[this.name].tilesets.length){var e=TileMaps[this.name].tilesets[t];if(e.image)loadImage(this.path+e.image,function(e){(function(t,e){var a=TileMaps[this.name].tilesets[e],s=a.firstgid,h=a.tilewidth,o=a.tileheight,r=a.tilecount||0,n=a.columns||0,l=a.spacing||0,p=a.margin||0;if(a.tileoffset)var c=a.tileoffset.x||0,m=a.tileoffset.y||0;else var c=0,m=0;0==n&&(n=floor((t.width-p)/(h+l)));0==r&&(r=n*floor((t.height-p)/(o+l)));a.transparentcolor&&function(t,i,e){t.loadPixels();for(var a=0;a<t.pixels.length;a+=4)abs(t.pixels[a]-red(i))<e&&abs(t.pixels[a+1]-green(i))<e&&abs(t.pixels[a+2]==blue(i))<e&&(t.pixels[a+3]=0);t.updatePixels()}(t,color(a.transparentcolor),this.transparentoffset);for(var f=0;f<r;f++){var d=t.get(p+f%n*(h+l),p+floor(f/n)*(o+l),h,o);this.tile[s+f]=new Tile(d,c,m)}i.call(this,++e)}).call(this,e,t)}.bind(this));else if(e.tiles){var a=e.firstgid;if(e.tileoffset)var s=e.tileoffset.x||0,h=e.tileoffset.y||0;else s=0,h=0;for(var o in e.tiles){var r=loadImage(this.path+e.tiles[o].image);this.tile[a+int(o)]=new Tile(r,s,h)}}}}t.TiledMap=function(t,e,a){"use strict";switch(this.name=t,this.path=e,this.drawmode=CORNER,this.positionmode="CANVAS",this.transparentoffset=a,this.drawmargin=2,this.tile=[],this.layerimage=[],this.orientation=TileMaps[this.name].orientation,this.tilewidth=TileMaps[this.name].tilewidth,this.tileheight=TileMaps[this.name].tileheight,this.mapwidth=TileMaps[this.name].width,this.mapheight=TileMaps[this.name].height,this.camleft=0,this.camtop=0,this.camwidth=parent.width,this.camheight=parent.height,this.staggeraxis=TileMaps[this.name].staggeraxis||"x",this.staggerindex=Number("even"==TileMaps[this.name].staggerindex),this.hexsidelength=TileMaps[this.name].hexsidelength||0,TileMaps[this.name].orientation){case"isometric":drawTileLayer=function(t,i){for(var e,a,s,h,o,r=t.offsetx||0,n=t.offsety||0,l=max(0,floor(this.canvasToMap(this.camleft,this.camtop).x-this.drawmargin)),p=min(this.mapwidth,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).x+this.drawmargin)),c=max(0,floor(this.canvasToMap(this.camleft+i.width,this.camtop).y-this.drawmargin)),m=min(this.mapheight,ceil(this.canvasToMap(this.camleft,this.camtop+i.height).y+this.drawmargin)),f=c;f<m;f++)for(var d=l;d<p;d++)e=t.data[d+f*this.mapwidth],o=this.tile[e]?this.tile[e]:this.tile[0],a=(h=this.mapToCam(d,f)).x-this.tilewidth/2+o.offsetx+r,s=h.y-this.tileheight/2+o.offsety+n+(this.tileheight-o.image.height),i.image(o.image,a,s)},mapToCanvas=function(t){return createVector((this.mapwidth+this.mapheight)*this.tilewidth/4+(t.x-t.y)*this.tilewidth/2,(t.x+t.y+1)*this.tileheight/2)},canvasToMap=function(t){var i=(this.mapwidth+this.mapheight)*this.tilewidth/4;return createVector(t.y/this.tileheight+(t.x-i)/this.tilewidth-.5,t.y/this.tileheight-(t.x-i)/this.tilewidth-.5)};break;case"staggered":this.hexsidelength=0;case"hexagonal":switch(this.staggeraxis){case"x":drawTileLayer=function(t,i){var e,a,s,h,o,r=t.offsetx||0,n=t.offsety||0,l=floor(this.canvasToMap(this.camleft,this.camtop).x-this.drawmargin);l=max(0,l-l%2);for(var p=min(this.mapwidth,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).x+this.drawmargin)),c=max(0,floor(this.canvasToMap(this.camleft,this.camtop).y-this.drawmargin)),m=min(this.mapheight,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).y+this.drawmargin)),f=c;f<m;f++)for(var d=l;d<=p;d+=2)for(var g=this.staggerindex;g>=this.staggerindex-1;g--)d+g>=0&&d+g<this.mapwidth&&(e=t.data[d+g+f*this.mapwidth],o=this.tile[e]?this.tile[e]:this.tile[0],a=(h=this.mapToCam(d+g,f)).x-this.tilewidth/2+o.offsetx+r,s=h.y-this.tileheight/2+o.offsety+n+(this.tileheight-o.image.height),i.image(o.image,a,s))},mapToCanvas=function(t){var i=(t.x*(this.hexsidelength+this.tilewidth)+this.tilewidth)/2,e=(t.y+.5+this.staggerindex/2)*this.tileheight+(2*this.staggerindex-1)*(abs(abs(t.x)%2-1)-1)*this.tileheight/2;return createVector(i,e)},canvasToMap=function(t){var i,e=(2*t.x-this.tilewidth)/(this.hexsidelength+this.tilewidth);return i=0==this.staggerindex?t.y/this.tileheight-1+abs(abs(e)%2-1)/2:t.y/this.tileheight-.5-abs(abs(e)%2-1)/2,createVector(e,i)};break;case"y":drawTileLayer=function(t,i){for(var e,a,s,h,o,r=t.offsetx||0,n=t.offsety||0,l=max(0,floor(this.canvasToMap(this.camleft,this.camtop).x-this.drawmargin)),p=min(this.mapwidth,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).x+this.drawmargin)),c=max(0,floor(this.canvasToMap(this.camleft,this.camtop).y-this.drawmargin)),m=min(this.mapheight,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).y+this.drawmargin)),f=c;f<m;f++)for(var d=l;d<p;d++)e=t.data[d+f*this.mapwidth],o=this.tile[e]?this.tile[e]:this.tile[0],a=(h=this.mapToCam(d,f)).x-this.tilewidth/2+o.offsetx+r,s=h.y-this.tileheight/2+o.offsety+n+(this.tileheight-o.image.height),i.image(o.image,a,s)},mapToCanvas=function(t){var i=t.y*(this.hexsidelength+this.tileheight)/2+this.tileheight/2,e=(t.x+.5+this.staggerindex/2)*this.tilewidth+(2*this.staggerindex-1)*(abs(abs(t.y)%2-1)-1)*this.tilewidth/2;return createVector(e,i)},canvasToMap=function(t){var i,e=(2*t.y-this.tileheight)/(this.hexsidelength+this.tileheight);return i=0==this.staggerindex?t.x/this.tilewidth-1+abs(abs(e)%2-1)/2:t.x/this.tilewidth-.5-abs(abs(e)%2-1)/2,createVector(i,e)}}break;default:drawTileLayer=function(t,i){for(var e,a,s,h,o,r=t.offsetx||0,n=t.offsety||0,l=max(0,floor(this.canvasToMap(this.camleft,this.camtop).x-this.drawmargin)),p=min(this.mapwidth,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).x+this.drawmargin)),c=max(0,floor(this.canvasToMap(this.camleft,this.camtop).y-this.drawmargin)),m=min(this.mapheight,ceil(this.canvasToMap(this.camleft+i.width,this.camtop+i.height).y+this.drawmargin)),f=c;f<m;f++)for(var d=l;d<p;d++)e=t.data[d+f*this.mapwidth],o=this.tile[e]?this.tile[e]:this.tile[0],a=(h=this.mapToCam(d,f)).x-this.tilewidth/2+o.offsetx+r,s=h.y-this.tileheight/2+o.offsety+n+(this.tileheight-o.image.height),i.image(o.image,a,s)},mapToCanvas=function(t){return createVector((t.x+.5)*this.tilewidth,(t.y+.5)*this.tileheight)},canvasToMap=function(t){return createVector(t.x/this.tilewidth-.5,t.y/this.tileheight-.5)}}for(var s=0;s<TileMaps[this.name].layers.length;s++){var h=TileMaps[this.name].layers[s];"imagelayer"==h.type&&(this.layerimage[h.image]=loadImage(this.path+h.image))}this.tile[0]=new Tile(createImage(1,1),0,0),i.call(this,0)},Tile=function(t,i,e){"use strict";this.image=t,this.offsetx=i,this.offsety=e},drawTileLayer=function(t,i){},mapToCanvas=function(t){},canvasToMap=function(t){},t.prototype.registerPreloadMethod("loadTiledMap",t.prototype),t.prototype.loadTiledMap=function(){"use strict";for(var i=arguments[0],e="",a=4,s=null,h=1;h<arguments.length;h++)"string"==typeof arguments[h]&&(e=arguments[h]),"number"==typeof arguments[h]&&(a=arguments[h]),"function"==typeof arguments[h]&&(s=arguments[h]);if("/"!=e[-1]&&(e+="/"),!TileMaps)throw"No Tiled Map found!";if(!TileMaps[i])throw"No Tiled Map named "+i+" found!";var o=new t.TiledMap(i,e,a);return"function"==typeof self._decrementPreload&&self._decrementPreload(),"function"==typeof s&&s(o),o},t.TiledMap.prototype.drawLayer=function(t,i,e,a){"use strict";if(a||(a=parent),this.camwidth=a.width,this.camheight=a.height,"MAP"==this.positionmode){var s=this.mapToCanvas(i,e);i=s.x,e=s.y}this.drawmode==CENTER?(this.camleft=i-this.camwidth/2,this.camtop=e-this.camheight/2):(this.camleft=i,this.camtop=e),a.push(),a.resetMatrix(),a.ellipseMode(CORNER),a.angleMode(DEGREES),a.imageMode(CORNER);var h=TileMaps[this.name].layers[t];switch(h.type){case"tilelayer":drawTileLayer.call(this,h,a);break;case"imagelayer":var o=h.offsetx||0,r=h.offsety||0;a.image(this.layerimage[h.image],-this.camleft+o,-this.camtop+r);break;case"objectgroup":o=h.offsetx||0,r=h.offsety||0;a.fill(color(h.color||0)),a.stroke(color(h.color||0)),a.strokeWeight(1);for(var t in h.objects){var n=h.objects[t];if(n.visible){if(a.push(),a.resetMatrix(),a.translate(n.x-o-this.camleft,n.y-r-this.camtop),n.rotation&&a.rotate(n.rotation),n.ellipse)a.ellipse(0,0,n.width,n.height);else if(n.polyline){a.noFill(),a.beginShape();for(var l in n.polyline)a.vertex(n.polyline[l].x,n.polyline[l].y);a.endShape()}else if(n.polygon){a.beginShape();for(var l in n.polygon)a.vertex(n.polygon[l].x,n.polygon[l].y);a.endShape(CLOSE)}else n.gid?(n.rotation&&a.rotate(-n.rotation),a.translate(0,-this.tile[n.gid].image.height),n.rotation&&a.rotate(n.rotation),a.image(this.tile[n.gid].image,this.tile[n.gid].offsetx,this.tile[n.gid].offsety,n.width,n.height)):a.rect(0,0,n.width,n.height);a.pop()}}}if(a!==parent&&h.opacity<1){a.loadPixels();var p=map(h.opacity,0,1,0,255);for(l=3;l<a.pixels.length;l+=4)a.pixels[l]>p&&(a.pixels[l]=p);a.updatePixels()}a.pop()},t.TiledMap.prototype.draw=function(t,i,e){for(var a,s=0;s<TileMaps[this.name].layers.length;s++)TileMaps[this.name].layers[s].visible&&(a=TileMaps[this.name].layers[s].opacity,TileMaps[this.name].layers[s].opacity=1,this.drawLayer(s,t,i,e),TileMaps[this.name].layers[s].opacity=a)},t.TiledMap.prototype.getName=function(){"use strict";return this.name},t.TiledMap.prototype.getVersion=function(){"use strict";return TileMaps[this.name].version},t.TiledMap.prototype.getOrientation=function(){"use strict";return TileMaps[this.name].orientation},t.TiledMap.prototype.getBackgroundColor=function(){"use strict";return TileMaps[this.name].backgroundcolor?color(TileMaps[this.name].backgroundcolor):color(255)},t.TiledMap.prototype.getMapSize=function(){return createVector(this.mapwidth,this.mapheight)},t.TiledMap.prototype.getTileSize=function(){return createVector(this.tilewidth,this.tileheight)},t.TiledMap.prototype.getHexSideLength=function(){return this.hexsidelength},t.TiledMap.prototype.getCamCorner=function(){return"MAP"==this.positionmode?this.canvasToMap(this.camleft,this.camtop):createVector(this.camleft,this.camtop)},t.TiledMap.prototype.getCamCenter=function(){return"MAP"==this.positionmode?this.canvasToMap(this.camleft+this.camwidth/2,this.camtop+this.camheight/2):createVector(this.camleft+this.camwidth/2,this.camtop+this.camheight/2)},t.TiledMap.prototype.getPosition=function(){return this.drawmode==parent.CORNER?this.getCamCorner():this.getCamCenter()},t.TiledMap.prototype.getCamSize=function(){return createVector(this.camwidth,this.camheight)},t.TiledMap.prototype.setCamSize=function(t,i){"object"==typeof t?(this.camwidth=t.x,this.camheight=t.y):(this.camwidth=t,this.camheight=i)},t.TiledMap.prototype.getDrawMargin=function(){return this.drawmargin},t.TiledMap.prototype.setDrawMargin=function(t){this.drawmargin=t},t.TiledMap.prototype.getDrawMode=function(){return this.drawmode},t.TiledMap.prototype.setDrawMode=function(t){t!=parent.CORNER&&t!=parent.CENTER||(this.drawmode=t)},t.TiledMap.prototype.getPositionMode=function(){return this.positionmode},t.TiledMap.prototype.setPositionMode=function(t){"CANVAS"!=t&&"MAP"!=t||(this.positionmode=t)},t.TiledMap.prototype.getType=function(t){"use strict";return TileMaps[this.name].layers[t].type},t.TiledMap.prototype.getVisible=function(t){"use strict";return TileMaps[this.name].layers[t].visible},t.TiledMap.prototype.setVisible=function(t,i){"use strict";TileMaps[this.name].layers[t].visible=i},t.TiledMap.prototype.getImage=function(t){"use strict";return this.layerimage[TileMaps[this.name].layers[t].image]},t.TiledMap.prototype.getObjects=function(t){"use strict";return TileMaps[this.name].layers[t].objects},t.TiledMap.prototype.getObjectsColor=function(t){"use strict";return color(TileMaps[this.name].layers[t].color||0)},t.TiledMap.prototype.getData=function(t){"use strict";return TileMaps[this.name].layers[t].data},t.TiledMap.prototype.getCustomProperties=function(t){"use strict";return TileMaps[this.name].layers[t].properties},t.TiledMap.prototype.getOpacity=function(t){"use strict";return TileMaps[this.name].layers[t].opacity},t.TiledMap.prototype.setOpacity=function(t,i){"use strict";TileMaps[this.name].layers[t].opacity=min(max(0,i),1)},t.TiledMap.prototype.getTileIndex=function(t,i,e){"use strict";var a,s;return"object"==typeof i?(a=i.x,s=i.y):(a=i,s=e),"tilelayer"==TileMaps[this.name].layers[t].type&&a>=0&&s>=0&&a<this.mapwidth&&s<this.mapheight?TileMaps[this.name].layers[t].data[a+s*this.mapwidth]:void 0},t.TiledMap.prototype.setTileIndex=function(t,i,e,a){"use strict";var s,h,o;"object"==typeof i?(s=i.x,h=i.y,o=e):(s=i,h=e,o=a),"tilelayer"==TileMaps[this.name].layers[t].type&&s>=0&&h>=0&&s<this.mapwidth&&h<this.mapheight&&(TileMaps[this.name].layers[t].data[s+h*this.mapwidth]=o)},t.TiledMap.prototype.canvasToMap=function(t,i){return p="number"==typeof t?createVector(t,i):t,canvasToMap.call(this,p)},t.TiledMap.prototype.mapToCanvas=function(t,i){var e;return e="number"==typeof t?createVector(t,i):t,mapToCanvas.call(this,e)},t.TiledMap.prototype.camToCanvas=function(t,i){var e,a;return"object"==typeof t?(e=t.x,a=t.y):(e=t,a=i),createVector(e+this.camleft,a+this.camtop)},t.TiledMap.prototype.canvasToCam=function(t,i){var e,a;return"object"==typeof t?(e=t.x,a=t.y):(e=t,a=i),createVector(e-this.camleft,a-this.camtop)},t.TiledMap.prototype.camToMap=function(t,i){var e,a;return"object"==typeof t?(e=t.x,a=t.y):(e=t,a=i),this.canvasToMap(this.camToCanvas(e,a))},t.TiledMap.prototype.mapToCam=function(t,i){var e,a;return"object"==typeof t?(e=t.x,a=t.y):(e=t,a=i),this.canvasToCam(this.mapToCanvas(e,a))}});