2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* Creates a new `Graphics` object.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Graphics
|
|
|
|
* @constructor
|
2014-09-16 16:35:08 +00:00
|
|
|
* @extends PIXI.Graphics
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Game} game Current game instance.
|
2013-10-25 14:22:45 +00:00
|
|
|
* @param {number} x - X position of the new graphics object.
|
|
|
|
* @param {number} y - Y position of the new graphics object.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-09 16:01:59 +00:00
|
|
|
Phaser.Graphics = function (game, x, y) {
|
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
x = x || 0;
|
|
|
|
y = y || 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
|
|
|
*/
|
2013-10-17 20:10:00 +00:00
|
|
|
this.game = game;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} exists - If exists = false then the Text isn't updated by the core game loop.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.exists = true;
|
2013-10-17 20:10:00 +00:00
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* @property {string} name - The user defined name given to this object.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.name = '';
|
2013-09-09 16:01:59 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-17 11:27:31 +00:00
|
|
|
* @property {number} type - The const type of this object.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-12 20:54:41 +00:00
|
|
|
this.type = Phaser.GRAPHICS;
|
|
|
|
|
2014-03-10 23:16:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} z - The z-depth value of this object within its Group (remember the World is a Group as well). No two objects in a Group can have the same z value.
|
|
|
|
*/
|
|
|
|
this.z = 0;
|
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} world - The world coordinates of this Sprite. This differs from the x/y coordinates which are relative to the Sprites container.
|
|
|
|
*/
|
|
|
|
this.world = new Phaser.Point(x, y);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that its drawn at, from the top-left of the camera view.
|
|
|
|
*/
|
|
|
|
this.cameraOffset = new Phaser.Point();
|
|
|
|
|
|
|
|
PIXI.Graphics.call(this);
|
|
|
|
|
|
|
|
this.position.set(x, y);
|
|
|
|
|
|
|
|
/**
|
2015-02-16 17:22:51 +00:00
|
|
|
* @property {Phaser.Point} previousPosition - The position the Sprite was in at the last update.
|
|
|
|
* @readOnly
|
2014-02-17 11:27:31 +00:00
|
|
|
*/
|
2015-02-16 17:22:51 +00:00
|
|
|
this.previousPosition = new Phaser.Point(x, y);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} previousRotation - The rotation angle the Sprite was in at the last update (in radians)
|
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
this.previousRotation = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} renderOrderID - The render order ID. This is used internally by the renderer and input manager and should not be modified.
|
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
this.renderOrderID = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Sprite that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Sprite.cameraOffset.
|
|
|
|
* Note that the cameraOffset values are in addition to any parent in the display list.
|
|
|
|
* So if this Sprite was in a Group that has x: 200, then this will be added to the cameraOffset.x
|
|
|
|
* @property {boolean} fixedToCamera
|
|
|
|
*/
|
|
|
|
this.fixedToCamera = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} destroyPhase - As a Sprite runs through its destroy method this flag is set to true, and can be checked in any sub-systems it is being destroyed from.
|
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
this.destroyPhase = false;
|
2013-11-13 06:49:24 +00:00
|
|
|
|
2013-09-09 16:01:59 +00:00
|
|
|
};
|
|
|
|
|
2013-09-22 21:55:34 +00:00
|
|
|
Phaser.Graphics.prototype = Object.create(PIXI.Graphics.prototype);
|
2013-09-09 16:01:59 +00:00
|
|
|
Phaser.Graphics.prototype.constructor = Phaser.Graphics;
|
2013-09-22 14:21:51 +00:00
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* Automatically called by World.preUpdate.
|
|
|
|
* @method Phaser.Graphics.prototype.preUpdate
|
|
|
|
*/
|
|
|
|
Phaser.Graphics.prototype.preUpdate = function () {
|
|
|
|
|
2015-02-16 17:22:51 +00:00
|
|
|
this.previousPosition.set(this.world.x, this.world.y);
|
|
|
|
this.previousRotation = this.rotation;
|
2014-02-17 11:27:31 +00:00
|
|
|
|
|
|
|
if (!this.exists || !this.parent.exists)
|
|
|
|
{
|
|
|
|
this.renderOrderID = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.autoCull)
|
|
|
|
{
|
|
|
|
// Won't get rendered but will still get its transform updated
|
|
|
|
this.renderable = this.game.world.camera.screenView.intersects(this.getBounds());
|
|
|
|
}
|
|
|
|
|
2014-07-15 21:43:48 +00:00
|
|
|
this.world.setTo(this.game.camera.x + this.worldTransform.tx, this.game.camera.y + this.worldTransform.ty);
|
2014-02-17 11:27:31 +00:00
|
|
|
|
|
|
|
if (this.visible)
|
|
|
|
{
|
2015-02-16 17:22:51 +00:00
|
|
|
this.renderOrderID = this.game.stage.currentRenderOrderID++;
|
2014-02-17 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-17 11:27:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override and use this function in your own custom objects to handle any update requirements you may have.
|
|
|
|
*
|
|
|
|
* @method Phaser.Graphics#update
|
|
|
|
* @memberof Phaser.Graphics
|
|
|
|
*/
|
|
|
|
Phaser.Graphics.prototype.update = function() {
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-17 11:27:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically called by World.postUpdate.
|
|
|
|
* @method Phaser.Graphics.prototype.postUpdate
|
|
|
|
*/
|
|
|
|
Phaser.Graphics.prototype.postUpdate = function () {
|
|
|
|
|
2015-02-16 17:22:51 +00:00
|
|
|
if (this.fixedToCamera)
|
2014-02-17 11:27:31 +00:00
|
|
|
{
|
2014-03-06 17:12:00 +00:00
|
|
|
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
|
|
|
|
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
|
2014-02-17 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-17 11:27:31 +00:00
|
|
|
|
2013-10-17 20:10:00 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* Destroy this Graphics instance.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-20 03:44:44 +00:00
|
|
|
* @method Phaser.Graphics.prototype.destroy
|
2014-02-27 20:05:16 +00:00
|
|
|
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called?
|
2013-10-17 20:10:00 +00:00
|
|
|
*/
|
2014-02-27 20:05:16 +00:00
|
|
|
Phaser.Graphics.prototype.destroy = function(destroyChildren) {
|
|
|
|
|
2014-04-22 00:43:22 +00:00
|
|
|
if (this.game === null || this.destroyPhase) { return; }
|
|
|
|
|
2014-02-27 20:05:16 +00:00
|
|
|
if (typeof destroyChildren === 'undefined') { destroyChildren = true; }
|
2013-10-17 20:10:00 +00:00
|
|
|
|
2015-02-16 17:22:51 +00:00
|
|
|
this.destroyPhase = false;
|
2014-04-22 00:43:22 +00:00
|
|
|
|
2013-10-17 20:10:00 +00:00
|
|
|
this.clear();
|
|
|
|
|
2014-02-07 19:44:14 +00:00
|
|
|
if (this.parent)
|
2013-10-17 20:10:00 +00:00
|
|
|
{
|
2014-02-28 09:30:53 +00:00
|
|
|
if (this.parent instanceof Phaser.Group)
|
2014-02-28 06:17:18 +00:00
|
|
|
{
|
|
|
|
this.parent.remove(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.parent.removeChild(this);
|
|
|
|
}
|
2013-10-17 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 20:05:16 +00:00
|
|
|
var i = this.children.length;
|
|
|
|
|
|
|
|
if (destroyChildren)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.children[i].destroy(destroyChildren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.removeChild(this.children[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
this.exists = false;
|
|
|
|
this.visible = false;
|
|
|
|
|
2013-10-17 20:10:00 +00:00
|
|
|
this.game = null;
|
|
|
|
|
2015-02-16 17:22:51 +00:00
|
|
|
this.destroyPhase = true;
|
2014-04-22 00:43:22 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-17 20:10:00 +00:00
|
|
|
|
2014-12-17 13:44:12 +00:00
|
|
|
/*
|
|
|
|
* Draws a circle.
|
|
|
|
*
|
|
|
|
* @method Phaser.Graphics.prototype.drawCircle
|
|
|
|
* @param {Number} x - The X coordinate of the center of the circle.
|
|
|
|
* @param {Number} y - The Y coordinate of the center of the circle.
|
2015-02-10 21:22:36 +00:00
|
|
|
* @param {Number} diameter - The diameter of the circle.
|
2014-12-17 13:44:12 +00:00
|
|
|
* @return {Phaser.Graphics} This Graphics object.
|
|
|
|
*/
|
|
|
|
Phaser.Graphics.prototype.drawCircle = function(x, y, diameter)
|
|
|
|
{
|
|
|
|
this.drawShape(new Phaser.Circle(x, y, diameter));
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-05-02 17:30:49 +00:00
|
|
|
/*
|
|
|
|
* Draws a single {Phaser.Polygon} triangle from a {Phaser.Point} array
|
|
|
|
*
|
2014-05-13 23:04:31 +00:00
|
|
|
* @method Phaser.Graphics.prototype.drawTriangle
|
2014-05-02 17:30:49 +00:00
|
|
|
* @param {Array<Phaser.Point>} points - An array of Phaser.Points that make up the three vertices of this triangle
|
|
|
|
* @param {boolean} [cull=false] - Should we check if the triangle is back-facing
|
|
|
|
*/
|
|
|
|
Phaser.Graphics.prototype.drawTriangle = function(points, cull) {
|
2014-05-13 23:04:31 +00:00
|
|
|
|
|
|
|
if (typeof cull === 'undefined') { cull = false; }
|
|
|
|
|
2014-05-02 17:30:49 +00:00
|
|
|
var triangle = new Phaser.Polygon(points);
|
2014-05-13 23:04:31 +00:00
|
|
|
|
|
|
|
if (cull)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
var cameraToFace = new Phaser.Point(this.game.camera.x - points[0].x, this.game.camera.y - points[0].y);
|
|
|
|
var ab = new Phaser.Point(points[1].x - points[0].x, points[1].y - points[0].y);
|
|
|
|
var cb = new Phaser.Point(points[1].x - points[2].x, points[1].y - points[2].y);
|
|
|
|
var faceNormal = cb.cross(ab);
|
2014-05-13 23:04:31 +00:00
|
|
|
|
|
|
|
if (cameraToFace.dot(faceNormal) > 0)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
this.drawPolygon(triangle);
|
|
|
|
}
|
2014-05-13 23:04:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
this.drawPolygon(triangle);
|
|
|
|
}
|
2014-05-13 23:04:31 +00:00
|
|
|
|
2014-05-02 17:30:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Draws {Phaser.Polygon} triangles
|
|
|
|
*
|
2014-05-13 23:04:31 +00:00
|
|
|
* @method Phaser.Graphics.prototype.drawTriangles
|
2014-05-02 17:30:49 +00:00
|
|
|
* @param {Array<Phaser.Point>|Array<number>} vertices - An array of Phaser.Points or numbers that make up the vertices of the triangles
|
|
|
|
* @param {Array<number>} {indices=null} - An array of numbers that describe what order to draw the vertices in
|
|
|
|
* @param {boolean} [cull=false] - Should we check if the triangle is back-facing
|
|
|
|
*/
|
|
|
|
Phaser.Graphics.prototype.drawTriangles = function(vertices, indices, cull) {
|
|
|
|
|
2014-05-13 23:04:31 +00:00
|
|
|
if (typeof cull === 'undefined') { cull = false; }
|
2014-05-02 17:30:49 +00:00
|
|
|
|
2014-05-13 23:04:31 +00:00
|
|
|
var point1 = new Phaser.Point();
|
|
|
|
var point2 = new Phaser.Point();
|
|
|
|
var point3 = new Phaser.Point();
|
|
|
|
var points = [];
|
|
|
|
var i;
|
|
|
|
|
|
|
|
if (!indices)
|
|
|
|
{
|
|
|
|
if (vertices[0] instanceof Phaser.Point)
|
|
|
|
{
|
|
|
|
for (i = 0; i < vertices.length / 3; i++)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
this.drawTriangle([vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]], cull);
|
|
|
|
}
|
2014-05-13 23:04:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < vertices.length / 6; i++)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
point1.x = vertices[i * 6 + 0];
|
|
|
|
point1.y = vertices[i * 6 + 1];
|
|
|
|
point2.x = vertices[i * 6 + 2];
|
|
|
|
point2.y = vertices[i * 6 + 3];
|
|
|
|
point3.x = vertices[i * 6 + 4];
|
|
|
|
point3.y = vertices[i * 6 + 5];
|
|
|
|
this.drawTriangle([point1, point2, point3], cull);
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 23:04:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (vertices[0] instanceof Phaser.Point)
|
|
|
|
{
|
|
|
|
for (i = 0; i < indices.length /3; i++)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
points.push(vertices[indices[i * 3 ]]);
|
|
|
|
points.push(vertices[indices[i * 3 + 1]]);
|
|
|
|
points.push(vertices[indices[i * 3 + 2]]);
|
2014-05-13 23:04:31 +00:00
|
|
|
|
|
|
|
if (points.length === 3)
|
|
|
|
{
|
|
|
|
this.drawTriangle(points, cull);
|
2014-05-02 17:30:49 +00:00
|
|
|
points = [];
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 23:04:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < indices.length; i++)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
point1.x = vertices[indices[i] * 2];
|
|
|
|
point1.y = vertices[indices[i] * 2 + 1];
|
|
|
|
points.push(point1.copyTo({}));
|
2014-05-13 23:04:31 +00:00
|
|
|
|
|
|
|
if (points.length === 3)
|
|
|
|
{
|
2014-05-02 17:30:49 +00:00
|
|
|
this.drawTriangle(points, cull);
|
|
|
|
points = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* Indicates the rotation of the Graphics, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
|
|
|
|
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90.
|
|
|
|
* If you wish to work in radians instead of degrees use the property Sprite.rotation instead.
|
2014-05-13 23:04:31 +00:00
|
|
|
*
|
2014-02-17 11:27:31 +00:00
|
|
|
* @name Phaser.Graphics#angle
|
|
|
|
* @property {number} angle - Gets or sets the angle of rotation in degrees.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Graphics.prototype, 'angle', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return Phaser.Math.radToDeg(this.rotation);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
this.rotation = Phaser.Math.degToRad(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|