/
Shake.js
313 lines (273 loc) · 9.03 KB
/
Shake.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Clamp = require('../../../math/Clamp');
var Class = require('../../../utils/Class');
var Events = require('../events');
var Vector2 = require('../../../math/Vector2');
/**
* @classdesc
* A Camera Shake effect.
*
* This effect will shake the camera viewport by a random amount, bounded by the specified intensity, each frame.
*
* Only the camera viewport is moved. None of the objects it is displaying are impacted, i.e. their positions do
* not change.
*
* The effect will dispatch several events on the Camera itself and you can also specify an `onUpdate` callback,
* which is invoked each frame for the duration of the effect if required.
*
* @class Shake
* @memberof Phaser.Cameras.Scene2D.Effects
* @constructor
* @since 3.5.0
*
* @param {Phaser.Cameras.Scene2D.Camera} camera - The camera this effect is acting upon.
*/
var Shake = new Class({
initialize:
function Shake (camera)
{
/**
* The Camera this effect belongs to.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#camera
* @type {Phaser.Cameras.Scene2D.Camera}
* @readonly
* @since 3.5.0
*/
this.camera = camera;
/**
* Is this effect actively running?
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#isRunning
* @type {boolean}
* @readonly
* @default false
* @since 3.5.0
*/
this.isRunning = false;
/**
* The duration of the effect, in milliseconds.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#duration
* @type {number}
* @readonly
* @default 0
* @since 3.5.0
*/
this.duration = 0;
/**
* The intensity of the effect. Use small float values. The default when the effect starts is 0.05.
* This is a Vector2 object, allowing you to control the shake intensity independently across x and y.
* You can modify this value while the effect is active to create more varied shake effects.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#intensity
* @type {Phaser.Math.Vector2}
* @since 3.5.0
*/
this.intensity = new Vector2();
/**
* If this effect is running this holds the current percentage of the progress, a value between 0 and 1.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#progress
* @type {number}
* @since 3.5.0
*/
this.progress = 0;
/**
* Effect elapsed timer.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#_elapsed
* @type {number}
* @private
* @since 3.5.0
*/
this._elapsed = 0;
/**
* How much to offset the camera by horizontally.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#_offsetX
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._offsetX = 0;
/**
* How much to offset the camera by vertically.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#_offsetY
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._offsetY = 0;
/**
* This callback is invoked every frame for the duration of the effect.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#_onUpdate
* @type {?Phaser.Types.Cameras.Scene2D.CameraShakeCallback}
* @private
* @default null
* @since 3.5.0
*/
this._onUpdate;
/**
* On Complete callback scope.
*
* @name Phaser.Cameras.Scene2D.Effects.Shake#_onUpdateScope
* @type {any}
* @private
* @since 3.5.0
*/
this._onUpdateScope;
},
/**
* Shakes the Camera by the given intensity over the duration specified.
*
* @method Phaser.Cameras.Scene2D.Effects.Shake#start
* @fires Phaser.Cameras.Scene2D.Events#SHAKE_START
* @fires Phaser.Cameras.Scene2D.Events#SHAKE_COMPLETE
* @since 3.5.0
*
* @param {number} [duration=100] - The duration of the effect in milliseconds.
* @param {(number|Phaser.Math.Vector2)} [intensity=0.05] - The intensity of the shake.
* @param {boolean} [force=false] - Force the shake effect to start immediately, even if already running.
* @param {Phaser.Types.Cameras.Scene2D.CameraShakeCallback} [callback] - This callback will be invoked every frame for the duration of the effect.
* It is sent two arguments: A reference to the camera and a progress amount between 0 and 1 indicating how complete the effect is.
* @param {any} [context] - The context in which the callback is invoked. Defaults to the Scene to which the Camera belongs.
*
* @return {Phaser.Cameras.Scene2D.Camera} The Camera on which the effect was started.
*/
start: function (duration, intensity, force, callback, context)
{
if (duration === undefined) { duration = 100; }
if (intensity === undefined) { intensity = 0.05; }
if (force === undefined) { force = false; }
if (callback === undefined) { callback = null; }
if (context === undefined) { context = this.camera.scene; }
if (!force && this.isRunning)
{
return this.camera;
}
this.isRunning = true;
this.duration = duration;
this.progress = 0;
if (typeof intensity === 'number')
{
this.intensity.set(intensity);
}
else
{
this.intensity.set(intensity.x, intensity.y);
}
this._elapsed = 0;
this._offsetX = 0;
this._offsetY = 0;
this._onUpdate = callback;
this._onUpdateScope = context;
this.camera.emit(Events.SHAKE_START, this.camera, this, duration, intensity);
return this.camera;
},
/**
* The pre-render step for this effect. Called automatically by the Camera.
*
* @method Phaser.Cameras.Scene2D.Effects.Shake#preRender
* @since 3.5.0
*/
preRender: function ()
{
if (this.isRunning)
{
this.camera.matrix.translate(this._offsetX, this._offsetY);
}
},
/**
* The main update loop for this effect. Called automatically by the Camera.
*
* @method Phaser.Cameras.Scene2D.Effects.Shake#update
* @since 3.5.0
*
* @param {number} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
* @param {number} delta - The delta time, in ms, elapsed since the last frame.
*/
update: function (time, delta)
{
if (!this.isRunning)
{
return;
}
this._elapsed += delta;
this.progress = Clamp(this._elapsed / this.duration, 0, 1);
if (this._onUpdate)
{
this._onUpdate.call(this._onUpdateScope, this.camera, this.progress);
}
if (this._elapsed < this.duration)
{
var intensity = this.intensity;
var width = this.camera.width;
var height = this.camera.height;
var zoom = this.camera.zoom;
this._offsetX = (Math.random() * intensity.x * width * 2 - intensity.x * width) * zoom;
this._offsetY = (Math.random() * intensity.y * height * 2 - intensity.y * height) * zoom;
if (this.camera.roundPixels)
{
this._offsetX = Math.round(this._offsetX);
this._offsetY = Math.round(this._offsetY);
}
}
else
{
this.effectComplete();
}
},
/**
* Called internally when the effect completes.
*
* @method Phaser.Cameras.Scene2D.Effects.Shake#effectComplete
* @fires Phaser.Cameras.Scene2D.Events#SHAKE_COMPLETE
* @since 3.5.0
*/
effectComplete: function ()
{
this._offsetX = 0;
this._offsetY = 0;
this._onUpdate = null;
this._onUpdateScope = null;
this.isRunning = false;
this.camera.emit(Events.SHAKE_COMPLETE, this.camera, this);
},
/**
* Resets this camera effect.
* If it was previously running, it stops instantly without calling its onComplete callback or emitting an event.
*
* @method Phaser.Cameras.Scene2D.Effects.Shake#reset
* @since 3.5.0
*/
reset: function ()
{
this.isRunning = false;
this._offsetX = 0;
this._offsetY = 0;
this._onUpdate = null;
this._onUpdateScope = null;
},
/**
* Destroys this effect, releasing it from the Camera.
*
* @method Phaser.Cameras.Scene2D.Effects.Shake#destroy
* @since 3.5.0
*/
destroy: function ()
{
this.reset();
this.camera = null;
this.intensity = null;
}
});
module.exports = Shake;