phaser/src/gameobjects/shape/curve/Curve.js

163 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-09-07 16:19:33 +00:00
/**
* @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 CurveRender = require('./CurveRender');
var Earcut = require('../../../geom/polygon/Earcut');
var Rectangle = require('../../../geom/rectangle/Rectangle');
var Shape = require('../Shape');
/**
* @classdesc
*
* @class Curve
* @extends Phaser.GameObjects.Shape
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.13.0
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
2018-09-11 14:21:22 +00:00
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
* @param {number} [y=0] - The vertical position of this Game Object in the world.
* @param {Phaser.Curves.Curve} [curve] - The Curve object to use to create the Shape.
* @param {number} [fillColor] - The color the curve will be filled with, i.e. 0xff0000 for red.
* @param {number} [fillAlpha] - The alpha the curve will be filled with. You can also set the alpha of the overall Shape using its `alpha` property.
2018-09-07 16:19:33 +00:00
*/
var Curve = new Class({
Extends: Shape,
Mixins: [
CurveRender
],
initialize:
function Curve (scene, x, y, curve, fillColor, fillAlpha)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
Shape.call(this, scene, 'Curve', curve);
2018-09-11 14:21:22 +00:00
/**
* Private internal value.
* The number of points used to draw the curve. Higher values create smoother renders at the cost of more triangles being drawn.
*
* @name Phaser.GameObjects.Curve#_smoothness
* @type {integer}
* @private
* @since 3.13.0
*/
2018-09-07 16:19:33 +00:00
this._smoothness = 32;
2018-09-11 14:21:22 +00:00
/**
* Private internal value.
* The Curve bounds rectangle.
*
* @name Phaser.GameObjects.Curve#_curveBounds
* @type {Phaser.Geom.Rectangle}
* @private
* @since 3.13.0
*/
2018-09-07 16:19:33 +00:00
this._curveBounds = new Rectangle();
this.closePath = false;
this.setPosition(x, y);
if (fillColor !== undefined)
{
this.setFillStyle(fillColor, fillAlpha);
}
this.updateData();
},
2018-09-11 14:21:22 +00:00
/**
* The smoothness of the curve. The number of points used when rendering it.
* Increase this value for smoother curves, at the cost of more polygons being rendered.
*
* @name Phaser.GameObjects.Curve#smoothness
* @type {integer}
* @default 32
* @since 3.13.0
*/
2018-09-07 16:19:33 +00:00
smoothness: {
get: function ()
{
return this._smoothness;
},
set: function (value)
{
this._smoothness = value;
this.updateData();
}
},
2018-09-11 14:21:22 +00:00
/**
* Sets the smoothness of the curve. The number of points used when rendering it.
* Increase this value for smoother curves, at the cost of more polygons being rendered.
* This call can be chained.
*
* @method Phaser.GameObjects.Curve#setSmoothness
* @since 3.13.0
*
* @param {integer} value - The value to set the smoothness to.
*
* @return {this} This Game Object instance.
*/
2018-09-07 16:19:33 +00:00
setSmoothness: function (value)
{
this._smoothness = value;
return this.updateData();
},
2018-09-11 14:21:22 +00:00
/**
* Internal method that updates the data and path values.
*
* @method Phaser.GameObjects.Curve#updateData
* @private
* @since 3.13.0
*
* @return {this} This Game Object instance.
*/
2018-09-07 16:19:33 +00:00
updateData: function ()
{
var bounds = this._curveBounds;
var smoothness = this._smoothness;
// Update the bounds in case the underlying data has changed
this.data.getBounds(bounds, smoothness);
this.setSize(bounds.width, bounds.height);
this.updateDisplayOrigin();
var path = [];
var points = this.data.getPoints(smoothness);
for (var i = 0; i < points.length; i++)
{
path.push(points[i].x, points[i].y);
}
path.push(points[0].x, points[0].y);
this.pathIndexes = Earcut(path);
this.pathData = path;
return this;
}
});
module.exports = Curve;