From b0d2e8436a4c9369d58ea8f8c21e9d71774b2785 Mon Sep 17 00:00:00 2001 From: dubrowgn Date: Fri, 19 Sep 2014 08:43:02 -0600 Subject: [PATCH] Node.js compatability changes Initial export/import implementation for easily saving/loading state --- LICENSE | 2 +- src/Init.js | 4 +++ src/Model2D.js | 53 +++++++++++++++++++++++++++-- src/Shape2D.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Util.js | 3 +- 5 files changed, 148 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 04ba464..519ca3a 100644 --- a/LICENSE +++ b/LICENSE @@ -2,6 +2,6 @@ * @license Impulse.js * http://www.impulsejs.com * - * Copyright 2012-2013 by Dustin Brown (dustin.brown@dubrowgn.com) + * Copyright 2012-2014 by Dustin Brown (dustin.brown@dubrowgn.com) * Licensed under GPLv2 (http://www.gnu.org/licenses/gpl-2.0.html) */ diff --git a/src/Init.js b/src/Init.js index 62c970e..68949f9 100644 --- a/src/Init.js +++ b/src/Init.js @@ -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; diff --git a/src/Model2D.js b/src/Model2D.js index bf8e930..0eaea44 100644 --- a/src/Model2D.js +++ b/src/Model2D.js @@ -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. * @@ -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; }; @@ -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; diff --git a/src/Shape2D.js b/src/Shape2D.js index cf3ebee..9ab5963 100644 --- a/src/Shape2D.js +++ b/src/Shape2D.js @@ -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( ) * @@ -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( ) * @@ -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; }); @@ -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); @@ -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; }); diff --git a/src/Util.js b/src/Util.js index d058d6b..503dbb4 100644 --- a/src/Util.js +++ b/src/Util.js @@ -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();