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.

This commit is contained in:
photonstorm 2014-02-06 12:29:07 +00:00
parent 8eae8feecc
commit e88b10323a
6 changed files with 173 additions and 41 deletions

View file

@ -38,6 +38,12 @@
<script src="$path/src/pixi/extras/Spine.js"></script>
<script src="$path/src/pixi/utils/Utils.js"></script>
<script src="$path/src/pixi/core/Point.js"></script>
<script src="$path/src/pixi/core/Rectangle.js"></script>
<script src="$path/src/pixi/core/Circle.js"></script>
<script src="$path/src/pixi/utils/Detector.js"></script>
*/
@ -48,11 +54,13 @@
<script src="$path/src/Phaser.js"></script>
<script src="$path/src/utils/Utils.js"></script>
<script src="$path/src/geom/Circle.js"></script>
<script src="$path/src/geom/Point.js"></script>
<script src="$path/src/geom/Rectangle.js"></script>
<script src="$path/src/geom/Line.js"></script>
<script src="$path/src/pixi/Pixi.js"></script>
<script src="$path/src/pixi/core/Point.js"></script>
<script src="$path/src/pixi/core/Rectangle.js"></script>
<script src="$path/src/pixi/core/Polygon.js"></script>
<script src="$path/src/pixi/core/Circle.js"></script>
<script src="$path/src/pixi/core/Ellipse.js"></script>
<script src="$path/src/pixi/core/Matrix.js"></script>
<script src="$path/src/pixi/display/DisplayObject.js"></script>
@ -65,7 +73,6 @@
<script src="$path/src/pixi/text/BitmapText.js"></script>
<script src="$path/src/pixi/display/Stage.js"></script>
<script src="$path/src/pixi/utils/EventTarget.js"></script>
<script src="$path/src/pixi/utils/Detector.js"></script>
<script src="$path/src/pixi/utils/Polyk.js"></script>
<script src="$path/src/pixi/renderers/webgl/utils/WebGLShaderUtils.js"></script>
<script src="$path/src/pixi/renderers/webgl/shaders/PixiShader.js"></script>
@ -138,10 +145,6 @@
<script src="$path/src/math/Math.js"></script>
<script src="$path/src/math/QuadTree.js"></script>
<script src="$path/src/geom/Circle.js"></script>
<script src="$path/src/geom/Point.js"></script>
<script src="$path/src/geom/Rectangle.js"></script>
<script src="$path/src/geom/Line.js"></script>
<script src="$path/src/net/Net.js"></script>

View file

@ -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() {

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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);