Skip to content

Commit bd72f1c

Browse files
committed
Merge branch 'develop'
2 parents 58e3560 + 0f2d39f commit bd72f1c

File tree

14 files changed

+427
-100
lines changed

14 files changed

+427
-100
lines changed

src/engine/audio.js

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@ game.createClass('Audio', {
2222
**/
2323
music: null,
2424
/**
25-
Is audio muted.
25+
Is all sounds and music muted.
2626
@property {Boolean} muted
2727
@default false
2828
**/
2929
muted: false,
30+
/**
31+
Is music muted.
32+
@property {Boolean} mutedMusic
33+
@default false
34+
**/
35+
mutedMusic: false,
36+
/**
37+
Is all sounds muted.
38+
@property {Boolean} mutedSound
39+
@default false
40+
**/
41+
mutedSound: false,
3042
/**
3143
Currently playing sounds.
3244
@property {Array} sounds
@@ -97,22 +109,45 @@ game.createClass('Audio', {
97109
},
98110

99111
/**
100-
Mute all audio.
112+
Mute all sounds and music.
101113
@method mute
102114
**/
103115
mute: function() {
104116
if (!this._mainGain) return;
105117
this._mainGain.gain.setValueAtTime(0, this._context.currentTime);
106118
this.muted = true;
107119
},
120+
121+
/**
122+
Mute music.
123+
@method muteMusic
124+
**/
125+
muteMusic: function() {
126+
if (!this._musicGain) return;
127+
this._musicGain.gain.setValueAtTime(0, this._context.currentTime);
128+
this.mutedMusic = true;
129+
},
130+
131+
/**
132+
Mute all sounds.
133+
@method muteSound
134+
**/
135+
muteSound: function() {
136+
if (!this._soundGain) return;
137+
this._soundGain.gain.setValueAtTime(0, this._context.currentTime);
138+
this.mutedSound = true;
139+
},
108140

109141
/**
110142
@method playMusic
111143
@param {String} name
144+
@param {Boolean} [noLoop] Do not loop the music
112145
@return {Music}
113146
**/
114-
playMusic: function(name) {
115-
var music = new game.Music(name).play();
147+
playMusic: function(name, noLoop) {
148+
var music = new game.Music(name);
149+
if (noLoop) music.loop = false;
150+
music.play();
116151
return music;
117152
},
118153

@@ -170,6 +205,26 @@ game.createClass('Audio', {
170205
this._mainGain.gain.setValueAtTime(1, this._context.currentTime);
171206
this.muted = false;
172207
},
208+
209+
/**
210+
Unmute music.
211+
@method muteMusic
212+
**/
213+
unmuteMusic: function() {
214+
if (!this._musicGain) return;
215+
this._musicGain.gain.setValueAtTime(game.Audio.musicVolume, this._context.currentTime);
216+
this.mutedMusic = false;
217+
},
218+
219+
/**
220+
Unmute all sounds.
221+
@method muteSound
222+
**/
223+
unmuteSound: function() {
224+
if (!this._soundGain) return;
225+
this._soundGain.gain.setValueAtTime(game.Audio.soundVolume, this._context.currentTime);
226+
this.mutedSound = false;
227+
},
173228

174229
/**
175230
@method _decode
@@ -302,6 +357,7 @@ game.addAttributes('Audio', {
302357
formats: [
303358
{ ext: 'ogg', type: 'audio/ogg; codecs="vorbis"' },
304359
{ ext: 'm4a', type: 'audio/mp4; codecs="mp4a.40.5"' },
360+
{ ext: 'mp3', type: 'audio/mp3' },
305361
{ ext: 'wav', type: 'audio/wav' }
306362
],
307363

@@ -421,7 +477,6 @@ game.createClass('Sound', {
421477

422478
init: function() {
423479
if (this._gainNode) this._gainNode.connect(game.audio._soundGain);
424-
this.volume = game.Audio.soundVolume;
425480
},
426481

427482
/**
@@ -552,11 +607,10 @@ game.createClass('Sound', {
552607
**/
553608
_fade: function(time, to) {
554609
if (!this._buffer) return;
555-
time = (time || 1000) / 1000;
610+
time = (time || 1000) / 1000;
556611

557612
var currTime = this._context.currentTime;
558-
if (to === this.volume) this._gainNode.gain.setValueAtTime(0, this._context.currentTime);;
559-
var from = this._gainNode.gain.value;
613+
var from = to === this.volume ? 0 : this._gainNode.gain.value;
560614

561615
this._gainNode.gain.linearRampToValueAtTime(from, currTime);
562616
this._gainNode.gain.linearRampToValueAtTime(to, currTime + time);
@@ -583,6 +637,16 @@ game.createClass('Sound', {
583637
});
584638

585639
game.defineProperties('Sound', {
640+
/**
641+
Duration of audio (seconds).
642+
@property {Number} duration
643+
**/
644+
duration: {
645+
get: function() {
646+
if (this._buffer) return this._buffer.duration;
647+
}
648+
},
649+
586650
/**
587651
Playback rate of audio (speed).
588652
@property {Number} rate
@@ -602,7 +666,7 @@ game.defineProperties('Sound', {
602666
/**
603667
Sound volume (0-1).
604668
@property {Number} volume
605-
@default game.Audio.soundVolume
669+
@default 1
606670
**/
607671
volume: {
608672
get: function() {
@@ -631,7 +695,6 @@ game.createClass('Music', 'Sound', {
631695

632696
init: function() {
633697
if (this._gainNode) this._gainNode.connect(game.audio._musicGain);
634-
this.volume = game.Audio.musicVolume;
635698
},
636699

637700
_onStart: function() {

src/engine/core.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var game = {
9999
Engine version.
100100
@property {String} version
101101
**/
102-
version: '2.9.0',
102+
version: '2.9.1dev',
103103
/**
104104
@property {Boolean} _booted
105105
@private
@@ -590,6 +590,19 @@ var game = {
590590
this.isStarted = true;
591591
if (!this.system._rotateScreenVisible) this.onStart();
592592
},
593+
594+
/**
595+
Stop engine completely.
596+
@method start
597+
@param {Boolean} removeCanvas Remove canvas
598+
**/
599+
stop: function(removeCanvas) {
600+
this.system._stopRunLoop();
601+
if (this.input) this.input._remove();
602+
if (this.keyboard) this.keyboard._remove();
603+
if (this.system) this.system._remove();
604+
if (this.renderer && removeCanvas) this.renderer.canvas.parentElement.removeChild(this.renderer.canvas);
605+
},
593606

594607
/**
595608
@method _boot
@@ -1060,7 +1073,7 @@ var game = {
10601073
this._gameLoops[id] = true;
10611074

10621075
var animate = function() {
1063-
if (!game._gameLoops[id]) return;
1076+
if (!game || !game._gameLoops[id]) return;
10641077
window.requestAnimationFrame(animate);
10651078
callback();
10661079
};

src/engine/debug.js

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ game.createClass('Debug', {
6666
@private
6767
**/
6868
_frames: 0,
69+
/**
70+
@property {Array} _hitAreas
71+
@private
72+
**/
73+
_hitAreas: [],
6974

7075
init: function() {
7176
if (game.Debug.showInfo) {
@@ -131,6 +136,12 @@ game.createClass('Debug', {
131136
_renderCachedSprite: function(context) {
132137
this.super(context);
133138
game.debug._draws++;
139+
},
140+
141+
_renderCanvas: function(context) {
142+
if (game.scene && game.scene.stage === this) return;
143+
if (game.Debug.showBounds) game.debug._drawBounds(this);
144+
if (game.Debug.showHitAreas && this.hitArea) game.debug._hitAreas.push(this);
134145
}
135146
});
136147

@@ -168,6 +179,7 @@ game.createClass('Debug', {
168179
_renderCanvas: function(context) {
169180
this.super(context);
170181
game.debug._draws += this.shapes.length;
182+
if (game.Debug.showBounds) game.debug._drawBounds(this);
171183
}
172184
});
173185

@@ -176,6 +188,8 @@ game.createClass('Debug', {
176188
this.super(context, transform, rect, offset);
177189
game.debug._draws++;
178190
if (game.Debug.showSprites) game.debug._drawSprite(this, transform, rect, offset);
191+
if (game.Debug.showBounds) game.debug._drawBounds(this);
192+
if (game.Debug.showHitAreas && this.hitArea) game.debug._hitAreas.push(this);
179193
}
180194
});
181195

@@ -272,6 +286,25 @@ game.createClass('Debug', {
272286
context.stroke();
273287
}
274288
},
289+
290+
/**
291+
@method _drawBounds
292+
@param {Container} container
293+
@private
294+
**/
295+
_drawBounds: function(container) {
296+
var context = game.renderer.context;
297+
var bounds = container._getBounds();
298+
299+
context.globalCompositeOperation = 'source-over';
300+
context.setTransform(1, 0, 0, 1, 0, 0);
301+
context.globalAlpha = game.Debug.boundAlpha;
302+
context.lineWidth = game.Debug.boundLineWidth;
303+
context.strokeStyle = game.Debug.boundColor;
304+
context.beginPath();
305+
context.rect(bounds.x, bounds.y, bounds.width, bounds.height);
306+
context.stroke();
307+
},
275308

276309
/**
277310
@method _drawCamera
@@ -319,7 +352,7 @@ game.createClass('Debug', {
319352
@param {Container} container
320353
**/
321354
_drawHitArea: function(container) {
322-
if (!container.visible || container.alpha <= 0 || !container.renderable) return;
355+
if (!container.visible || container.alpha <= 0 || !container.renderable) return;
323356

324357
var context = game.renderer.context;
325358
var wt = container._worldTransform;
@@ -336,20 +369,20 @@ game.createClass('Debug', {
336369
if (hitArea) {
337370
var wt = container._worldTransform;
338371
var bounds = container._getBounds();
339-
var tx = (bounds.x || wt.tx);
340-
var ty = (bounds.y || wt.ty);
372+
var tx = typeof bounds.x === 'number' ? bounds.x : wt.tx;
373+
var ty = typeof bounds.y === 'number' ? bounds.y : wt.ty;
341374
var scaleX = Math.abs(wt.a / container._cosCache);
342375
var scaleY = Math.abs(wt.d / container._cosCache);
343-
var aPercX = (container.anchor.x / container.width) || 0;
344-
var aPercY = (container.anchor.y / container.height) || 0;
376+
var aPercX = (container.anchor.x / container.width) || 0;
377+
var aPercY = (container.anchor.y / container.height) || 0;
345378
var hx = tx + hitArea.x * scaleX;
346379
var hy = ty + hitArea.y * scaleY;
347380
hx += bounds.width * scaleX * aPercX;
348381
hy += bounds.height * scaleY * aPercY;
349382
if (hitArea.radius) {
350383
// Circle
351-
var r = hitArea.radius / 2 * game.scale;
352-
context.setTransform(1, 0, 0, 1, hx, hy);
384+
var r = hitArea.radius * game.scale;
385+
context.setTransform(1, 0, 0, 1, hx - r, hy - r);
353386
context.beginPath();
354387
context.arc(r, r, r, 0, Math.PI * 2);
355388
context.fill();
@@ -384,8 +417,8 @@ game.createClass('Debug', {
384417
@private
385418
**/
386419
_drawHitAreas: function() {
387-
for (var i = 0; i < game.input.items.length; i++) {
388-
var item = game.input.items[i];
420+
for (var i = 0; i < this._hitAreas.length; i++) {
421+
var item = this._hitAreas[i];
389422
this._drawHitArea(item);
390423
}
391424
},
@@ -428,9 +461,9 @@ game.createClass('Debug', {
428461

429462
context.globalCompositeOperation = 'source-over';
430463
context.setTransform(wt.a, wt.b, wt.c, wt.d, x, y);
431-
context.globalAlpha = game.Debug.boundAlpha;
432-
context.lineWidth = game.Debug.boundLineWidth;
433-
context.strokeStyle = game.Debug.boundColor;
464+
context.globalAlpha = game.Debug.spriteAlpha;
465+
context.lineWidth = game.Debug.spriteLineWidth;
466+
context.strokeStyle = game.Debug.spriteColor;
434467
context.beginPath();
435468
context.rect(tx, ty, width, height);
436469
context.stroke();
@@ -442,6 +475,7 @@ game.createClass('Debug', {
442475
**/
443476
_reset: function() {
444477
this._draws = 0;
478+
this._hitAreas.length = 0;
445479
},
446480

447481
/**
@@ -692,6 +726,12 @@ game.addAttributes('Debug', {
692726
@default false
693727
**/
694728
showBodies: false,
729+
/**
730+
Draw bounds of containers and graphics.
731+
@attribute {Boolean} showBounds
732+
@default false
733+
**/
734+
showBounds: false,
695735
/**
696736
Draw camera debug.
697737
@attribute {Boolean} showCamera
@@ -722,6 +762,24 @@ game.addAttributes('Debug', {
722762
@default false
723763
**/
724764
showSprites: false,
765+
/**
766+
Alpha of sprites.
767+
@attribute {Number} spriteAlpha
768+
@default 0.5
769+
**/
770+
spriteAlpha: 0.5,
771+
/**
772+
Color of sprites.
773+
@attribute {Number} spriteColor
774+
@default #00ff00
775+
**/
776+
spriteColor: '#00ff00',
777+
/**
778+
Bounds line width.
779+
@attribute {Number} spriteLineWidth
780+
@default 1
781+
**/
782+
spriteLineWidth: 1,
725783
/**
726784
Function that is called every time the debug panel is updated.
727785
@method updatePanel
@@ -806,9 +864,10 @@ var href = document.location.href.toLowerCase();
806864
if (href.match(/\?debug/)) game.Debug.enabled = true;
807865
if (href.match(/\?debugdraw/)) {
808866
game.Debug.showBodies = true;
809-
game.Debug.showSprites = true;
867+
game.Debug.showBounds = true;
810868
game.Debug.showCamera = true;
811869
game.Debug.showHitAreas = true;
870+
game.Debug.showSprites = true;
812871
}
813872
if (href.match(/\?debugtouch/)) {
814873
game.Debug.fakeTouch = true;

0 commit comments

Comments
 (0)