function SpriteAnimation(opts) {
	this.loop = (opts && opts.loop instanceof Boolean) ? opts.loop : false;
	this.frames = (opts && opts.frames instanceof Array) ? opts.frames : [];
	this.isPlaying = false;
	this.currentFrame;
	this.currentFrameIdx = 0;
	this.currentLifetime = 0;
	this.ended = false;
}

SpriteAnimation.prototype.addFrame = function(sprite, lifetime) {
	this.frames.push(new SpriteAnimationFrame(sprite, lifetime));
}

SpriteAnimation.prototype.play = function() {
	this.isPlaying = true;
	this.currentFrame = this.frames[this.currentFrameIdx];
}

SpriteAnimation.prototype.playFromBeginning = function() {
	this.stop();
	this.play();
}

SpriteAnimation.prototype.pause = function() {
	this.isPlaying = false;
}

SpriteAnimation.prototype.stop = function() {
	this.currentFrame = this.frames[0];
	this.currentFrameIdx = 0;
	this.currentLifetime = 0;
	this.ended = false;
	this.isPlaying = false;
}

SpriteAnimation.prototype.update = function(amt) {
	if (!this.ended || !this.isPlaying) {
		this.currentLifetime += amt;
		if (this.currentLifetime >= this.currentFrame.lifetime) {
			if (this.currentFrameIdx + 1 >= this.frames.length) {
				if (this.loop) {
					this.playFromBeginning();
				} else {
					this.pause();
					this.ended = 1;
				}
			} else {
				this.currentFrame = this.frames[++this.currentFrameIdx];
			}
			this.currentLifetime = 0;
		}
	}
}

function SpriteAnimationFrame(sprite, lifetime) {
	this.sprite = sprite;
	this.lifetime = lifetime;
}
