From e88b10323a3acbf84e4726abda1130806ef72836 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Thu, 6 Feb 2014 12:29:07 +0000 Subject: [PATCH] Updated Phaser geometry classes so they over-ride the PIXI native ones, means we can do away with a whole bunch of over-rides and object changes in Sprite, etc. --- build/config.php | 19 +++--- examples/wip/pixi1.js | 3 + src/Phaser.js | 2 +- src/geom/Circle.js | 24 ++++++-- src/geom/Point.js | 35 +++++++++-- src/geom/Rectangle.js | 131 ++++++++++++++++++++++++++++++++++-------- 6 files changed, 173 insertions(+), 41 deletions(-) diff --git a/build/config.php b/build/config.php index 59c48231e..9129af4a0 100644 --- a/build/config.php +++ b/build/config.php @@ -38,6 +38,12 @@ + + + + + + */ @@ -48,11 +54,13 @@ + + + + + - - - @@ -65,7 +73,6 @@ - @@ -138,10 +145,6 @@ - - - - diff --git a/examples/wip/pixi1.js b/examples/wip/pixi1.js index a2c96b3ae..bf8abf8ef 100644 --- a/examples/wip/pixi1.js +++ b/examples/wip/pixi1.js @@ -10,6 +10,7 @@ function preload() { var sprite; var sprite2; var g; +var p; function create() { @@ -28,6 +29,8 @@ function create() { game.add.tween(sprite).to({y: 500}, 3000, Phaser.Easing.Linear.None, true); + p = new PIXI.Point(43, 45); + } function tint() { diff --git a/src/Phaser.js b/src/Phaser.js index 6cb140441..ad2bf892c 100644 --- a/src/Phaser.js +++ b/src/Phaser.js @@ -44,7 +44,7 @@ var Phaser = Phaser || { CANVAS_PX_ROUND: false, CANVAS_CLEAR_RECT: true - }; +}; PIXI.InteractionManager = function (dummy) { // We don't need this in Pixi, so we've removed it to save space diff --git a/src/geom/Circle.js b/src/geom/Circle.js index 86cc21b1f..d8a390b3f 100644 --- a/src/geom/Circle.js +++ b/src/geom/Circle.js @@ -132,9 +132,16 @@ Phaser.Circle.prototype = { */ clone: function(out) { - if (typeof out === "undefined") { out = new Phaser.Circle(); } + if (typeof out === "undefined") + { + out = new Phaser.Circle(this.x, this.y, this.diameter); + } + else + { + out.setTo(this.x, this.y, this.diameter); + } - return out.setTo(this.x, this.y, this.diameter); + return out; }, @@ -377,14 +384,18 @@ Object.defineProperty(Phaser.Circle.prototype, "empty", { */ Phaser.Circle.contains = function (a, x, y) { - // Check if x/y are within the bounds first - if (x >= a.left && x <= a.right && y >= a.top && y <= a.bottom) { + if (a.radius <= 0) + { + return false; + } + // Check if x/y are within the bounds first + if (x >= a.left && x <= a.right && y >= a.top && y <= a.bottom) + { var dx = (a.x - x) * (a.x - x); var dy = (a.y - y) * (a.y - y); return (dx + dy) <= (a.radius * a.radius); - } return false; @@ -475,3 +486,6 @@ Phaser.Circle.intersectsRectangle = function (c, r) { return xCornerDistSq + yCornerDistSq <= maxCornerDistSq; }; + +// Because PIXI uses its own Circle, we'll replace it with ours to avoid duplicating code or confusion. +PIXI.Circle = Phaser.Circle; diff --git a/src/geom/Point.js b/src/geom/Point.js index 029341c56..c4c291d95 100644 --- a/src/geom/Point.js +++ b/src/geom/Point.js @@ -59,8 +59,25 @@ Phaser.Point.prototype = { */ setTo: function (x, y) { - this.x = x; - this.y = y; + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ); + + return this; + + }, + + /** + * Sets the x and y values of this Point object to the given coordinates. + * @method Phaser.Point#set + * @param {number} x - The horizontal position of this point. + * @param {number} y - The vertical position of this point. + * @return {Point} This Point object. Useful for chaining method calls. + */ + set: function (x, y) { + + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ); + return this; }, @@ -176,9 +193,16 @@ Phaser.Point.prototype = { */ clone: function (output) { - if (typeof output === "undefined") { output = new Phaser.Point(); } + if (typeof output === "undefined") + { + output = new Phaser.Point(this.x, this.y); + } + else + { + output.setTo(this.x, this.y); + } - return output.setTo(this.x, this.y); + return output; }, @@ -430,3 +454,6 @@ Phaser.Point.rotate = function (a, x, y, angle, asDegrees, distance) { return a.setTo(x + distance * Math.cos(angle), y + distance * Math.sin(angle)); }; + +// Because PIXI uses its own Point, we'll replace it with ours to avoid duplicating code or confusion. +PIXI.Point = Phaser.Point; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js index 0861dcf36..2d8724881 100644 --- a/src/geom/Rectangle.js +++ b/src/geom/Rectangle.js @@ -13,7 +13,7 @@ * @param {number} y - The y coordinate of the top-left corner of the Rectangle. * @param {number} width - The width of the Rectangle. * @param {number} height - The height of the Rectangle. -* @return {Rectangle} This Rectangle object. +* @return {Phaser.Rectangle} This Rectangle object. */ Phaser.Rectangle = function (x, y, width, height) { @@ -51,7 +51,7 @@ Phaser.Rectangle.prototype = { * @method Phaser.Rectangle#offset * @param {number} dx - Moves the x value of the Rectangle object by this amount. * @param {number} dy - Moves the y value of the Rectangle object by this amount. - * @return {Rectangle} This Rectangle object. + * @return {Phaser.Rectangle} This Rectangle object. */ offset: function (dx, dy) { @@ -65,11 +65,13 @@ Phaser.Rectangle.prototype = { /** * Adjusts the location of the Rectangle object using a Point object as a parameter. This method is similar to the Rectangle.offset() method, except that it takes a Point object as a parameter. * @method Phaser.Rectangle#offsetPoint - * @param {Point} point - A Point object to use to offset this Rectangle object. - * @return {Rectangle} This Rectangle object. + * @param {Phaser.Point} point - A Point object to use to offset this Rectangle object. + * @return {Phaser.Rectangle} This Rectangle object. */ offsetPoint: function (point) { + return this.offset(point.x, point.y); + }, /** @@ -79,7 +81,7 @@ Phaser.Rectangle.prototype = { * @param {number} y - The y coordinate of the top-left corner of the Rectangle. * @param {number} width - The width of the Rectangle in pixels. * @param {number} height - The height of the Rectangle in pixels. - * @return {Rectangle} This Rectangle object + * @return {Phaser.Rectangle} This Rectangle object */ setTo: function (x, y, width, height) { @@ -91,7 +93,7 @@ Phaser.Rectangle.prototype = { return this; }, - + /** * Runs Math.floor() on both the x and y values of this Rectangle. * @method Phaser.Rectangle#floor @@ -120,10 +122,12 @@ Phaser.Rectangle.prototype = { * Copies the x, y, width and height properties from any given object to this Rectangle. * @method Phaser.Rectangle#copyFrom * @param {any} source - The object to copy from. - * @return {Rectangle} This Rectangle object. + * @return {Phaser.Rectangle} This Rectangle object. */ copyFrom: function (source) { + return this.setTo(source.x, source.y, source.width, source.height); + }, /** @@ -151,7 +155,9 @@ Phaser.Rectangle.prototype = { * @return {Phaser.Rectangle} This Rectangle object. */ inflate: function (dx, dy) { + return Phaser.Rectangle.inflate(this, dx, dy); + }, /** @@ -161,7 +167,9 @@ Phaser.Rectangle.prototype = { * @return {Phaser.Point} The size of the Rectangle object. */ size: function (output) { + return Phaser.Rectangle.size(this, output); + }, /** @@ -171,7 +179,9 @@ Phaser.Rectangle.prototype = { * @return {Phaser.Rectangle} */ clone: function (output) { + return Phaser.Rectangle.clone(this, output); + }, /** @@ -182,7 +192,9 @@ Phaser.Rectangle.prototype = { * @return {boolean} A value of true if the Rectangle object contains the specified point; otherwise false. */ contains: function (x, y) { + return Phaser.Rectangle.contains(this, x, y); + }, /** @@ -193,7 +205,9 @@ Phaser.Rectangle.prototype = { * @return {boolean} A value of true if the Rectangle object contains the specified point; otherwise false. */ containsRect: function (b) { + return Phaser.Rectangle.containsRect(this, b); + }, /** @@ -204,7 +218,9 @@ Phaser.Rectangle.prototype = { * @return {boolean} A value of true if the two Rectangles have exactly the same values for the x, y, width and height properties; otherwise false. */ equals: function (b) { + return Phaser.Rectangle.equals(this, b); + }, /** @@ -215,7 +231,9 @@ Phaser.Rectangle.prototype = { * @return {Phaser.Rectangle} A Rectangle object that equals the area of intersection. If the Rectangles do not intersect, this method returns an empty Rectangle object; that is, a Rectangle with its x, y, width, and height properties set to 0. */ intersection: function (b, out) { + return Phaser.Rectangle.intersection(this, b, out); + }, /** @@ -227,7 +245,9 @@ Phaser.Rectangle.prototype = { * @return {boolean} A value of true if the specified object intersects with this Rectangle object; otherwise false. */ intersects: function (b, tolerance) { + return Phaser.Rectangle.intersects(this, b, tolerance); + }, /** @@ -241,7 +261,9 @@ Phaser.Rectangle.prototype = { * @return {boolean} A value of true if the specified object intersects with the Rectangle; otherwise false. */ intersectsRaw: function (left, right, top, bottom, tolerance) { + return Phaser.Rectangle.intersectsRaw(this, left, right, top, bottom, tolerance); + }, /** @@ -252,7 +274,9 @@ Phaser.Rectangle.prototype = { * @return {Phaser.Rectangle} A Rectangle object that is the union of the two Rectangles. */ union: function (b, out) { + return Phaser.Rectangle.union(this, b, out); + }, /** @@ -261,7 +285,9 @@ Phaser.Rectangle.prototype = { * @return {string} A string representation of the instance. */ toString: function () { + return "[{Rectangle (x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + " empty=" + this.empty + ")}]"; + } }; @@ -511,11 +537,14 @@ Object.defineProperty(Phaser.Rectangle.prototype, "empty", { * @return {Phaser.Rectangle} This Rectangle object. */ Phaser.Rectangle.inflate = function (a, dx, dy) { + a.x -= dx; a.width += 2 * dx; a.y -= dy; a.height += 2 * dy; + return a; + }; /** @@ -526,7 +555,9 @@ Phaser.Rectangle.inflate = function (a, dx, dy) { * @return {Phaser.Rectangle} The Rectangle object. */ Phaser.Rectangle.inflatePoint = function (a, point) { + return Phaser.Rectangle.inflate(a, point.x, point.y); + }; /** @@ -537,8 +568,18 @@ Phaser.Rectangle.inflatePoint = function (a, point) { * @return {Phaser.Point} The size of the Rectangle object */ Phaser.Rectangle.size = function (a, output) { - if (typeof output === "undefined") { output = new Phaser.Point(); } - return output.setTo(a.width, a.height); + + if (typeof output === "undefined") + { + output = new Phaser.Point(a.width, a.height); + } + else + { + output.setTo(a.width, a.height); + } + + return output; + }; /** @@ -549,8 +590,18 @@ Phaser.Rectangle.size = function (a, output) { * @return {Phaser.Rectangle} */ Phaser.Rectangle.clone = function (a, output) { - if (typeof output === "undefined") { output = new Phaser.Rectangle(); } - return output.setTo(a.x, a.y, a.width, a.height); + + if (typeof output === "undefined") + { + output = new Phaser.Rectangle(a.x, a.y, a.width, a.height); + } + else + { + output.setTo(a.x, a.y, a.width, a.height); + } + + return output; + }; /** @@ -562,11 +613,31 @@ Phaser.Rectangle.clone = function (a, output) { * @return {boolean} A value of true if the Rectangle object contains the specified point; otherwise false. */ Phaser.Rectangle.contains = function (a, x, y) { + + if (a.width <= 0 || a.height <= 0) + { + return false; + } + return (x >= a.x && x <= a.right && y >= a.y && y <= a.bottom); + }; +/** +* Determines whether the specified coordinates are contained within the region defined by the given raw values. +* @method Phaser.Rectangle.containsRaw +* @param {number} rx - The x coordinate of the top left of the area. +* @param {number} ry - The y coordinate of the top left of the area. +* @param {number} rw - The width of the area. +* @param {number} rh - The height of the area. +* @param {number} x - The x coordinate of the point to test. +* @param {number} y - The y coordinate of the point to test. +* @return {boolean} A value of true if the Rectangle object contains the specified point; otherwise false. +*/ Phaser.Rectangle.containsRaw = function (rx, ry, rw, rh, x, y) { + return (x >= rx && x <= (rx + rw) && y >= ry && y <= (ry + rh)); + }; /** @@ -577,7 +648,9 @@ Phaser.Rectangle.containsRaw = function (rx, ry, rw, rh, x, y) { * @return {boolean} A value of true if the Rectangle object contains the specified point; otherwise false. */ Phaser.Rectangle.containsPoint = function (a, point) { + return Phaser.Rectangle.contains(a, point.x, point.y); + }; /** @@ -609,7 +682,9 @@ Phaser.Rectangle.containsRect = function (a, b) { * @return {boolean} A value of true if the two Rectangles have exactly the same values for the x, y, width and height properties; otherwise false. */ Phaser.Rectangle.equals = function (a, b) { + return (a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height); + }; /** @@ -617,22 +692,25 @@ Phaser.Rectangle.equals = function (a, b) { * @method Phaser.Rectangle.intersection * @param {Phaser.Rectangle} a - The first Rectangle object. * @param {Phaser.Rectangle} b - The second Rectangle object. -* @param {Phaser.Rectangle} [out] - Optional Rectangle object. If given the intersection values will be set into this object, otherwise a brand new Rectangle object will be created and returned. +* @param {Phaser.Rectangle} [output] - Optional Rectangle object. If given the intersection values will be set into this object, otherwise a brand new Rectangle object will be created and returned. * @return {Phaser.Rectangle} A Rectangle object that equals the area of intersection. If the Rectangles do not intersect, this method returns an empty Rectangle object; that is, a Rectangle with its x, y, width, and height properties set to 0. */ -Phaser.Rectangle.intersection = function (a, b, out) { +Phaser.Rectangle.intersection = function (a, b, output) { - out = out || new Phaser.Rectangle(); + if (typeof output === "undefined") + { + output = new Phaser.Rectangle(); + } if (Phaser.Rectangle.intersects(a, b)) { - out.x = Math.max(a.x, b.x); - out.y = Math.max(a.y, b.y); - out.width = Math.min(a.right, b.right) - out.x; - out.height = Math.min(a.bottom, b.bottom) - out.y; + output.x = Math.max(a.x, b.x); + output.y = Math.max(a.y, b.y); + output.width = Math.min(a.right, b.right) - output.x; + output.height = Math.min(a.bottom, b.bottom) - output.y; } - return out; + return output; }; @@ -678,13 +756,20 @@ Phaser.Rectangle.intersectsRaw = function (a, left, right, top, bottom, toleranc * @method Phaser.Rectangle.union * @param {Phaser.Rectangle} a - The first Rectangle object. * @param {Phaser.Rectangle} b - The second Rectangle object. -* @param {Phaser.Rectangle} [out] - Optional Rectangle object. If given the new values will be set into this object, otherwise a brand new Rectangle object will be created and returned. +* @param {Phaser.Rectangle} [output] - Optional Rectangle object. If given the new values will be set into this object, otherwise a brand new Rectangle object will be created and returned. * @return {Phaser.Rectangle} A Rectangle object that is the union of the two Rectangles. */ -Phaser.Rectangle.union = function (a, b, out) { +Phaser.Rectangle.union = function (a, b, output) { - if (typeof out === "undefined") { out = new Phaser.Rectangle(); } + if (typeof output === "undefined") + { + output = new Phaser.Rectangle(); + } - return out.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right) - Math.min(a.left, b.left), Math.max(a.bottom, b.bottom) - Math.min(a.top, b.top)); + return output.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right) - Math.min(a.left, b.left), Math.max(a.bottom, b.bottom) - Math.min(a.top, b.top)); }; + +// Because PIXI uses its own Rectangle, we'll replace it with ours to avoid duplicating code or confusion. +PIXI.Rectangle = Phaser.Rectangle; +PIXI.EmptyRectangle = new Phaser.Rectangle(0,0,0,0);