Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial audio, update scene init
- Loading branch information
Showing
13 changed files
with
310 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
*.as binary | ||
|
||
*.mp3 binary | ||
*.ogg binary | ||
*.wav binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/*global Howl*/ | ||
var Tweens = App.Tweens; | ||
|
||
App.AudioController = AudioController; | ||
function AudioController(config) { | ||
this._isEnabled = true; | ||
this._urls = {}; | ||
this._soundNames = []; | ||
this._volume = {}; | ||
this._volumeTarget = {}; | ||
this._playing = {}; | ||
|
||
this.baseUrl = config.baseUrl; | ||
this.sounds = {}; | ||
|
||
this.tweenVolume = Tweens.factorTween(this._volume, 0.05); | ||
} | ||
|
||
AudioController.create = App.ctor(AudioController); | ||
|
||
AudioController.prototype.addSound = function (path, name) { | ||
name = name || path; | ||
var url = this.baseUrl + path; | ||
|
||
this._soundNames.push(name); | ||
|
||
this._urls[name] = [ | ||
url + '.mp3', | ||
url + '.ogg' | ||
]; | ||
}; | ||
|
||
AudioController.prototype.createSound = function (name, params) { | ||
var sound = this.sounds[name]; | ||
if (sound) { return sound; } | ||
|
||
params = params || {}; | ||
params.urls = this._urls[name]; | ||
|
||
sound = new Howl(params); | ||
sound.sid = name; | ||
|
||
this._volume[name] = 0; | ||
this._volumeTarget[name] = params.volume || 0; | ||
this.sounds[name] = sound; | ||
return sound; | ||
}; | ||
|
||
AudioController.prototype.getSound = function (name) { | ||
return this.sounds[name] || this.createSound(name); | ||
}; | ||
|
||
AudioController.prototype.playSound = function (name) { | ||
if (!this._isEnabled) { return; } | ||
var sound = this.getSound(name); | ||
if (!sound) { return; } | ||
if (!sound._loaded) { | ||
setTimeout(this.playSound.bind(this, name), 250); | ||
return; | ||
} | ||
|
||
this._playing[name] = true; | ||
sound.play(); | ||
}; | ||
|
||
AudioController.prototype.pauseSound = function (name) { | ||
if (!this._playing[name]) { return; } | ||
this.sounds[name].pause(); | ||
this._playing[name] = false; | ||
}; | ||
|
||
AudioController.prototype.setVolume = function (name, volume) { | ||
this._volumeTarget[name] = volume; | ||
}; | ||
|
||
AudioController.prototype.stopSound = function (name) { | ||
this.getSound(name).stop(); | ||
}; | ||
|
||
AudioController.prototype.stopAllSounds = function () { | ||
var names = this._soundNames; | ||
var sounds = this.sounds; | ||
var sound; | ||
|
||
for (var i = 0, il = names.length; i < il; i ++) { | ||
sound = sounds[names[i]]; | ||
if (!sound) { continue; } | ||
sound.stop(); | ||
} | ||
}; | ||
|
||
AudioController.prototype.enableSound = function () { | ||
this._isEnabled = true; | ||
}; | ||
|
||
AudioController.prototype.disableSound = function () { | ||
this._isEnabled = false; | ||
this.stopAllSounds(); | ||
}; | ||
|
||
AudioController.prototype.update = function () { | ||
var sounds = this.sounds; | ||
var names = this._soundNames; | ||
var playing = this._playing; | ||
var volumeTarget = this._volumeTarget; | ||
var name, sound, volume; | ||
|
||
for (var i = 0, il = names.length; i < il; i ++) { | ||
name = names[i]; | ||
sound = sounds[name]; | ||
if (!(sound && sound._loaded)) { continue; } | ||
|
||
// Update volume | ||
volume = this.tweenVolume(name, volumeTarget[name]); | ||
|
||
if (volume < 0.0001 && playing[name]) { | ||
sound.pause(); | ||
} else { | ||
if (!playing[name]) { this.playSound(name); } | ||
sound.volume(volume); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
App.ToggleController = ToggleController; | ||
function ToggleController(config) { | ||
var name = config.name; | ||
var toggle = this.toggle = document.getElementById('toggle-' + name); | ||
|
||
this.isActive = config.isActive != null ? config.isActive : false; | ||
this._toggleClassName = toggle.className; | ||
this._listeners = []; | ||
|
||
toggle.addEventListener('click', this.toggleState.bind(this), false); | ||
} | ||
|
||
ToggleController.create = App.ctor(ToggleController); | ||
|
||
ToggleController.prototype.addListener = function (context, fn) { | ||
this._listeners.push({ | ||
context : context, | ||
fn : fn | ||
}); | ||
}; | ||
|
||
ToggleController.prototype.triggerListeners = function () { | ||
var listeners = this._listeners; | ||
var listener; | ||
|
||
for (var i = 0, il = listeners.length; i < il; i ++) { | ||
listener = listeners[i]; | ||
listener.context[listener.fn].call(listener.context, this.isActive); | ||
} | ||
}; | ||
|
||
ToggleController.prototype.toggleState = function (event) { | ||
if (this.isActive) { | ||
this.toggle.className = this._toggleClassName; | ||
this.isActive = false; | ||
} else { | ||
this.toggle.className += ' active'; | ||
this.isActive = true; | ||
} | ||
|
||
this.triggerListeners(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.