forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGravityWell.js
More file actions
219 lines (185 loc) · 5.43 KB
/
Copy pathGravityWell.js
File metadata and controls
219 lines (185 loc) · 5.43 KB
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
/**
* @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 GetFastValue = require('../../utils/object/GetFastValue');
/**
* @typedef {object} GravityWellConfig
*
* @property {number} [x=0] - The x coordinate of the Gravity Well, in world space.
* @property {number} [y=0] - The y coordinate of the Gravity Well, in world space.
* @property {number} [power=0] - The power of the Gravity Well.
* @property {number} [epsilon=100] - [description]
* @property {number} [gravity=50] - The gravitational force of this Gravity Well.
*/
/**
* @classdesc
* [description]
*
* @class GravityWell
* @memberOf Phaser.GameObjects.Particles
* @constructor
* @since 3.0.0
*
* @param {(number|GravityWellConfig)} [x=0] - The x coordinate of the Gravity Well, in world space.
* @param {number} [y=0] - The y coordinate of the Gravity Well, in world space.
* @param {number} [power=0] - The power of the Gravity Well.
* @param {number} [epsilon=100] - [description]
* @param {number} [gravity=50] - The gravitational force of this Gravity Well.
*/
var GravityWell = new Class({
initialize:
function GravityWell (x, y, power, epsilon, gravity)
{
if (typeof x === 'object')
{
var config = x;
x = GetFastValue(config, 'x', 0);
y = GetFastValue(config, 'y', 0);
power = GetFastValue(config, 'power', 0);
epsilon = GetFastValue(config, 'epsilon', 100);
gravity = GetFastValue(config, 'gravity', 50);
}
else
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (power === undefined) { power = 0; }
if (epsilon === undefined) { epsilon = 100; }
if (gravity === undefined) { gravity = 50; }
}
/**
* The x coordinate of the Gravity Well, in world space.
*
* @name Phaser.GameObjects.Particles.GravityWell#x
* @type {number}
* @since 3.0.0
*/
this.x = x;
/**
* The y coordinate of the Gravity Well, in world space.
*
* @name Phaser.GameObjects.Particles.GravityWell#y
* @type {number}
* @since 3.0.0
*/
this.y = y;
/**
* The active state of the Gravity Well. An inactive Gravity Well will not influence any particles.
*
* @name Phaser.GameObjects.Particles.GravityWell#active
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.active = true;
/**
* Internal gravity value.
*
* @name Phaser.GameObjects.Particles.GravityWell#_gravity
* @type {number}
* @private
* @since 3.0.0
*/
this._gravity = gravity;
/**
* Internal power value.
*
* @name Phaser.GameObjects.Particles.GravityWell#_power
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._power = 0;
/**
* Internal epsilon value.
*
* @name Phaser.GameObjects.Particles.GravityWell#_epsilon
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._epsilon = 0;
/**
* The power of the Gravity Well.
*
* @name Phaser.GameObjects.Particles.GravityWell#power
* @type {number}
* @since 3.0.0
*/
this.power = power;
/**
* [description]
*
* @name Phaser.GameObjects.Particles.GravityWell#epsilon
* @type {number}
* @since 3.0.0
*/
this.epsilon = epsilon;
},
/**
* Takes a Particle and updates it based on the properties of this Gravity Well.
*
* @method Phaser.GameObjects.Particles.GravityWell#update
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.Particle} particle - The Particle to update.
* @param {number} delta - The delta time in ms.
* @param {number} step - The delta value divided by 1000.
*/
update: function (particle, delta)
{
var x = this.x - particle.x;
var y = this.y - particle.y;
var dSq = x * x + y * y;
if (dSq === 0)
{
return;
}
var d = Math.sqrt(dSq);
if (dSq < this._epsilon)
{
dSq = this._epsilon;
}
var factor = ((this._power * delta) / (dSq * d)) * 100;
particle.velocityX += x * factor;
particle.velocityY += y * factor;
},
epsilon: {
get: function ()
{
return Math.sqrt(this._epsilon);
},
set: function (value)
{
this._epsilon = value * value;
}
},
power: {
get: function ()
{
return this._power / this._gravity;
},
set: function (value)
{
this._power = value * this._gravity;
}
},
gravity: {
get: function ()
{
return this._gravity;
},
set: function (value)
{
var pwr = this.power;
this._gravity = value;
this.power = pwr;
}
}
});
module.exports = GravityWell;