/
EdgeZone.js
258 lines (227 loc) · 7.73 KB
/
EdgeZone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../../utils/Class');
/**
* @classdesc
* A zone that places particles on a shape's edges.
*
* @class EdgeZone
* @memberof Phaser.GameObjects.Particles.Zones
* @constructor
* @since 3.0.0
*
* @param {Phaser.Types.GameObjects.Particles.EdgeZoneSource} source - An object instance with a `getPoints(quantity, stepRate)` method returning an array of points.
* @param {number} quantity - The number of particles to place on the source edge. Set to 0 to use `stepRate` instead.
* @param {number} stepRate - The distance between each particle. When set, `quantity` is implied and should be set to 0.
* @param {boolean} [yoyo=false] - Whether particles are placed from start to end and then end to start.
* @param {boolean} [seamless=true] - Whether one endpoint will be removed if it's identical to the other.
* @param {number} [total=-1] - The total number of particles this zone will emit before passing over to the next emission zone in the Emitter. -1 means it will never pass over and you must use `setEmitZone` to change it.
*/
var EdgeZone = new Class({
initialize:
function EdgeZone (source, quantity, stepRate, yoyo, seamless, total)
{
if (yoyo === undefined) { yoyo = false; }
if (seamless === undefined) { seamless = true; }
if (total === undefined) { total = -1; }
/**
* An object instance with a `getPoints(quantity, stepRate)` method returning an array of points.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#source
* @type {Phaser.Types.GameObjects.Particles.EdgeZoneSource|Phaser.Types.GameObjects.Particles.RandomZoneSource}
* @since 3.0.0
*/
this.source = source;
/**
* The points placed on the source edge.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#points
* @type {Phaser.Geom.Point[]}
* @default []
* @since 3.0.0
*/
this.points = [];
/**
* The number of particles to place on the source edge. Set to 0 to use `stepRate` instead.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#quantity
* @type {number}
* @since 3.0.0
*/
this.quantity = quantity;
/**
* The distance between each particle. When set, `quantity` is implied and should be set to 0.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#stepRate
* @type {number}
* @since 3.0.0
*/
this.stepRate = stepRate;
/**
* Whether particles are placed from start to end and then end to start.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#yoyo
* @type {boolean}
* @since 3.0.0
*/
this.yoyo = yoyo;
/**
* The counter used for iterating the EdgeZone's points.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#counter
* @type {number}
* @default -1
* @since 3.0.0
*/
this.counter = -1;
/**
* Whether one endpoint will be removed if it's identical to the other.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#seamless
* @type {boolean}
* @since 3.0.0
*/
this.seamless = seamless;
/**
* An internal count of the points belonging to this EdgeZone.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#_length
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._length = 0;
/**
* An internal value used to keep track of the current iteration direction for the EdgeZone's points.
*
* 0 = forwards, 1 = backwards
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#_direction
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._direction = 0;
/**
* The total number of particles this zone will emit before the Emitter
* transfers control over to the next zone in its emission zone list.
*
* By default this is -1, meaning it will never pass over from this
* zone to another one. You can call the `ParticleEmitter.setEmitZone`
* method to change it, or set this value to something else via the
* config, or directly at runtime.
*
* A value of 1 would mean the zones rotate in order, but it can
* be set to any integer value.
*
* @name Phaser.GameObjects.Particles.Zones.EdgeZone#total
* @type {number}
* @since 3.60.0
*/
this.total = total;
this.updateSource();
},
/**
* Update the {@link Phaser.GameObjects.Particles.Zones.EdgeZone#points} from the EdgeZone's
* {@link Phaser.GameObjects.Particles.Zones.EdgeZone#source}.
*
* Also updates internal properties.
*
* @method Phaser.GameObjects.Particles.Zones.EdgeZone#updateSource
* @since 3.0.0
*
* @return {this} This Edge Zone.
*/
updateSource: function ()
{
this.points = this.source.getPoints(this.quantity, this.stepRate);
// Remove ends?
if (this.seamless)
{
var a = this.points[0];
var b = this.points[this.points.length - 1];
if (a.x === b.x && a.y === b.y)
{
this.points.pop();
}
}
var oldLength = this._length;
this._length = this.points.length;
// Adjust counter if we now have less points than before
if (this._length < oldLength && this.counter > this._length)
{
this.counter = this._length - 1;
}
return this;
},
/**
* Change the source of the EdgeZone.
*
* @method Phaser.GameObjects.Particles.Zones.EdgeZone#changeSource
* @since 3.0.0
*
* @param {Phaser.Types.GameObjects.Particles.EdgeZoneSource} source - An object instance with a `getPoints(quantity, stepRate)` method returning an array of points.
*
* @return {this} This Edge Zone.
*/
changeSource: function (source)
{
this.source = source;
return this.updateSource();
},
/**
* Get the next point in the Zone and set its coordinates on the given Particle.
*
* @method Phaser.GameObjects.Particles.Zones.EdgeZone#getPoint
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.Particle} particle - The Particle.
*/
getPoint: function (particle)
{
if (this._direction === 0)
{
this.counter++;
if (this.counter >= this._length)
{
if (this.yoyo)
{
this._direction = 1;
this.counter = this._length - 1;
}
else
{
this.counter = 0;
}
}
}
else
{
this.counter--;
if (this.counter === -1)
{
if (this.yoyo)
{
this._direction = 0;
this.counter = 0;
}
else
{
this.counter = this._length - 1;
}
}
}
var point = this.points[this.counter];
if (point)
{
particle.x = point.x;
particle.y = point.y;
}
}
});
module.exports = EdgeZone;