Skip to content

Commit

Permalink
Document the sprite class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Feb 27, 2010
1 parent 48ddc65 commit 75ee5f5
Showing 1 changed file with 103 additions and 49 deletions.
152 changes: 103 additions & 49 deletions dev/engine/class.sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ goog.require('Breeze.Engine');
goog.require('Breeze.Engine.Texture');

/**
* @param {Object.<string, *>} options
* @constructor
*/
Breeze.Engine.Sprite = function(options) {
Expand All @@ -33,61 +34,114 @@ Breeze.Engine.Sprite = function(options) {
var settings = {};
goog.object.extend(settings, defaults, options);

/**
* @type {number}
* @public
*/
this.x = 0;

/**
* @type {number}
* @public
*/
this.y = 0;
this._image = settings.image;
this._width = settings.width;
this._height = settings.height;
this._nCols = parseInt(settings.image.width / settings.width, 10);
this._nRows = parseInt(settings.image.height / settings.height, 10);
this._animations = settings.animations;

this._frame = 0;

this._frameTickStart = 0;
this._animationIndex = 0;
this._currentAnimation = null;

/**
* @type {Image}
* @private
*/
this.image_ = settings.image;

/**
* @type {number}
* @private
*/
this.width_ = settings.width;

/**
* @type {number}
* @private
*/
this.height_ = settings.height;

/**
* @type {number}
* @private
*/
this.nCols_ = parseInt(settings.image.width / settings.width, 10);

/**
* @type {number}
* @private
*/
this.nRows_ = parseInt(settings.image.height / settings.height, 10);

/**
* @type {Object.<string, {Object}>}
* @private
*/
this.animations_ = settings.animations;

/**
* @type {number}
* @private
*/
this.frame_ = 0;

/**
* @type {number}
* @private
*/
this.frameTickStart_ = 0;

/**
* @type {number}
* @private
*/
this.animationFrameIndex_ = 0;

/**
* @type {string|null}
* @private
*/
this.currentAnimation_ = null;
};

Breeze.Engine.Sprite.prototype.draw = function(context) {
context.save();
context.translate(parseInt(-this.width_ / 2, 10), parseInt(-this.height_ / 2, 10));
var tx = (this.frame_ % this.nCols_) * this.width_;
var ty = parseInt(this.frame_ / this.nCols_, 10) * this.height_;
context.drawImage(this.image_, tx, ty, this.width_, this.height_,
this.x, this.y, this.width_, this.height_);
context.restore();
};

Breeze.Engine.Sprite.prototype = {

draw : function(context) {
context.save();
context.translate(parseInt(-this._width / 2, 10), parseInt(-this._height / 2, 10));
var tx = (this._frame % this._nCols) * this._width;
var ty = parseInt(this._frame / this._nCols, 10) * this._height;
context.drawImage(this._image, tx, ty, this._width, this._height,
this.x, this.y, this._width, this._height);
context.restore();
},

tick : function(deltaSeconds) {
if (this._currentAnimation) {
var animation = this._animations[this._currentAnimation];
var animationFrame = animation.frames[this._animationIndex];
var animationTime = animation.times[this._animationIndex];
this._frame = animationFrame;
if ((new Date() - this._frameTickStart) > animationTime) {
this._animationIndex++;
this._frameTickStart = new Date();

// Check for the end of the animation.
if (this._animationIndex == animation.frames.length) {
if (animation.repeats) {
this._animationIndex = 0;
} else {
this._currentAnimation = null;
}
Breeze.Engine.Sprite.prototype.tick = function(deltaSeconds) {
if (this.currentAnimation_) {
var animation = this.animations_[this.currentAnimation_];
var animationFrame = animation.frames[this.animationFrameIndex_];
var animationTime = animation.times[this.animationFrameIndex_];
this.frame_ = animationFrame;
if ((new Date() - this.frame_TickStart) > animationTime) {
this.animationFrameIndex_++;
this.frame_TickStart = new Date();

// Check for the end of the animation.
if (this.animationFrameIndex_ == animation.frames.length) {
if (animation.repeats) {
this.animationFrameIndex_ = 0;
} else {
this.currentAnimation_ = null;
}
}
}
},

animate : function(animation) {
this._currentAnimation = animation;
this._animationIndex = 0;
this._frame = this._animations[this._currentAnimation].frames[this._animationIndex];
this._frameTickStart = new Date();
}

};

Breeze.Engine.Sprite.prototype.animate = function(animation) {
this.currentAnimation_ = animation;
this.animationFrameIndex_ = 0;
this.frame_ = this.animations_[this.currentAnimation_].frames[this.animationFrameIndex_];
this.frame_TickStart = new Date();
};

0 comments on commit 75ee5f5

Please sign in to comment.