2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
2013-08-11 23:52:35 +00:00
|
|
|
* @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
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
var Phaser;
|
|
|
|
(function (Phaser) {
|
2013-08-11 23:52:35 +00:00
|
|
|
/**
|
|
|
|
* A collection of methods useful for manipulating and comparing Sprites.
|
|
|
|
*
|
|
|
|
* @class SpriteUtils
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
var SpriteUtils = (function () {
|
|
|
|
function SpriteUtils() { }
|
|
|
|
SpriteUtils.updateCameraView = /**
|
2013-08-11 23:52:35 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function updateCameraView(camera, sprite) {
|
|
|
|
if(sprite.rotation == 0 || sprite.texture.renderRotation == false) {
|
2013-06-07 18:18:39 +00:00
|
|
|
// Easy out
|
2013-08-08 00:07:22 +00:00
|
|
|
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));
|
2013-06-07 18:18:39 +00:00
|
|
|
sprite.cameraView.width = sprite.width;
|
|
|
|
sprite.cameraView.height = sprite.height;
|
2013-08-28 06:02:55 +00:00
|
|
|
} else {
|
2013-06-07 18:18:39 +00:00
|
|
|
// If the sprite is rotated around its center we can use this quicker method:
|
2013-08-28 06:02:55 +00:00
|
|
|
if(sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5) {
|
2013-08-08 18:16:47 +00:00
|
|
|
Phaser.SpriteUtils._sin = sprite.transform.sin;
|
|
|
|
Phaser.SpriteUtils._cos = sprite.transform.cos;
|
2013-08-28 06:02:55 +00:00
|
|
|
if(Phaser.SpriteUtils._sin < 0) {
|
2013-08-08 18:16:47 +00:00
|
|
|
Phaser.SpriteUtils._sin = -Phaser.SpriteUtils._sin;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
if(Phaser.SpriteUtils._cos < 0) {
|
2013-08-08 18:16:47 +00:00
|
|
|
Phaser.SpriteUtils._cos = -Phaser.SpriteUtils._cos;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
2013-08-08 18:16:47 +00:00
|
|
|
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);
|
2013-06-07 18:18:39 +00:00
|
|
|
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));
|
2013-08-28 06:02:55 +00:00
|
|
|
} else {
|
2013-06-12 18:53:48 +00:00
|
|
|
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;
|
2013-06-07 18:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sprite.cameraView;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.getAsPoints = /**
|
2013-08-11 23:52:35 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function getAsPoints(sprite) {
|
|
|
|
var out = [];
|
2013-05-31 03:20:49 +00:00
|
|
|
// top left
|
2013-08-08 18:16:47 +00:00
|
|
|
out.push(new Phaser.Point(sprite.x, sprite.y));
|
2013-05-31 03:20:49 +00:00
|
|
|
// top right
|
2013-08-08 18:16:47 +00:00
|
|
|
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y));
|
2013-05-31 03:20:49 +00:00
|
|
|
// bottom right
|
2013-08-08 18:16:47 +00:00
|
|
|
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y + sprite.height));
|
2013-05-31 03:20:49 +00:00
|
|
|
// bottom left
|
2013-08-08 18:16:47 +00:00
|
|
|
out.push(new Phaser.Point(sprite.x, sprite.y + sprite.height));
|
2013-05-31 03:20:49 +00:00
|
|
|
return out;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.overlapsPointer = /**
|
2013-05-28 20:38:37 +00:00
|
|
|
* 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.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @param inScreenSpace {bool} Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
|
2013-05-28 20:38:37 +00:00
|
|
|
* @param camera {Camera} Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
|
|
|
|
*
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool} Whether or not the objects overlap this.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
|
|
|
/*
|
2013-08-13 03:22:24 +00:00
|
|
|
static overlaps(objectOrGroup, inScreenSpace: bool = false, camera: Camera = null): bool {
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
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);
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function overlapsPointer(sprite, pointer) {
|
|
|
|
if(sprite.transform.scrollFactor.equals(1)) {
|
2013-08-15 23:14:57 +00:00
|
|
|
// We can do a world vs. world check
|
|
|
|
return Phaser.SpriteUtils.overlapsXY(sprite, pointer.worldX, pointer.worldY);
|
2013-08-28 06:02:55 +00:00
|
|
|
} else if(sprite.transform.scrollFactor.equals(0)) {
|
2013-08-15 23:14:57 +00:00
|
|
|
// scroll factor 0 means a screen view check, as the sprite will be absolutely positioned
|
|
|
|
return Phaser.SpriteUtils.overlapsXY(sprite, pointer.x, pointer.y);
|
2013-08-28 06:02:55 +00:00
|
|
|
} else {
|
2013-08-15 23:14:57 +00:00
|
|
|
// 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
|
2013-08-28 06:02:55 +00:00
|
|
|
var px = pointer.worldX * sprite.transform.scrollFactor.x;
|
|
|
|
var py = pointer.worldY * sprite.transform.scrollFactor.y;
|
2013-08-15 23:14:57 +00:00
|
|
|
return Phaser.SpriteUtils.overlapsXY(sprite, px, py);
|
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.overlapsXY = /**
|
2013-06-12 18:53:48 +00:00
|
|
|
* 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.
|
2013-05-28 20:38:37 +00:00
|
|
|
*
|
2013-08-11 23:52:35 +00:00
|
|
|
* @method overlapsXY
|
2013-08-15 23:14:57 +00:00
|
|
|
* @param {Sprite} sprite The Sprite to check. It will take scaling and rotation into account, but NOT scroll factor.
|
2013-08-11 23:52:35 +00:00
|
|
|
* @param {Number} x The x coordinate in world space.
|
|
|
|
* @param {Number} y The y coordinate in world space.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool} Whether or not the point overlaps this object.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function overlapsXY(sprite, x, y) {
|
2013-06-12 18:53:48 +00:00
|
|
|
// if rotation == 0 then just do a rect check instead!
|
2013-08-15 23:14:57 +00:00
|
|
|
//if (sprite.transform.rotation == 0)
|
|
|
|
//{
|
|
|
|
// return Phaser.RectangleUtils.contains(sprite.worldView, x, y);
|
|
|
|
//}
|
2013-08-28 06:02:55 +00:00
|
|
|
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) {
|
2013-06-12 18:53:48 +00:00
|
|
|
return false;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
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) {
|
2013-06-12 18:53:48 +00:00
|
|
|
return false;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
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) {
|
2013-06-12 18:53:48 +00:00
|
|
|
return false;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
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) {
|
2013-06-12 18:53:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.overlapsPoint = /**
|
2013-06-12 18:53:48 +00:00
|
|
|
* 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.
|
2013-05-28 20:38:37 +00:00
|
|
|
*
|
2013-08-11 23:52:35 +00:00
|
|
|
* @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.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool} Whether or not the point overlaps this object.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function overlapsPoint(sprite, point) {
|
2013-08-08 18:16:47 +00:00
|
|
|
return Phaser.SpriteUtils.overlapsXY(sprite, point.x, point.y);
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.onScreen = /**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Check and see if this object is currently on screen.
|
|
|
|
*
|
2013-08-11 23:52:35 +00:00
|
|
|
* @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.
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool} Whether the object is on screen or not.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function onScreen(sprite, camera) {
|
|
|
|
if (typeof camera === "undefined") { camera = null; }
|
|
|
|
if(camera == null) {
|
2013-05-31 17:21:32 +00:00
|
|
|
camera = sprite.game.camera;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-08-08 18:16:47 +00:00
|
|
|
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);
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.getScreenXY = /**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Call this to figure out the on-screen position of the object.
|
|
|
|
*
|
2013-08-11 23:52:35 +00:00
|
|
|
* @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.
|
2013-05-31 17:21:32 +00:00
|
|
|
* @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.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function getScreenXY(sprite, point, camera) {
|
|
|
|
if (typeof point === "undefined") { point = null; }
|
|
|
|
if (typeof camera === "undefined") { camera = null; }
|
|
|
|
if(point == null) {
|
|
|
|
point = new Phaser.Point();
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
if(camera == null) {
|
2013-08-08 18:16:47 +00:00
|
|
|
camera = sprite.game.camera;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-06-06 01:47:08 +00:00
|
|
|
point.x = sprite.x - camera.x * sprite.transform.scrollFactor.x;
|
|
|
|
point.y = sprite.y - camera.y * sprite.transform.scrollFactor.y;
|
2013-05-28 20:38:37 +00:00
|
|
|
point.x += (point.x > 0) ? 0.0000001 : -0.0000001;
|
|
|
|
point.y += (point.y > 0) ? 0.0000001 : -0.0000001;
|
|
|
|
return point;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.reset = /**
|
2013-05-28 20:38:37 +00:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
/*
|
2013-08-08 10:34:33 +00:00
|
|
|
static setBoundsFromWorld(action: number = GameObject.OUT_OF_BOUNDS_STOP) {
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
/**
|
2013-08-11 23:52:35 +00:00
|
|
|
* Handy for reviving game objects. Resets their existence flags and position.
|
2013-05-28 20:38:37 +00:00
|
|
|
*
|
2013-08-11 23:52:35 +00:00
|
|
|
* @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.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function reset(sprite, x, y) {
|
2013-05-31 17:21:32 +00:00
|
|
|
sprite.revive();
|
|
|
|
sprite.x = x;
|
|
|
|
sprite.y = y;
|
2013-08-12 03:08:15 +00:00
|
|
|
//sprite.body.velocity.x = 0;
|
|
|
|
//sprite.body.velocity.y = 0;
|
|
|
|
//sprite.body.position.x = x;
|
|
|
|
//sprite.body.position.y = y;
|
2013-08-11 23:52:35 +00:00
|
|
|
return sprite;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
SpriteUtils.setBounds = /**
|
2013-05-28 20:38:37 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2013-08-11 23:52:35 +00:00
|
|
|
* @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
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function setBounds(x, y, width, height) {
|
2013-08-11 23:52:35 +00:00
|
|
|
// Needed?
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
return SpriteUtils;
|
|
|
|
})();
|
|
|
|
Phaser.SpriteUtils = SpriteUtils;
|
|
|
|
})(Phaser || (Phaser = {}));
|