Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ekelokorpi committed Nov 28, 2018
2 parents 58e3560 + 0f2d39f commit bd72f1c
Show file tree
Hide file tree
Showing 14 changed files with 427 additions and 100 deletions.
83 changes: 73 additions & 10 deletions src/engine/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ game.createClass('Audio', {
**/
music: null,
/**
Is audio muted.
Is all sounds and music muted.
@property {Boolean} muted
@default false
**/
muted: false,
/**
Is music muted.
@property {Boolean} mutedMusic
@default false
**/
mutedMusic: false,
/**
Is all sounds muted.
@property {Boolean} mutedSound
@default false
**/
mutedSound: false,
/**
Currently playing sounds.
@property {Array} sounds
Expand Down Expand Up @@ -97,22 +109,45 @@ game.createClass('Audio', {
},

/**
Mute all audio.
Mute all sounds and music.
@method mute
**/
mute: function() {
if (!this._mainGain) return;
this._mainGain.gain.setValueAtTime(0, this._context.currentTime);
this.muted = true;
},

/**
Mute music.
@method muteMusic
**/
muteMusic: function() {
if (!this._musicGain) return;
this._musicGain.gain.setValueAtTime(0, this._context.currentTime);
this.mutedMusic = true;
},

/**
Mute all sounds.
@method muteSound
**/
muteSound: function() {
if (!this._soundGain) return;
this._soundGain.gain.setValueAtTime(0, this._context.currentTime);
this.mutedSound = true;
},

/**
@method playMusic
@param {String} name
@param {Boolean} [noLoop] Do not loop the music
@return {Music}
**/
playMusic: function(name) {
var music = new game.Music(name).play();
playMusic: function(name, noLoop) {
var music = new game.Music(name);
if (noLoop) music.loop = false;
music.play();
return music;
},

Expand Down Expand Up @@ -170,6 +205,26 @@ game.createClass('Audio', {
this._mainGain.gain.setValueAtTime(1, this._context.currentTime);
this.muted = false;
},

/**
Unmute music.
@method muteMusic
**/
unmuteMusic: function() {
if (!this._musicGain) return;
this._musicGain.gain.setValueAtTime(game.Audio.musicVolume, this._context.currentTime);
this.mutedMusic = false;
},

/**
Unmute all sounds.
@method muteSound
**/
unmuteSound: function() {
if (!this._soundGain) return;
this._soundGain.gain.setValueAtTime(game.Audio.soundVolume, this._context.currentTime);
this.mutedSound = false;
},

/**
@method _decode
Expand Down Expand Up @@ -302,6 +357,7 @@ game.addAttributes('Audio', {
formats: [
{ ext: 'ogg', type: 'audio/ogg; codecs="vorbis"' },
{ ext: 'm4a', type: 'audio/mp4; codecs="mp4a.40.5"' },
{ ext: 'mp3', type: 'audio/mp3' },
{ ext: 'wav', type: 'audio/wav' }
],

Expand Down Expand Up @@ -421,7 +477,6 @@ game.createClass('Sound', {

init: function() {
if (this._gainNode) this._gainNode.connect(game.audio._soundGain);
this.volume = game.Audio.soundVolume;
},

/**
Expand Down Expand Up @@ -552,11 +607,10 @@ game.createClass('Sound', {
**/
_fade: function(time, to) {
if (!this._buffer) return;
time = (time || 1000) / 1000;
time = (time || 1000) / 1000;

var currTime = this._context.currentTime;
if (to === this.volume) this._gainNode.gain.setValueAtTime(0, this._context.currentTime);;
var from = this._gainNode.gain.value;
var from = to === this.volume ? 0 : this._gainNode.gain.value;

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

game.defineProperties('Sound', {
/**
Duration of audio (seconds).
@property {Number} duration
**/
duration: {
get: function() {
if (this._buffer) return this._buffer.duration;
}
},

/**
Playback rate of audio (speed).
@property {Number} rate
Expand All @@ -602,7 +666,7 @@ game.defineProperties('Sound', {
/**
Sound volume (0-1).
@property {Number} volume
@default game.Audio.soundVolume
@default 1
**/
volume: {
get: function() {
Expand Down Expand Up @@ -631,7 +695,6 @@ game.createClass('Music', 'Sound', {

init: function() {
if (this._gainNode) this._gainNode.connect(game.audio._musicGain);
this.volume = game.Audio.musicVolume;
},

_onStart: function() {
Expand Down
17 changes: 15 additions & 2 deletions src/engine/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var game = {
Engine version.
@property {String} version
**/
version: '2.9.0',
version: '2.9.1dev',
/**
@property {Boolean} _booted
@private
Expand Down Expand Up @@ -590,6 +590,19 @@ var game = {
this.isStarted = true;
if (!this.system._rotateScreenVisible) this.onStart();
},

/**
Stop engine completely.
@method start
@param {Boolean} removeCanvas Remove canvas
**/
stop: function(removeCanvas) {
this.system._stopRunLoop();
if (this.input) this.input._remove();
if (this.keyboard) this.keyboard._remove();
if (this.system) this.system._remove();
if (this.renderer && removeCanvas) this.renderer.canvas.parentElement.removeChild(this.renderer.canvas);
},

/**
@method _boot
Expand Down Expand Up @@ -1060,7 +1073,7 @@ var game = {
this._gameLoops[id] = true;

var animate = function() {
if (!game._gameLoops[id]) return;
if (!game || !game._gameLoops[id]) return;
window.requestAnimationFrame(animate);
callback();
};
Expand Down
85 changes: 72 additions & 13 deletions src/engine/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ game.createClass('Debug', {
@private
**/
_frames: 0,
/**
@property {Array} _hitAreas
@private
**/
_hitAreas: [],

init: function() {
if (game.Debug.showInfo) {
Expand Down Expand Up @@ -131,6 +136,12 @@ game.createClass('Debug', {
_renderCachedSprite: function(context) {
this.super(context);
game.debug._draws++;
},

_renderCanvas: function(context) {
if (game.scene && game.scene.stage === this) return;
if (game.Debug.showBounds) game.debug._drawBounds(this);
if (game.Debug.showHitAreas && this.hitArea) game.debug._hitAreas.push(this);
}
});

Expand Down Expand Up @@ -168,6 +179,7 @@ game.createClass('Debug', {
_renderCanvas: function(context) {
this.super(context);
game.debug._draws += this.shapes.length;
if (game.Debug.showBounds) game.debug._drawBounds(this);
}
});

Expand All @@ -176,6 +188,8 @@ game.createClass('Debug', {
this.super(context, transform, rect, offset);
game.debug._draws++;
if (game.Debug.showSprites) game.debug._drawSprite(this, transform, rect, offset);
if (game.Debug.showBounds) game.debug._drawBounds(this);
if (game.Debug.showHitAreas && this.hitArea) game.debug._hitAreas.push(this);
}
});

Expand Down Expand Up @@ -272,6 +286,25 @@ game.createClass('Debug', {
context.stroke();
}
},

/**
@method _drawBounds
@param {Container} container
@private
**/
_drawBounds: function(container) {
var context = game.renderer.context;
var bounds = container._getBounds();

context.globalCompositeOperation = 'source-over';
context.setTransform(1, 0, 0, 1, 0, 0);
context.globalAlpha = game.Debug.boundAlpha;
context.lineWidth = game.Debug.boundLineWidth;
context.strokeStyle = game.Debug.boundColor;
context.beginPath();
context.rect(bounds.x, bounds.y, bounds.width, bounds.height);
context.stroke();
},

/**
@method _drawCamera
Expand Down Expand Up @@ -319,7 +352,7 @@ game.createClass('Debug', {
@param {Container} container
**/
_drawHitArea: function(container) {
if (!container.visible || container.alpha <= 0 || !container.renderable) return;
if (!container.visible || container.alpha <= 0 || !container.renderable) return;

var context = game.renderer.context;
var wt = container._worldTransform;
Expand All @@ -336,20 +369,20 @@ game.createClass('Debug', {
if (hitArea) {
var wt = container._worldTransform;
var bounds = container._getBounds();
var tx = (bounds.x || wt.tx);
var ty = (bounds.y || wt.ty);
var tx = typeof bounds.x === 'number' ? bounds.x : wt.tx;
var ty = typeof bounds.y === 'number' ? bounds.y : wt.ty;
var scaleX = Math.abs(wt.a / container._cosCache);
var scaleY = Math.abs(wt.d / container._cosCache);
var aPercX = (container.anchor.x / container.width) || 0;
var aPercY = (container.anchor.y / container.height) || 0;
var aPercX = (container.anchor.x / container.width) || 0;
var aPercY = (container.anchor.y / container.height) || 0;
var hx = tx + hitArea.x * scaleX;
var hy = ty + hitArea.y * scaleY;
hx += bounds.width * scaleX * aPercX;
hy += bounds.height * scaleY * aPercY;
if (hitArea.radius) {
// Circle
var r = hitArea.radius / 2 * game.scale;
context.setTransform(1, 0, 0, 1, hx, hy);
var r = hitArea.radius * game.scale;
context.setTransform(1, 0, 0, 1, hx - r, hy - r);
context.beginPath();
context.arc(r, r, r, 0, Math.PI * 2);
context.fill();
Expand Down Expand Up @@ -384,8 +417,8 @@ game.createClass('Debug', {
@private
**/
_drawHitAreas: function() {
for (var i = 0; i < game.input.items.length; i++) {
var item = game.input.items[i];
for (var i = 0; i < this._hitAreas.length; i++) {
var item = this._hitAreas[i];
this._drawHitArea(item);
}
},
Expand Down Expand Up @@ -428,9 +461,9 @@ game.createClass('Debug', {

context.globalCompositeOperation = 'source-over';
context.setTransform(wt.a, wt.b, wt.c, wt.d, x, y);
context.globalAlpha = game.Debug.boundAlpha;
context.lineWidth = game.Debug.boundLineWidth;
context.strokeStyle = game.Debug.boundColor;
context.globalAlpha = game.Debug.spriteAlpha;
context.lineWidth = game.Debug.spriteLineWidth;
context.strokeStyle = game.Debug.spriteColor;
context.beginPath();
context.rect(tx, ty, width, height);
context.stroke();
Expand All @@ -442,6 +475,7 @@ game.createClass('Debug', {
**/
_reset: function() {
this._draws = 0;
this._hitAreas.length = 0;
},

/**
Expand Down Expand Up @@ -692,6 +726,12 @@ game.addAttributes('Debug', {
@default false
**/
showBodies: false,
/**
Draw bounds of containers and graphics.
@attribute {Boolean} showBounds
@default false
**/
showBounds: false,
/**
Draw camera debug.
@attribute {Boolean} showCamera
Expand Down Expand Up @@ -722,6 +762,24 @@ game.addAttributes('Debug', {
@default false
**/
showSprites: false,
/**
Alpha of sprites.
@attribute {Number} spriteAlpha
@default 0.5
**/
spriteAlpha: 0.5,
/**
Color of sprites.
@attribute {Number} spriteColor
@default #00ff00
**/
spriteColor: '#00ff00',
/**
Bounds line width.
@attribute {Number} spriteLineWidth
@default 1
**/
spriteLineWidth: 1,
/**
Function that is called every time the debug panel is updated.
@method updatePanel
Expand Down Expand Up @@ -806,9 +864,10 @@ var href = document.location.href.toLowerCase();
if (href.match(/\?debug/)) game.Debug.enabled = true;
if (href.match(/\?debugdraw/)) {
game.Debug.showBodies = true;
game.Debug.showSprites = true;
game.Debug.showBounds = true;
game.Debug.showCamera = true;
game.Debug.showHitAreas = true;
game.Debug.showSprites = true;
}
if (href.match(/\?debugtouch/)) {
game.Debug.fakeTouch = true;
Expand Down

0 comments on commit bd72f1c

Please sign in to comment.