2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
2013-06-07 15:27:33 +00:00
|
|
|
/// <reference path="../geom/Point.ts" />
|
|
|
|
/// <reference path="../geom/Rectangle.ts" />
|
|
|
|
/// <reference path="../geom/Circle.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="../gameobjects/Sprite.ts" />
|
2013-06-01 00:30:36 +00:00
|
|
|
/// <reference path="RectangleUtils.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Phaser - SpriteUtils
|
|
|
|
*
|
|
|
|
* A collection of methods useful for manipulating and checking Sprites.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
|
|
|
export class SpriteUtils {
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
static _tempPoint: Point;
|
2013-06-07 18:18:39 +00:00
|
|
|
static _sin: number;
|
|
|
|
static _cos: number;
|
2013-05-31 03:20:49 +00:00
|
|
|
|
2013-06-03 02:08:24 +00:00
|
|
|
/**
|
2013-06-07 18:18:39 +00:00
|
|
|
* Updates a Sprites cameraView Rectangle based on the given camera, sprite world position and rotation
|
|
|
|
* @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-06-03 02:08:24 +00:00
|
|
|
*/
|
2013-06-07 18:18:39 +00:00
|
|
|
static updateCameraView(camera: Camera, sprite: Sprite): Rectangle {
|
2013-06-03 02:08:24 +00:00
|
|
|
|
2013-06-07 18:18:39 +00:00
|
|
|
if (sprite.rotation == 0 || sprite.texture.renderRotation == false)
|
2013-06-03 02:08:24 +00:00
|
|
|
{
|
2013-06-07 18:18:39 +00:00
|
|
|
// Easy out
|
|
|
|
sprite.cameraView.x = Math.round(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.width * sprite.transform.origin.x));
|
|
|
|
sprite.cameraView.y = Math.round(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:
|
2013-06-11 20:30:15 +00:00
|
|
|
if (sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5)
|
2013-06-07 18:18:39 +00:00
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
SpriteUtils._sin = sprite.transform.sin;
|
|
|
|
SpriteUtils._cos = sprite.transform.cos;
|
2013-06-07 18:18:39 +00:00
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
if (SpriteUtils._sin < 0)
|
|
|
|
{
|
|
|
|
SpriteUtils._sin = -SpriteUtils._sin;
|
|
|
|
}
|
2013-06-07 18:18:39 +00:00
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
if (SpriteUtils._cos < 0)
|
|
|
|
{
|
|
|
|
SpriteUtils._cos = -SpriteUtils._cos;
|
|
|
|
}
|
2013-06-07 18:18:39 +00:00
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
sprite.cameraView.width = Math.round(sprite.height * SpriteUtils._sin + sprite.width * SpriteUtils._cos);
|
|
|
|
sprite.cameraView.height = Math.round(sprite.height * SpriteUtils._cos + sprite.width * 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));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2013-06-03 02:08:24 +00:00
|
|
|
|
2013-06-07 18:18:39 +00:00
|
|
|
return sprite.cameraView;
|
2013-06-03 02:08:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-31 03:20:49 +00:00
|
|
|
static getAsPoints(sprite: Sprite): Phaser.Point[] {
|
|
|
|
|
|
|
|
var out: Phaser.Point[] = [];
|
|
|
|
|
|
|
|
// top left
|
|
|
|
out.push(new Point(sprite.x, sprite.y));
|
|
|
|
|
|
|
|
// top right
|
|
|
|
out.push(new Point(sprite.x + sprite.width, sprite.y));
|
|
|
|
|
|
|
|
// bottom right
|
|
|
|
out.push(new Point(sprite.x + sprite.width, sprite.y + sprite.height));
|
|
|
|
|
|
|
|
// bottom left
|
|
|
|
out.push(new Point(sprite.x, sprite.y + sprite.height));
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @param inScreenSpace {boolean} 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 {boolean} 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);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2013-06-12 18:53:48 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
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-06-12 18:53:48 +00:00
|
|
|
* @param sprite {Sprite} The Sprite to check. It will take scaling and rotation into account.
|
|
|
|
* @param x {Number} The x coordinate in world space.
|
|
|
|
* @param y {Number} The y coordinate in world space.
|
2013-05-28 20:38:37 +00:00
|
|
|
*
|
2013-06-12 18:53:48 +00:00
|
|
|
* @return Whether or not the point overlaps this object.
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-06-12 18:53:48 +00:00
|
|
|
static overlapsXY(sprite: Phaser.Sprite, x: number, y: number): bool {
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-06-12 18:53:48 +00:00
|
|
|
// if rotation == 0 then just do a rect check instead!
|
|
|
|
if (sprite.transform.rotation == 0)
|
2013-05-28 20:38:37 +00:00
|
|
|
{
|
2013-06-12 18:53:48 +00:00
|
|
|
return Phaser.RectangleUtils.contains(sprite.cameraView, x, y);
|
|
|
|
}
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-06-12 18:53:48 +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)
|
|
|
|
{
|
|
|
|
return false;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:53:48 +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-05-28 20:38:37 +00:00
|
|
|
{
|
2013-06-12 18:53:48 +00:00
|
|
|
return false;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:53:48 +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-05-28 20:38:37 +00:00
|
|
|
{
|
2013-06-12 18:53:48 +00:00
|
|
|
return false;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:53:48 +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)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-06-12 18:53:48 +00:00
|
|
|
return true;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-06-12 18:53:48 +00:00
|
|
|
* @param sprite {Sprite} The Sprite to check. It will take scaling and rotation into account.
|
2013-05-28 20:38:37 +00:00
|
|
|
* @param point {Point} The point in world space you want to check.
|
|
|
|
*
|
|
|
|
* @return Whether or not the point overlaps this object.
|
|
|
|
*/
|
2013-06-12 18:53:48 +00:00
|
|
|
static overlapsPoint(sprite: Sprite, point: Point): bool {
|
|
|
|
return overlapsXY(sprite, point.x, point.y);
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check and see if this object is currently on screen.
|
|
|
|
*
|
|
|
|
* @param camera {Camera} Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether the object is on screen or not.
|
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
static onScreen(sprite: Sprite, camera: Camera = null): bool {
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
if (camera == null)
|
|
|
|
{
|
2013-05-31 17:21:32 +00:00
|
|
|
camera = sprite.game.camera;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
SpriteUtils.getScreenXY(sprite, SpriteUtils._tempPoint, camera);
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
return (SpriteUtils._tempPoint.x + sprite.width > 0) && (SpriteUtils._tempPoint.x < camera.width) && (SpriteUtils._tempPoint.y + sprite.height > 0) && (SpriteUtils._tempPoint.y < camera.height);
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this to figure out the on-screen position of the object.
|
|
|
|
*
|
2013-06-03 02:08:24 +00:00
|
|
|
* @param point {Point} Takes a <code>Point</code> object and assigns the post-scrolled X and Y values of this object to it.
|
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-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-05-31 17:21:32 +00:00
|
|
|
static getScreenXY(sprite: Sprite, point: Point = null, camera: Camera = null): Point {
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
if (point == null)
|
|
|
|
{
|
2013-05-31 17:21:32 +00:00
|
|
|
point = new Point();
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (camera == null)
|
|
|
|
{
|
|
|
|
camera = this._game.camera;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param x {number} The new X position of this object.
|
|
|
|
* @param y {number} The new Y position of this object.
|
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
static reset(sprite: Sprite, x: number, y: number) {
|
|
|
|
|
|
|
|
sprite.revive();
|
|
|
|
sprite.body.touching = Types.NONE;
|
|
|
|
sprite.body.wasTouching = Types.NONE;
|
|
|
|
sprite.x = x;
|
|
|
|
sprite.y = y;
|
|
|
|
sprite.body.velocity.x = 0;
|
|
|
|
sprite.body.velocity.y = 0;
|
2013-05-31 18:28:16 +00:00
|
|
|
sprite.body.position.x = x;
|
|
|
|
sprite.body.position.y = y;
|
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.
|
|
|
|
*
|
|
|
|
* @param x {number} x position of the bound
|
|
|
|
* @param y {number} y position of the bound
|
|
|
|
* @param width {number} width of its bound
|
|
|
|
* @param height {number} height of its bound
|
|
|
|
*/
|
|
|
|
static setBounds(x: number, y: number, width: number, height: number) {
|
|
|
|
|
|
|
|
//this.worldBounds = new Quad(x, y, width, height);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function creates a flat colored square image dynamically.
|
|
|
|
* @param width {number} The width of the sprite you want to generate.
|
|
|
|
* @param height {number} The height of the sprite you want to generate.
|
|
|
|
* @param [color] {number} specifies the color of the generated block. (format is 0xAARRGGBB)
|
|
|
|
* @return {Sprite} Sprite instance itself.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
static makeGraphic(width: number, height: number, color: string = 'rgb(255,255,255)'): Sprite {
|
|
|
|
|
|
|
|
this._texture = null;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
this.fillColor = color;
|
|
|
|
this._dynamicTexture = false;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|