From e28cdbeb6a5543c4d62eaa2f4e628e130518a9fc Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 3 Sep 2013 04:43:26 +0100 Subject: [PATCH] Moving to using a small extend / mixin method to avoid code repeating now we're extending Pixi display objects. --- examples/inherit.php | 167 +++++++++++++++++++++++++++++++++++++++++++ examples/js.php | 1 + src/utils/Utils.js | 106 +++++++++++++++++++++++++++ 3 files changed, 274 insertions(+) create mode 100644 examples/inherit.php create mode 100644 src/utils/Utils.js diff --git a/examples/inherit.php b/examples/inherit.php new file mode 100644 index 00000000..89dc7e2c --- /dev/null +++ b/examples/inherit.php @@ -0,0 +1,167 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/js.php b/examples/js.php index c4b086f3..3a02be59 100644 --- a/examples/js.php +++ b/examples/js.php @@ -3,6 +3,7 @@ // Much easier for debugging ?> + diff --git a/src/utils/Utils.js b/src/utils/Utils.js new file mode 100644 index 00000000..9a85fe1f --- /dev/null +++ b/src/utils/Utils.js @@ -0,0 +1,106 @@ +Phaser.Utils = { + + // This is a slightly modified version of jQuery.isPlainObject + isPlainObject: function (obj) { + + // Not plain objects: + // - Any object or value whose internal [[Class]] property is not "[object Object]" + // - DOM nodes + // - window + if (typeof(obj) !== "object" || obj.nodeType || obj === obj.window) + { + return false; + } + + // Support: Firefox <20 + // The try/catch suppresses exceptions thrown when attempting to access + // the "constructor" property of certain host objects, ie. |window.location| + // https://bugzilla.mozilla.org/show_bug.cgi?id=814622 + try { + if (obj.constructor && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) + { + return false; + } + } catch (e) { + return false; + } + + // If the function hasn't returned already, we're confident that + // |obj| is a plain object, created by {} or constructed with new Object + return true; + }, + + // deep, target, objects to copy to the target object + // This is a slightly modified version of jQuery.extend (http://api.jquery.com/jQuery.extend/) + extend: function () { + + var options, name, src, copy, copyIsArray, clone, + target = arguments[0] || {}, + i = 1, + length = arguments.length, + deep = false; + + // Handle a deep copy situation + if (typeof target === "boolean") + { + deep = target; + target = arguments[1] || {}; + // skip the boolean and the target + i = 2; + } + + // extend Phaser if only one argument is passed + if (length === i) + { + target = this; + --i; + } + + for ( ; i < length; i++ ) + { + // Only deal with non-null/undefined values + if ((options = arguments[i]) != null) + { + // Extend the base object + for (name in options) + { + src = target[name]; + copy = options[name]; + + // Prevent never-ending loop + if (target === copy) + { + continue; + } + + // Recurse if we're merging plain objects or arrays + if (deep && copy && (Phaser.Utils.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) + { + if (copyIsArray) + { + copyIsArray = false; + clone = src && Array.isArray(src) ? src : []; + } + else + { + clone = src && Phaser.Utils.isPlainObject(src) ? src : {}; + } + + // Never move original objects, clone them + target[name] = extend(deep, clone, copy); + + // Don't bring in undefined values + } + else if (copy !== undefined) + { + target[name] = copy; + } + } + } + } + + // Return the modified object + return target; + } + +} \ No newline at end of file