Skip to content

Commit

Permalink
Node.js compatability changes
Browse files Browse the repository at this point in the history
Initial export/import implementation for easily saving/loading state
  • Loading branch information
dubrowgn committed Sep 19, 2014
1 parent 39e81c6 commit b0d2e84
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* @license Impulse.js
* http://www.impulsejs.com
*
* Copyright 2012-2013 by Dustin Brown ([email protected])
* Copyright 2012-2014 by Dustin Brown ([email protected])
* Licensed under GPLv2 (http://www.gnu.org/licenses/gpl-2.0.html)
*/
4 changes: 4 additions & 0 deletions src/Init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ Impulse.Input = Impulse.Input(); // depends Shape2D
Impulse.Model2D = Impulse.Model2D(); // depends Shape2D, Util
Impulse.Entity = Impulse.Entity(); // depends Model2D
Impulse.Scene2D = Impulse.Scene2D(); // depends Entity, Input, Shape2D

// CommonJS export if available
if (typeof exports === 'object')
module.exports = Impulse;
53 changes: 51 additions & 2 deletions src/Model2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ Impulse.Model2D = (function() {
Animation.prototype._numberOfFrames = undefined;
Animation.prototype.matrix = undefined;

/**
* export( )
*
* Returns a generic object containing the current state of this
* animation. This is useful for storing state via JSON for example.
*
* @public
* @sig public {Object} export();
* @return {Object}
*/
Animation.prototype.export = function() {
return {
rect: this._firstFrameRect.export(),
frames: this._numberOfFrames,
duration: this._frameDuration * this._numberOfFrames,
matrix: this.matrix.export()
};
}; // export( )

/**
* Returns the correct frame Rect from this animation for the specified point in time.
*
Expand All @@ -52,6 +71,36 @@ Impulse.Model2D = (function() {
this._firstFrameRect.h);
}; // GetFrameRect( )

/**
* toJSON( )
*
* Returns a JSON ready copy of this object's current state.
* @return {Object}
*/
Animation.prototype.toJSON = Animation.prototype.export;

/**
* import( )
*
* Creates a new animation with an internal state equal to the values of
* the passed generic object. This is useful for restoring state from
* JSON for example.
*
* @public
* @static
* @sig public {Animation} import({Object});
* @param {Object} obj
* @return {Animation}
*/
Animation.import = function(obj) {
return new Animation(
Rect.import(obj.rect),
obj.frames,
obj.duration,
Matrix.import(obj.matrix)
);
}; // import( )

return Animation;
};

Expand All @@ -71,9 +120,9 @@ Impulse.Model2D = (function() {

Model2D.Model = function() {
// Model Model(HTMLImage);
var Model = function(_img) {
var Model = function(img) {
this.animations = [];
this.image = _img;
this.image = img;
}; // class Model

Model.prototype.animations = undefined;
Expand Down
90 changes: 90 additions & 0 deletions src/Shape2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,27 @@ Impulse.Shape2D = (function() {
this.d == mtrxRH.d && this.e == mtrxRH.e && this.f == mtrxRH.f;
}; // equals( )

/**
* export( )
*
* Returns a generic object containing the current state of this matrix.
* This is useful for storing state via JSON for example.
*
* @public
* @sig public {Object} export();
* @return {Object}
*/
Matrix.prototype.export = function() {
return {
a: this.a,
b: this.b,
c: this.c,
d: this.d,
e: this.e,
f: this.f
};
}; // export( )

/**
* getDeterminant( )
*
Expand Down Expand Up @@ -1196,6 +1217,14 @@ Impulse.Shape2D = (function() {
return this;
}; // setValues( )

/**
* toJSON( )
*
* Returns a JSON ready copy of this object's current state.
* @return {Object}
*/
Matrix.prototype.toJSON = Matrix.prototype.export;

/**
* toString( )
*
Expand Down Expand Up @@ -1234,6 +1263,23 @@ Impulse.Shape2D = (function() {
return this;
}; // translate( )

/**
* import( )
*
* Creates a new matrix with an internal state equal to the values of
* the passed generic object. This is useful for restoring state from
* JSON for example.
*
* @public
* @static
* @sig public {Animation} import({Object});
* @param {Object} obj
* @return {Animation}
*/
Matrix.import = function(obj) {
return new Matrix(obj.a, obj.b, obj.c, obj.d, obj.e, obj.f);
}; // import( )

return Matrix;
});

Expand Down Expand Up @@ -1437,6 +1483,25 @@ Impulse.Shape2D = (function() {
this.h == rect.h);
} // equals( )

/**
* export( )
*
* Returns a generic object containing the current state of this rect.
* This is useful for storing state via JSON for example.
*
* @public
* @sig public {Object} export();
* @return {Object}
*/
Rect.prototype.export = function() {
return {
x: this.x,
y: this.y,
w: this.w,
h: this.h
};
}; // export( )

// Vector getCenter();
Rect.prototype.getCenter = function() {
return new Shape2D.Vector(this.x + this.w/2, this.y - this.h/2);
Expand Down Expand Up @@ -1469,11 +1534,36 @@ Impulse.Shape2D = (function() {
return this;
} // setCenter( )

/**
* toJSON( )
*
* Returns a JSON ready copy of this object's current state.
* @return {Object}
*/
Rect.prototype.toJSON = Rect.prototype.export;

// String toString();
Rect.prototype.toString = function() {
return "Rect(" + this.x + ", " + this.y + ", " + this.w + ", " + this.h + ")";
} // toString( )

/**
* import( )
*
* Creates a new rect with an internal state equal to the values of the
* passed generic object. This is useful for restoring state from JSON
* for example.
*
* @public
* @static
* @sig public {Rect} import({Object});
* @param {Object} obj
* @return {Rect}
*/
Rect.import = function(obj) {
return new Rect(obj.x, obj.y, obj.w, obj.h);
}; // import( )

return Rect;
});

Expand Down
3 changes: 2 additions & 1 deletion src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ Impulse.Util = (function() {
};

// IE 9 has performance, but not performance.now
if (window.performance !== undefined && performance.now !== undefined) {
// make sure window exists for node.js compatability
if (typeof window !== "undefined" && window.performance !== undefined && performance.now !== undefined) {
Timing.isHighResolution = true;
Timing.now = function() {
return performance.now();
Expand Down

0 comments on commit b0d2e84

Please sign in to comment.