@vpmedia/phaser 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/phaser.cjs.LICENSE.txt +1 -1
- package/dist/phaser.js.LICENSE.txt +1 -1
- package/package.json +2 -3
- package/src/index.js +99 -0
- package/src/phaser/core/animation.js +355 -0
- package/src/phaser/core/animation_manager.js +238 -0
- package/src/phaser/core/animation_parser.js +130 -0
- package/src/phaser/core/array_set.js +108 -0
- package/src/phaser/core/cache.js +558 -0
- package/src/phaser/core/const.js +106 -0
- package/src/phaser/core/device.js +67 -0
- package/src/phaser/core/device_util.js +386 -0
- package/src/phaser/core/dom.js +207 -0
- package/src/phaser/core/event_manager.js +243 -0
- package/src/phaser/core/factory.js +74 -0
- package/src/phaser/core/frame.js +75 -0
- package/src/phaser/core/frame_data.js +84 -0
- package/src/phaser/core/frame_util.js +31 -0
- package/src/phaser/core/game.js +412 -0
- package/src/phaser/core/input.js +401 -0
- package/src/phaser/core/input_button.js +102 -0
- package/src/phaser/core/input_handler.js +687 -0
- package/src/phaser/core/input_mouse.js +289 -0
- package/src/phaser/core/input_mspointer.js +197 -0
- package/src/phaser/core/input_pointer.js +427 -0
- package/src/phaser/core/input_touch.js +157 -0
- package/src/phaser/core/loader.js +946 -0
- package/src/phaser/core/loader_parser.js +105 -0
- package/src/phaser/core/raf.js +46 -0
- package/src/phaser/core/raf_fb.js +75 -0
- package/src/phaser/core/raf_to.js +34 -0
- package/src/phaser/core/scale_manager.js +806 -0
- package/src/phaser/core/scene.js +66 -0
- package/src/phaser/core/scene_manager.js +310 -0
- package/src/phaser/core/signal.js +175 -0
- package/src/phaser/core/signal_binding.js +69 -0
- package/src/phaser/core/sound.js +538 -0
- package/src/phaser/core/sound_manager.js +365 -0
- package/src/phaser/core/stage.js +108 -0
- package/src/phaser/core/time.js +203 -0
- package/src/phaser/core/timer.js +276 -0
- package/src/phaser/core/timer_event.js +21 -0
- package/src/phaser/core/tween.js +329 -0
- package/src/phaser/core/tween_data.js +258 -0
- package/src/phaser/core/tween_easing.js +316 -0
- package/src/phaser/core/tween_manager.js +185 -0
- package/src/phaser/core/world.js +18 -0
- package/src/phaser/display/bitmap_text.js +322 -0
- package/src/phaser/display/button.js +194 -0
- package/src/phaser/display/canvas/buffer.js +36 -0
- package/src/phaser/display/canvas/graphics.js +227 -0
- package/src/phaser/display/canvas/masker.js +39 -0
- package/src/phaser/display/canvas/pool.js +121 -0
- package/src/phaser/display/canvas/renderer.js +123 -0
- package/src/phaser/display/canvas/tinter.js +141 -0
- package/src/phaser/display/canvas/util.js +151 -0
- package/src/phaser/display/display_object.js +597 -0
- package/src/phaser/display/graphics.js +723 -0
- package/src/phaser/display/graphics_data.js +27 -0
- package/src/phaser/display/graphics_data_util.js +14 -0
- package/src/phaser/display/group.js +227 -0
- package/src/phaser/display/image.js +288 -0
- package/src/phaser/display/sprite_batch.js +15 -0
- package/src/phaser/display/sprite_util.js +248 -0
- package/src/phaser/display/text.js +1089 -0
- package/src/phaser/display/webgl/abstract_filter.js +25 -0
- package/src/phaser/display/webgl/base_texture.js +68 -0
- package/src/phaser/display/webgl/blend_manager.js +35 -0
- package/src/phaser/display/webgl/earcut.js +647 -0
- package/src/phaser/display/webgl/earcut_node.js +28 -0
- package/src/phaser/display/webgl/fast_sprite_batch.js +242 -0
- package/src/phaser/display/webgl/filter_manager.js +46 -0
- package/src/phaser/display/webgl/filter_texture.js +61 -0
- package/src/phaser/display/webgl/graphics.js +618 -0
- package/src/phaser/display/webgl/graphics_data.js +42 -0
- package/src/phaser/display/webgl/mask_manager.js +36 -0
- package/src/phaser/display/webgl/render_texture.js +81 -0
- package/src/phaser/display/webgl/renderer.js +234 -0
- package/src/phaser/display/webgl/shader/complex.js +74 -0
- package/src/phaser/display/webgl/shader/fast.js +97 -0
- package/src/phaser/display/webgl/shader/normal.js +225 -0
- package/src/phaser/display/webgl/shader/primitive.js +72 -0
- package/src/phaser/display/webgl/shader/strip.js +77 -0
- package/src/phaser/display/webgl/shader_manager.js +89 -0
- package/src/phaser/display/webgl/sprite_batch.js +320 -0
- package/src/phaser/display/webgl/stencil_manager.js +170 -0
- package/src/phaser/display/webgl/texture.js +117 -0
- package/src/phaser/display/webgl/texture_util.js +32 -0
- package/src/phaser/display/webgl/util.js +74 -0
- package/src/phaser/geom/circle.js +186 -0
- package/src/phaser/geom/ellipse.js +65 -0
- package/src/phaser/geom/line.js +190 -0
- package/src/phaser/geom/matrix.js +147 -0
- package/src/phaser/geom/point.js +164 -0
- package/src/phaser/geom/polygon.js +141 -0
- package/src/phaser/geom/rectangle.js +306 -0
- package/src/phaser/geom/rounded_rectangle.js +36 -0
- package/src/phaser/geom/util/circle.js +115 -0
- package/src/phaser/geom/util/ellipse.js +30 -0
- package/src/phaser/geom/util/line.js +130 -0
- package/src/phaser/geom/util/matrix.js +48 -0
- package/src/phaser/geom/util/point.js +276 -0
- package/src/phaser/geom/util/polygon.js +24 -0
- package/src/phaser/geom/util/rectangle.js +212 -0
- package/src/phaser/geom/util/rounded_rectangle.js +28 -0
- package/src/phaser/util/math.js +279 -0
- package/src/phaser/util/string.js +26 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
5
|
+
*/
|
|
6
|
+
import Signal from './signal';
|
|
7
|
+
|
|
8
|
+
export default class {
|
|
9
|
+
|
|
10
|
+
constructor(game, key, volume = 1, loop = false, connect = null) {
|
|
11
|
+
if (!connect) {
|
|
12
|
+
connect = game.sound.connectToMaster;
|
|
13
|
+
}
|
|
14
|
+
this.game = game;
|
|
15
|
+
this.name = key;
|
|
16
|
+
this.key = key;
|
|
17
|
+
this.loop = loop;
|
|
18
|
+
this.markers = {};
|
|
19
|
+
this.context = null;
|
|
20
|
+
this.autoplay = false;
|
|
21
|
+
this.totalDuration = 0;
|
|
22
|
+
this.startTime = 0;
|
|
23
|
+
this.currentTime = 0;
|
|
24
|
+
this.duration = 0;
|
|
25
|
+
this.durationMS = 0;
|
|
26
|
+
this.position = 0;
|
|
27
|
+
this.stopTime = 0;
|
|
28
|
+
this.paused = false;
|
|
29
|
+
this.pausedPosition = 0;
|
|
30
|
+
this.pausedTime = 0;
|
|
31
|
+
this.isPlaying = false;
|
|
32
|
+
this.currentMarker = '';
|
|
33
|
+
this.fadeTween = null;
|
|
34
|
+
this.pendingPlayback = false;
|
|
35
|
+
this.override = false;
|
|
36
|
+
this.allowMultiple = false;
|
|
37
|
+
this.usingWebAudio = this.game.sound.usingWebAudio;
|
|
38
|
+
this.usingAudioTag = this.game.sound.usingAudioTag;
|
|
39
|
+
this.externalNode = null;
|
|
40
|
+
this.masterGainNode = null;
|
|
41
|
+
this.gainNode = null;
|
|
42
|
+
this._sound = null;
|
|
43
|
+
this._markedToDelete = false;
|
|
44
|
+
this._removeFromSoundManager = false;
|
|
45
|
+
if (this.usingWebAudio) {
|
|
46
|
+
this.context = this.game.sound.context;
|
|
47
|
+
this.masterGainNode = this.game.sound.masterGain;
|
|
48
|
+
if (this.context.createGain === undefined) {
|
|
49
|
+
this.gainNode = this.context.createGainNode();
|
|
50
|
+
} else {
|
|
51
|
+
this.gainNode = this.context.createGain();
|
|
52
|
+
}
|
|
53
|
+
this.gainNode.gain.value = volume * this.game.sound.volume;
|
|
54
|
+
if (connect) {
|
|
55
|
+
this.gainNode.connect(this.masterGainNode);
|
|
56
|
+
}
|
|
57
|
+
} else if (this.usingAudioTag) {
|
|
58
|
+
if (this.game.cache.getSound(key) && this.game.cache.isSoundReady(key)) {
|
|
59
|
+
this._sound = this.game.cache.getSoundData(key);
|
|
60
|
+
this.totalDuration = 0;
|
|
61
|
+
if (this._sound.duration) {
|
|
62
|
+
this.totalDuration = this._sound.duration;
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
this.game.cache.onSoundUnlock.add(this.soundHasUnlocked, this);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
this.onDecoded = new Signal();
|
|
69
|
+
this.onPlay = new Signal();
|
|
70
|
+
this.onPause = new Signal();
|
|
71
|
+
this.onResume = new Signal();
|
|
72
|
+
this.onLoop = new Signal();
|
|
73
|
+
this.onStop = new Signal();
|
|
74
|
+
this.onMute = new Signal();
|
|
75
|
+
this.onMarkerComplete = new Signal();
|
|
76
|
+
this.onFadeComplete = new Signal();
|
|
77
|
+
this._volume = volume;
|
|
78
|
+
this._buffer = null;
|
|
79
|
+
this._muted = false;
|
|
80
|
+
this._tempMarker = 0;
|
|
81
|
+
this._tempPosition = 0;
|
|
82
|
+
this._tempVolume = 0;
|
|
83
|
+
this._tempPause = 0;
|
|
84
|
+
this._muteVolume = 0;
|
|
85
|
+
this._tempLoop = 0;
|
|
86
|
+
this._paused = false;
|
|
87
|
+
this._onDecodedEventDispatched = false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
soundHasUnlocked(key) {
|
|
91
|
+
if (key === this.key) {
|
|
92
|
+
this._sound = this.game.cache.getSoundData(this.key);
|
|
93
|
+
this.totalDuration = this._sound.duration;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
addMarker(name, start, duration = 1, volume = 1, loop = false) {
|
|
98
|
+
this.markers[name] = {
|
|
99
|
+
name,
|
|
100
|
+
start,
|
|
101
|
+
stop: start + duration,
|
|
102
|
+
volume,
|
|
103
|
+
duration,
|
|
104
|
+
durationMS: duration * 1000,
|
|
105
|
+
loop,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
removeMarker(name) {
|
|
110
|
+
delete this.markers[name];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
onEndedHandler() {
|
|
114
|
+
this._sound.onended = null;
|
|
115
|
+
this.isPlaying = false;
|
|
116
|
+
this.currentTime = this.durationMS;
|
|
117
|
+
this.stop();
|
|
118
|
+
if (this._markedToDelete) {
|
|
119
|
+
if (this.externalNode) {
|
|
120
|
+
this._sound.disconnect(this.externalNode);
|
|
121
|
+
} else if (this.gainNode) {
|
|
122
|
+
this._sound.disconnect(this.gainNode);
|
|
123
|
+
}
|
|
124
|
+
if (this._removeFromSoundManager) {
|
|
125
|
+
this.game.sound.remove(this);
|
|
126
|
+
} else {
|
|
127
|
+
this.markers = {};
|
|
128
|
+
this.context = null;
|
|
129
|
+
this._buffer = null;
|
|
130
|
+
this.externalNode = null;
|
|
131
|
+
this.onDecoded.dispose();
|
|
132
|
+
this.onPlay.dispose();
|
|
133
|
+
this.onPause.dispose();
|
|
134
|
+
this.onResume.dispose();
|
|
135
|
+
this.onLoop.dispose();
|
|
136
|
+
this.onStop.dispose();
|
|
137
|
+
this.onMute.dispose();
|
|
138
|
+
this.onMarkerComplete.dispose();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
update() {
|
|
144
|
+
if (!this.game.cache.checkSoundKey(this.key)) {
|
|
145
|
+
this.destroy();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (this.isDecoded && !this._onDecodedEventDispatched) {
|
|
149
|
+
this.onDecoded.dispatch(this);
|
|
150
|
+
this._onDecodedEventDispatched = true;
|
|
151
|
+
}
|
|
152
|
+
if (this.pendingPlayback && this.game.cache.isSoundReady(this.key)) {
|
|
153
|
+
this.pendingPlayback = false;
|
|
154
|
+
this.play(this._tempMarker, this._tempPosition, this._tempVolume, this._tempLoop);
|
|
155
|
+
}
|
|
156
|
+
if (!this.isPlaying) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.currentTime = this.game.time.time - this.startTime;
|
|
160
|
+
if (this.currentTime >= this.durationMS) {
|
|
161
|
+
if (this.usingWebAudio) {
|
|
162
|
+
if (this.loop) {
|
|
163
|
+
// won't work with markers, needs to reset the position
|
|
164
|
+
this.onLoop.dispatch(this);
|
|
165
|
+
// Gets reset by the play function
|
|
166
|
+
this.isPlaying = false;
|
|
167
|
+
if (this.currentMarker === '') {
|
|
168
|
+
this.currentTime = 0;
|
|
169
|
+
this.startTime = this.game.time.time;
|
|
170
|
+
this.isPlaying = true; // play not called again in this case
|
|
171
|
+
} else {
|
|
172
|
+
this.onMarkerComplete.dispatch(this.currentMarker, this);
|
|
173
|
+
this.play(this.currentMarker, 0, this.volume, true, true);
|
|
174
|
+
}
|
|
175
|
+
} else if (this.currentMarker !== '') {
|
|
176
|
+
// Stop if we're using an audio marker, otherwise we let onended handle it
|
|
177
|
+
this.stop();
|
|
178
|
+
}
|
|
179
|
+
} else if (this.loop) {
|
|
180
|
+
this.onLoop.dispatch(this);
|
|
181
|
+
if (this.currentMarker === '') {
|
|
182
|
+
this.currentTime = 0;
|
|
183
|
+
this.startTime = this.game.time.time;
|
|
184
|
+
}
|
|
185
|
+
// Gets reset by the play function
|
|
186
|
+
this.isPlaying = false;
|
|
187
|
+
this.play(this.currentMarker, 0, this.volume, true, true);
|
|
188
|
+
} else {
|
|
189
|
+
this.stop();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
loopFull(volume) {
|
|
195
|
+
return this.play(null, 0, volume, true);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
play(marker, position, volume, loop, forceRestart) {
|
|
199
|
+
if (marker === undefined || marker === false || marker === null) { marker = ''; }
|
|
200
|
+
if (forceRestart === undefined) { forceRestart = true; }
|
|
201
|
+
|
|
202
|
+
if (this.isPlaying && !this.allowMultiple && !forceRestart && !this.override) {
|
|
203
|
+
// Use Restart instead
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
if (this._sound && this.isPlaying && !this.allowMultiple && (this.override || forceRestart)) {
|
|
207
|
+
if (this.usingWebAudio) {
|
|
208
|
+
if (this._sound.stop === undefined) {
|
|
209
|
+
this._sound.noteOff(0);
|
|
210
|
+
} else {
|
|
211
|
+
try {
|
|
212
|
+
this._sound.stop(0);
|
|
213
|
+
} catch (e) {
|
|
214
|
+
// pass
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (this.externalNode) {
|
|
218
|
+
this._sound.disconnect(this.externalNode);
|
|
219
|
+
} else if (this.gainNode) {
|
|
220
|
+
this._sound.disconnect(this.gainNode);
|
|
221
|
+
}
|
|
222
|
+
} else if (this.usingAudioTag) {
|
|
223
|
+
this._sound.pause();
|
|
224
|
+
this._sound.currentTime = 0;
|
|
225
|
+
}
|
|
226
|
+
this.isPlaying = false;
|
|
227
|
+
}
|
|
228
|
+
if (marker === '' && Object.keys(this.markers).length > 0) {
|
|
229
|
+
// If they didn't specify a marker but this is an audio sprite,
|
|
230
|
+
// we should never play the entire thing
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
if (marker !== '') {
|
|
234
|
+
if (this.markers[marker]) {
|
|
235
|
+
this.currentMarker = marker;
|
|
236
|
+
// Playing a marker? Then we default to the marker values
|
|
237
|
+
this.position = this.markers[marker].start;
|
|
238
|
+
this.volume = this.markers[marker].volume;
|
|
239
|
+
this.loop = this.markers[marker].loop;
|
|
240
|
+
this.duration = this.markers[marker].duration;
|
|
241
|
+
this.durationMS = this.markers[marker].durationMS;
|
|
242
|
+
if (typeof volume !== 'undefined') {
|
|
243
|
+
this.volume = volume;
|
|
244
|
+
}
|
|
245
|
+
if (typeof loop !== 'undefined') {
|
|
246
|
+
this.loop = loop;
|
|
247
|
+
}
|
|
248
|
+
this._tempMarker = marker;
|
|
249
|
+
this._tempPosition = this.position;
|
|
250
|
+
this._tempVolume = this.volume;
|
|
251
|
+
this._tempLoop = this.loop;
|
|
252
|
+
} else {
|
|
253
|
+
console.warn('Sound.play: audio marker ' + marker + ' does not exist');
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
position = position || 0;
|
|
258
|
+
if (volume === undefined) { volume = this._volume; }
|
|
259
|
+
if (loop === undefined) { loop = this.loop; }
|
|
260
|
+
this.position = Math.max(0, position);
|
|
261
|
+
this.volume = volume;
|
|
262
|
+
this.loop = loop;
|
|
263
|
+
this.duration = 0;
|
|
264
|
+
this.durationMS = 0;
|
|
265
|
+
this._tempMarker = marker;
|
|
266
|
+
this._tempPosition = position;
|
|
267
|
+
this._tempVolume = volume;
|
|
268
|
+
this._tempLoop = loop;
|
|
269
|
+
}
|
|
270
|
+
if (this.usingWebAudio) {
|
|
271
|
+
// Does the sound need decoding?
|
|
272
|
+
if (this.game.cache.isSoundDecoded(this.key)) {
|
|
273
|
+
this._sound = this.context.createBufferSource();
|
|
274
|
+
if (this.externalNode) {
|
|
275
|
+
this._sound.connect(this.externalNode);
|
|
276
|
+
} else {
|
|
277
|
+
this._sound.connect(this.gainNode);
|
|
278
|
+
}
|
|
279
|
+
this._buffer = this.game.cache.getSoundData(this.key);
|
|
280
|
+
this._sound.buffer = this._buffer;
|
|
281
|
+
if (this.loop && marker === '') {
|
|
282
|
+
this._sound.loop = true;
|
|
283
|
+
}
|
|
284
|
+
if (!this.loop && marker === '') {
|
|
285
|
+
this._sound.onended = this.onEndedHandler.bind(this);
|
|
286
|
+
}
|
|
287
|
+
this.totalDuration = this._sound.buffer.duration;
|
|
288
|
+
if (this.duration === 0) {
|
|
289
|
+
this.duration = this.totalDuration;
|
|
290
|
+
this.durationMS = Math.ceil(this.totalDuration * 1000);
|
|
291
|
+
}
|
|
292
|
+
// Useful to cache this somewhere perhaps?
|
|
293
|
+
if (this._sound.start === undefined) {
|
|
294
|
+
this._sound.noteGrainOn(0, this.position, this.duration);
|
|
295
|
+
} else if (this.loop && marker === '') {
|
|
296
|
+
this._sound.start(0, 0);
|
|
297
|
+
} else {
|
|
298
|
+
this._sound.start(0, this.position, this.duration);
|
|
299
|
+
}
|
|
300
|
+
this.isPlaying = true;
|
|
301
|
+
this.startTime = this.game.time.time;
|
|
302
|
+
this.currentTime = 0;
|
|
303
|
+
this.stopTime = this.startTime + this.durationMS;
|
|
304
|
+
this.onPlay.dispatch(this);
|
|
305
|
+
} else {
|
|
306
|
+
this.pendingPlayback = true;
|
|
307
|
+
if (this.game.cache.getSound(this.key) && this.game.cache.getSound(this.key).isDecoding === false) {
|
|
308
|
+
this.game.sound.decode(this.key, this);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
} else if (this.game.cache.getSound(this.key) && this.game.cache.getSound(this.key).locked) {
|
|
312
|
+
this.game.cache.reloadSound(this.key);
|
|
313
|
+
this.pendingPlayback = true;
|
|
314
|
+
} else if (this._sound && (this.game.device.cocoonJS || this._sound.readyState === 4)) {
|
|
315
|
+
this._sound.play();
|
|
316
|
+
// This doesn't become available until you call play(), wonderful ...
|
|
317
|
+
this.totalDuration = this._sound.duration;
|
|
318
|
+
if (this.duration === 0) {
|
|
319
|
+
this.duration = this.totalDuration;
|
|
320
|
+
this.durationMS = this.totalDuration * 1000;
|
|
321
|
+
}
|
|
322
|
+
this._sound.currentTime = this.position;
|
|
323
|
+
this._sound.muted = this._muted;
|
|
324
|
+
if (this._muted || this.game.sound.mute) {
|
|
325
|
+
this._sound.volume = 0;
|
|
326
|
+
} else {
|
|
327
|
+
this._sound.volume = this._volume;
|
|
328
|
+
}
|
|
329
|
+
this.isPlaying = true;
|
|
330
|
+
this.startTime = this.game.time.time;
|
|
331
|
+
this.currentTime = 0;
|
|
332
|
+
this.stopTime = this.startTime + this.durationMS;
|
|
333
|
+
this.onPlay.dispatch(this);
|
|
334
|
+
} else {
|
|
335
|
+
this.pendingPlayback = true;
|
|
336
|
+
}
|
|
337
|
+
return this;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
restart(marker = '', position = 0, volume = 1, loop = false) {
|
|
341
|
+
this.play(marker, position, volume, loop, true);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
pause() {
|
|
345
|
+
if (this.isPlaying && this._sound) {
|
|
346
|
+
this.paused = true;
|
|
347
|
+
this.pausedPosition = this.currentTime;
|
|
348
|
+
this.pausedTime = this.game.time.time;
|
|
349
|
+
this._tempPause = this._sound.currentTime;
|
|
350
|
+
this.onPause.dispatch(this);
|
|
351
|
+
this.stop();
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
resume() {
|
|
356
|
+
if (this.paused && this._sound) {
|
|
357
|
+
if (this.usingWebAudio) {
|
|
358
|
+
const p = Math.max(0, this.position + (this.pausedPosition / 1000));
|
|
359
|
+
this._sound = this.context.createBufferSource();
|
|
360
|
+
this._sound.buffer = this._buffer;
|
|
361
|
+
if (this.externalNode) {
|
|
362
|
+
this._sound.connect(this.externalNode);
|
|
363
|
+
} else {
|
|
364
|
+
this._sound.connect(this.gainNode);
|
|
365
|
+
}
|
|
366
|
+
if (this.loop) {
|
|
367
|
+
this._sound.loop = true;
|
|
368
|
+
}
|
|
369
|
+
if (!this.loop && this.currentMarker === '') {
|
|
370
|
+
this._sound.onended = this.onEndedHandler.bind(this);
|
|
371
|
+
}
|
|
372
|
+
const duration = this.duration - (this.pausedPosition / 1000);
|
|
373
|
+
if (this._sound.start === undefined) {
|
|
374
|
+
this._sound.noteGrainOn(0, p, duration);
|
|
375
|
+
// this._sound.noteOn(0); // the zero is vitally important, crashes iOS6 without it
|
|
376
|
+
} else if (this.loop && this.game.device.chrome) {
|
|
377
|
+
// Handle chrome bug: https://code.google.com/p/chromium/issues/detail?id=457099
|
|
378
|
+
if (this.game.device.chromeVersion === 42) {
|
|
379
|
+
this._sound.start(0);
|
|
380
|
+
} else {
|
|
381
|
+
this._sound.start(0, p);
|
|
382
|
+
}
|
|
383
|
+
} else {
|
|
384
|
+
this._sound.start(0, p, duration);
|
|
385
|
+
}
|
|
386
|
+
} else {
|
|
387
|
+
this._sound.currentTime = this._tempPause;
|
|
388
|
+
this._sound.play();
|
|
389
|
+
}
|
|
390
|
+
this.isPlaying = true;
|
|
391
|
+
this.paused = false;
|
|
392
|
+
this.startTime += (this.game.time.time - this.pausedTime);
|
|
393
|
+
this.onResume.dispatch(this);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
stop() {
|
|
398
|
+
if (this.isPlaying && this._sound) {
|
|
399
|
+
if (this.usingWebAudio) {
|
|
400
|
+
if (this._sound.stop === undefined) {
|
|
401
|
+
this._sound.noteOff(0);
|
|
402
|
+
} else {
|
|
403
|
+
try {
|
|
404
|
+
this._sound.stop(0);
|
|
405
|
+
} catch (e) {
|
|
406
|
+
// Thanks Android 4.4
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (this.externalNode) {
|
|
410
|
+
this._sound.disconnect(this.externalNode);
|
|
411
|
+
} else if (this.gainNode) {
|
|
412
|
+
this._sound.disconnect(this.gainNode);
|
|
413
|
+
}
|
|
414
|
+
} else if (this.usingAudioTag) {
|
|
415
|
+
this._sound.pause();
|
|
416
|
+
this._sound.currentTime = 0;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
this.pendingPlayback = false;
|
|
420
|
+
this.isPlaying = false;
|
|
421
|
+
if (!this.paused) {
|
|
422
|
+
const prevMarker = this.currentMarker;
|
|
423
|
+
if (this.currentMarker !== '') {
|
|
424
|
+
this.onMarkerComplete.dispatch(this.currentMarker, this);
|
|
425
|
+
}
|
|
426
|
+
this.currentMarker = '';
|
|
427
|
+
if (this.fadeTween !== null) {
|
|
428
|
+
this.fadeTween.stop();
|
|
429
|
+
}
|
|
430
|
+
this.onStop.dispatch(this, prevMarker);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
fadeIn(duration, loop = false, marker = this.currentMarker) {
|
|
435
|
+
if (this.paused) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
this.play(marker, 0, 0, loop);
|
|
439
|
+
this.fadeTo(duration, 1);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
fadeOut(duration) {
|
|
443
|
+
this.fadeTo(duration, 0);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
fadeTo(duration = 100, volume = 0) {
|
|
447
|
+
if (!this.isPlaying || this.paused || volume === this.volume) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
this.fadeTween = this.game.tweens.create(this).to({ volume }, duration, 'Linear', true);
|
|
451
|
+
this.fadeTween.onComplete.add(this.fadeComplete, this);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
fadeComplete() {
|
|
455
|
+
this.onFadeComplete.dispatch(this, this.volume);
|
|
456
|
+
if (this.volume === 0) {
|
|
457
|
+
this.stop();
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
updateGlobalVolume(globalVolume) {
|
|
462
|
+
// this._volume is the % of the global volume this sound should be played at
|
|
463
|
+
if (this.usingAudioTag && this._sound) {
|
|
464
|
+
this._sound.volume = globalVolume * this._volume;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
destroy(remove = true) {
|
|
470
|
+
this._markedToDelete = true;
|
|
471
|
+
this._removeFromSoundManager = remove;
|
|
472
|
+
this.stop();
|
|
473
|
+
if (remove) {
|
|
474
|
+
this.game.sound.remove(this);
|
|
475
|
+
} else {
|
|
476
|
+
this.markers = {};
|
|
477
|
+
this.context = null;
|
|
478
|
+
this._buffer = null;
|
|
479
|
+
this.externalNode = null;
|
|
480
|
+
this.onDecoded.dispose();
|
|
481
|
+
this.onPlay.dispose();
|
|
482
|
+
this.onPause.dispose();
|
|
483
|
+
this.onResume.dispose();
|
|
484
|
+
this.onLoop.dispose();
|
|
485
|
+
this.onStop.dispose();
|
|
486
|
+
this.onMute.dispose();
|
|
487
|
+
this.onMarkerComplete.dispose();
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
get mute() {
|
|
492
|
+
return (this._muted || this.game.sound.mute);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
set mute(value) {
|
|
496
|
+
value = value || false;
|
|
497
|
+
if (value === this._muted) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (value) {
|
|
501
|
+
this._muted = true;
|
|
502
|
+
this._muteVolume = this._tempVolume;
|
|
503
|
+
if (this.usingWebAudio) {
|
|
504
|
+
this.gainNode.gain.value = 0;
|
|
505
|
+
} else if (this.usingAudioTag && this._sound) {
|
|
506
|
+
this._sound.volume = 0;
|
|
507
|
+
}
|
|
508
|
+
} else {
|
|
509
|
+
this._muted = false;
|
|
510
|
+
if (this.usingWebAudio) {
|
|
511
|
+
this.gainNode.gain.value = this._muteVolume;
|
|
512
|
+
} else if (this.usingAudioTag && this._sound) {
|
|
513
|
+
this._sound.volume = this._muteVolume;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
this.onMute.dispatch(this);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
get volume() {
|
|
520
|
+
return this._volume;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
set volume(value) {
|
|
524
|
+
const normalizedValue = this.usingAudioTag ? Math.max(0, Math.min(1, value)) : value;
|
|
525
|
+
if (this._muted) {
|
|
526
|
+
this._muteVolume = normalizedValue;
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
this._tempVolume = normalizedValue;
|
|
530
|
+
this._volume = normalizedValue;
|
|
531
|
+
if (this.usingWebAudio) {
|
|
532
|
+
this.gainNode.gain.value = normalizedValue;
|
|
533
|
+
} else if (this.usingAudioTag && this._sound) {
|
|
534
|
+
this._sound.volume = normalizedValue;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
}
|