phaser/src/gameobjects/shape/Shape.js
2018-09-06 15:49:42 +01:00

128 lines
3.3 KiB
JavaScript

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var Line = require('../../geom/line/Line');
/**
* @classdesc
* A Shape Game Object.
*
* @class Shape
* @extends Phaser.GameObjects.GameObject
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.13.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.ComputedSize
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScaleMode
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Tint
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
*/
var Shape = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.ComputedSize,
Components.Depth,
Components.GetBounds,
Components.Mask,
Components.Origin,
Components.Pipeline,
Components.ScaleMode,
Components.ScrollFactor,
Components.Tint,
Components.Transform,
Components.Visible
],
initialize:
function Shape (scene, type, data)
{
if (type === undefined) { type = 'Shape'; }
GameObject.call(this, scene, type);
this.data = data;
// Holds earcut polygon fill data
this.pathData = [];
this.pathIndexes = [];
this.fillColor = 0xffffff;
this.fillAlpha = 1;
this.lineWidth = 1;
this.strokeColor = 0xffffff;
this.strokeAlpha = 1;
this.isFilled = false;
this.isStroked = false;
this._tempLine = new Line();
this.initPipeline();
},
setFillStyle: function (color, alpha)
{
if (alpha === undefined) { alpha = 1; }
if (color === undefined)
{
this.isFilled = false;
}
else
{
this.fillColor = color;
this.fillAlpha = alpha;
this.isFilled = true;
}
return this;
},
setStrokeStyle: function (lineWidth, color, alpha)
{
if (alpha === undefined) { alpha = 1; }
if (lineWidth === undefined)
{
this.isStroked = false;
}
else
{
this.lineWidth = lineWidth;
this.strokeColor = color;
this.strokeAlpha = alpha;
this.isStroked = true;
}
return this;
}
});
module.exports = Shape;