Raspsnir/public_html/browserquest/js/sprite.js

1 line
2.1 KiB
JavaScript

define(["jquery","animation","sprites"],function(e,t,n){var r=Class.extend({init:function(e,t){this.name=e,this.scale=t,this.isLoaded=!1,this.offsetX=0,this.offsetY=0,this.loadJSON(n[e])},loadJSON:function(e){this.id=e.id,this.filepath="img/"+this.scale+"/"+this.id+".png",this.animationData=e.animations,this.width=e.width,this.height=e.height,this.offsetX=e.offset_x!==undefined?e.offset_x:-16,this.offsetY=e.offset_y!==undefined?e.offset_y:-16,this.load()},load:function(){var e=this;this.image=new Image,this.image.src=this.filepath,this.image.onload=function(){e.isLoaded=!0,e.onload_func&&e.onload_func()}},createAnimations:function(){var e={};for(var n in this.animationData){var r=this.animationData[n];e[n]=new t(n,r.length,r.row,this.width,this.height)}return e},createHurtSprite:function(){var e=document.createElement("canvas"),t=e.getContext("2d"),n=this.image.width,r=this.image.height,i,s;e.width=n,e.height=r,t.drawImage(this.image,0,0,n,r);try{i=t.getImageData(0,0,n,r),s=i.data;for(var o=0;o<s.length;o+=4)s[o]=255,s[o+1]=s[o+2]=75;i.data=s,t.putImageData(i,0,0),this.whiteSprite={image:e,isLoaded:!0,offsetX:this.offsetX,offsetY:this.offsetY,width:this.width,height:this.height}}catch(u){log.error("Error getting image data for sprite : "+this.name)}},getHurtSprite:function(){return this.whiteSprite},createSilhouette:function(){var e=document.createElement("canvas"),t=e.getContext("2d"),n=this.image.width,r=this.image.height,i,s,o;e.width=n,e.height=r,t.drawImage(this.image,0,0,n,r),o=t.getImageData(0,0,n,r).data,s=t.getImageData(0,0,n,r),fdata=s.data;var u=function(e,t){return(n*(t-1)+e-1)*4},a=function(e){var t,r;return e=e/4+1,t=e%n,r=(e-t)/n+1,{x:t,y:r}},f=function(e){var t=a(e);return t.x<n&&!l(u(t.x+1,t.y))?!0:t.x>1&&!l(u(t.x-1,t.y))?!0:t.y<r&&!l(u(t.x,t.y+1))?!0:t.y>1&&!l(u(t.x,t.y-1))?!0:!1},l=function(e){return e<0||e>=o.length?!0:o[e]===0&&o[e+1]===0&&o[e+2]===0&&o[e+3]===0};for(var c=0;c<o.length;c+=4)l(c)&&f(c)&&(fdata[c]=fdata[c+1]=255,fdata[c+2]=150,fdata[c+3]=150);s.data=fdata,t.putImageData(s,0,0),this.silhouetteSprite={image:e,isLoaded:!0,offsetX:this.offsetX,offsetY:this.offsetY,width:this.width,height:this.height}}});return r});