"use strict";function isIntersect(t,e,i){!0===i&&console.log(e);var n=t.x,r=-t.y,s=t.x+t.width,o=-(t.y+t.height),h=e.x,a=-e.y,l=e.x+e.width,d=-(e.y+e.height);return!(n>l||h>s)&&!(o>a||d>r)}function Pathfinder(t){function e(e,i){var n=[];n.push(i),t.canvasContext.lineWidth=t.tileSize,t.canvasContext.fillStyle="#00f";for(var r in e)try{if(void 0===e[i.getIndex()])continue;i=e[i.getIndex()],n.push(i)}catch(t){console.log("ERROR: "+t.message+"\n for index: "+r)}var s=!0,o=!1,h=void 0;try{for(var a,l=n[Symbol.iterator]();!(s=(a=l.next()).done);s=!0){var d=a.value;t.canvasContext.fillRect(d.x*t.tileSize,d.y*t.tileSize,t.tileSize,t.tileSize)}}catch(t){o=!0,h=t}finally{try{!s&&l.return&&l.return()}finally{if(o)throw h}}return n}function i(e,i){do{var n=i.pop(),r=e.indexOf(t.tiles[n])}while(-1===r);return{value:n,index:r}}function n(){var t=this;this.data=[],this.push=function(e,i){for(var n in t.data)if(t.data[n][0][0]===e[0]&&t.data[n][0][1]===e[1]){t.data.splice(n,1);break}for(var r=0;r0)return this.data.shift()[0]},this.size=function(){return t.data.length},this.contains=function(e){var i=!0,n=!1,r=void 0;try{for(var s,o=t.data[Symbol.iterator]();!(i=(s=o.next()).done);i=!0){var h=s.value;if(h[0][0]===e[0]&&h[0][1]===e[1])return!0}}catch(t){n=!0,r=t}finally{try{!i&&o.return&&o.return()}finally{if(n)throw r}}return!1}}var r=this;this.INFINITY=1e9,this.heuristicCostEstimate=function(t,e){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))},this.findNeighbours=function(e){var i=[];if(e.x-1>=0){n=t.tiles[[e.x-1,e.y]];i.push(n)}if(e.x+1=0){n=t.tiles[[e.x,e.y-1]];i.push(n)}if(e.y+1=0&&e.y-1>=0){n=t.tiles[[e.x-1,e.y-1]];i.push(n)}if(e.x+1=0&&e.y+1=0){var n=t.tiles[[e.x+1,e.y-1]];i.push(n)}return i},this.findPath=function(s,o){var h=[],a=[];a.push(s);var l=[],d=[],f=new n;for(var c in t.tiles)d[c]=1e9;for(d[s.getIndex()]=0,f.push(s.getIndex(),r.heuristicCostEstimate(s,o));a.length>0;){var u=a[i(a,f).index];if(u===o)return e(l,u);t.canvasContext.lineWidth=t.tileSize,t.canvasContext.fillStyle="#f00",t.canvasContext.fillRect(u.x*t.tileSize,u.y*t.tileSize,t.tileSize,t.tileSize),a.splice(a.indexOf(u),1),h[u.getIndex()]=1;var y=!0,v=!1,x=void 0;try{for(var p,g=r.findNeighbours(u)[Symbol.iterator]();!(y=(p=g.next()).done);y=!0){var S=p.value;if(1!==h[S.getIndex()]){var T=d[u.getIndex()]+S.weight*r.heuristicCostEstimate(u,S);if(-1===a.indexOf(S))a.push(S);else if(T>=d[S])continue;l[S.getIndex()]=u,d[S.getIndex()]=T,f.push(S.getIndex(),d[S.getIndex()]+r.heuristicCostEstimate(S,o))}}}catch(t){v=!0,x=t}finally{try{!y&&g.return&&g.return()}finally{if(v)throw x}}}return"failed to find path"}}function Tile(t,e,i,n){var r=this;this.x=t,this.y=e,this.weight=i,this.side=n,this.getIndex=function(){return[r.x,r.y]}}function Unit(t,e,i,n,r){var s=this;Body.call(this,t,e,i,n,r),Unit.prototype=Object.create(Body.prototype),Unit.prototype.constructor=Unit,this.player=0,this.rectColor="#00f",this.setPlayer=function(t){switch(t){case 1:s.player=1,s.rectColor="#f00";break;case 2:s.player=2,s.rectColor="#0f0"}}}function World(t){var e=this;this.canvasContext=t,this.bodies=[],this.height=t.canvas.height,this.width=t.canvas.width,this.removeBuffer=[],this.worldRenderer=new WorldRenderer(this),this.renderRect=!1,this.tileSize=2,this.addBody=function(t){e.bodies.push(t)},this.removeBody=function(t){console.log("removed: "+JSON.stringify(t)),t.resetTileWeights();var i=e.bodies.indexOf(t);i>-1&&e.bodies.splice(i,1)},this.render=function(){e.worldRenderer.renderWorld()},this.FPS=30,this.currentFrame=0,this.update=function(t){e.usersUpdate=t,e.innerUpdate=function(){e.currentFrame++,e.updateTiles(),e.render(),e.usersUpdate();for(var t in e.removeBuffer)e.removeBody(e.removeBuffer[t]);e.removeBuffer=[],requestAnimationFrame(e.innerUpdate)},requestAnimationFrame(e.innerUpdate)},this.updateTiles=function(){var t=!0,i=!1,n=void 0;try{for(var r,s=e.bodies[Symbol.iterator]();!(t=(r=s.next()).done);t=!0){var o=r.value;if("function"==typeof o.currentAnimation){o.currentAnimation(),e.tiles[[o.xTile,o.yTile]].weight=Pathfinder.INFINITY;for(var h=0;he.length-1&&(o=0),s.setCurrentSprite(e[o++]))}},this.setCurrentAnimation=function(t){s.currentAnimation=s.animations[t]},this.addAnimation("default",[0],30),this.currentAnimation=this.animations.default,this.speed=1,this.isMoving=!1,this.pathFinder=new Pathfinder(r).findPath,this.path=[],this.resetTileWeights=function(){for(var t=0;t=r.widthInTiles&&(t=r.widthInTiles-1),e>=r.heightInTiles&&(e=r.heightInTiles-1),s.path=s.pathFinder(r.tiles[[s.xTile,s.yTile]],r.tiles[[t,e]]),s.isMoving=!0;if(r.currentFrame%s.speed==0){var i=s.path.pop();if(void 0===i)return;s.setPosition(i.x*r.tileSize,i.y*r.tileSize)}else s.path.length>0&&(s.xs.path[s.path.length-1].x*r.tileSize&&s.setPosition(s.x-r.tileSize/s.speed,s.y),s.ys.path[s.path.length-1].y*r.tileSize&&s.setPosition(s.x,s.y-r.tileSize/s.speed))},this.getTransform=function(){return{x:s.xTile,y:s.yTile,height:s.height,width:s.width}}},keyEvent={key:void 0,pressed:!1};document.onkeydown=function(t){keyEvent.key=t.key,keyEvent.pressed=!0,console.log(keyEvent)},document.onkeyup=function(t){console.log(keyEvent.key),keyEvent.key=void 0,keyEvent.pressed=!1};var WorldRenderer=function(t){this.renderWorld=function(){t.canvasContext.clearRect(0,0,t.width,t.height);var e=0,i=0;t.canvasContext.lineWidth=.5;for(var n=0;n=t.widthInTiles&&(e++,i=0),t.canvasContext.strokeRect(i*t.tileSize,e*t.tileSize,t.tileSize,t.tileSize),i++;t.canvasContext.lineWidth=1;var r=!0,s=!1,o=void 0;try{for(var h,a=t.bodies[Symbol.iterator]();!(r=(h=a.next()).done);r=!0){var l=h.value;!0===t.renderRect&&(t.canvasContext.strokeStyle=l.rectColor,t.canvasContext.strokeRect(l.x,l.y,l.width,l.height),null!=l.destBody&&(t.canvasContext.strokeStyle="#00f",t.canvasContext.strokeRect(l.destBody.x,l.destBody.y,l.destBody.width,l.destBody.height))),t.canvasContext.drawImage(l.currentSprite,l.x,l.y,l.width,l.height)}}catch(t){s=!0,o=t}finally{try{!r&&a.return&&a.return()}finally{if(s)throw o}}}};