2018-03-27 17:39:08 +00:00
|
|
|
function Sprite(config){
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
self.config = config;
|
|
|
|
|
|
|
|
// Properties...
|
|
|
|
self.x = 0;
|
|
|
|
self.y = 0;
|
|
|
|
self.pivotX = 0;
|
|
|
|
self.pivotY = 0;
|
2018-04-01 11:50:29 +00:00
|
|
|
self.scale = null;
|
|
|
|
self.scaleX = 1;
|
|
|
|
self.scaleY = 1;
|
2018-03-27 17:39:08 +00:00
|
|
|
self.rotation = 0; // radians
|
|
|
|
|
|
|
|
// The image!
|
|
|
|
self.image = new Image();
|
|
|
|
self.image.src = config.src;
|
|
|
|
|
|
|
|
// Frames
|
|
|
|
self.currentFrame = 0;
|
|
|
|
self.totalFrames = config.frames;
|
|
|
|
self.nextFrame = function(){
|
|
|
|
self.currentFrame = (self.currentFrame+1)%self.totalFrames;
|
|
|
|
};
|
|
|
|
self.gotoFrame = function(frame){
|
|
|
|
self.currentFrame = frame;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
self.draw = function(ctx){
|
|
|
|
|
|
|
|
var sw = config.sw;
|
|
|
|
var sh = config.sh;
|
|
|
|
var sx = self.currentFrame*sw;
|
|
|
|
var sy = 0;
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
ctx.translate(self.x, self.y);
|
|
|
|
ctx.rotate(self.rotation);
|
2018-04-01 11:50:29 +00:00
|
|
|
if(self.scale){
|
|
|
|
ctx.scale(self.scale, self.scale);
|
|
|
|
}else{
|
|
|
|
ctx.scale(self.scaleX, self.scaleY);
|
|
|
|
}
|
2018-03-27 17:39:08 +00:00
|
|
|
ctx.translate(-self.pivotX, -self.pivotY);
|
|
|
|
ctx.drawImage(self.image, sx, sy, sw, sh, 0, 0, sw, sh);
|
|
|
|
ctx.restore();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|