|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var ArcRender = require('./ArcRender'); |
| 8 | +var Class = require('../../utils/Class'); |
| 9 | +var DegToRad = require('../../math/DegToRad'); |
| 10 | +var Earcut = require('../../geom/polygon/Earcut'); |
| 11 | +var GeomCircle = require('../../geom/circle/Circle'); |
| 12 | +var Shape = require('./Shape'); |
| 13 | +var MATH_CONST = require('../../math/const'); |
| 14 | + |
| 15 | +/** |
| 16 | + * @classdesc |
| 17 | + * |
| 18 | + * @class Arc |
| 19 | + * @extends Phaser.GameObjects.Shape |
| 20 | + * @memberOf Phaser.GameObjects |
| 21 | + * @constructor |
| 22 | + * @since 3.13.0 |
| 23 | + * |
| 24 | + * @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time. |
| 25 | + * @param {number} x - The horizontal position of this Game Object in the world. |
| 26 | + * @param {number} y - The vertical position of this Game Object in the world. |
| 27 | + */ |
| 28 | +var Arc = new Class({ |
| 29 | + |
| 30 | + Extends: Shape, |
| 31 | + |
| 32 | + Mixins: [ |
| 33 | + ArcRender |
| 34 | + ], |
| 35 | + |
| 36 | + initialize: |
| 37 | + |
| 38 | + function Arc (scene, x, y, radius, fillColor, fillAlpha, startAngle, endAngle, anticlockwise) |
| 39 | + { |
| 40 | + if (startAngle === undefined) { startAngle = 0; } |
| 41 | + if (endAngle === undefined) { endAngle = 360; } |
| 42 | + if (anticlockwise === undefined) { anticlockwise = false; } |
| 43 | + |
| 44 | + Shape.call(this, scene, 'Arc', new GeomCircle(x, y, radius)); |
| 45 | + |
| 46 | + this.pathData = []; |
| 47 | + this.pathIndexes = []; |
| 48 | + |
| 49 | + this.startAngle = DegToRad(startAngle); |
| 50 | + this.endAngle = DegToRad(endAngle); |
| 51 | + this.anticlockwise = anticlockwise; |
| 52 | + this.iterations = 0.01; |
| 53 | + |
| 54 | + this.setPosition(x, y); |
| 55 | + this.setSize(this.data.radius, this.data.radius); |
| 56 | + |
| 57 | + this.updateDisplayOrigin(); |
| 58 | + |
| 59 | + if (fillColor !== undefined) |
| 60 | + { |
| 61 | + this.setFillStyle(fillColor, fillAlpha); |
| 62 | + } |
| 63 | + |
| 64 | + this.updateData(); |
| 65 | + }, |
| 66 | + |
| 67 | + setSmoothing: function (value) |
| 68 | + { |
| 69 | + if (value === undefined) { value = 0.01; } |
| 70 | + |
| 71 | + this.iterations = value; |
| 72 | + |
| 73 | + return this.updateData(); |
| 74 | + }, |
| 75 | + |
| 76 | + setStartAngle: function (angle, anticlockwise) |
| 77 | + { |
| 78 | + this.startAngle = DegToRad(angle); |
| 79 | + |
| 80 | + if (anticlockwise !== undefined) |
| 81 | + { |
| 82 | + this.anticlockwise = anticlockwise; |
| 83 | + } |
| 84 | + |
| 85 | + return this.updateData(); |
| 86 | + }, |
| 87 | + |
| 88 | + setEndAngle: function (angle, anticlockwise) |
| 89 | + { |
| 90 | + this.endAngle = DegToRad(angle); |
| 91 | + |
| 92 | + if (anticlockwise !== undefined) |
| 93 | + { |
| 94 | + this.anticlockwise = anticlockwise; |
| 95 | + } |
| 96 | + |
| 97 | + return this.updateData(); |
| 98 | + }, |
| 99 | + |
| 100 | + updateData: function () |
| 101 | + { |
| 102 | + var step = this.iterations; |
| 103 | + var iteration = step; |
| 104 | + |
| 105 | + var x = 0; |
| 106 | + var y = 0; |
| 107 | + |
| 108 | + var radius = this.data.radius; |
| 109 | + var startAngle = this.startAngle; |
| 110 | + var endAngle = this.endAngle; |
| 111 | + var anticlockwise = this.anticlockwise; |
| 112 | + |
| 113 | + endAngle -= startAngle; |
| 114 | + |
| 115 | + if (anticlockwise) |
| 116 | + { |
| 117 | + if (endAngle < -MATH_CONST.PI2) |
| 118 | + { |
| 119 | + endAngle = -MATH_CONST.PI2; |
| 120 | + } |
| 121 | + else if (endAngle > 0) |
| 122 | + { |
| 123 | + endAngle = -MATH_CONST.PI2 + endAngle % MATH_CONST.PI2; |
| 124 | + } |
| 125 | + } |
| 126 | + else if (endAngle > MATH_CONST.PI2) |
| 127 | + { |
| 128 | + endAngle = MATH_CONST.PI2; |
| 129 | + } |
| 130 | + else if (endAngle < 0) |
| 131 | + { |
| 132 | + endAngle = MATH_CONST.PI2 + endAngle % MATH_CONST.PI2; |
| 133 | + } |
| 134 | + |
| 135 | + var path = [ x + Math.cos(startAngle) * radius, y + Math.sin(startAngle) * radius ]; |
| 136 | + |
| 137 | + var ta; |
| 138 | + |
| 139 | + while (iteration < 1) |
| 140 | + { |
| 141 | + ta = endAngle * iteration + startAngle; |
| 142 | + |
| 143 | + path.push(x + Math.cos(ta) * radius); |
| 144 | + path.push(y + Math.sin(ta) * radius); |
| 145 | + |
| 146 | + iteration += step; |
| 147 | + } |
| 148 | + |
| 149 | + ta = endAngle + startAngle; |
| 150 | + |
| 151 | + path.push(x + Math.cos(ta) * radius); |
| 152 | + path.push(y + Math.sin(ta) * radius); |
| 153 | + |
| 154 | + this.pathIndexes = Earcut(path); |
| 155 | + this.pathData = path; |
| 156 | + |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | +}); |
| 161 | + |
| 162 | +module.exports = Arc; |
0 commit comments