Farewell TypeScript, see you on the other side.

This commit is contained in:
Richard Davey
2013-08-28 07:02:57 +01:00
parent 0e55e644a9
commit 09def364c3
989 changed files with 67634 additions and 14984 deletions
+146
View File
@@ -0,0 +1,146 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods useful for manipulating canvas objects.
*
* @class CanvasUtils
*/
var CanvasUtils = (function () {
function CanvasUtils() { }
CanvasUtils.getAspectRatio = /**
* Returns the aspect ratio of the given canvas.
*
* @method getAspectRatio
* @param {HTMLCanvasElement} canvas The canvas to get the aspect ratio from.
* @return {Number} Returns true on success
*/
function getAspectRatio(canvas) {
return canvas.width / canvas.height;
};
CanvasUtils.setBackgroundColor = /**
* Sets the background color behind the canvas. This changes the canvas style property.
*
* @method setBackgroundColor
* @param {HTMLCanvasElement} canvas The canvas to set the background color on.
* @param {String} color The color to set. Can be in the format 'rgb(r,g,b)', or '#RRGGBB' or any valid CSS color.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
function setBackgroundColor(canvas, color) {
if (typeof color === "undefined") { color = 'rgb(0,0,0)'; }
canvas.style.backgroundColor = color;
return canvas;
};
CanvasUtils.setTouchAction = /**
* Sets the touch-action property on the canvas style. Can be used to disable default browser touch actions.
*
* @method setTouchAction
* @param {HTMLCanvasElement} canvas The canvas to set the touch action on.
* @param {String} value The touch action to set. Defaults to 'none'.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
function setTouchAction(canvas, value) {
if (typeof value === "undefined") { value = 'none'; }
canvas.style.msTouchAction = value;
canvas.style['ms-touch-action'] = value;
canvas.style['touch-action'] = value;
return canvas;
};
CanvasUtils.addToDOM = /**
* Adds the given canvas element to the DOM. The canvas will be added as a child of the given parent.
* If no parent is given it will be added as a child of the document.body.
*
* @method addToDOM
* @param {HTMLCanvasElement} canvas The canvas to set the touch action on.
* @param {String} parent The DOM element to add the canvas to. Defaults to ''.
* @param {bool} overflowHidden If set to true it will add the overflow='hidden' style to the parent DOM element.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
function addToDOM(canvas, parent, overflowHidden) {
if (typeof parent === "undefined") { parent = ''; }
if (typeof overflowHidden === "undefined") { overflowHidden = true; }
if((parent !== '' || parent !== null) && document.getElementById(parent)) {
document.getElementById(parent).appendChild(canvas);
if(overflowHidden) {
document.getElementById(parent).style.overflow = 'hidden';
}
} else {
document.body.appendChild(canvas);
}
return canvas;
};
CanvasUtils.setTransform = /**
* Sets the transform of the given canvas to the matrix values provided.
*
* @method setTransform
* @param {CanvasRenderingContext2D} context The context to set the transform on.
* @param {Number} translateX The value to translate horizontally by.
* @param {Number} translateY The value to translate vertically by.
* @param {Number} scaleX The value to scale horizontally by.
* @param {Number} scaleY The value to scale vertically by.
* @param {Number} skewX The value to skew horizontaly by.
* @param {Number} skewY The value to skew vertically by.
* @return {CanvasRenderingContext2D} Returns the source context.
*/
function setTransform(context, translateX, translateY, scaleX, scaleY, skewX, skewY) {
context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY);
return context;
};
CanvasUtils.setSmoothingEnabled = /**
* Sets the Image Smoothing property on the given context. Set to false to disable image smoothing.
* By default browsers have image smoothing enabled, which isn't always what you visually want, especially
* when using pixel art in a game. Note that this sets the property on the context itself, so that any image
* drawn to the context will be affected. This sets the property across all current browsers but support is
* patchy on earlier browsers, especially on mobile.
*
* @method setSmoothingEnabled
* @param {CanvasRenderingContext2D} context The context to enable or disable the image smoothing on.
* @param {bool} overflowHidden If set to true it will enable image smoothing, false will disable it.
* @return {CanvasRenderingContext2D} Returns the source context.
*/
function setSmoothingEnabled(context, value) {
context['imageSmoothingEnabled'] = value;
context['mozImageSmoothingEnabled'] = value;
context['oImageSmoothingEnabled'] = value;
context['webkitImageSmoothingEnabled'] = value;
context['msImageSmoothingEnabled'] = value;
return context;
};
CanvasUtils.setImageRenderingCrisp = /**
* Sets the CSS image-rendering property on the given canvas to be 'crisp' (aka 'optimize contrast on webkit').
* Note that if this doesn't given the desired result then see the CanvasUtils.setSmoothingEnabled method.
*
* @method setImageRenderingCrisp
* @param {HTMLCanvasElement} canvas The canvas to set image-rendering crisp on.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
function setImageRenderingCrisp(canvas) {
canvas.style['image-rendering'] = 'crisp-edges';
canvas.style['image-rendering'] = '-moz-crisp-edges';
canvas.style['image-rendering'] = '-webkit-optimize-contrast';
canvas.style.msInterpolationMode = 'nearest-neighbor';
return canvas;
};
CanvasUtils.setImageRenderingBicubic = /**
* Sets the CSS image-rendering property on the given canvas to be 'bicubic' (aka 'auto').
* Note that if this doesn't given the desired result then see the CanvasUtils.setSmoothingEnabled method.
*
* @method setImageRenderingBicubic
* @param {HTMLCanvasElement} canvas The canvas to set image-rendering bicubic on.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
function setImageRenderingBicubic(canvas) {
canvas.style['image-rendering'] = 'auto';
canvas.style.msInterpolationMode = 'bicubic';
return canvas;
};
return CanvasUtils;
})();
Phaser.CanvasUtils = CanvasUtils;
})(Phaser || (Phaser = {}));
+172
View File
@@ -0,0 +1,172 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods useful for manipulating canvas objects.
*
* @class CanvasUtils
*/
export class CanvasUtils {
/**
* Returns the aspect ratio of the given canvas.
*
* @method getAspectRatio
* @param {HTMLCanvasElement} canvas The canvas to get the aspect ratio from.
* @return {Number} Returns true on success
*/
public static getAspectRatio(canvas: HTMLCanvasElement): number {
return canvas.width / canvas.height;
}
/**
* Sets the background color behind the canvas. This changes the canvas style property.
*
* @method setBackgroundColor
* @param {HTMLCanvasElement} canvas The canvas to set the background color on.
* @param {String} color The color to set. Can be in the format 'rgb(r,g,b)', or '#RRGGBB' or any valid CSS color.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
public static setBackgroundColor(canvas: HTMLCanvasElement, color: string = 'rgb(0,0,0)'): HTMLCanvasElement {
canvas.style.backgroundColor = color;
return canvas;
}
/**
* Sets the touch-action property on the canvas style. Can be used to disable default browser touch actions.
*
* @method setTouchAction
* @param {HTMLCanvasElement} canvas The canvas to set the touch action on.
* @param {String} value The touch action to set. Defaults to 'none'.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
public static setTouchAction(canvas: HTMLCanvasElement, value: string= 'none'): HTMLCanvasElement {
canvas.style.msTouchAction = value;
canvas.style['ms-touch-action'] = value;
canvas.style['touch-action'] = value;
return canvas;
}
/**
* Adds the given canvas element to the DOM. The canvas will be added as a child of the given parent.
* If no parent is given it will be added as a child of the document.body.
*
* @method addToDOM
* @param {HTMLCanvasElement} canvas The canvas to set the touch action on.
* @param {String} parent The DOM element to add the canvas to. Defaults to ''.
* @param {bool} overflowHidden If set to true it will add the overflow='hidden' style to the parent DOM element.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
public static addToDOM(canvas: HTMLCanvasElement, parent: string = '', overflowHidden: bool = true): HTMLCanvasElement {
if ((parent !== '' || parent !== null) && document.getElementById(parent))
{
document.getElementById(parent).appendChild(canvas);
if (overflowHidden)
{
document.getElementById(parent).style.overflow = 'hidden';
}
}
else
{
document.body.appendChild(canvas);
}
return canvas;
}
/**
* Sets the transform of the given canvas to the matrix values provided.
*
* @method setTransform
* @param {CanvasRenderingContext2D} context The context to set the transform on.
* @param {Number} translateX The value to translate horizontally by.
* @param {Number} translateY The value to translate vertically by.
* @param {Number} scaleX The value to scale horizontally by.
* @param {Number} scaleY The value to scale vertically by.
* @param {Number} skewX The value to skew horizontaly by.
* @param {Number} skewY The value to skew vertically by.
* @return {CanvasRenderingContext2D} Returns the source context.
*/
public static setTransform(context: CanvasRenderingContext2D, translateX: number, translateY: number, scaleX: number, scaleY: number, skewX: number, skewY: number): CanvasRenderingContext2D {
context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY);
return context;
}
/**
* Sets the Image Smoothing property on the given context. Set to false to disable image smoothing.
* By default browsers have image smoothing enabled, which isn't always what you visually want, especially
* when using pixel art in a game. Note that this sets the property on the context itself, so that any image
* drawn to the context will be affected. This sets the property across all current browsers but support is
* patchy on earlier browsers, especially on mobile.
*
* @method setSmoothingEnabled
* @param {CanvasRenderingContext2D} context The context to enable or disable the image smoothing on.
* @param {bool} overflowHidden If set to true it will enable image smoothing, false will disable it.
* @return {CanvasRenderingContext2D} Returns the source context.
*/
public static setSmoothingEnabled(context: CanvasRenderingContext2D, value: bool): CanvasRenderingContext2D {
context['imageSmoothingEnabled'] = value;
context['mozImageSmoothingEnabled'] = value;
context['oImageSmoothingEnabled'] = value;
context['webkitImageSmoothingEnabled'] = value;
context['msImageSmoothingEnabled'] = value;
return context;
}
/**
* Sets the CSS image-rendering property on the given canvas to be 'crisp' (aka 'optimize contrast on webkit').
* Note that if this doesn't given the desired result then see the CanvasUtils.setSmoothingEnabled method.
*
* @method setImageRenderingCrisp
* @param {HTMLCanvasElement} canvas The canvas to set image-rendering crisp on.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
public static setImageRenderingCrisp(canvas: HTMLCanvasElement): HTMLCanvasElement {
canvas.style['image-rendering'] = 'crisp-edges';
canvas.style['image-rendering'] = '-moz-crisp-edges';
canvas.style['image-rendering'] = '-webkit-optimize-contrast';
canvas.style.msInterpolationMode = 'nearest-neighbor';
return canvas;
}
/**
* Sets the CSS image-rendering property on the given canvas to be 'bicubic' (aka 'auto').
* Note that if this doesn't given the desired result then see the CanvasUtils.setSmoothingEnabled method.
*
* @method setImageRenderingBicubic
* @param {HTMLCanvasElement} canvas The canvas to set image-rendering bicubic on.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
public static setImageRenderingBicubic(canvas: HTMLCanvasElement): HTMLCanvasElement {
canvas.style['image-rendering'] = 'auto';
canvas.style.msInterpolationMode = 'bicubic';
return canvas;
}
}
}
+154
View File
@@ -0,0 +1,154 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods useful for manipulating and comparing Circle objects.
*
* @class CircleUtils
*/
var CircleUtils = (function () {
function CircleUtils() { }
CircleUtils.clone = /**
* Returns a new Circle object with the same values for the x, y, width, and height properties as the given Circle object.
* @method clone
* @param {Phaser.Circle} a The Circle object to be cloned.
* @param {Phaser.Circle} out Optional Circle object. If given the values will be set into the object, otherwise a brand new Circle object will be created and returned.
* @return {Phaser.Circle} The cloned Circle object.
*/
function clone(a, out) {
if (typeof out === "undefined") { out = new Phaser.Circle(); }
return out.setTo(a.x, a.y, a.diameter);
};
CircleUtils.contains = /**
* Return true if the given x/y coordinates are within the Circle object.
* @method contains
* @param {Phaser.Circle} a The Circle to be checked.
* @param {Number} x The X value of the coordinate to test.
* @param {Number} y The Y value of the coordinate to test.
* @return {bool} True if the coordinates are within this circle, otherwise false.
*/
function contains(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) {
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;
};
CircleUtils.containsPoint = /**
* Return true if the coordinates of the given Point object are within this Circle object.
* @method containsPoint
* @param {Phaser.Circle} a The Circle object.
* @param {Phaser.Point} point The Point object to test.
* @return {bool} True if the coordinates are within this circle, otherwise false.
*/
function containsPoint(a, point) {
return CircleUtils.contains(a, point.x, point.y);
};
CircleUtils.containsCircle = /**
* Return true if the given Circle is contained entirely within this Circle object.
* @method containsCircle
* @param {Phaser.Circle} a The Circle object to test.
* @param {Phaser.Circle} b The Circle object to test.
* @return {bool} True if Circle B is contained entirely inside of Circle A, otherwise false.
*/
function containsCircle(a, b) {
//return ((a.radius + b.radius) * (a.radius + b.radius)) >= Collision.distanceSquared(a.x, a.y, b.x, b.y);
return true;
};
CircleUtils.distanceBetween = /**
* Returns the distance from the center of the Circle object to the given object
* (can be Circle, Point or anything with x/y properties)
* @method distanceBetween
* @param {Phaser.Circle} a The Circle object.
* @param {Phaser.Circle} b The target object. Must have visible x and y properties that represent the center of the object.
* @param {bool} [optional] round Round the distance to the nearest integer (default false)
* @return {Number} The distance between this Point object and the destination Point object.
*/
function distanceBetween(a, target, round) {
if (typeof round === "undefined") { round = false; }
var dx = a.x - target.x;
var dy = a.y - target.y;
if(round === true) {
return Math.round(Math.sqrt(dx * dx + dy * dy));
} else {
return Math.sqrt(dx * dx + dy * dy);
}
};
CircleUtils.equals = /**
* Determines whether the two Circle objects match. This method compares the x, y and diameter properties.
* @method equals
* @param {Phaser.Circle} a The first Circle object.
* @param {Phaser.Circle} b The second Circle object.
* @return {bool} A value of true if the object has exactly the same values for the x, y and diameter properties as this Circle object; otherwise false.
*/
function equals(a, b) {
return (a.x == b.x && a.y == b.y && a.diameter == b.diameter);
};
CircleUtils.intersects = /**
* Determines whether the two Circle objects intersect.
* This method checks the radius distances between the two Circle objects to see if they intersect.
* @method intersects
* @param {Phaser.Circle} a The first Circle object.
* @param {Phaser.Circle} b The second Circle object.
* @return {bool} A value of true if the specified object intersects with this Circle object; otherwise false.
*/
function intersects(a, b) {
return (Phaser.CircleUtils.distanceBetween(a, b) <= (a.radius + b.radius));
};
CircleUtils.circumferencePoint = /**
* Returns a Point object containing the coordinates of a point on the circumference of the Circle based on the given angle.
* @method circumferencePoint
* @param {Phaser.Circle} a The first Circle object.
* @param {Number} angle The angle in radians (unless asDegrees is true) to return the point from.
* @param {bool} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Phaser.Point} [optional] output An optional Point object to put the result in to. If none specified a new Point object will be created.
* @return {Phaser.Point} The Point object holding the result.
*/
function circumferencePoint(a, angle, asDegrees, out) {
if (typeof asDegrees === "undefined") { asDegrees = false; }
if (typeof out === "undefined") { out = new Phaser.Point(); }
if(asDegrees === true) {
angle = angle * Phaser.GameMath.DEG_TO_RAD;
}
return out.setTo(a.x + a.radius * Math.cos(angle), a.y + a.radius * Math.sin(angle));
};
CircleUtils.intersectsRectangle = /**
* Checks if the given Circle and Rectangle objects intersect.
* @method intersectsRectangle
* @param {Phaser.Circle} c The Circle object to test.
* @param {Phaser.Rectangle} r The Rectangle object to test.
* @return {bool} True if the two objects intersect, otherwise false.
*/
function intersectsRectangle(c, r) {
var cx = Math.abs(c.x - r.x - r.halfWidth);
var xDist = r.halfWidth + c.radius;
if(cx > xDist) {
return false;
}
var cy = Math.abs(c.y - r.y - r.halfHeight);
var yDist = r.halfHeight + c.radius;
if(cy > yDist) {
return false;
}
if(cx <= r.halfWidth || cy <= r.halfHeight) {
return true;
}
var xCornerDist = cx - r.halfWidth;
var yCornerDist = cy - r.halfHeight;
var xCornerDistSq = xCornerDist * xCornerDist;
var yCornerDistSq = yCornerDist * yCornerDist;
var maxCornerDistSq = c.radius * c.radius;
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
};
return CircleUtils;
})();
Phaser.CircleUtils = CircleUtils;
})(Phaser || (Phaser = {}));
+184
View File
@@ -0,0 +1,184 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods useful for manipulating and comparing Circle objects.
*
* @class CircleUtils
*/
export class CircleUtils {
/**
* Returns a new Circle object with the same values for the x, y, width, and height properties as the given Circle object.
* @method clone
* @param {Phaser.Circle} a The Circle object to be cloned.
* @param {Phaser.Circle} out Optional Circle object. If given the values will be set into the object, otherwise a brand new Circle object will be created and returned.
* @return {Phaser.Circle} The cloned Circle object.
*/
public static clone(a: Phaser.Circle, out: Phaser.Circle = new Phaser.Circle): Phaser.Circle {
return out.setTo(a.x, a.y, a.diameter);
}
/**
* Return true if the given x/y coordinates are within the Circle object.
* @method contains
* @param {Phaser.Circle} a The Circle to be checked.
* @param {Number} x The X value of the coordinate to test.
* @param {Number} y The Y value of the coordinate to test.
* @return {bool} True if the coordinates are within this circle, otherwise false.
*/
public static contains(a: Phaser.Circle, x: number, y: number): bool {
// Check if x/y are within the bounds first
if (x >= a.left && x <= a.right && y >= a.top && y <= a.bottom)
{
var dx: number = (a.x - x) * (a.x - x);
var dy: number = (a.y - y) * (a.y - y);
return (dx + dy) <= (a.radius * a.radius);
}
return false;
}
/**
* Return true if the coordinates of the given Point object are within this Circle object.
* @method containsPoint
* @param {Phaser.Circle} a The Circle object.
* @param {Phaser.Point} point The Point object to test.
* @return {bool} True if the coordinates are within this circle, otherwise false.
*/
public static containsPoint(a: Phaser.Circle, point: Phaser.Point): bool {
return CircleUtils.contains(a, point.x, point.y);
}
/**
* Return true if the given Circle is contained entirely within this Circle object.
* @method containsCircle
* @param {Phaser.Circle} a The Circle object to test.
* @param {Phaser.Circle} b The Circle object to test.
* @return {bool} True if Circle B is contained entirely inside of Circle A, otherwise false.
*/
public static containsCircle(a: Phaser.Circle, b: Phaser.Circle): bool {
//return ((a.radius + b.radius) * (a.radius + b.radius)) >= Collision.distanceSquared(a.x, a.y, b.x, b.y);
return true;
}
/**
* Returns the distance from the center of the Circle object to the given object
* (can be Circle, Point or anything with x/y properties)
* @method distanceBetween
* @param {Phaser.Circle} a The Circle object.
* @param {Phaser.Circle} b The target object. Must have visible x and y properties that represent the center of the object.
* @param {bool} [optional] round Round the distance to the nearest integer (default false)
* @return {Number} The distance between this Point object and the destination Point object.
*/
public static distanceBetween(a: Phaser.Circle, target: any, round: bool = false): number {
var dx = a.x - target.x;
var dy = a.y - target.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Determines whether the two Circle objects match. This method compares the x, y and diameter properties.
* @method equals
* @param {Phaser.Circle} a The first Circle object.
* @param {Phaser.Circle} b The second Circle object.
* @return {bool} A value of true if the object has exactly the same values for the x, y and diameter properties as this Circle object; otherwise false.
*/
public static equals(a: Phaser.Circle, b: Phaser.Circle): bool {
return (a.x == b.x && a.y == b.y && a.diameter == b.diameter);
}
/**
* Determines whether the two Circle objects intersect.
* This method checks the radius distances between the two Circle objects to see if they intersect.
* @method intersects
* @param {Phaser.Circle} a The first Circle object.
* @param {Phaser.Circle} b The second Circle object.
* @return {bool} A value of true if the specified object intersects with this Circle object; otherwise false.
*/
public static intersects(a: Phaser.Circle, b: Phaser.Circle): bool {
return (Phaser.CircleUtils.distanceBetween(a, b) <= (a.radius + b.radius));
}
/**
* Returns a Point object containing the coordinates of a point on the circumference of the Circle based on the given angle.
* @method circumferencePoint
* @param {Phaser.Circle} a The first Circle object.
* @param {Number} angle The angle in radians (unless asDegrees is true) to return the point from.
* @param {bool} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Phaser.Point} [optional] output An optional Point object to put the result in to. If none specified a new Point object will be created.
* @return {Phaser.Point} The Point object holding the result.
*/
public static circumferencePoint(a: Phaser.Circle, angle: number, asDegrees: bool = false, out: Phaser.Point = new Phaser.Point): Phaser.Point {
if (asDegrees === true)
{
angle = angle * Phaser.GameMath.DEG_TO_RAD;
}
return out.setTo(a.x + a.radius * Math.cos(angle), a.y + a.radius * Math.sin(angle));
}
/**
* Checks if the given Circle and Rectangle objects intersect.
* @method intersectsRectangle
* @param {Phaser.Circle} c The Circle object to test.
* @param {Phaser.Rectangle} r The Rectangle object to test.
* @return {bool} True if the two objects intersect, otherwise false.
*/
public static intersectsRectangle(c: Phaser.Circle, r: Phaser.Rectangle): bool {
var cx: number = Math.abs(c.x - r.x - r.halfWidth);
var xDist: number = r.halfWidth + c.radius;
if (cx > xDist)
{
return false;
}
var cy: number = Math.abs(c.y - r.y - r.halfHeight);
var yDist: number = r.halfHeight + c.radius;
if (cy > yDist)
{
return false;
}
if (cx <= r.halfWidth || cy <= r.halfHeight)
{
return true;
}
var xCornerDist: number = cx - r.halfWidth;
var yCornerDist: number = cy - r.halfHeight;
var xCornerDistSq = xCornerDist * xCornerDist;
var yCornerDistSq = yCornerDist * yCornerDist;
var maxCornerDistSq = c.radius * c.radius;
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
}
}
}
+485
View File
@@ -0,0 +1,485 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods useful for manipulating and comparing colors.
*
* @class ColorUtils
*/
var ColorUtils = (function () {
function ColorUtils() { }
ColorUtils.getColor32 = /**
* Given an alpha and 3 color values this will return an integer representation of it
*
* @method getColor32
* @param {Number} alpha The Alpha value (between 0 and 255)
* @param {Number} red The Red channel value (between 0 and 255)
* @param {Number} green The Green channel value (between 0 and 255)
* @param {Number} blue The Blue channel value (between 0 and 255)
* @return {Number} A native color value integer (format: 0xAARRGGBB)
*/
function getColor32(alpha, red, green, blue) {
return alpha << 24 | red << 16 | green << 8 | blue;
};
ColorUtils.getColor = /**
* Given 3 color values this will return an integer representation of it.
*
* @method getColor
* @param {Number} red The Red channel value (between 0 and 255)
* @param {Number} green The Green channel value (between 0 and 255)
* @param {Number} blue The Blue channel value (between 0 and 255)
* @return {Number} A native color value integer (format: 0xRRGGBB)
*/
function getColor(red, green, blue) {
return red << 16 | green << 8 | blue;
};
ColorUtils.getHSVColorWheel = /**
* Get HSV color wheel values in an array which will be 360 elements in size.
*
* @method getHSVColorWheel
* @param {Number} alpha Alpha value for each color of the color wheel, between 0 (transparent) and 255 (opaque)
* @return {Array} An array containing 360 elements corresponding to the HSV color wheel.
*/
function getHSVColorWheel(alpha) {
if (typeof alpha === "undefined") { alpha = 255; }
var colors = [];
for(var c = 0; c <= 359; c++) {
colors[c] = Phaser.ColorUtils.getWebRGB(Phaser.ColorUtils.HSVtoRGB(c, 1.0, 1.0, alpha));
}
return colors;
};
ColorUtils.hexToRGB = /**
* Converts the given hex string into an object containing the RGB values.
*
* @method hexToRGB
* @param {String} The string hex color to convert.
* @return {Object} An object with 3 properties: r,g and b.
*/
function hexToRGB(h) {
var hex16 = (h.charAt(0) == "#") ? h.substring(1, 7) : h;
var r = parseInt(hex16.substring(0, 2), 16);
var g = parseInt(hex16.substring(2, 4), 16);
var b = parseInt(hex16.substring(4, 6), 16);
return {
r: r,
g: g,
b: b
};
};
ColorUtils.getComplementHarmony = /**
* Returns a Complementary Color Harmony for the given color.
* <p>A complementary hue is one directly opposite the color given on the color wheel</p>
* <p>Value returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getComplementHarmony
* @param {Number} color The color to base the harmony on.
* @return {Number} 0xAARRGGBB format color value.
*/
function getComplementHarmony(color) {
var hsv = Phaser.ColorUtils.RGBtoHSV(color);
var opposite = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 180, 359);
return Phaser.ColorUtils.HSVtoRGB(opposite, 1.0, 1.0);
};
ColorUtils.getAnalogousHarmony = /**
* Returns an Analogous Color Harmony for the given color.
* <p>An Analogous harmony are hues adjacent to each other on the color wheel</p>
* <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getAnalogousHarmony
* @param {Number} color The color to base the harmony on.
* @param {Number} threshold Control how adjacent the colors will be (default +- 30 degrees)
* @return {Object} Object containing 3 properties: color1 (the original color), color2 (the warmer analogous color) and color3 (the colder analogous color)
*/
function getAnalogousHarmony(color, threshold) {
if (typeof threshold === "undefined") { threshold = 30; }
var hsv = Phaser.ColorUtils.RGBtoHSV(color);
if(threshold > 359 || threshold < 0) {
throw Error("Color Warning: Invalid threshold given to getAnalogousHarmony()");
}
var warmer = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 359 - threshold, 359);
var colder = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, threshold, 359);
return {
color1: color,
color2: Phaser.ColorUtils.HSVtoRGB(warmer, 1.0, 1.0),
color3: Phaser.ColorUtils.HSVtoRGB(colder, 1.0, 1.0),
hue1: hsv.hue,
hue2: warmer,
hue3: colder
};
};
ColorUtils.getSplitComplementHarmony = /**
* Returns an Split Complement Color Harmony for the given color.
* <p>A Split Complement harmony are the two hues on either side of the color's Complement</p>
* <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getSplitComplementHarmony
* @param {Number} color The color to base the harmony on
* @param {Number} threshold Control how adjacent the colors will be to the Complement (default +- 30 degrees)
* @return {Object} An object containing 3 properties: color1 (the original color), color2 (the warmer analogous color) and color3 (the colder analogous color)
*/
function getSplitComplementHarmony(color, threshold) {
if (typeof threshold === "undefined") { threshold = 30; }
var hsv = Phaser.ColorUtils.RGBtoHSV(color);
if(threshold >= 359 || threshold <= 0) {
throw Error("Phaser.ColorUtils Warning: Invalid threshold given to getSplitComplementHarmony()");
}
var opposite = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 180, 359);
var warmer = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, opposite - threshold, 359);
var colder = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, opposite + threshold, 359);
return {
color1: color,
color2: Phaser.ColorUtils.HSVtoRGB(warmer, hsv.saturation, hsv.value),
color3: Phaser.ColorUtils.HSVtoRGB(colder, hsv.saturation, hsv.value),
hue1: hsv.hue,
hue2: warmer,
hue3: colder
};
};
ColorUtils.getTriadicHarmony = /**
* Returns a Triadic Color Harmony for the given color.
* <p>A Triadic harmony are 3 hues equidistant from each other on the color wheel</p>
* <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getTriadicHarmony
* @param {Number} color The color to base the harmony on.
* @return {Object} An Object containing 3 properties: color1 (the original color), color2 and color3 (the equidistant colors)
*/
function getTriadicHarmony(color) {
var hsv = Phaser.ColorUtils.RGBtoHSV(color);
var triadic1 = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 120, 359);
var triadic2 = Phaser.ColorUtils.game.math.wrapValue(triadic1, 120, 359);
return {
color1: color,
color2: Phaser.ColorUtils.HSVtoRGB(triadic1, 1.0, 1.0),
color3: Phaser.ColorUtils.HSVtoRGB(triadic2, 1.0, 1.0)
};
};
ColorUtils.getColorInfo = /**
* Returns a string containing handy information about the given color including string hex value,
* RGB format information and HSL information. Each section starts on a newline, 3 lines in total.
*
* @method getColorInfo
* @param {Number} color A color value in the format 0xAARRGGBB
* @return {String} string containing the 3 lines of information
*/
function getColorInfo(color) {
var argb = Phaser.ColorUtils.getRGB(color);
var hsl = Phaser.ColorUtils.RGBtoHSV(color);
// Hex format
var result = Phaser.ColorUtils.RGBtoHexstring(color) + "\n";
// RGB format
result = result.concat("Alpha: " + argb.alpha + " Red: " + argb.red + " Green: " + argb.green + " Blue: " + argb.blue) + "\n";
// HSL info
result = result.concat("Hue: " + hsl.hue + " Saturation: " + hsl.saturation + " Lightnes: " + hsl.lightness);
return result;
};
ColorUtils.RGBtoHexstring = /**
* Return a string representation of the color in the format 0xAARRGGBB
*
* @method RGBtoHexstring
* @param {Number} color The color to get the string representation for
* @return {String A string of length 10 characters in the format 0xAARRGGBB
*/
function RGBtoHexstring(color) {
var argb = Phaser.ColorUtils.getRGB(color);
return "0x" + Phaser.ColorUtils.colorToHexstring(argb.alpha) + Phaser.ColorUtils.colorToHexstring(argb.red) + Phaser.ColorUtils.colorToHexstring(argb.green) + Phaser.ColorUtils.colorToHexstring(argb.blue);
};
ColorUtils.RGBtoWebstring = /**
* Return a string representation of the color in the format #RRGGBB
*
* @method RGBtoWebstring
* @param {Number} color The color to get the string representation for
* @return {String} A string of length 10 characters in the format 0xAARRGGBB
*/
function RGBtoWebstring(color) {
var argb = Phaser.ColorUtils.getRGB(color);
return "#" + Phaser.ColorUtils.colorToHexstring(argb.red) + Phaser.ColorUtils.colorToHexstring(argb.green) + Phaser.ColorUtils.colorToHexstring(argb.blue);
};
ColorUtils.colorToHexstring = /**
* Return a string containing a hex representation of the given color
*
* @method colorToHexstring
* @param {Number} color The color channel to get the hex value for, must be a value between 0 and 255)
* @return {String} A string of length 2 characters, i.e. 255 = FF, 0 = 00
*/
function colorToHexstring(color) {
var digits = "0123456789ABCDEF";
var lsd = color % 16;
var msd = (color - lsd) / 16;
var hexified = digits.charAt(msd) + digits.charAt(lsd);
return hexified;
};
ColorUtils.HSVtoRGB = /**
* Convert a HSV (hue, saturation, lightness) color space value to an RGB color
*
* @method HSVtoRGB
* @param {Number} h Hue degree, between 0 and 359
* @param {Number} s Saturation, between 0.0 (grey) and 1.0
* @param {Number} v Value, between 0.0 (black) and 1.0
* @param {Number} alpha Alpha value to set per color (between 0 and 255)
* @return {Number} 32-bit ARGB color value (0xAARRGGBB)
*/
function HSVtoRGB(h, s, v, alpha) {
if (typeof alpha === "undefined") { alpha = 255; }
var result;
if(s == 0.0) {
result = Phaser.ColorUtils.getColor32(alpha, v * 255, v * 255, v * 255);
} else {
h = h / 60.0;
var f = h - Math.floor(h);
var p = v * (1.0 - s);
var q = v * (1.0 - s * f);
var t = v * (1.0 - s * (1.0 - f));
switch(Math.floor(h)) {
case 0:
result = Phaser.ColorUtils.getColor32(alpha, v * 255, t * 255, p * 255);
break;
case 1:
result = Phaser.ColorUtils.getColor32(alpha, q * 255, v * 255, p * 255);
break;
case 2:
result = Phaser.ColorUtils.getColor32(alpha, p * 255, v * 255, t * 255);
break;
case 3:
result = Phaser.ColorUtils.getColor32(alpha, p * 255, q * 255, v * 255);
break;
case 4:
result = Phaser.ColorUtils.getColor32(alpha, t * 255, p * 255, v * 255);
break;
case 5:
result = Phaser.ColorUtils.getColor32(alpha, v * 255, p * 255, q * 255);
break;
default:
throw new Error("Phaser.ColorUtils.HSVtoRGB : Unknown color");
}
}
return result;
};
ColorUtils.RGBtoHSV = /**
* Convert an RGB color value to an object containing the HSV color space values: Hue, Saturation and Lightness
*
* @method RGBtoHSV
* @param {Number} color In format 0xRRGGBB
* @return {Object} An Object with the properties hue (from 0 to 360), saturation (from 0 to 1.0) and lightness (from 0 to 1.0, also available under .value)
*/
function RGBtoHSV(color) {
var rgb = Phaser.ColorUtils.getRGB(color);
var red = rgb.red / 255;
var green = rgb.green / 255;
var blue = rgb.blue / 255;
var min = Math.min(red, green, blue);
var max = Math.max(red, green, blue);
var delta = max - min;
var lightness = (max + min) / 2;
var hue;
var saturation;
// Grey color, no chroma
if(delta == 0) {
hue = 0;
saturation = 0;
} else {
if(lightness < 0.5) {
saturation = delta / (max + min);
} else {
saturation = delta / (2 - max - min);
}
var delta_r = (((max - red) / 6) + (delta / 2)) / delta;
var delta_g = (((max - green) / 6) + (delta / 2)) / delta;
var delta_b = (((max - blue) / 6) + (delta / 2)) / delta;
if(red == max) {
hue = delta_b - delta_g;
} else if(green == max) {
hue = (1 / 3) + delta_r - delta_b;
} else if(blue == max) {
hue = (2 / 3) + delta_g - delta_r;
}
if(hue < 0) {
hue += 1;
}
if(hue > 1) {
hue -= 1;
}
}
// Keep the value with 0 to 359
hue *= 360;
hue = Math.round(hue);
return {
hue: hue,
saturation: saturation,
lightness: lightness,
value: lightness
};
};
ColorUtils.interpolateColor = /**
* Interpolates the two given colours based on the supplied step and currentStep properties.
* @method interpolateColor
* @param {Number} color1
* @param {Number} color2
* @param {Number} steps
* @param {Number} currentStep
* @param {Number} alpha
* @return {Number} The interpolated color value.
*/
function interpolateColor(color1, color2, steps, currentStep, alpha) {
if (typeof alpha === "undefined") { alpha = 255; }
var src1 = Phaser.ColorUtils.getRGB(color1);
var src2 = Phaser.ColorUtils.getRGB(color2);
var r = (((src2.red - src1.red) * currentStep) / steps) + src1.red;
var g = (((src2.green - src1.green) * currentStep) / steps) + src1.green;
var b = (((src2.blue - src1.blue) * currentStep) / steps) + src1.blue;
return Phaser.ColorUtils.getColor32(alpha, r, g, b);
};
ColorUtils.interpolateColorWithRGB = /**
* Interpolates the two given colours based on the supplied step and currentStep properties.
* @method interpolateColorWithRGB
* @param {Number} color
* @param {Number} r
* @param {Number} g
* @param {Number} b
* @param {Number} steps
* @param {Number} currentStep
* @return {Number} The interpolated color value.
*/
function interpolateColorWithRGB(color, r, g, b, steps, currentStep) {
var src = Phaser.ColorUtils.getRGB(color);
var or = (((r - src.red) * currentStep) / steps) + src.red;
var og = (((g - src.green) * currentStep) / steps) + src.green;
var ob = (((b - src.blue) * currentStep) / steps) + src.blue;
return Phaser.ColorUtils.getColor(or, og, ob);
};
ColorUtils.interpolateRGB = /**
* Interpolates the two given colours based on the supplied step and currentStep properties.
* @method interpolateRGB
* @param {Number} r1
* @param {Number} g1
* @param {Number} b1
* @param {Number} r2
* @param {Number} g2
* @param {Number} b2
* @param {Number} steps
* @param {Number} currentStep
* @return {Number} The interpolated color value.
*/
function interpolateRGB(r1, g1, b1, r2, g2, b2, steps, currentStep) {
var r = (((r2 - r1) * currentStep) / steps) + r1;
var g = (((g2 - g1) * currentStep) / steps) + g1;
var b = (((b2 - b1) * currentStep) / steps) + b1;
return Phaser.ColorUtils.getColor(r, g, b);
};
ColorUtils.getRandomColor = /**
* Returns a random color value between black and white
* <p>Set the min value to start each channel from the given offset.</p>
* <p>Set the max value to restrict the maximum color used per channel</p>
*
* @method getRandomColor
* @param {Number} min The lowest value to use for the color
* @param {Number} max The highest value to use for the color
* @param {Number} alpha The alpha value of the returning color (default 255 = fully opaque)
* @return {Number} 32-bit color value with alpha
*/
function getRandomColor(min, max, alpha) {
if (typeof min === "undefined") { min = 0; }
if (typeof max === "undefined") { max = 255; }
if (typeof alpha === "undefined") { alpha = 255; }
// Sanity checks
if(max > 255) {
return Phaser.ColorUtils.getColor(255, 255, 255);
}
if(min > max) {
return Phaser.ColorUtils.getColor(255, 255, 255);
}
var red = min + Math.round(Math.random() * (max - min));
var green = min + Math.round(Math.random() * (max - min));
var blue = min + Math.round(Math.random() * (max - min));
return Phaser.ColorUtils.getColor32(alpha, red, green, blue);
};
ColorUtils.getRGB = /**
* Return the component parts of a color as an Object with the properties alpha, red, green, blue
*
* <p>Alpha will only be set if it exist in the given color (0xAARRGGBB)</p>
*
* @method getRGB
* @param {Number} color in RGB (0xRRGGBB) or ARGB format (0xAARRGGBB)
* @return {Object} An Object with properties: alpha, red, green, blue
*/
function getRGB(color) {
return {
alpha: color >>> 24,
red: color >> 16 & 0xFF,
green: color >> 8 & 0xFF,
blue: color & 0xFF
};
};
ColorUtils.getWebRGB = /**
* Returns a CSS friendly string value from the given color.
* @method getWebRGB
* @param {Number} color
* @return {String} A string in the format: 'rgba(r,g,b,a)'
*/
function getWebRGB(color) {
var alpha = (color >>> 24) / 255;
var red = color >> 16 & 0xFF;
var green = color >> 8 & 0xFF;
var blue = color & 0xFF;
return 'rgba(' + red.toString() + ',' + green.toString() + ',' + blue.toString() + ',' + alpha.toString() + ')';
};
ColorUtils.getAlpha = /**
* Given a native color value (in the format 0xAARRGGBB) this will return the Alpha component, as a value between 0 and 255
*
* @method getAlpha
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Alpha component of the color, will be between 0 and 1 (0 being no Alpha (opaque), 1 full Alpha (transparent))
*/
function getAlpha(color) {
return color >>> 24;
};
ColorUtils.getAlphaFloat = /**
* Given a native color value (in the format 0xAARRGGBB) this will return the Alpha component as a value between 0 and 1
*
* @method getAlphaFloat
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Alpha component of the color, will be between 0 and 1 (0 being no Alpha (opaque), 1 full Alpha (transparent))
*/
function getAlphaFloat(color) {
return (color >>> 24) / 255;
};
ColorUtils.getRed = /**
* Given a native color value (in the format 0xAARRGGBB) this will return the Red component, as a value between 0 and 255
*
* @method getRed
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Red component of the color, will be between 0 and 255 (0 being no color, 255 full Red)
*/
function getRed(color) {
return color >> 16 & 0xFF;
};
ColorUtils.getGreen = /**
* Given a native color value (in the format 0xAARRGGBB) this will return the Green component, as a value between 0 and 255
*
* @method getGreen
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Green component of the color, will be between 0 and 255 (0 being no color, 255 full Green)
*/
function getGreen(color) {
return color >> 8 & 0xFF;
};
ColorUtils.getBlue = /**
* Given a native color value (in the format 0xAARRGGBB) this will return the Blue component, as a value between 0 and 255
*
* @method getBlue
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Blue component of the color, will be between 0 and 255 (0 being no color, 255 full Blue)
*/
function getBlue(color) {
return color & 0xFF;
};
return ColorUtils;
})();
Phaser.ColorUtils = ColorUtils;
})(Phaser || (Phaser = {}));
+586
View File
@@ -0,0 +1,586 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods useful for manipulating and comparing colors.
*
* @class ColorUtils
*/
export class ColorUtils {
/**
* A reference to the currently running Game.
* @property game
* @type {Phaser.Game}
*/
public static game: Phaser.Game;
/**
* Given an alpha and 3 color values this will return an integer representation of it
*
* @method getColor32
* @param {Number} alpha The Alpha value (between 0 and 255)
* @param {Number} red The Red channel value (between 0 and 255)
* @param {Number} green The Green channel value (between 0 and 255)
* @param {Number} blue The Blue channel value (between 0 and 255)
* @return {Number} A native color value integer (format: 0xAARRGGBB)
*/
public static getColor32(alpha: number, red: number, green: number, blue: number): number {
return alpha << 24 | red << 16 | green << 8 | blue;
}
/**
* Given 3 color values this will return an integer representation of it.
*
* @method getColor
* @param {Number} red The Red channel value (between 0 and 255)
* @param {Number} green The Green channel value (between 0 and 255)
* @param {Number} blue The Blue channel value (between 0 and 255)
* @return {Number} A native color value integer (format: 0xRRGGBB)
*/
public static getColor(red: number, green: number, blue: number): number {
return red << 16 | green << 8 | blue;
}
/**
* Get HSV color wheel values in an array which will be 360 elements in size.
*
* @method getHSVColorWheel
* @param {Number} alpha Alpha value for each color of the color wheel, between 0 (transparent) and 255 (opaque)
* @return {Array} An array containing 360 elements corresponding to the HSV color wheel.
*/
public static getHSVColorWheel(alpha: number = 255):number[] {
var colors = [];
for (var c: number = 0; c <= 359; c++)
{
colors[c] = Phaser.ColorUtils.getWebRGB(Phaser.ColorUtils.HSVtoRGB(c, 1.0, 1.0, alpha));
}
return colors;
}
/**
* Converts the given hex string into an object containing the RGB values.
*
* @method hexToRGB
* @param {String} The string hex color to convert.
* @return {Object} An object with 3 properties: r,g and b.
*/
public static hexToRGB(h: string) {
var hex16 = (h.charAt(0) == "#") ? h.substring(1, 7) : h;
var r = parseInt(hex16.substring(0, 2), 16);
var g = parseInt(hex16.substring(2, 4), 16);
var b = parseInt(hex16.substring(4, 6), 16);
return {
r: r,
g: g,
b: b
}
}
/**
* Returns a Complementary Color Harmony for the given color.
* <p>A complementary hue is one directly opposite the color given on the color wheel</p>
* <p>Value returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getComplementHarmony
* @param {Number} color The color to base the harmony on.
* @return {Number} 0xAARRGGBB format color value.
*/
public static getComplementHarmony(color: number): number {
var hsv: any = Phaser.ColorUtils.RGBtoHSV(color);
var opposite: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 180, 359);
return Phaser.ColorUtils.HSVtoRGB(opposite, 1.0, 1.0);
}
/**
* Returns an Analogous Color Harmony for the given color.
* <p>An Analogous harmony are hues adjacent to each other on the color wheel</p>
* <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getAnalogousHarmony
* @param {Number} color The color to base the harmony on.
* @param {Number} threshold Control how adjacent the colors will be (default +- 30 degrees)
* @return {Object} Object containing 3 properties: color1 (the original color), color2 (the warmer analogous color) and color3 (the colder analogous color)
*/
public static getAnalogousHarmony(color: number, threshold: number = 30) {
var hsv: any = Phaser.ColorUtils.RGBtoHSV(color);
if (threshold > 359 || threshold < 0)
{
throw Error("Color Warning: Invalid threshold given to getAnalogousHarmony()");
}
var warmer: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 359 - threshold, 359);
var colder: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, threshold, 359);
return { color1: color, color2: Phaser.ColorUtils.HSVtoRGB(warmer, 1.0, 1.0), color3: Phaser.ColorUtils.HSVtoRGB(colder, 1.0, 1.0), hue1: hsv.hue, hue2: warmer, hue3: colder }
}
/**
* Returns an Split Complement Color Harmony for the given color.
* <p>A Split Complement harmony are the two hues on either side of the color's Complement</p>
* <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getSplitComplementHarmony
* @param {Number} color The color to base the harmony on
* @param {Number} threshold Control how adjacent the colors will be to the Complement (default +- 30 degrees)
* @return {Object} An object containing 3 properties: color1 (the original color), color2 (the warmer analogous color) and color3 (the colder analogous color)
*/
public static getSplitComplementHarmony(color: number, threshold: number = 30): any {
var hsv: any = Phaser.ColorUtils.RGBtoHSV(color);
if (threshold >= 359 || threshold <= 0)
{
throw Error("Phaser.ColorUtils Warning: Invalid threshold given to getSplitComplementHarmony()");
}
var opposite: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 180, 359);
var warmer: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, opposite - threshold, 359);
var colder: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, opposite + threshold, 359);
return { color1: color, color2: Phaser.ColorUtils.HSVtoRGB(warmer, hsv.saturation, hsv.value), color3: Phaser.ColorUtils.HSVtoRGB(colder, hsv.saturation, hsv.value), hue1: hsv.hue, hue2: warmer, hue3: colder }
}
/**
* Returns a Triadic Color Harmony for the given color.
* <p>A Triadic harmony are 3 hues equidistant from each other on the color wheel</p>
* <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p>
*
* @method getTriadicHarmony
* @param {Number} color The color to base the harmony on.
* @return {Object} An Object containing 3 properties: color1 (the original color), color2 and color3 (the equidistant colors)
*/
public static getTriadicHarmony(color: number): any {
var hsv: any = Phaser.ColorUtils.RGBtoHSV(color);
var triadic1: number = Phaser.ColorUtils.game.math.wrapValue(hsv.hue, 120, 359);
var triadic2: number = Phaser.ColorUtils.game.math.wrapValue(triadic1, 120, 359);
return { color1: color, color2: Phaser.ColorUtils.HSVtoRGB(triadic1, 1.0, 1.0), color3: Phaser.ColorUtils.HSVtoRGB(triadic2, 1.0, 1.0) }
}
/**
* Returns a string containing handy information about the given color including string hex value,
* RGB format information and HSL information. Each section starts on a newline, 3 lines in total.
*
* @method getColorInfo
* @param {Number} color A color value in the format 0xAARRGGBB
* @return {String} string containing the 3 lines of information
*/
public static getColorInfo(color: number): string {
var argb: any = Phaser.ColorUtils.getRGB(color);
var hsl: any = Phaser.ColorUtils.RGBtoHSV(color);
// Hex format
var result: string = Phaser.ColorUtils.RGBtoHexstring(color) + "\n";
// RGB format
result = result.concat("Alpha: " + argb.alpha + " Red: " + argb.red + " Green: " + argb.green + " Blue: " + argb.blue) + "\n";
// HSL info
result = result.concat("Hue: " + hsl.hue + " Saturation: " + hsl.saturation + " Lightnes: " + hsl.lightness);
return result;
}
/**
* Return a string representation of the color in the format 0xAARRGGBB
*
* @method RGBtoHexstring
* @param {Number} color The color to get the string representation for
* @return {String A string of length 10 characters in the format 0xAARRGGBB
*/
public static RGBtoHexstring(color: number): string {
var argb: any = Phaser.ColorUtils.getRGB(color);
return "0x" + Phaser.ColorUtils.colorToHexstring(argb.alpha) + Phaser.ColorUtils.colorToHexstring(argb.red) + Phaser.ColorUtils.colorToHexstring(argb.green) + Phaser.ColorUtils.colorToHexstring(argb.blue);
}
/**
* Return a string representation of the color in the format #RRGGBB
*
* @method RGBtoWebstring
* @param {Number} color The color to get the string representation for
* @return {String} A string of length 10 characters in the format 0xAARRGGBB
*/
public static RGBtoWebstring(color: number): string {
var argb: any = Phaser.ColorUtils.getRGB(color);
return "#" + Phaser.ColorUtils.colorToHexstring(argb.red) + Phaser.ColorUtils.colorToHexstring(argb.green) + Phaser.ColorUtils.colorToHexstring(argb.blue);
}
/**
* Return a string containing a hex representation of the given color
*
* @method colorToHexstring
* @param {Number} color The color channel to get the hex value for, must be a value between 0 and 255)
* @return {String} A string of length 2 characters, i.e. 255 = FF, 0 = 00
*/
public static colorToHexstring(color: number): string {
var digits: string = "0123456789ABCDEF";
var lsd: number = color % 16;
var msd: number = (color - lsd) / 16;
var hexified: string = digits.charAt(msd) + digits.charAt(lsd);
return hexified;
}
/**
* Convert a HSV (hue, saturation, lightness) color space value to an RGB color
*
* @method HSVtoRGB
* @param {Number} h Hue degree, between 0 and 359
* @param {Number} s Saturation, between 0.0 (grey) and 1.0
* @param {Number} v Value, between 0.0 (black) and 1.0
* @param {Number} alpha Alpha value to set per color (between 0 and 255)
* @return {Number} 32-bit ARGB color value (0xAARRGGBB)
*/
static HSVtoRGB(h: number, s: number, v: number, alpha: number = 255): number {
var result: number;
if (s == 0.0)
{
result = Phaser.ColorUtils.getColor32(alpha, v * 255, v * 255, v * 255);
}
else
{
h = h / 60.0;
var f: number = h - Math.floor(h);
var p: number = v * (1.0 - s);
var q: number = v * (1.0 - s * f);
var t: number = v * (1.0 - s * (1.0 - f));
switch (Math.floor(h))
{
case 0:
result = Phaser.ColorUtils.getColor32(alpha, v * 255, t * 255, p * 255);
break;
case 1:
result = Phaser.ColorUtils.getColor32(alpha, q * 255, v * 255, p * 255);
break;
case 2:
result = Phaser.ColorUtils.getColor32(alpha, p * 255, v * 255, t * 255);
break;
case 3:
result = Phaser.ColorUtils.getColor32(alpha, p * 255, q * 255, v * 255);
break;
case 4:
result = Phaser.ColorUtils.getColor32(alpha, t * 255, p * 255, v * 255);
break;
case 5:
result = Phaser.ColorUtils.getColor32(alpha, v * 255, p * 255, q * 255);
break;
default:
throw new Error("Phaser.ColorUtils.HSVtoRGB : Unknown color");
}
}
return result;
}
/**
* Convert an RGB color value to an object containing the HSV color space values: Hue, Saturation and Lightness
*
* @method RGBtoHSV
* @param {Number} color In format 0xRRGGBB
* @return {Object} An Object with the properties hue (from 0 to 360), saturation (from 0 to 1.0) and lightness (from 0 to 1.0, also available under .value)
*/
public static RGBtoHSV(color: number): any {
var rgb: any = Phaser.ColorUtils.getRGB(color);
var red: number = rgb.red / 255;
var green: number = rgb.green / 255;
var blue: number = rgb.blue / 255;
var min: number = Math.min(red, green, blue);
var max: number = Math.max(red, green, blue);
var delta: number = max - min;
var lightness: number = (max + min) / 2;
var hue: number;
var saturation: number;
// Grey color, no chroma
if (delta == 0)
{
hue = 0;
saturation = 0;
}
else
{
if (lightness < 0.5)
{
saturation = delta / (max + min);
}
else
{
saturation = delta / (2 - max - min);
}
var delta_r: number = (((max - red) / 6) + (delta / 2)) / delta;
var delta_g: number = (((max - green) / 6) + (delta / 2)) / delta;
var delta_b: number = (((max - blue) / 6) + (delta / 2)) / delta;
if (red == max)
{
hue = delta_b - delta_g;
}
else if (green == max)
{
hue = (1 / 3) + delta_r - delta_b;
}
else if (blue == max)
{
hue = (2 / 3) + delta_g - delta_r;
}
if (hue < 0)
{
hue += 1;
}
if (hue > 1)
{
hue -= 1;
}
}
// Keep the value with 0 to 359
hue *= 360;
hue = Math.round(hue);
return { hue: hue, saturation: saturation, lightness: lightness, value: lightness };
}
/**
* Interpolates the two given colours based on the supplied step and currentStep properties.
* @method interpolateColor
* @param {Number} color1
* @param {Number} color2
* @param {Number} steps
* @param {Number} currentStep
* @param {Number} alpha
* @return {Number} The interpolated color value.
*/
public static interpolateColor(color1: number, color2: number, steps: number, currentStep: number, alpha: number = 255): number {
var src1: any = Phaser.ColorUtils.getRGB(color1);
var src2: any = Phaser.ColorUtils.getRGB(color2);
var r: number = (((src2.red - src1.red) * currentStep) / steps) + src1.red;
var g: number = (((src2.green - src1.green) * currentStep) / steps) + src1.green;
var b: number = (((src2.blue - src1.blue) * currentStep) / steps) + src1.blue;
return Phaser.ColorUtils.getColor32(alpha, r, g, b);
}
/**
* Interpolates the two given colours based on the supplied step and currentStep properties.
* @method interpolateColorWithRGB
* @param {Number} color
* @param {Number} r
* @param {Number} g
* @param {Number} b
* @param {Number} steps
* @param {Number} currentStep
* @return {Number} The interpolated color value.
*/
public static interpolateColorWithRGB(color: number, r: number, g: number, b: number, steps: number, currentStep: number): number {
var src: any = Phaser.ColorUtils.getRGB(color);
var or: number = (((r - src.red) * currentStep) / steps) + src.red;
var og: number = (((g - src.green) * currentStep) / steps) + src.green;
var ob: number = (((b - src.blue) * currentStep) / steps) + src.blue;
return Phaser.ColorUtils.getColor(or, og, ob);
}
/**
* Interpolates the two given colours based on the supplied step and currentStep properties.
* @method interpolateRGB
* @param {Number} r1
* @param {Number} g1
* @param {Number} b1
* @param {Number} r2
* @param {Number} g2
* @param {Number} b2
* @param {Number} steps
* @param {Number} currentStep
* @return {Number} The interpolated color value.
*/
public static interpolateRGB(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number, steps: number, currentStep: number): number {
var r: number = (((r2 - r1) * currentStep) / steps) + r1;
var g: number = (((g2 - g1) * currentStep) / steps) + g1;
var b: number = (((b2 - b1) * currentStep) / steps) + b1;
return Phaser.ColorUtils.getColor(r, g, b);
}
/**
* Returns a random color value between black and white
* <p>Set the min value to start each channel from the given offset.</p>
* <p>Set the max value to restrict the maximum color used per channel</p>
*
* @method getRandomColor
* @param {Number} min The lowest value to use for the color
* @param {Number} max The highest value to use for the color
* @param {Number} alpha The alpha value of the returning color (default 255 = fully opaque)
* @return {Number} 32-bit color value with alpha
*/
public static getRandomColor(min: number = 0, max: number = 255, alpha: number = 255): number {
// Sanity checks
if (max > 255)
{
return Phaser.ColorUtils.getColor(255, 255, 255);
}
if (min > max)
{
return Phaser.ColorUtils.getColor(255, 255, 255);
}
var red: number = min + Math.round(Math.random() * (max - min));
var green: number = min + Math.round(Math.random() * (max - min));
var blue: number = min + Math.round(Math.random() * (max - min));
return Phaser.ColorUtils.getColor32(alpha, red, green, blue);
}
/**
* Return the component parts of a color as an Object with the properties alpha, red, green, blue
*
* <p>Alpha will only be set if it exist in the given color (0xAARRGGBB)</p>
*
* @method getRGB
* @param {Number} color in RGB (0xRRGGBB) or ARGB format (0xAARRGGBB)
* @return {Object} An Object with properties: alpha, red, green, blue
*/
public static getRGB(color: number): any {
return { alpha: color >>> 24, red: color >> 16 & 0xFF, green: color >> 8 & 0xFF, blue: color & 0xFF };
}
/**
* Returns a CSS friendly string value from the given color.
* @method getWebRGB
* @param {Number} color
* @return {String} A string in the format: 'rgba(r,g,b,a)'
*/
public static getWebRGB(color: number): any {
var alpha: number = (color >>> 24) / 255;
var red: number = color >> 16 & 0xFF;
var green: number = color >> 8 & 0xFF;
var blue: number = color & 0xFF;
return 'rgba(' + red.toString() + ',' + green.toString() + ',' + blue.toString() + ',' + alpha.toString() + ')';
}
/**
* Given a native color value (in the format 0xAARRGGBB) this will return the Alpha component, as a value between 0 and 255
*
* @method getAlpha
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Alpha component of the color, will be between 0 and 1 (0 being no Alpha (opaque), 1 full Alpha (transparent))
*/
public static getAlpha(color: number): number {
return color >>> 24;
}
/**
* Given a native color value (in the format 0xAARRGGBB) this will return the Alpha component as a value between 0 and 1
*
* @method getAlphaFloat
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Alpha component of the color, will be between 0 and 1 (0 being no Alpha (opaque), 1 full Alpha (transparent))
*/
public static getAlphaFloat(color: number): number {
return (color >>> 24) / 255;
}
/**
* Given a native color value (in the format 0xAARRGGBB) this will return the Red component, as a value between 0 and 255
*
* @method getRed
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Red component of the color, will be between 0 and 255 (0 being no color, 255 full Red)
*/
public static getRed(color: number): number {
return color >> 16 & 0xFF;
}
/**
* Given a native color value (in the format 0xAARRGGBB) this will return the Green component, as a value between 0 and 255
*
* @method getGreen
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Green component of the color, will be between 0 and 255 (0 being no color, 255 full Green)
*/
public static getGreen(color: number): number {
return color >> 8 & 0xFF;
}
/**
* Given a native color value (in the format 0xAARRGGBB) this will return the Blue component, as a value between 0 and 255
*
* @method getBlue
* @param {Number} color In the format 0xAARRGGBB
* @return {Number} The Blue component of the color, will be between 0 and 255 (0 being no color, 255 full Blue)
*/
public static getBlue(color: number): number {
return color & 0xFF;
}
}
}
+323
View File
@@ -0,0 +1,323 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods for displaying debug information about game objects.
*
* @class DebugUtils
*/
var DebugUtils = (function () {
function DebugUtils() { }
DebugUtils.font = '14px Courier';
DebugUtils.lineHeight = 16;
DebugUtils.renderShadow = true;
DebugUtils.start = /**
* Internal method that resets the debug output values.
* @method start
* @param {Number} x The X value the debug info will start from.
* @param {Number} y The Y value the debug info will start from.
* @param {String} color The color the debug info will drawn in.
*/
function start(x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.currentX = x;
Phaser.DebugUtils.currentY = y;
Phaser.DebugUtils.currentColor = color;
Phaser.DebugUtils.context.fillStyle = color;
Phaser.DebugUtils.context.font = Phaser.DebugUtils.font;
};
DebugUtils.line = /**
* Internal method that outputs a single line of text.
* @method line
* @param {String} text The line of text to draw.
* @param {Number} x The X value the debug info will start from.
* @param {Number} y The Y value the debug info will start from.
*/
function line(text, x, y) {
if (typeof x === "undefined") { x = null; }
if (typeof y === "undefined") { y = null; }
if(x !== null) {
Phaser.DebugUtils.currentX = x;
}
if(y !== null) {
Phaser.DebugUtils.currentY = y;
}
if(Phaser.DebugUtils.renderShadow) {
Phaser.DebugUtils.context.fillStyle = 'rgb(0,0,0)';
Phaser.DebugUtils.context.fillText(text, Phaser.DebugUtils.currentX + 1, Phaser.DebugUtils.currentY + 1);
Phaser.DebugUtils.context.fillStyle = Phaser.DebugUtils.currentColor;
}
Phaser.DebugUtils.context.fillText(text, Phaser.DebugUtils.currentX, Phaser.DebugUtils.currentY);
Phaser.DebugUtils.currentY += Phaser.DebugUtils.lineHeight;
};
DebugUtils.renderSpriteCorners = function renderSpriteCorners(sprite, color) {
if (typeof color === "undefined") { color = 'rgb(255,0,255)'; }
Phaser.DebugUtils.start(0, 0, color);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.upperLeft.x) + ' y: ' + Math.floor(sprite.transform.upperLeft.y), sprite.transform.upperLeft.x, sprite.transform.upperLeft.y);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.upperRight.x) + ' y: ' + Math.floor(sprite.transform.upperRight.y), sprite.transform.upperRight.x, sprite.transform.upperRight.y);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.bottomLeft.x) + ' y: ' + Math.floor(sprite.transform.bottomLeft.y), sprite.transform.bottomLeft.x, sprite.transform.bottomLeft.y);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.bottomRight.x) + ' y: ' + Math.floor(sprite.transform.bottomRight.y), sprite.transform.bottomRight.x, sprite.transform.bottomRight.y);
};
DebugUtils.renderSoundInfo = /**
* Render debug infos. (including id, position, rotation, scrolling factor, worldBounds and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
function renderSoundInfo(sound, x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sound: ' + sound.key + ' Locked: ' + sound.game.sound.touchLocked + ' Pending Playback: ' + sound.pendingPlayback);
Phaser.DebugUtils.line('Decoded: ' + sound.isDecoded + ' Decoding: ' + sound.isDecoding);
Phaser.DebugUtils.line('Total Duration: ' + sound.totalDuration + ' Playing: ' + sound.isPlaying);
Phaser.DebugUtils.line('Time: ' + sound.currentTime);
Phaser.DebugUtils.line('Volume: ' + sound.volume + ' Muted: ' + sound.mute);
Phaser.DebugUtils.line('WebAudio: ' + sound.usingWebAudio + ' Audio: ' + sound.usingAudioTag);
if(sound.currentMarker !== '') {
Phaser.DebugUtils.line('Marker: ' + sound.currentMarker + ' Duration: ' + sound.duration);
Phaser.DebugUtils.line('Start: ' + sound.markers[sound.currentMarker].start + ' Stop: ' + sound.markers[sound.currentMarker].stop);
Phaser.DebugUtils.line('Position: ' + sound.position);
}
};
DebugUtils.renderCameraInfo = /**
* Render debug infos. (including id, position, rotation, scrolling factor, worldBounds and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
function renderCameraInfo(camera, x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Camera ID: ' + camera.ID + ' (' + camera.screenView.width + ' x ' + camera.screenView.height + ')');
Phaser.DebugUtils.line('X: ' + camera.x + ' Y: ' + camera.y + ' Rotation: ' + camera.transform.rotation);
Phaser.DebugUtils.line('WorldView X: ' + camera.worldView.x + ' Y: ' + camera.worldView.y + ' W: ' + camera.worldView.width + ' H: ' + camera.worldView.height);
Phaser.DebugUtils.line('ScreenView X: ' + camera.screenView.x + ' Y: ' + camera.screenView.y + ' W: ' + camera.screenView.width + ' H: ' + camera.screenView.height);
if(camera.worldBounds) {
Phaser.DebugUtils.line('Bounds: ' + camera.worldBounds.width + ' x ' + camera.worldBounds.height);
}
};
DebugUtils.renderPointer = /**
* Renders the Pointer.circle object onto the stage in green if down or red if up.
* @method renderDebug
*/
function renderPointer(pointer, hideIfUp, downColor, upColor, color) {
if (typeof hideIfUp === "undefined") { hideIfUp = false; }
if (typeof downColor === "undefined") { downColor = 'rgba(0,255,0,0.5)'; }
if (typeof upColor === "undefined") { upColor = 'rgba(255,0,0,0.5)'; }
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
if(hideIfUp == true && pointer.isUp == true) {
return;
}
Phaser.DebugUtils.context.beginPath();
Phaser.DebugUtils.context.arc(pointer.x, pointer.y, pointer.circle.radius, 0, Math.PI * 2);
if(pointer.active) {
Phaser.DebugUtils.context.fillStyle = downColor;
} else {
Phaser.DebugUtils.context.fillStyle = upColor;
}
Phaser.DebugUtils.context.fill();
Phaser.DebugUtils.context.closePath();
// Render the points
Phaser.DebugUtils.context.beginPath();
Phaser.DebugUtils.context.moveTo(pointer.positionDown.x, pointer.positionDown.y);
Phaser.DebugUtils.context.lineTo(pointer.position.x, pointer.position.y);
Phaser.DebugUtils.context.lineWidth = 2;
Phaser.DebugUtils.context.stroke();
Phaser.DebugUtils.context.closePath();
// Render the text
Phaser.DebugUtils.start(pointer.x, pointer.y - 100, color);
Phaser.DebugUtils.line('ID: ' + pointer.id + " Active: " + pointer.active);
Phaser.DebugUtils.line('World X: ' + pointer.worldX + " World Y: " + pointer.worldY);
Phaser.DebugUtils.line('Screen X: ' + pointer.x + " Screen Y: " + pointer.y);
Phaser.DebugUtils.line('Duration: ' + pointer.duration + " ms");
};
DebugUtils.renderSpriteInputInfo = /**
* Render Sprite Input Debug information
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
function renderSpriteInputInfo(sprite, x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sprite Input: (' + sprite.width + ' x ' + sprite.height + ')');
Phaser.DebugUtils.line('x: ' + sprite.input.pointerX().toFixed(1) + ' y: ' + sprite.input.pointerY().toFixed(1));
Phaser.DebugUtils.line('over: ' + sprite.input.pointerOver() + ' duration: ' + sprite.input.overDuration().toFixed(0));
Phaser.DebugUtils.line('down: ' + sprite.input.pointerDown() + ' duration: ' + sprite.input.downDuration().toFixed(0));
Phaser.DebugUtils.line('just over: ' + sprite.input.justOver() + ' just out: ' + sprite.input.justOut());
};
DebugUtils.renderInputInfo = /**
* Render debug information about the Input object.
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
function renderInputInfo(x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.start(x, y, color);
if(Phaser.DebugUtils.game.input.camera) {
Phaser.DebugUtils.line('Input - Camera: ' + Phaser.DebugUtils.game.input.camera.ID);
} else {
Phaser.DebugUtils.line('Input - Camera: null');
}
Phaser.DebugUtils.line('X: ' + Phaser.DebugUtils.game.input.x + ' Y: ' + Phaser.DebugUtils.game.input.y);
Phaser.DebugUtils.line('World X: ' + Phaser.DebugUtils.game.input.worldX + ' World Y: ' + Phaser.DebugUtils.game.input.worldY);
Phaser.DebugUtils.line('Scale X: ' + Phaser.DebugUtils.game.input.scale.x.toFixed(1) + ' Scale Y: ' + Phaser.DebugUtils.game.input.scale.x.toFixed(1));
Phaser.DebugUtils.line('Screen X: ' + Phaser.DebugUtils.game.input.activePointer.screenX + ' Screen Y: ' + Phaser.DebugUtils.game.input.activePointer.screenY);
};
DebugUtils.renderSpriteWorldView = function renderSpriteWorldView(sprite, x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sprite World Coords (' + sprite.width + ' x ' + sprite.height + ')');
Phaser.DebugUtils.line('x: ' + sprite.worldView.x + ' y: ' + sprite.worldView.y);
Phaser.DebugUtils.line('bottom: ' + sprite.worldView.bottom + ' right: ' + sprite.worldView.right.toFixed(1));
};
DebugUtils.renderSpriteWorldViewBounds = function renderSpriteWorldViewBounds(sprite, color) {
if (typeof color === "undefined") { color = 'rgba(0,255,0,0.3)'; }
Phaser.DebugUtils.renderRectangle(sprite.worldView, color);
};
DebugUtils.renderSpriteInfo = /**
* Render debug infos. (including name, bounds info, position and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
function renderSpriteInfo(sprite, x, y, color) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sprite: ' + ' (' + sprite.width + ' x ' + sprite.height + ') origin: ' + sprite.transform.origin.x + ' x ' + sprite.transform.origin.y);
Phaser.DebugUtils.line('x: ' + sprite.x.toFixed(1) + ' y: ' + sprite.y.toFixed(1) + ' rotation: ' + sprite.rotation.toFixed(1));
Phaser.DebugUtils.line('wx: ' + sprite.worldView.x + ' wy: ' + sprite.worldView.y + ' ww: ' + sprite.worldView.width.toFixed(1) + ' wh: ' + sprite.worldView.height.toFixed(1) + ' wb: ' + sprite.worldView.bottom + ' wr: ' + sprite.worldView.right);
Phaser.DebugUtils.line('sx: ' + sprite.transform.scale.x.toFixed(1) + ' sy: ' + sprite.transform.scale.y.toFixed(1));
Phaser.DebugUtils.line('tx: ' + sprite.texture.width.toFixed(1) + ' ty: ' + sprite.texture.height.toFixed(1));
Phaser.DebugUtils.line('center x: ' + sprite.transform.center.x + ' y: ' + sprite.transform.center.y);
Phaser.DebugUtils.line('cameraView x: ' + sprite.cameraView.x + ' y: ' + sprite.cameraView.y + ' width: ' + sprite.cameraView.width + ' height: ' + sprite.cameraView.height);
Phaser.DebugUtils.line('inCamera: ' + Phaser.DebugUtils.game.renderer.spriteRenderer.inCamera(Phaser.DebugUtils.game.camera, sprite));
};
DebugUtils.renderSpriteBounds = function renderSpriteBounds(sprite, camera, color) {
if (typeof camera === "undefined") { camera = null; }
if (typeof color === "undefined") { color = 'rgba(0,255,0,0.2)'; }
if(camera == null) {
camera = Phaser.DebugUtils.game.camera;
}
var dx = sprite.worldView.x;
var dy = sprite.worldView.y;
Phaser.DebugUtils.context.fillStyle = color;
Phaser.DebugUtils.context.fillRect(dx, dy, sprite.width, sprite.height);
};
DebugUtils.renderPixel = function renderPixel(x, y, fillStyle) {
if (typeof fillStyle === "undefined") { fillStyle = 'rgba(0,255,0,1)'; }
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.fillRect(x, y, 1, 1);
};
DebugUtils.renderPoint = function renderPoint(point, fillStyle) {
if (typeof fillStyle === "undefined") { fillStyle = 'rgba(0,255,0,1)'; }
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.fillRect(point.x, point.y, 1, 1);
};
DebugUtils.renderRectangle = function renderRectangle(rect, fillStyle) {
if (typeof fillStyle === "undefined") { fillStyle = 'rgba(0,255,0,0.3)'; }
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.fillRect(rect.x, rect.y, rect.width, rect.height);
};
DebugUtils.renderCircle = function renderCircle(circle, fillStyle) {
if (typeof fillStyle === "undefined") { fillStyle = 'rgba(0,255,0,0.3)'; }
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2, false);
Phaser.DebugUtils.context.fill();
};
DebugUtils.renderText = /**
* Render text
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
function renderText(text, x, y, color, font) {
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
if (typeof font === "undefined") { font = '16px Courier'; }
Phaser.DebugUtils.context.font = font;
Phaser.DebugUtils.context.fillStyle = color;
Phaser.DebugUtils.context.fillText(text, x, y);
};
return DebugUtils;
})();
Phaser.DebugUtils = DebugUtils;
/*
public render(context:CanvasRenderingContext2D) {
context.beginPath();
context.strokeStyle = 'rgb(0,255,0)';
context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
context.stroke();
context.closePath();
// center point
context.fillStyle = 'rgb(0,255,0)';
context.fillRect(this.position.x, this.position.y, 2, 2);
if (this.touching & Phaser.Types.LEFT)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.lineTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.stroke();
context.closePath();
}
if (this.touching & Phaser.Types.RIGHT)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.stroke();
context.closePath();
}
if (this.touching & Phaser.Types.UP)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.stroke();
context.closePath();
}
if (this.touching & Phaser.Types.DOWN)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.stroke();
context.closePath();
}
}
*/
/**
* Render debug infos. (including name, bounds info, position and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
/*
public renderDebugInfo(x: number, y: number, color: string = 'rgb(255,255,255)') {
this.sprite.texture.context.fillStyle = color;
this.sprite.texture.context.fillText('Sprite: (' + this.sprite.width + ' x ' + this.sprite.height + ')', x, y);
//this.sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
this.sprite.texture.context.fillText('x: ' + this.bounds.x.toFixed(1) + ' y: ' + this.bounds.y.toFixed(1) + ' rotation: ' + this.sprite.transform.rotation.toFixed(0), x, y + 14);
this.sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
this.sprite.texture.context.fillText('acx: ' + this.acceleration.x.toFixed(1) + ' acy: ' + this.acceleration.y.toFixed(1), x, y + 42);
this.sprite.texture.context.fillText('angVx: ' + this.angularVelocity.toFixed(1) + ' angAc: ' + this.angularAcceleration.toFixed(1), x, y + 56);
}
*/
})(Phaser || (Phaser = {}));
+441
View File
@@ -0,0 +1,441 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods for displaying debug information about game objects.
*
* @class DebugUtils
*/
export class DebugUtils {
/**
* A reference to the currently running Game.
* @property game
* @type {Phaser.Game}
*/
public static game: Phaser.Game;
/**
* The context to which the debug info will be drawn.
* Defaults to the Game.Stage.context, but can be redirected anywhere.
* @property context
* @type {CanvasRenderingContext2D}
*/
public static context: CanvasRenderingContext2D;
/**
* An internally used value that holds the X value of the debug text to be rendered.
* @property currentX
* @type {Number}
*/
public static currentX: number;
/**
* An internally used value that holds the Y value of the debug text to be rendered.
* @property currentY
* @type {Number}
*/
public static currentY: number;
/**
* The font of the debug text to be rendered.
* @property font
* @type {String}
*/
public static font: string = '14px Courier';
/**
* The height in pixels of a line of debug text. If you adjust the font size then adjust this accordingly.
* @property lineHeight
* @type {Number}
*/
public static lineHeight: number = 16;
/**
* The color of the debug text to be rendered in CSS string format (i.e. 'rgb(r,g,b)')
* @property font
* @type {String}
*/
public static currentColor: string;
/**
* If set to true this will render a shadow below any debug text, often making it easier to read.
* @property renderShadow
* @type {bool}
*/
public static renderShadow: bool = true;
/**
* Internal method that resets the debug output values.
* @method start
* @param {Number} x The X value the debug info will start from.
* @param {Number} y The Y value the debug info will start from.
* @param {String} color The color the debug info will drawn in.
*/
public static start(x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.currentX = x;
Phaser.DebugUtils.currentY = y;
Phaser.DebugUtils.currentColor = color;
Phaser.DebugUtils.context.fillStyle = color;
Phaser.DebugUtils.context.font = Phaser.DebugUtils.font;
}
/**
* Internal method that outputs a single line of text.
* @method line
* @param {String} text The line of text to draw.
* @param {Number} x The X value the debug info will start from.
* @param {Number} y The Y value the debug info will start from.
*/
public static line(text: string, x:number = null, y:number = null) {
if (x !== null)
{
Phaser.DebugUtils.currentX = x;
}
if (y !== null)
{
Phaser.DebugUtils.currentY = y;
}
if (Phaser.DebugUtils.renderShadow)
{
Phaser.DebugUtils.context.fillStyle = 'rgb(0,0,0)';
Phaser.DebugUtils.context.fillText(text, Phaser.DebugUtils.currentX + 1, Phaser.DebugUtils.currentY + 1);
Phaser.DebugUtils.context.fillStyle = Phaser.DebugUtils.currentColor;
}
Phaser.DebugUtils.context.fillText(text, Phaser.DebugUtils.currentX, Phaser.DebugUtils.currentY);
Phaser.DebugUtils.currentY += Phaser.DebugUtils.lineHeight;
}
public static renderSpriteCorners(sprite: Phaser.Sprite, color: string = 'rgb(255,0,255)') {
Phaser.DebugUtils.start(0, 0, color);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.upperLeft.x) + ' y: ' + Math.floor(sprite.transform.upperLeft.y), sprite.transform.upperLeft.x, sprite.transform.upperLeft.y);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.upperRight.x) + ' y: ' + Math.floor(sprite.transform.upperRight.y), sprite.transform.upperRight.x, sprite.transform.upperRight.y);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.bottomLeft.x) + ' y: ' + Math.floor(sprite.transform.bottomLeft.y), sprite.transform.bottomLeft.x, sprite.transform.bottomLeft.y);
Phaser.DebugUtils.line('x: ' + Math.floor(sprite.transform.bottomRight.x) + ' y: ' + Math.floor(sprite.transform.bottomRight.y), sprite.transform.bottomRight.x, sprite.transform.bottomRight.y);
}
/**
* Render debug infos. (including id, position, rotation, scrolling factor, worldBounds and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
public static renderSoundInfo(sound: Phaser.Sound, x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sound: ' + sound.key + ' Locked: ' + sound.game.sound.touchLocked + ' Pending Playback: ' + sound.pendingPlayback);
Phaser.DebugUtils.line('Decoded: ' + sound.isDecoded + ' Decoding: ' + sound.isDecoding);
Phaser.DebugUtils.line('Total Duration: ' + sound.totalDuration + ' Playing: ' + sound.isPlaying);
Phaser.DebugUtils.line('Time: ' + sound.currentTime);
Phaser.DebugUtils.line('Volume: ' + sound.volume + ' Muted: ' + sound.mute);
Phaser.DebugUtils.line('WebAudio: ' + sound.usingWebAudio + ' Audio: ' + sound.usingAudioTag);
if (sound.currentMarker !== '')
{
Phaser.DebugUtils.line('Marker: ' + sound.currentMarker + ' Duration: ' + sound.duration);
Phaser.DebugUtils.line('Start: ' + sound.markers[sound.currentMarker].start + ' Stop: ' + sound.markers[sound.currentMarker].stop);
Phaser.DebugUtils.line('Position: ' + sound.position);
}
}
/**
* Render debug infos. (including id, position, rotation, scrolling factor, worldBounds and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
static renderCameraInfo(camera: Phaser.Camera, x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Camera ID: ' + camera.ID + ' (' + camera.screenView.width + ' x ' + camera.screenView.height + ')');
Phaser.DebugUtils.line('X: ' + camera.x + ' Y: ' + camera.y + ' Rotation: ' + camera.transform.rotation);
Phaser.DebugUtils.line('WorldView X: ' + camera.worldView.x + ' Y: ' + camera.worldView.y + ' W: ' + camera.worldView.width + ' H: ' + camera.worldView.height);
Phaser.DebugUtils.line('ScreenView X: ' + camera.screenView.x + ' Y: ' + camera.screenView.y + ' W: ' + camera.screenView.width + ' H: ' + camera.screenView.height);
if (camera.worldBounds)
{
Phaser.DebugUtils.line('Bounds: ' + camera.worldBounds.width + ' x ' + camera.worldBounds.height);
}
}
/**
* Renders the Pointer.circle object onto the stage in green if down or red if up.
* @method renderDebug
*/
static renderPointer(pointer: Phaser.Pointer, hideIfUp: bool = false, downColor: string = 'rgba(0,255,0,0.5)', upColor: string = 'rgba(255,0,0,0.5)', color: string = 'rgb(255,255,255)') {
if (hideIfUp == true && pointer.isUp == true)
{
return;
}
Phaser.DebugUtils.context.beginPath();
Phaser.DebugUtils.context.arc(pointer.x, pointer.y, pointer.circle.radius, 0, Math.PI * 2);
if (pointer.active)
{
Phaser.DebugUtils.context.fillStyle = downColor;
}
else
{
Phaser.DebugUtils.context.fillStyle = upColor;
}
Phaser.DebugUtils.context.fill();
Phaser.DebugUtils.context.closePath();
// Render the points
Phaser.DebugUtils.context.beginPath();
Phaser.DebugUtils.context.moveTo(pointer.positionDown.x, pointer.positionDown.y);
Phaser.DebugUtils.context.lineTo(pointer.position.x, pointer.position.y);
Phaser.DebugUtils.context.lineWidth = 2;
Phaser.DebugUtils.context.stroke();
Phaser.DebugUtils.context.closePath();
// Render the text
Phaser.DebugUtils.start(pointer.x, pointer.y - 100, color);
Phaser.DebugUtils.line('ID: ' + pointer.id + " Active: " + pointer.active);
Phaser.DebugUtils.line('World X: ' + pointer.worldX + " World Y: " + pointer.worldY);
Phaser.DebugUtils.line('Screen X: ' + pointer.x + " Screen Y: " + pointer.y);
Phaser.DebugUtils.line('Duration: ' + pointer.duration + " ms");
}
/**
* Render Sprite Input Debug information
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
static renderSpriteInputInfo(sprite: Phaser.Sprite, x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sprite Input: (' + sprite.width + ' x ' + sprite.height + ')');
Phaser.DebugUtils.line('x: ' + sprite.input.pointerX().toFixed(1) + ' y: ' + sprite.input.pointerY().toFixed(1));
Phaser.DebugUtils.line('over: ' + sprite.input.pointerOver() + ' duration: ' + sprite.input.overDuration().toFixed(0));
Phaser.DebugUtils.line('down: ' + sprite.input.pointerDown() + ' duration: ' + sprite.input.downDuration().toFixed(0));
Phaser.DebugUtils.line('just over: ' + sprite.input.justOver() + ' just out: ' + sprite.input.justOut());
}
/**
* Render debug information about the Input object.
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
static renderInputInfo(x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.start(x, y, color);
if (Phaser.DebugUtils.game.input.camera)
{
Phaser.DebugUtils.line('Input - Camera: ' + Phaser.DebugUtils.game.input.camera.ID);
}
else
{
Phaser.DebugUtils.line('Input - Camera: null');
}
Phaser.DebugUtils.line('X: ' + Phaser.DebugUtils.game.input.x + ' Y: ' + Phaser.DebugUtils.game.input.y);
Phaser.DebugUtils.line('World X: ' + Phaser.DebugUtils.game.input.worldX + ' World Y: ' + Phaser.DebugUtils.game.input.worldY);
Phaser.DebugUtils.line('Scale X: ' + Phaser.DebugUtils.game.input.scale.x.toFixed(1) + ' Scale Y: ' + Phaser.DebugUtils.game.input.scale.x.toFixed(1));
Phaser.DebugUtils.line('Screen X: ' + Phaser.DebugUtils.game.input.activePointer.screenX + ' Screen Y: ' + Phaser.DebugUtils.game.input.activePointer.screenY);
}
static renderSpriteWorldView(sprite: Phaser.Sprite, x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sprite World Coords (' + sprite.width + ' x ' + sprite.height + ')');
Phaser.DebugUtils.line('x: ' + sprite.worldView.x + ' y: ' + sprite.worldView.y);
Phaser.DebugUtils.line('bottom: ' + sprite.worldView.bottom + ' right: ' + sprite.worldView.right.toFixed(1));
}
static renderSpriteWorldViewBounds(sprite: Phaser.Sprite, color: string = 'rgba(0,255,0,0.3)') {
Phaser.DebugUtils.renderRectangle(sprite.worldView, color);
}
/**
* Render debug infos. (including name, bounds info, position and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
static renderSpriteInfo(sprite: Phaser.Sprite, x: number, y: number, color: string = 'rgb(255,255,255)') {
Phaser.DebugUtils.start(x, y, color);
Phaser.DebugUtils.line('Sprite: ' + ' (' + sprite.width + ' x ' + sprite.height + ') origin: ' + sprite.transform.origin.x + ' x ' + sprite.transform.origin.y);
Phaser.DebugUtils.line('x: ' + sprite.x.toFixed(1) + ' y: ' + sprite.y.toFixed(1) + ' rotation: ' + sprite.rotation.toFixed(1));
Phaser.DebugUtils.line('wx: ' + sprite.worldView.x + ' wy: ' + sprite.worldView.y + ' ww: ' + sprite.worldView.width.toFixed(1) + ' wh: ' + sprite.worldView.height.toFixed(1) + ' wb: ' + sprite.worldView.bottom + ' wr: ' + sprite.worldView.right);
Phaser.DebugUtils.line('sx: ' + sprite.transform.scale.x.toFixed(1) + ' sy: ' + sprite.transform.scale.y.toFixed(1));
Phaser.DebugUtils.line('tx: ' + sprite.texture.width.toFixed(1) + ' ty: ' + sprite.texture.height.toFixed(1));
Phaser.DebugUtils.line('center x: ' + sprite.transform.center.x + ' y: ' + sprite.transform.center.y);
Phaser.DebugUtils.line('cameraView x: ' + sprite.cameraView.x + ' y: ' + sprite.cameraView.y + ' width: ' + sprite.cameraView.width + ' height: ' + sprite.cameraView.height);
Phaser.DebugUtils.line('inCamera: ' + Phaser.DebugUtils.game.renderer.spriteRenderer.inCamera(Phaser.DebugUtils.game.camera, sprite));
}
static renderSpriteBounds(sprite: Phaser.Sprite, camera: Phaser.Camera = null, color: string = 'rgba(0,255,0,0.2)') {
if (camera == null)
{
camera = Phaser.DebugUtils.game.camera;
}
var dx = sprite.worldView.x;
var dy = sprite.worldView.y;
Phaser.DebugUtils.context.fillStyle = color;
Phaser.DebugUtils.context.fillRect(dx, dy, sprite.width, sprite.height);
}
static renderPixel(x: number, y: number, fillStyle: string = 'rgba(0,255,0,1)') {
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.fillRect(x, y, 1, 1);
}
static renderPoint(point: Phaser.Point, fillStyle: string = 'rgba(0,255,0,1)') {
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.fillRect(point.x, point.y, 1, 1);
}
static renderRectangle(rect: Phaser.Rectangle, fillStyle: string = 'rgba(0,255,0,0.3)') {
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.fillRect(rect.x, rect.y, rect.width, rect.height);
}
static renderCircle(circle: Phaser.Circle, fillStyle: string = 'rgba(0,255,0,0.3)') {
Phaser.DebugUtils.context.fillStyle = fillStyle;
Phaser.DebugUtils.context.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2, false);
Phaser.DebugUtils.context.fill();
}
/**
* Render text
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
static renderText(text: string, x: number, y: number, color: string = 'rgb(255,255,255)', font: string = '16px Courier') {
Phaser.DebugUtils.context.font = font;
Phaser.DebugUtils.context.fillStyle = color;
Phaser.DebugUtils.context.fillText(text, x, y);
}
/*
public render(context:CanvasRenderingContext2D) {
context.beginPath();
context.strokeStyle = 'rgb(0,255,0)';
context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
context.stroke();
context.closePath();
// center point
context.fillStyle = 'rgb(0,255,0)';
context.fillRect(this.position.x, this.position.y, 2, 2);
if (this.touching & Phaser.Types.LEFT)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.lineTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.stroke();
context.closePath();
}
if (this.touching & Phaser.Types.RIGHT)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.stroke();
context.closePath();
}
if (this.touching & Phaser.Types.UP)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
context.stroke();
context.closePath();
}
if (this.touching & Phaser.Types.DOWN)
{
context.beginPath();
context.strokeStyle = 'rgb(255,0,0)';
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
context.stroke();
context.closePath();
}
}
*/
/**
* Render debug infos. (including name, bounds info, position and some other properties)
* @param x {number} X position of the debug info to be rendered.
* @param y {number} Y position of the debug info to be rendered.
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
*/
/*
public renderDebugInfo(x: number, y: number, color: string = 'rgb(255,255,255)') {
this.sprite.texture.context.fillStyle = color;
this.sprite.texture.context.fillText('Sprite: (' + this.sprite.width + ' x ' + this.sprite.height + ')', x, y);
//this.sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
this.sprite.texture.context.fillText('x: ' + this.bounds.x.toFixed(1) + ' y: ' + this.bounds.y.toFixed(1) + ' rotation: ' + this.sprite.transform.rotation.toFixed(0), x, y + 14);
this.sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
this.sprite.texture.context.fillText('acx: ' + this.acceleration.x.toFixed(1) + ' acy: ' + this.acceleration.y.toFixed(1), x, y + 42);
this.sprite.texture.context.fillText('angVx: ' + this.angularVelocity.toFixed(1) + ' angAc: ' + this.angularAcceleration.toFixed(1), x, y + 56);
}
*/
}
}
+203
View File
@@ -0,0 +1,203 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods useful for manipulating and comparing Point objects.
*
* @class PointUtils
*/
var PointUtils = (function () {
function PointUtils() { }
PointUtils.add = /**
* Adds the coordinates of two points together to create a new point.
* @method add
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
function add(a, b, out) {
if (typeof out === "undefined") { out = new Phaser.Point(); }
return out.setTo(a.x + b.x, a.y + b.y);
};
PointUtils.subtract = /**
* Subtracts the coordinates of two points to create a new point.
* @method subtract
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
function subtract(a, b, out) {
if (typeof out === "undefined") { out = new Phaser.Point(); }
return out.setTo(a.x - b.x, a.y - b.y);
};
PointUtils.multiply = /**
* Multiplies the coordinates of two points to create a new point.
* @method subtract
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
function multiply(a, b, out) {
if (typeof out === "undefined") { out = new Phaser.Point(); }
return out.setTo(a.x * b.x, a.y * b.y);
};
PointUtils.divide = /**
* Divides the coordinates of two points to create a new point.
* @method subtract
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
function divide(a, b, out) {
if (typeof out === "undefined") { out = new Phaser.Point(); }
return out.setTo(a.x / b.x, a.y / b.y);
};
PointUtils.clamp = /**
* Clamps the Point object values to be between the given min and max
* @method clamp
* @param {Phaser.Point} a The point.
* @param {Number} min The minimum value to clamp this Point to
* @param {Number} max The maximum value to clamp this Point to
* @return {Phaser.Point} This Point object.
*/
function clamp(a, min, max) {
Phaser.PointUtils.clampX(a, min, max);
Phaser.PointUtils.clampY(a, min, max);
return a;
};
PointUtils.clampX = /**
* Clamps the x value of the given Point object to be between the min and max values.
* @method clampX
* @param {Phaser.Point} a The point.
* @param {Number} min The minimum value to clamp this Point to
* @param {Number} max The maximum value to clamp this Point to
* @return {Phaser.Point} This Point object.
*/
function clampX(a, min, max) {
a.x = Math.max(Math.min(a.x, max), min);
return a;
};
PointUtils.clampY = /**
* Clamps the y value of the given Point object to be between the min and max values.
* @method clampY
* @param {Phaser.Point} a The point.
* @param {Number} min The minimum value to clamp this Point to
* @param {Number} max The maximum value to clamp this Point to
* @return {Phaser.Point} This Point object.
*/
function clampY(a, min, max) {
a.y = Math.max(Math.min(a.y, max), min);
return a;
};
PointUtils.clone = /**
* Creates a copy of the given Point.
* @method clone
* @param {Phaser.Point} output Optional Point object. If given the values will be set into this object, otherwise a brand new Point object will be created and returned.
* @return {Phaser.Point} The new Point object.
*/
function clone(a, output) {
if (typeof output === "undefined") { output = new Phaser.Point(); }
return output.setTo(a.x, a.y);
};
PointUtils.distanceBetween = /**
* Returns the distance between the two given Point objects.
* @method distanceBetween
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {bool} round Round the distance to the nearest integer (default false)
* @return {Number} The distance between the two Point objects.
*/
function distanceBetween(a, b, round) {
if (typeof round === "undefined") { round = false; }
var dx = a.x - b.x;
var dy = a.y - b.y;
if(round === true) {
return Math.round(Math.sqrt(dx * dx + dy * dy));
} else {
return Math.sqrt(dx * dx + dy * dy);
}
};
PointUtils.equals = /**
* Determines whether the two given Point objects are equal. They are considered equal if they have the same x and y values.
* @method equals
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @return {bool} A value of true if the Points are equal, otherwise false.
*/
function equals(a, b) {
return (a.x == b.x && a.y == b.y);
};
PointUtils.rotate = /**
* Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2.
* The closer the value of the parameter f is to 1.0, the closer the interpolated point is to the first point (parameter pt1). The closer the value of the parameter f is to 0, the closer the interpolated point is to the second point (parameter pt2).
* @method interpolate
* @param {Phaser.Point} pointA The first Point object.
* @param {Phaser.Point} pointB The second Point object.
* @param {Number} f The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned.
* @return {Phaser.Point} The new interpolated Point object.
*/
//public static interpolate(pointA, pointB, f) {
// TODO!
//}
/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
* @method polar
* @param {Number} length The length coordinate of the polar pair.
* @param {Number} angle The angle, in radians, of the polar pair.
* @return {Phaser.Point} The new Cartesian Point object.
*/
//public static polar(length, angle) {
// TODO!
//}
/**
* Rotates a Point around the x/y coordinates given to the desired angle.
* @method rotate
* @param {Phaser.Point} a The Point object to rotate.
* @param {Number} x The x coordinate of the anchor point
* @param {Number} y The y coordinate of the anchor point
* @param {Number} angle The angle in radians (unless asDegrees is true) to rotate the Point to.
* @param {bool} asDegrees Is the given rotation in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the Point and the anchor.
* @return {Phaser.Point} The modified point object
*/
function rotate(a, x, y, angle, asDegrees, distance) {
if (typeof asDegrees === "undefined") { asDegrees = false; }
if (typeof distance === "undefined") { distance = null; }
if(asDegrees) {
angle = angle * Phaser.GameMath.DEG_TO_RAD;
}
// Get distance from origin (cx/cy) to this point
if(distance === null) {
distance = Math.sqrt(((x - a.x) * (x - a.x)) + ((y - a.y) * (y - a.y)));
}
return a.setTo(x + distance * Math.cos(angle), y + distance * Math.sin(angle));
};
PointUtils.rotateAroundPoint = /**
* Rotates a Point around the given Point to the desired angle.
* @method rotateAroundPoint
* @param {Phaser.Point} a The Point object to rotate.
* @param {Phaser.Point} b The Point object to serve as point of rotation.
* @param {Number} angle The angle in radians (unless asDegrees is true) to rotate the Point to.
* @param {bool} asDegrees Is the given rotation in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the Point and the anchor.
* @return {Phaser.Point} The modified point object
*/
function rotateAroundPoint(a, b, angle, asDegrees, distance) {
if (typeof asDegrees === "undefined") { asDegrees = false; }
if (typeof distance === "undefined") { distance = null; }
return Phaser.PointUtils.rotate(a, b.x, b.y, angle, asDegrees, distance);
};
return PointUtils;
})();
Phaser.PointUtils = PointUtils;
})(Phaser || (Phaser = {}));
+225
View File
@@ -0,0 +1,225 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods useful for manipulating and comparing Point objects.
*
* @class PointUtils
*/
export class PointUtils {
/**
* Adds the coordinates of two points together to create a new point.
* @method add
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
public static add(a: Phaser.Point, b: Phaser.Point, out: Phaser.Point = new Phaser.Point): Phaser.Point {
return out.setTo(a.x + b.x, a.y + b.y);
}
/**
* Subtracts the coordinates of two points to create a new point.
* @method subtract
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
public static subtract(a: Phaser.Point, b: Phaser.Point, out: Phaser.Point = new Phaser.Point): Phaser.Point {
return out.setTo(a.x - b.x, a.y - b.y);
}
/**
* Multiplies the coordinates of two points to create a new point.
* @method subtract
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
public static multiply(a: Phaser.Point, b: Phaser.Point, out: Phaser.Point = new Phaser.Point): Phaser.Point {
return out.setTo(a.x * b.x, a.y * b.y);
}
/**
* Divides the coordinates of two points to create a new point.
* @method subtract
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
public static divide(a: Phaser.Point, b: Phaser.Point, out: Phaser.Point = new Phaser.Point): Phaser.Point {
return out.setTo(a.x / b.x, a.y / b.y);
}
/**
* Clamps the Point object values to be between the given min and max
* @method clamp
* @param {Phaser.Point} a The point.
* @param {Number} min The minimum value to clamp this Point to
* @param {Number} max The maximum value to clamp this Point to
* @return {Phaser.Point} This Point object.
*/
public static clamp(a: Phaser.Point, min: number, max: number): Phaser.Point {
Phaser.PointUtils.clampX(a, min, max);
Phaser.PointUtils.clampY(a, min, max);
return a;
}
/**
* Clamps the x value of the given Point object to be between the min and max values.
* @method clampX
* @param {Phaser.Point} a The point.
* @param {Number} min The minimum value to clamp this Point to
* @param {Number} max The maximum value to clamp this Point to
* @return {Phaser.Point} This Point object.
*/
public static clampX(a: Phaser.Point, min: number, max: number): Phaser.Point {
a.x = Math.max(Math.min(a.x, max), min);
return a;
}
/**
* Clamps the y value of the given Point object to be between the min and max values.
* @method clampY
* @param {Phaser.Point} a The point.
* @param {Number} min The minimum value to clamp this Point to
* @param {Number} max The maximum value to clamp this Point to
* @return {Phaser.Point} This Point object.
*/
public static clampY(a: Phaser.Point, min: number, max: number): Phaser.Point {
a.y = Math.max(Math.min(a.y, max), min);
return a;
}
/**
* Creates a copy of the given Point.
* @method clone
* @param {Phaser.Point} output Optional Point object. If given the values will be set into this object, otherwise a brand new Point object will be created and returned.
* @return {Phaser.Point} The new Point object.
*/
public static clone(a: Phaser.Point, output: Phaser.Point = new Phaser.Point): Phaser.Point {
return output.setTo(a.x, a.y);
}
/**
* Returns the distance between the two given Point objects.
* @method distanceBetween
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @param {bool} round Round the distance to the nearest integer (default false)
* @return {Number} The distance between the two Point objects.
*/
public static distanceBetween(a: Phaser.Point, b: Phaser.Point, round: bool = false): number {
var dx = a.x - b.x;
var dy = a.y - b.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Determines whether the two given Point objects are equal. They are considered equal if they have the same x and y values.
* @method equals
* @param {Phaser.Point} a The first Point object.
* @param {Phaser.Point} b The second Point object.
* @return {bool} A value of true if the Points are equal, otherwise false.
*/
public static equals(a: Phaser.Point, b: Phaser.Point): bool {
return (a.x == b.x && a.y == b.y);
}
/**
* Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2.
* The closer the value of the parameter f is to 1.0, the closer the interpolated point is to the first point (parameter pt1). The closer the value of the parameter f is to 0, the closer the interpolated point is to the second point (parameter pt2).
* @method interpolate
* @param {Phaser.Point} pointA The first Point object.
* @param {Phaser.Point} pointB The second Point object.
* @param {Number} f The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned.
* @return {Phaser.Point} The new interpolated Point object.
*/
//public static interpolate(pointA, pointB, f) {
// TODO!
//}
/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
* @method polar
* @param {Number} length The length coordinate of the polar pair.
* @param {Number} angle The angle, in radians, of the polar pair.
* @return {Phaser.Point} The new Cartesian Point object.
*/
//public static polar(length, angle) {
// TODO!
//}
/**
* Rotates a Point around the x/y coordinates given to the desired angle.
* @method rotate
* @param {Phaser.Point} a The Point object to rotate.
* @param {Number} x The x coordinate of the anchor point
* @param {Number} y The y coordinate of the anchor point
* @param {Number} angle The angle in radians (unless asDegrees is true) to rotate the Point to.
* @param {bool} asDegrees Is the given rotation in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the Point and the anchor.
* @return {Phaser.Point} The modified point object
*/
public static rotate(a: Phaser.Point, x: number, y: number, angle: number, asDegrees: bool = false, distance: number = null): Phaser.Point {
if (asDegrees)
{
angle = angle * Phaser.GameMath.DEG_TO_RAD;
}
// Get distance from origin (cx/cy) to this point
if (distance === null)
{
distance = Math.sqrt(((x - a.x) * (x - a.x)) + ((y - a.y) * (y - a.y)));
}
return a.setTo(x + distance * Math.cos(angle), y + distance * Math.sin(angle));
}
/**
* Rotates a Point around the given Point to the desired angle.
* @method rotateAroundPoint
* @param {Phaser.Point} a The Point object to rotate.
* @param {Phaser.Point} b The Point object to serve as point of rotation.
* @param {Number} angle The angle in radians (unless asDegrees is true) to rotate the Point to.
* @param {bool} asDegrees Is the given rotation in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the Point and the anchor.
* @return {Phaser.Point} The modified point object
*/
public static rotateAroundPoint(a: Phaser.Point, b: Phaser.Point, angle: number, asDegrees: bool = false, distance: number = null): Phaser.Point {
return Phaser.PointUtils.rotate(a, b.x, b.y, angle, asDegrees, distance);
}
}
}
+193
View File
@@ -0,0 +1,193 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods useful for manipulating and comparing Rectangle objects.
*
* @class RectangleUtils
*/
var RectangleUtils = (function () {
function RectangleUtils() { }
RectangleUtils.getTopLeftAsPoint = /**
* Get the location of the Rectangles top-left corner as a Point object.
* @method getTopLeftAsPoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
function getTopLeftAsPoint(a, out) {
if (typeof out === "undefined") { out = new Phaser.Point(); }
return out.setTo(a.x, a.y);
};
RectangleUtils.getBottomRightAsPoint = /**
* Get the location of the Rectangles bottom-right corner as a Point object.
* @method getTopLeftAsPoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
**/
function getBottomRightAsPoint(a, out) {
if (typeof out === "undefined") { out = new Phaser.Point(); }
return out.setTo(a.right, a.bottom);
};
RectangleUtils.inflate = /**
* Increases the size of the Rectangle object by the specified amounts. The center point of the Rectangle object stays the same, and its size increases to the left and right by the dx value, and to the top and the bottom by the dy value.
* @method inflate
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Number} dx The amount to be added to the left side of the Rectangle.
* @param {Number} dy The amount to be added to the bottom side of the Rectangle.
* @return {Phaser.Rectangle} This Rectangle object.
*/
function inflate(a, dx, dy) {
a.x -= dx;
a.width += 2 * dx;
a.y -= dy;
a.height += 2 * dy;
return a;
};
RectangleUtils.inflatePoint = /**
* Increases the size of the Rectangle object. This method is similar to the Rectangle.inflate() method except it takes a Point object as a parameter.
* @method inflatePoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} point The x property of this Point object is used to increase the horizontal dimension of the Rectangle object. The y property is used to increase the vertical dimension of the Rectangle object.
* @return {Phaser.Rectangle} The Rectangle object.
*/
function inflatePoint(a, point) {
return Phaser.RectangleUtils.inflate(a, point.x, point.y);
};
RectangleUtils.size = /**
* The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.
* @method size
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} output Optional Point object. If given the values will be set into the object, otherwise a brand new Point object will be created and returned.
* @return {Phaser.Point} The size of the Rectangle object
*/
function size(a, output) {
if (typeof output === "undefined") { output = new Phaser.Point(); }
return output.setTo(a.width, a.height);
};
RectangleUtils.clone = /**
* Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
* @method clone
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Rectangle} output Optional Rectangle object. If given the values will be set into the object, otherwise a brand new Rectangle object will be created and returned.
* @return {Phaser.Rectangle}
*/
function clone(a, output) {
if (typeof output === "undefined") { output = new Phaser.Rectangle(); }
return output.setTo(a.x, a.y, a.width, a.height);
};
RectangleUtils.contains = /**
* Determines whether the specified coordinates are contained within the region defined by this Rectangle object.
* @method contains
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Number} x The x coordinate of the point to test.
* @param {Number} y The y coordinate of the point to test.
* @return {bool} A value of true if the Rectangle object contains the specified point; otherwise false.
*/
function contains(a, x, y) {
return (x >= a.x && x <= a.right && y >= a.y && y <= a.bottom);
};
RectangleUtils.containsPoint = /**
* Determines whether the specified point is contained within the rectangular region defined by this Rectangle object. This method is similar to the Rectangle.contains() method, except that it takes a Point object as a parameter.
* @method containsPoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} point The point object being checked. Can be Point or any object with .x and .y values.
* @return {bool} A value of true if the Rectangle object contains the specified point; otherwise false.
*/
function containsPoint(a, point) {
return Phaser.RectangleUtils.contains(a, point.x, point.y);
};
RectangleUtils.containsRect = /**
* Determines whether the first Rectangle object is fully contained within the second Rectangle object.
* A Rectangle object is said to contain another if the second Rectangle object falls entirely within the boundaries of the first.
* @method containsRect
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @return {bool} A value of true if the Rectangle object contains the specified point; otherwise false.
*/
function containsRect(a, b) {
// If the given rect has a larger volume than this one then it can never contain it
if(a.volume > b.volume) {
return false;
}
return (a.x >= b.x && a.y >= b.y && a.right <= b.right && a.bottom <= b.bottom);
};
RectangleUtils.equals = /**
* Determines whether the two Rectangles are equal.
* This method compares the x, y, width and height properties of each Rectangle.
* @method equals
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @return {bool} A value of true if the two Rectangles have exactly the same values for the x, y, width and height properties; otherwise false.
*/
function equals(a, b) {
return (a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height);
};
RectangleUtils.intersection = /**
* If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object, returns the area of intersection as a Rectangle object. If the Rectangles do not intersect, this method returns an empty Rectangle object with its properties set to 0.
* @method intersection
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @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.
*/
function intersection(a, b, out) {
if (typeof out === "undefined") { out = new Phaser.Rectangle(); }
if(Phaser.RectangleUtils.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;
}
return out;
};
RectangleUtils.intersects = /**
* Determines whether the two Rectangles intersect with each other.
* This method checks the x, y, width, and height properties of the Rectangles.
* @method intersects
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {bool} A value of true if the specified object intersects with this Rectangle object; otherwise false.
*/
function intersects(a, b, tolerance) {
if (typeof tolerance === "undefined") { tolerance = 0; }
return !(a.left > b.right + tolerance || a.right < b.left - tolerance || a.top > b.bottom + tolerance || a.bottom < b.top - tolerance);
};
RectangleUtils.intersectsRaw = /**
* Determines whether the object specified intersects (overlaps) with the given values.
* @method intersectsRaw
* @param {Number} left
* @param {Number} right
* @param {Number} top
* @param {Number} bottomt
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {bool} A value of true if the specified object intersects with the Rectangle; otherwise false.
*/
function intersectsRaw(a, left, right, top, bottom, tolerance) {
if (typeof tolerance === "undefined") { tolerance = 0; }
return !(left > a.right + tolerance || right < a.left - tolerance || top > a.bottom + tolerance || bottom < a.top - tolerance);
};
RectangleUtils.union = /**
* Adds two Rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two Rectangles.
* @method union
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @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.
*/
function union(a, b, out) {
if (typeof out === "undefined") { out = 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.max(a.bottom, b.bottom));
};
return RectangleUtils;
})();
Phaser.RectangleUtils = RectangleUtils;
})(Phaser || (Phaser = {}));
+211
View File
@@ -0,0 +1,211 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods useful for manipulating and comparing Rectangle objects.
*
* @class RectangleUtils
*/
export class RectangleUtils {
/**
* Get the location of the Rectangles top-left corner as a Point object.
* @method getTopLeftAsPoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
public static getTopLeftAsPoint(a: Phaser.Rectangle, out: Phaser.Point = new Phaser.Point): Phaser.Point {
return out.setTo(a.x, a.y);
}
/**
* Get the location of the Rectangles bottom-right corner as a Point object.
* @method getTopLeftAsPoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
**/
public static getBottomRightAsPoint(a: Phaser.Rectangle, out: Phaser.Point = new Phaser.Point): Phaser.Point {
return out.setTo(a.right, a.bottom);
}
/**
* Increases the size of the Rectangle object by the specified amounts. The center point of the Rectangle object stays the same, and its size increases to the left and right by the dx value, and to the top and the bottom by the dy value.
* @method inflate
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Number} dx The amount to be added to the left side of the Rectangle.
* @param {Number} dy The amount to be added to the bottom side of the Rectangle.
* @return {Phaser.Rectangle} This Rectangle object.
*/
public static inflate(a: Phaser.Rectangle, dx: number, dy: number): Phaser.Rectangle {
a.x -= dx;
a.width += 2 * dx;
a.y -= dy;
a.height += 2 * dy;
return a;
}
/**
* Increases the size of the Rectangle object. This method is similar to the Rectangle.inflate() method except it takes a Point object as a parameter.
* @method inflatePoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} point The x property of this Point object is used to increase the horizontal dimension of the Rectangle object. The y property is used to increase the vertical dimension of the Rectangle object.
* @return {Phaser.Rectangle} The Rectangle object.
*/
public static inflatePoint(a: Phaser.Rectangle, point: Phaser.Point): Phaser.Rectangle {
return Phaser.RectangleUtils.inflate(a, point.x, point.y);
}
/**
* The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.
* @method size
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} output Optional Point object. If given the values will be set into the object, otherwise a brand new Point object will be created and returned.
* @return {Phaser.Point} The size of the Rectangle object
*/
public static size(a: Phaser.Rectangle, output: Phaser.Point = new Phaser.Point): Phaser.Point {
return output.setTo(a.width, a.height);
}
/**
* Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
* @method clone
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Rectangle} output Optional Rectangle object. If given the values will be set into the object, otherwise a brand new Rectangle object will be created and returned.
* @return {Phaser.Rectangle}
*/
public static clone(a: Phaser.Rectangle, output: Phaser.Rectangle = new Phaser.Rectangle): Phaser.Rectangle {
return output.setTo(a.x, a.y, a.width, a.height);
}
/**
* Determines whether the specified coordinates are contained within the region defined by this Rectangle object.
* @method contains
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Number} x The x coordinate of the point to test.
* @param {Number} y The y coordinate of the point to test.
* @return {bool} A value of true if the Rectangle object contains the specified point; otherwise false.
*/
public static contains(a: Phaser.Rectangle, x: number, y: number): bool {
return (x >= a.x && x <= a.right && y >= a.y && y <= a.bottom);
}
/**
* Determines whether the specified point is contained within the rectangular region defined by this Rectangle object. This method is similar to the Rectangle.contains() method, except that it takes a Point object as a parameter.
* @method containsPoint
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} point The point object being checked. Can be Point or any object with .x and .y values.
* @return {bool} A value of true if the Rectangle object contains the specified point; otherwise false.
*/
public static containsPoint(a: Phaser.Rectangle, point: Phaser.Point): bool {
return Phaser.RectangleUtils.contains(a, point.x, point.y);
}
/**
* Determines whether the first Rectangle object is fully contained within the second Rectangle object.
* A Rectangle object is said to contain another if the second Rectangle object falls entirely within the boundaries of the first.
* @method containsRect
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @return {bool} A value of true if the Rectangle object contains the specified point; otherwise false.
*/
public static containsRect(a: Phaser.Rectangle, b: Phaser.Rectangle): bool {
// If the given rect has a larger volume than this one then it can never contain it
if (a.volume > b.volume)
{
return false;
}
return (a.x >= b.x && a.y >= b.y && a.right <= b.right && a.bottom <= b.bottom);
}
/**
* Determines whether the two Rectangles are equal.
* This method compares the x, y, width and height properties of each Rectangle.
* @method equals
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @return {bool} A value of true if the two Rectangles have exactly the same values for the x, y, width and height properties; otherwise false.
*/
public static equals(a: Phaser.Rectangle, b: Phaser.Rectangle): bool {
return (a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height);
}
/**
* If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object, returns the area of intersection as a Rectangle object. If the Rectangles do not intersect, this method returns an empty Rectangle object with its properties set to 0.
* @method intersection
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @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.
*/
public static intersection(a: Phaser.Rectangle, b: Phaser.Rectangle, out: Phaser.Rectangle = new Phaser.Rectangle): Phaser.Rectangle {
if (Phaser.RectangleUtils.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;
}
return out;
}
/**
* Determines whether the two Rectangles intersect with each other.
* This method checks the x, y, width, and height properties of the Rectangles.
* @method intersects
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {bool} A value of true if the specified object intersects with this Rectangle object; otherwise false.
*/
public static intersects(a: Phaser.Rectangle, b: Phaser.Rectangle, tolerance: number = 0): bool {
return !(a.left > b.right + tolerance || a.right < b.left - tolerance || a.top > b.bottom + tolerance || a.bottom < b.top - tolerance);
}
/**
* Determines whether the object specified intersects (overlaps) with the given values.
* @method intersectsRaw
* @param {Number} left
* @param {Number} right
* @param {Number} top
* @param {Number} bottomt
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {bool} A value of true if the specified object intersects with the Rectangle; otherwise false.
*/
public static intersectsRaw(a: Phaser.Rectangle, left: number, right: number, top: number, bottom: number, tolerance: number = 0): bool {
return !(left > a.right + tolerance || right < a.left - tolerance || top > a.bottom + tolerance || bottom < a.top - tolerance);
}
/**
* Adds two Rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two Rectangles.
* @method union
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @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.
*/
public static union(a: Phaser.Rectangle, b: Phaser.Rectangle, out: Phaser.Rectangle = new Phaser.Rectangle): Phaser.Rectangle {
return out.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right), Math.max(a.bottom, b.bottom));
}
}
}
+270
View File
@@ -0,0 +1,270 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
var Phaser;
(function (Phaser) {
/**
* A collection of methods useful for manipulating and comparing Sprites.
*
* @class SpriteUtils
*/
var SpriteUtils = (function () {
function SpriteUtils() { }
SpriteUtils.updateCameraView = /**
* Updates a Sprites cameraView Rectangle based on the given camera, sprite world position and rotation.
* @method updateCameraView
* @param {Camera} camera The Camera to use in the view
* @param {Sprite} sprite The Sprite that will have its cameraView property modified
* @return {Rectangle} A reference to the Sprite.cameraView property
*/
function updateCameraView(camera, sprite) {
if(sprite.rotation == 0 || sprite.texture.renderRotation == false) {
// Easy out
sprite.cameraView.x = Math.floor(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.width * sprite.transform.origin.x));
sprite.cameraView.y = Math.floor(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.height * sprite.transform.origin.y));
sprite.cameraView.width = sprite.width;
sprite.cameraView.height = sprite.height;
} else {
// If the sprite is rotated around its center we can use this quicker method:
if(sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5) {
Phaser.SpriteUtils._sin = sprite.transform.sin;
Phaser.SpriteUtils._cos = sprite.transform.cos;
if(Phaser.SpriteUtils._sin < 0) {
Phaser.SpriteUtils._sin = -Phaser.SpriteUtils._sin;
}
if(Phaser.SpriteUtils._cos < 0) {
Phaser.SpriteUtils._cos = -Phaser.SpriteUtils._cos;
}
sprite.cameraView.width = Math.round(sprite.height * Phaser.SpriteUtils._sin + sprite.width * Phaser.SpriteUtils._cos);
sprite.cameraView.height = Math.round(sprite.height * Phaser.SpriteUtils._cos + sprite.width * Phaser.SpriteUtils._sin);
sprite.cameraView.x = Math.round(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.cameraView.width * sprite.transform.origin.x));
sprite.cameraView.y = Math.round(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.cameraView.height * sprite.transform.origin.y));
} else {
sprite.cameraView.x = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x);
sprite.cameraView.y = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y);
sprite.cameraView.width = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x) - sprite.cameraView.x;
sprite.cameraView.height = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y) - sprite.cameraView.y;
}
}
return sprite.cameraView;
};
SpriteUtils.getAsPoints = /**
* Returns an array containing 4 Point objects corresponding to the 4 corners of the sprite bounds.
* @method getAsPoints
* @param {Sprite} sprite The Sprite that will have its cameraView property modified
* @return {Array} An array of Point objects.
*/
function getAsPoints(sprite) {
var out = [];
// top left
out.push(new Phaser.Point(sprite.x, sprite.y));
// top right
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y));
// bottom right
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y + sprite.height));
// bottom left
out.push(new Phaser.Point(sprite.x, sprite.y + sprite.height));
return out;
};
SpriteUtils.overlapsPointer = /**
* Checks to see if some <code>GameObject</code> overlaps this <code>GameObject</code> or <code>Group</code>.
* If the group has a LOT of things in it, it might be faster to use <code>Collision.overlaps()</code>.
* WARNING: Currently tilemaps do NOT support screen space overlap checks!
*
* @param objectOrGroup {object} The object or group being tested.
* @param inScreenSpace {bool} Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
* @param camera {Camera} Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return {bool} Whether or not the objects overlap this.
*/
/*
static overlaps(objectOrGroup, inScreenSpace: bool = false, camera: Camera = null): bool {
if (objectOrGroup.isGroup)
{
var results: bool = false;
var i: number = 0;
var members = <Group> objectOrGroup.members;
while (i < length)
{
if (this.overlaps(members[i++], inScreenSpace, camera))
{
results = true;
}
}
return results;
}
if (!inScreenSpace)
{
return (objectOrGroup.x + objectOrGroup.width > this.x) && (objectOrGroup.x < this.x + this.width) &&
(objectOrGroup.y + objectOrGroup.height > this.y) && (objectOrGroup.y < this.y + this.height);
}
if (camera == null)
{
camera = this.game.camera;
}
var objectScreenPos: Point = objectOrGroup.getScreenXY(null, camera);
this.getScreenXY(this._point, camera);
return (objectScreenPos.x + objectOrGroup.width > this._point.x) && (objectScreenPos.x < this._point.x + this.width) &&
(objectScreenPos.y + objectOrGroup.height > this._point.y) && (objectScreenPos.y < this._point.y + this.height);
}
*/
function overlapsPointer(sprite, pointer) {
if(sprite.transform.scrollFactor.equals(1)) {
// We can do a world vs. world check
return Phaser.SpriteUtils.overlapsXY(sprite, pointer.worldX, pointer.worldY);
} else if(sprite.transform.scrollFactor.equals(0)) {
// scroll factor 0 means a screen view check, as the sprite will be absolutely positioned
return Phaser.SpriteUtils.overlapsXY(sprite, pointer.x, pointer.y);
} else {
// If the sprite has a scroll factor other than 0 or 1 then we need to work out
// what the pointers scroll factor values would be
var px = pointer.worldX * sprite.transform.scrollFactor.x;
var py = pointer.worldY * sprite.transform.scrollFactor.y;
return Phaser.SpriteUtils.overlapsXY(sprite, px, py);
}
};
SpriteUtils.overlapsXY = /**
* Checks to see if the given x and y coordinates overlaps this <code>Sprite</code>, taking scaling and rotation into account.
* The coordinates must be given in world space, not local or camera space.
*
* @method overlapsXY
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account, but NOT scroll factor.
* @param {Number} x The x coordinate in world space.
* @param {Number} y The y coordinate in world space.
* @return {bool} Whether or not the point overlaps this object.
*/
function overlapsXY(sprite, x, y) {
// if rotation == 0 then just do a rect check instead!
//if (sprite.transform.rotation == 0)
//{
// return Phaser.RectangleUtils.contains(sprite.worldView, x, y);
//}
if((x - sprite.transform.upperLeft.x) * (sprite.transform.upperRight.x - sprite.transform.upperLeft.x) + (y - sprite.transform.upperLeft.y) * (sprite.transform.upperRight.y - sprite.transform.upperLeft.y) < 0) {
return false;
}
if((x - sprite.transform.upperRight.x) * (sprite.transform.upperRight.x - sprite.transform.upperLeft.x) + (y - sprite.transform.upperRight.y) * (sprite.transform.upperRight.y - sprite.transform.upperLeft.y) > 0) {
return false;
}
if((x - sprite.transform.upperLeft.x) * (sprite.transform.bottomLeft.x - sprite.transform.upperLeft.x) + (y - sprite.transform.upperLeft.y) * (sprite.transform.bottomLeft.y - sprite.transform.upperLeft.y) < 0) {
return false;
}
if((x - sprite.transform.bottomLeft.x) * (sprite.transform.bottomLeft.x - sprite.transform.upperLeft.x) + (y - sprite.transform.bottomLeft.y) * (sprite.transform.bottomLeft.y - sprite.transform.upperLeft.y) > 0) {
return false;
}
return true;
};
SpriteUtils.overlapsPoint = /**
* Checks to see if the given point overlaps this <code>Sprite</code>, taking scaling and rotation into account.
* The point must be given in world space, not local or camera space.
*
* @method overlapsPoint
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account.
* @param {Point} point The point in world space you want to check.
* @return {bool} Whether or not the point overlaps this object.
*/
function overlapsPoint(sprite, point) {
return Phaser.SpriteUtils.overlapsXY(sprite, point.x, point.y);
};
SpriteUtils.onScreen = /**
* Check and see if this object is currently on screen.
*
* @method onScreen
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account.
* @param {Camera} camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* @return {bool} Whether the object is on screen or not.
*/
function onScreen(sprite, camera) {
if (typeof camera === "undefined") { camera = null; }
if(camera == null) {
camera = sprite.game.camera;
}
Phaser.SpriteUtils.getScreenXY(sprite, SpriteUtils._tempPoint, camera);
return (Phaser.SpriteUtils._tempPoint.x + sprite.width > 0) && (Phaser.SpriteUtils._tempPoint.x < camera.width) && (Phaser.SpriteUtils._tempPoint.y + sprite.height > 0) && (Phaser.SpriteUtils._tempPoint.y < camera.height);
};
SpriteUtils.getScreenXY = /**
* Call this to figure out the on-screen position of the object.
*
* @method getScreenXY
* @param {Sprite} sprite The Sprite to check.
* @param {Point} point Takes a <code>Point</code> object and assigns the post-scrolled X and Y values of this object to it.
* @param {Camera} camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* @return {Point} The <code>Point</code> you passed in, or a new <code>Point</code> if you didn't pass one, containing the screen X and Y position of this object.
*/
function getScreenXY(sprite, point, camera) {
if (typeof point === "undefined") { point = null; }
if (typeof camera === "undefined") { camera = null; }
if(point == null) {
point = new Phaser.Point();
}
if(camera == null) {
camera = sprite.game.camera;
}
point.x = sprite.x - camera.x * sprite.transform.scrollFactor.x;
point.y = sprite.y - camera.y * sprite.transform.scrollFactor.y;
point.x += (point.x > 0) ? 0.0000001 : -0.0000001;
point.y += (point.y > 0) ? 0.0000001 : -0.0000001;
return point;
};
SpriteUtils.reset = /**
* Set the world bounds that this GameObject can exist within based on the size of the current game world.
*
* @param action {number} The action to take if the object hits the world bounds, either OUT_OF_BOUNDS_KILL or OUT_OF_BOUNDS_STOP
*/
/*
static setBoundsFromWorld(action: number = GameObject.OUT_OF_BOUNDS_STOP) {
this.setBounds(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height);
this.outOfBoundsAction = action;
}
*/
/**
* Handy for reviving game objects. Resets their existence flags and position.
*
* @method reset
* @param {Sprite} sprite The Sprite to reset.
* @param {number} x The new X position of this object.
* @param {number} y The new Y position of this object.
* @return {Sprite} The reset Sprite object.
*/
function reset(sprite, x, y) {
sprite.revive();
sprite.x = x;
sprite.y = y;
//sprite.body.velocity.x = 0;
//sprite.body.velocity.y = 0;
//sprite.body.position.x = x;
//sprite.body.position.y = y;
return sprite;
};
SpriteUtils.setBounds = /**
* Set the world bounds that this GameObject can exist within. By default a GameObject can exist anywhere
* in the world. But by setting the bounds (which are given in world dimensions, not screen dimensions)
* it can be stopped from leaving the world, or a section of it.
*
* @method setBounds
* @param {number} x x position of the bound
* @param {number} y y position of the bound
* @param {number} width width of its bound
* @param {number} height height of its bound
*/
function setBounds(x, y, width, height) {
// Needed?
};
return SpriteUtils;
})();
Phaser.SpriteUtils = SpriteUtils;
})(Phaser || (Phaser = {}));
+355
View File
@@ -0,0 +1,355 @@
/// <reference path="../_definitions.ts" />
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
*/
module Phaser {
/**
* A collection of methods useful for manipulating and comparing Sprites.
*
* @class SpriteUtils
*/
export class SpriteUtils {
/**
* A temporary internal variable.
* @property _tempPoint
* @type {Phaser.Point}
*/
public static _tempPoint: Phaser.Point;
/**
* A temporary internal variable.
* @property _sin
* @type {Number}
*/
public static _sin: number;
/**
* A temporary internal variable.
* @property _cos
* @type {Number}
*/
public static _cos: number;
/**
* Updates a Sprites cameraView Rectangle based on the given camera, sprite world position and rotation.
* @method updateCameraView
* @param {Camera} camera The Camera to use in the view
* @param {Sprite} sprite The Sprite that will have its cameraView property modified
* @return {Rectangle} A reference to the Sprite.cameraView property
*/
public static updateCameraView(camera: Phaser.Camera, sprite: Phaser.Sprite): Phaser.Rectangle {
if (sprite.rotation == 0 || sprite.texture.renderRotation == false)
{
// Easy out
sprite.cameraView.x = Math.floor(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.width * sprite.transform.origin.x));
sprite.cameraView.y = Math.floor(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.height * sprite.transform.origin.y));
sprite.cameraView.width = sprite.width;
sprite.cameraView.height = sprite.height;
}
else
{
// If the sprite is rotated around its center we can use this quicker method:
if (sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5)
{
Phaser.SpriteUtils._sin = sprite.transform.sin;
Phaser.SpriteUtils._cos = sprite.transform.cos;
if (Phaser.SpriteUtils._sin < 0)
{
Phaser.SpriteUtils._sin = -Phaser.SpriteUtils._sin;
}
if (Phaser.SpriteUtils._cos < 0)
{
Phaser.SpriteUtils._cos = -Phaser.SpriteUtils._cos;
}
sprite.cameraView.width = Math.round(sprite.height * Phaser.SpriteUtils._sin + sprite.width * Phaser.SpriteUtils._cos);
sprite.cameraView.height = Math.round(sprite.height * Phaser.SpriteUtils._cos + sprite.width * Phaser.SpriteUtils._sin);
sprite.cameraView.x = Math.round(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.cameraView.width * sprite.transform.origin.x));
sprite.cameraView.y = Math.round(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.cameraView.height * sprite.transform.origin.y));
}
else
{
sprite.cameraView.x = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x);
sprite.cameraView.y = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y);
sprite.cameraView.width = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x) - sprite.cameraView.x;
sprite.cameraView.height = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y) - sprite.cameraView.y;
}
}
return sprite.cameraView;
}
/**
* Returns an array containing 4 Point objects corresponding to the 4 corners of the sprite bounds.
* @method getAsPoints
* @param {Sprite} sprite The Sprite that will have its cameraView property modified
* @return {Array} An array of Point objects.
*/
public static getAsPoints(sprite: Phaser.Sprite): Phaser.Point[] {
var out: Phaser.Point[] = [];
// top left
out.push(new Phaser.Point(sprite.x, sprite.y));
// top right
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y));
// bottom right
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y + sprite.height));
// bottom left
out.push(new Phaser.Point(sprite.x, sprite.y + sprite.height));
return out;
}
/**
* Checks to see if some <code>GameObject</code> overlaps this <code>GameObject</code> or <code>Group</code>.
* If the group has a LOT of things in it, it might be faster to use <code>Collision.overlaps()</code>.
* WARNING: Currently tilemaps do NOT support screen space overlap checks!
*
* @param objectOrGroup {object} The object or group being tested.
* @param inScreenSpace {bool} Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
* @param camera {Camera} Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return {bool} Whether or not the objects overlap this.
*/
/*
static overlaps(objectOrGroup, inScreenSpace: bool = false, camera: Camera = null): bool {
if (objectOrGroup.isGroup)
{
var results: bool = false;
var i: number = 0;
var members = <Group> objectOrGroup.members;
while (i < length)
{
if (this.overlaps(members[i++], inScreenSpace, camera))
{
results = true;
}
}
return results;
}
if (!inScreenSpace)
{
return (objectOrGroup.x + objectOrGroup.width > this.x) && (objectOrGroup.x < this.x + this.width) &&
(objectOrGroup.y + objectOrGroup.height > this.y) && (objectOrGroup.y < this.y + this.height);
}
if (camera == null)
{
camera = this.game.camera;
}
var objectScreenPos: Point = objectOrGroup.getScreenXY(null, camera);
this.getScreenXY(this._point, camera);
return (objectScreenPos.x + objectOrGroup.width > this._point.x) && (objectScreenPos.x < this._point.x + this.width) &&
(objectScreenPos.y + objectOrGroup.height > this._point.y) && (objectScreenPos.y < this._point.y + this.height);
}
*/
public static overlapsPointer(sprite:Phaser.Sprite, pointer: Phaser.Pointer): bool {
if (sprite.transform.scrollFactor.equals(1))
{
// We can do a world vs. world check
return Phaser.SpriteUtils.overlapsXY(sprite, pointer.worldX, pointer.worldY);
}
else if (sprite.transform.scrollFactor.equals(0))
{
// scroll factor 0 means a screen view check, as the sprite will be absolutely positioned
return Phaser.SpriteUtils.overlapsXY(sprite, pointer.x, pointer.y);
}
else
{
// If the sprite has a scroll factor other than 0 or 1 then we need to work out
// what the pointers scroll factor values would be
var px: number = pointer.worldX * sprite.transform.scrollFactor.x;
var py: number = pointer.worldY * sprite.transform.scrollFactor.y;
return Phaser.SpriteUtils.overlapsXY(sprite, px, py);
}
}
/**
* Checks to see if the given x and y coordinates overlaps this <code>Sprite</code>, taking scaling and rotation into account.
* The coordinates must be given in world space, not local or camera space.
*
* @method overlapsXY
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account, but NOT scroll factor.
* @param {Number} x The x coordinate in world space.
* @param {Number} y The y coordinate in world space.
* @return {bool} Whether or not the point overlaps this object.
*/
public static overlapsXY(sprite: Phaser.Sprite, x: number, y: number): bool {
// if rotation == 0 then just do a rect check instead!
//if (sprite.transform.rotation == 0)
//{
// return Phaser.RectangleUtils.contains(sprite.worldView, x, y);
//}
if ((x - sprite.transform.upperLeft.x) * (sprite.transform.upperRight.x - sprite.transform.upperLeft.x) + (y - sprite.transform.upperLeft.y) * (sprite.transform.upperRight.y - sprite.transform.upperLeft.y) < 0)
{
return false;
}
if ((x - sprite.transform.upperRight.x) * (sprite.transform.upperRight.x - sprite.transform.upperLeft.x) + (y - sprite.transform.upperRight.y) * (sprite.transform.upperRight.y - sprite.transform.upperLeft.y) > 0)
{
return false;
}
if ((x - sprite.transform.upperLeft.x) * (sprite.transform.bottomLeft.x - sprite.transform.upperLeft.x) + (y - sprite.transform.upperLeft.y) * (sprite.transform.bottomLeft.y - sprite.transform.upperLeft.y) < 0)
{
return false;
}
if ((x - sprite.transform.bottomLeft.x) * (sprite.transform.bottomLeft.x - sprite.transform.upperLeft.x) + (y - sprite.transform.bottomLeft.y) * (sprite.transform.bottomLeft.y - sprite.transform.upperLeft.y) > 0)
{
return false;
}
return true;
}
/**
* Checks to see if the given point overlaps this <code>Sprite</code>, taking scaling and rotation into account.
* The point must be given in world space, not local or camera space.
*
* @method overlapsPoint
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account.
* @param {Point} point The point in world space you want to check.
* @return {bool} Whether or not the point overlaps this object.
*/
public static overlapsPoint(sprite: Phaser.Sprite, point: Phaser.Point): bool {
return Phaser.SpriteUtils.overlapsXY(sprite, point.x, point.y);
}
/**
* Check and see if this object is currently on screen.
*
* @method onScreen
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account.
* @param {Camera} camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* @return {bool} Whether the object is on screen or not.
*/
public static onScreen(sprite: Phaser.Sprite, camera: Phaser.Camera = null): bool {
if (camera == null)
{
camera = sprite.game.camera;
}
Phaser.SpriteUtils.getScreenXY(sprite, SpriteUtils._tempPoint, camera);
return (Phaser.SpriteUtils._tempPoint.x + sprite.width > 0) && (Phaser.SpriteUtils._tempPoint.x < camera.width) && (Phaser.SpriteUtils._tempPoint.y + sprite.height > 0) && (Phaser.SpriteUtils._tempPoint.y < camera.height);
}
/**
* Call this to figure out the on-screen position of the object.
*
* @method getScreenXY
* @param {Sprite} sprite The Sprite to check.
* @param {Point} point Takes a <code>Point</code> object and assigns the post-scrolled X and Y values of this object to it.
* @param {Camera} camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* @return {Point} The <code>Point</code> you passed in, or a new <code>Point</code> if you didn't pass one, containing the screen X and Y position of this object.
*/
public static getScreenXY(sprite: Phaser.Sprite, point: Phaser.Point = null, camera: Phaser.Camera = null): Phaser.Point {
if (point == null)
{
point = new Point();
}
if (camera == null)
{
camera = sprite.game.camera;
}
point.x = sprite.x - camera.x * sprite.transform.scrollFactor.x;
point.y = sprite.y - camera.y * sprite.transform.scrollFactor.y;
point.x += (point.x > 0) ? 0.0000001 : -0.0000001;
point.y += (point.y > 0) ? 0.0000001 : -0.0000001;
return point;
}
/**
* Set the world bounds that this GameObject can exist within based on the size of the current game world.
*
* @param action {number} The action to take if the object hits the world bounds, either OUT_OF_BOUNDS_KILL or OUT_OF_BOUNDS_STOP
*/
/*
static setBoundsFromWorld(action: number = GameObject.OUT_OF_BOUNDS_STOP) {
this.setBounds(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height);
this.outOfBoundsAction = action;
}
*/
/**
* Handy for reviving game objects. Resets their existence flags and position.
*
* @method reset
* @param {Sprite} sprite The Sprite to reset.
* @param {number} x The new X position of this object.
* @param {number} y The new Y position of this object.
* @return {Sprite} The reset Sprite object.
*/
public static reset(sprite: Phaser.Sprite, x: number, y: number):Phaser.Sprite {
sprite.revive();
sprite.x = x;
sprite.y = y;
//sprite.body.velocity.x = 0;
//sprite.body.velocity.y = 0;
//sprite.body.position.x = x;
//sprite.body.position.y = y;
return sprite;
}
/**
* Set the world bounds that this GameObject can exist within. By default a GameObject can exist anywhere
* in the world. But by setting the bounds (which are given in world dimensions, not screen dimensions)
* it can be stopped from leaving the world, or a section of it.
*
* @method setBounds
* @param {number} x x position of the bound
* @param {number} y y position of the bound
* @param {number} width width of its bound
* @param {number} height height of its bound
*/
public static setBounds(x: number, y: number, width: number, height: number) {
// Needed?
}
}
}