1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
define(["camera","item","character","player","timer"],function(e,t,n,r,i){var s=Class.extend({init:function(e,t,n,r){this.game=e,this.context=t&&t.getContext?t.getContext("2d"):null,this.background=n&&n.getContext?n.getContext("2d"):null,this.foreground=r&&r.getContext?r.getContext("2d"):null,this.canvas=t,this.backcanvas=n,this.forecanvas=r,this.initFPS(),this.tilesize=16,this.upscaledRendering=this.context.mozImageSmoothingEnabled!==undefined,this.supportsSilhouettes=this.upscaledRendering,this.rescale(this.getScaleFactor()),this.lastTime=new Date,this.frameCount=0,this.maxFPS=this.FPS,this.realFPS=0,this.isDebugInfoVisible=!1,this.animatedTileCount=0,this.highTileCount=0,this.tablet=Detect.isTablet(window.innerWidth),this.fixFlickeringTimer=new i(100)},getWidth:function(){return this.canvas.width},getHeight:function(){return this.canvas.height},setTileset:function(e){this.tileset=e},getScaleFactor:function(){var e=window.innerWidth,t=window.innerHeight,n;return this.mobile=!1,e<=1e3?(n=2,this.mobile=!0):e<=1500||t<=870?n=2:n=3,n},rescale:function(e){this.scale=this.getScaleFactor(),this.createCamera(),this.context.mozImageSmoothingEnabled=!1,this.background.mozImageSmoothingEnabled=!1,this.foreground.mozImageSmoothingEnabled=!1,this.initFont(),this.initFPS(),!this.upscaledRendering&&this.game.map&&this.game.map.tilesets&&this.setTileset(this.game.map.tilesets[this.scale-1]),this.game.renderer&&this.game.setSpriteScale(this.scale)},createCamera:function(){this.camera=new e(this),this.camera.rescale(),this.canvas.width=this.camera.gridW*this.tilesize*this.scale,this.canvas.height=this.camera.gridH*this.tilesize*this.scale,log.debug("#entities set to "+this.canvas.width+" x "+this.canvas.height),this.backcanvas.width=this.canvas.width,this.backcanvas.height=this.canvas.height,log.debug("#background set to "+this.backcanvas.width+" x "+this.backcanvas.height),this.forecanvas.width=this.canvas.width,this.forecanvas.height=this.canvas.height,log.debug("#foreground set to "+this.forecanvas.width+" x "+this.forecanvas.height)},initFPS:function(){this.FPS=this.mobile?50:50},initFont:function(){var e;switch(this.scale){case 1:e=10;break;case 2:e=Detect.isWindows()?10:13;break;case 3:e=20}this.setFontSize(e)},setFontSize:function(e){var t=e+"px GraphicPixel";this.context.font=t,this.background.font=t},drawText:function(e,t,n,r,i,s){var o=this.context,u;switch(this.scale){case 1:u=3;break;case 2:u=3;break;case 3:u=5}e&&t&&n&&(o.save(),r&&(o.textAlign="center"),o.strokeStyle=s||"#373737",o.lineWidth=u,o.strokeText(e,t,n),o.fillStyle=i||"white",o.fillText(e,t,n),o.restore())},drawCellRect:function(e,t,n){this.context.save(),this.context.lineWidth=2*this.scale,this.context.strokeStyle=n,this.context.translate(e+2,t+2),this.context.strokeRect(0,0,this.tilesize*this.scale-4,this.tilesize*this.scale-4),this.context.restore()},drawCellHighlight:function(e,t,n){var r=this.scale,i=this.tilesize,s=e*i*r,o=t*i*r;this.drawCellRect(s,o,n)},drawTargetCell:function(){var e=this.game.getMouseGridPosition();this.game.targetCellVisible&&(e.x!==this.game.selectedX||e.y!==this.game.selectedY)&&this.drawCellHighlight(e.x,e.y,this.game.targetColor)},drawAttackTargetCell:function(){var e=this.game.getMouseGridPosition(),t=this.game.getEntityAt(e.x,e.y),n=this.scale;t&&this.drawCellRect(t.x*n,t.y*n,"rgba(255, 0, 0, 0.5)")},drawOccupiedCells:function(){var e=this.game.entityGrid;if(e)for(var t=0;t<e.length;t+=1)for(var n=0;n<e[t].length;n+=1)_.isNull(e[t][n])||this.drawCellHighlight(t,n,"rgba(50, 50, 255, 0.5)")},drawPathingCells:function(){var e=this.game.pathingGrid;if(e&&this.game.debugPathing)for(var t=0;t<e.length;t+=1)for(var n=0;n<e[t].length;n+=1)e[t][n]===1&&this.game.camera.isVisiblePosition(n,t)&&this.drawCellHighlight(n,t,"rgba(50, 50, 255, 0.5)")},drawSelectedCell:function(){var e=this.game.cursors.target,t=this.game.targetAnimation,n=this.upscaledRendering?1:this.scale,r=this.upscaledRendering?this.scale:1;if(this.game.selectedCellVisible)if(this.mobile||this.tablet){if(this.game.drawTarget){var i=this.game.selectedX,s=this.game.selectedY;this.drawCellHighlight(this.game.selectedX,this.game.selectedY,"rgb(51, 255, 0)"),this.lastTargetPos={x:i,y:s},this.game.drawTarget=!1}}else if(e&&t){var o=t.currentFrame,u=this.scale,i=o.x*n,s=o.y*n,a=e.width*n,f=e.height*n,l=16,c=this.game.selectedX*l*u,h=this.game.selectedY*l*u,p=a*r,d=f*r;this.context.save(),this.context.translate(c,h),this.context.drawImage(e.image,i,s,a,f,0,0,p,d),this.context.restore()}},clearScaledRect:function(e,t,n,r,i){var s=this.scale;e.clearRect(t*s,n*s,r*s,i*s)},drawCursor:function(){var e=this.game.mouse.x,t=this.game.mouse.y,n=this.scale,r=this.upscaledRendering?1:this.scale;this.context.save(),this.game.currentCursor&&this.game.currentCursor.isLoaded&&this.context.drawImage(this.game.currentCursor.image,0,0,14*r,14*r,e,t,14*n,14*n),this.context.restore()},drawScaledImage:function(e,t,n,r,i,s,o,u){var a=this.upscaledRendering?1:this.scale;_.each(arguments,function(e){if(_.isUndefined(e)||_.isNaN(e)||_.isNull(e)||e<0)throw log.error("x:"+n+" y:"+r+" w:"+i+" h:"+s+" dx:"+o+" dy:"+u,!0),Error("A problem occured when trying to draw on the canvas")}),e.drawImage(t,n*a,r*a,i*a,s*a,o*this.scale,u*this.scale,i*this.scale,s*this.scale)},drawTile:function(e,t,n,r,i,s){var u=this.upscaledRendering?1:this.scale;t!==-1&&this.drawScaledImage(e,n,o(t+1,r/u)*this.tilesize,Math.floor(t/(r/u))*this.tilesize,this.tilesize,this.tilesize,o(s+1,i)*this.tilesize,Math.floor(s/i)*this.tilesize)},clearTile:function(e,t,n){var r=this.scale,i=this.tilesize,s=o(n+1,t)*i*r,u=Math.floor(n/t)*i*r,a=i*r,f=a;e.clearRect(s,u,f,a)},drawEntity:function(e){var r=e.sprite,i=this.game.shadows.small,s=e.currentAnimation,o=this.upscaledRendering?1:this.scale,u=this.upscaledRendering?this.scale:1;if(s&&r){var a=s.currentFrame,f=this.scale,l=a.x*o,c=a.y*o,h=r.width*o,p=r.height*o,d=r.offsetX*f,v=r.offsetY*f,m=e.x*f,g=e.y*f,y=h*u,b=p*u;e.isFading&&(this.context.save(),this.context.globalAlpha=e.fadingAlpha),!this.mobile&&!this.tablet&&this.drawEntityName(e),this.context.save(),e.flipSpriteX?(this.context.translate(m+this.tilesize*f,g),this.context.scale(-1,1)):e.flipSpriteY?(this.context.translate(m,g+b),this.context.scale(1,-1)):this.context.translate(m,g);if(e.isVisible()){e.hasShadow()&&this.context.drawImage(i.image,0,0,i.width*o,i.height*o,0,e.shadowOffsetY*u,i.width*o*u,i.height*o*u),this.context.drawImage(r.image,l,c,h,p,d,v,y,b);if(e instanceof t&&e.kind!==Types.Entities.CAKE){var w=this.game.sprites.sparks,s=this.game.sparksAnimation,a=s.currentFrame,E=w.width*a.index*o,S=w.height*s.row*o,x=w.width*o,T=w.width*o;this.context.drawImage(w.image,E,S,x,T,w.offsetX*f,w.offsetY*f,x*u,T*u)}}if(e instanceof n&&!e.isDead&&e.hasWeapon()){var N=this.game.sprites[e.getWeaponName()];if(N){var C=N.animationData[s.name],k=a.index<C.length?a.index:a.index%C.length;wx=N.width*k*o,wy=N.height*s.row*o,ww=N.width*o,wh=N.height*o,this.context.drawImage(N.image,wx,wy,ww,wh,N.offsetX*f,N.offsetY*f,ww*u,wh*u)}}this.context.restore(),e.isFading&&this.context.restore()}},drawEntities:function(e){var t=this;this.game.forEachVisibleEntityByDepth(function(n){n.isLoaded&&(e?n.isDirty&&(t.drawEntity(n),n.isDirty=!1,n.oldDirtyRect=n.dirtyRect,n.dirtyRect=null):t.drawEntity(n))})},drawDirtyEntities:function(){this.drawEntities(!0)},clearDirtyRect:function(e){this.context.clearRect(e.x,e.y,e.w,e.h)},clearDirtyRects:function(){var e=this,t=0;this.game.forEachVisibleEntityByDepth(function(n){n.isDirty&&n.oldDirtyRect&&(e.clearDirtyRect(n.oldDirtyRect),t+=1)}),this.game.forEachAnimatedTile(function(n){n.isDirty&&(e.clearDirtyRect(n.dirtyRect),t+=1)});if(this.game.clearTarget&&this.lastTargetPos){var n=this.lastTargetPos;rect=this.getTargetBoundingRect(n.x,n.y),this.clearDirtyRect(rect),this.game.clearTarget=!1,t+=1}t>0},getEntityBoundingRect:function(e){var t={},n=this.scale,i;if(e instanceof r&&e.hasWeapon()){var s=this.game.sprites[e.getWeaponName()];i=s}else i=e.sprite;return i&&(t.x=(e.x+i.offsetX-this.camera.x)*n,t.y=(e.y+i.offsetY-this.camera.y)*n,t.w=i.width*n,t.h=i.height*n,t.left=t.x,t.right=t.x+t.w,t.top=t.y,t.bottom=t.y+t.h),t},getTileBoundingRect:function(e){var t={},n=this.game.map.width,r=this.scale,i=this.tilesize,s=e.index;return t.x=(o(s+1,n)*i-this.camera.x)*r,t.y=(Math.floor(s/n)*i-this.camera.y)*r,t.w=i*r,t.h=i*r,t.left=t.x,t.right=t.x+t.w,t.top=t.y,t.bottom=t.y+t.h,t},getTargetBoundingRect:function(e,t){var n={},r=this.scale,i=this.tilesize,s=e||this.game.selectedX,o=t||this.game.selectedY;return n.x=(s*i-this.camera.x)*r,n.y=(o*i-this.camera.y)*r,n.w=i*r,n.h=i*r,n.left=n.x,n.right=n.x+n.w,n.top=n.y,n.bottom=n.y+n.h,n},isIntersecting:function(e,t){return!(t.left>e.right||t.right<e.left||t.top>e.bottom||t.bottom<e.top)},drawEntityName:function(e){this.context.save();if(e.name&&e instanceof r){var t=e.id===this.game.playerId?"#fcda5c":"white";this.drawText(e.name,(e.x+8)*this.scale,(e.y+e.nameOffsetY)*this.scale,!0,t)}this.context.restore()},drawTerrain:function(){var e=this,t=this.game.map,n=this.tileset.width/t.tilesize;this.game.forEachVisibleTile(function(r,i){!t.isHighTile(r)&&!t.isAnimatedTile(r)&&e.drawTile(e.background,r,e.tileset,n,t.width,i)},1)},drawAnimatedTiles:function(e){var t=this,n=this.game.map,r=this.tileset.width/n.tilesize;this.animatedTileCount=0,this.game.forEachAnimatedTile(function(i){e?i.isDirty&&(t.drawTile(t.context,i.id,t.tileset,r,n.width,i.index),i.isDirty=!1):(t.drawTile(t.context,i.id,t.tileset,r,n.width,i.index),t.animatedTileCount+=1)})},drawDirtyAnimatedTiles:function(){this.drawAnimatedTiles(!0)},drawHighTiles:function(e){var t=this,n=this.game.map,r=this.tileset.width/n.tilesize;this.highTileCount=0,this.game.forEachVisibleTile(function(i,s){n.isHighTile(i)&&(t.drawTile(e,i,t.tileset,r,n.width,s),t.highTileCount+=1)},1)},drawBackground:function(e,t){e.fillStyle=t,e.fillRect(0,0,this.canvas.width,this.canvas.height)},drawFPS:function(){var e=new Date,t=e.getTime()-this.lastTime.getTime();t>=1e3&&(this.realFPS=this.frameCount,this.frameCount=0,this.lastTime=e),this.frameCount++,this.drawText("FPS: "+this.realFPS,30,30,!1)},drawDebugInfo:function(){this.isDebugInfoVisible&&(this.drawFPS(),this.drawText("A: "+this.animatedTileCount,100,30,!1),this.drawText("H: "+this.highTileCount,140,30,!1))},drawCombatInfo:function(){var e=this;switch(this.scale){case 2:this.setFontSize(20);break;case 3:this.setFontSize(30)}this.game.infoManager.forEachInfo(function(t){e.context.save(),e.context.globalAlpha=t.opacity,e.drawText(t.value,(t.x+8)*e.scale,Math.floor(t.y*e.scale),!0,t.fillColor,t.strokeColor),e.context.restore()}),this.initFont()},setCameraView:function(e){e.translate(-this.camera.x*this.scale,-this.camera.y*this.scale)},clearScreen:function(e){e.clearRect(0,0,this.canvas.width,this.canvas.height)},getPlayerImage:function(){var e=document.createElement("canvas"),t=e.getContext("2d"),n=this.upscaledRendering?1:this.scale,r=this.game.player,i=r.getArmorSprite(),s=i.animationData.idle_down,o=s.row,u=i.width*n,a=i.height*n,f=o*a,l=this.game.sprites[this.game.player.getWeaponName()],c=l.width*n,h=l.height*n,p=h*o,d=(l.offsetX-i.offsetX)*n,v=(l.offsetY-i.offsetY)*n,m=this.game.shadows.small,g=m.width*n,y=m.height*n,b=-i.offsetX*n;return oy=-i.offsetY*n,e.width=u,e.height=a,t.clearRect(0,0,u,a),t.drawImage(m.image,0,0,g,y,b,oy,g,y),t.drawImage(i.image,0,f,u,a,0,0,u,a),t.drawImage(l.image,0,p,c,h,d,v,c,h),e.toDataURL("image/png")},renderStaticCanvases:function(){this.background.save(),this.setCameraView(this.background),this.drawTerrain(),this.background.restore();if(this.mobile||this.tablet)this.clearScreen(this.foreground),this.foreground.save(),this.setCameraView(this.foreground),this.drawHighTiles(this.foreground),this.foreground.restore()},renderFrame:function(){this.mobile||this.tablet?this.renderFrameMobile():this.renderFrameDesktop()},renderFrameDesktop:function(){this.clearScreen(this.context),this.context.save(),this.setCameraView(this.context),this.drawAnimatedTiles(),this.game.started&&(this.drawSelectedCell(),this.drawTargetCell()),this.drawPathingCells(),this.drawEntities(),this.drawCombatInfo(),this.drawHighTiles(this.context),this.context.restore(),this.drawCursor(),this.drawDebugInfo()},renderFrameMobile:function(){this.clearDirtyRects(),this.preventFlickeringBug(),this.context.save(),this.setCameraView(this.context),this.drawDirtyAnimatedTiles(),this.drawSelectedCell(),this.drawDirtyEntities(),this.context.restore()},preventFlickeringBug:function(){this.fixFlickeringTimer.isOver(this.game.currentTime)&&(this.background.fillRect(0,0,0,0),this.context.fillRect(0,0,0,0),this.foreground.fillRect(0,0,0,0))}}),o=function(e,t){return e==0?0:e%t==0?t-1:e%t-1};return s}); |