phaser/src/gameobjects/Graphics.js

189 lines
5.3 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2015-02-25 03:36:23 +00:00
* @copyright 2015 Photon Storm Ltd.
2013-10-01 12:54:29 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* 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
* @extends PIXI.Graphics
2013-10-01 12:54:29 +00:00
* @param {Phaser.Game} game Current game instance.
* @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
*/
Phaser.Graphics = function (game, x, y) {
x = x || 0;
y = y || 0;
2013-10-01 12:54:29 +00:00
/**
* @property {number} type - The const type of this object.
* @default
*/
this.type = Phaser.GRAPHICS;
PIXI.Graphics.call(this);
Phaser.Utils.mixinPrototype(this, Phaser.Component.Core.prototype);
var components = [
'Angle',
'AutoCull',
'Bounds',
'Destroy',
'FixedToCamera',
'InputEnabled',
'InWorld',
'LifeSpan',
'PhysicsBody',
'Reset'
];
Phaser.Component.Core.install.call(this, components);
Phaser.Component.Core.init.call(this, game, x, y, '', null);
};
2013-09-22 21:55:34 +00:00
Phaser.Graphics.prototype = Object.create(PIXI.Graphics.prototype);
Phaser.Graphics.prototype.constructor = Phaser.Graphics;
2013-09-22 14:21:51 +00:00
/**
* Automatically called by World.preUpdate.
* @method Phaser.Graphics.prototype.preUpdate
*/
Phaser.Graphics.prototype.preUpdate = function () {
// Phaser.Component.PhysicsBody.preUpdate.call(this);
// Phaser.Component.LifeSpan.preUpdate.call(this);
Phaser.Component.InWorld.preUpdate.call(this);
Phaser.Component.Core.preUpdate.call(this);
return true;
2014-03-23 08:40:24 +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
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called?
*/
Phaser.Graphics.prototype.destroy = function(destroyChildren) {
this.clear();
Phaser.Component.Destroy.prototype.destroy.call(this, destroyChildren);
2014-03-23 08:40:24 +00:00
};
/*
* Draws a single {Phaser.Polygon} triangle from a {Phaser.Point} array
*
* @method Phaser.Graphics.prototype.drawTriangle
* @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) {
if (typeof cull === 'undefined') { cull = false; }
var triangle = new Phaser.Polygon(points);
if (cull)
{
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);
if (cameraToFace.dot(faceNormal) > 0)
{
this.drawPolygon(triangle);
}
}
else
{
this.drawPolygon(triangle);
}
};
/*
* Draws {Phaser.Polygon} triangles
*
* @method Phaser.Graphics.prototype.drawTriangles
* @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) {
if (typeof cull === 'undefined') { cull = false; }
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++)
{
this.drawTriangle([vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]], cull);
}
}
else
{
for (i = 0; i < vertices.length / 6; i++)
{
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);
}
}
}
else
{
if (vertices[0] instanceof Phaser.Point)
{
for (i = 0; i < indices.length /3; i++)
{
points.push(vertices[indices[i * 3 ]]);
points.push(vertices[indices[i * 3 + 1]]);
points.push(vertices[indices[i * 3 + 2]]);
if (points.length === 3)
{
this.drawTriangle(points, cull);
points = [];
}
}
}
else
{
for (i = 0; i < indices.length; i++)
{
point1.x = vertices[indices[i] * 2];
point1.y = vertices[indices[i] * 2 + 1];
points.push(point1.copyTo({}));
if (points.length === 3)
{
this.drawTriangle(points, cull);
points = [];
}
}
}
}
};