Skip to content

Commit

Permalink
Changed audioQueue type to Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Eemeli Kelokorpi committed Jun 9, 2014
1 parent 079e623 commit caf82d5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
11 changes: 5 additions & 6 deletions src/engine/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,10 @@ game.Audio = game.Class.extend({
},

loaded: function(path, callback, audio) {
if (this.sources[game.audioQueue[path]]) throw('Duplicate audio source: ' + game.audioQueue[path]);
if (!game.audioQueue[path]) throw('Cannot find audio resource: ' + path);

// Get id for path
var id = game.audioQueue[path];
for (var name in game.paths) {
if (game.paths[name] === path) var id = name;
}
if (!id) throw('No id found for audio source');

this.sources[id] = {
clips: [],
Expand All @@ -170,7 +169,7 @@ game.Audio = game.Class.extend({
},

play: function(id, volume, loop, callback, rate) {
if (!this.sources[id]) throw('Cannot find source: ' + id);
if (!this.sources[id]) throw('Cannot find audio: ' + id);

// Web Audio
if (this.context) {
Expand Down
47 changes: 24 additions & 23 deletions src/engine/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,28 @@ var core = {
@property {Object} device
**/
device: {},
assets: {},
paths: {},
plugins: {},
json: {},
renderer: null,
modules: {},
renderer: null,
nocache: '',
current: null,
loadQueue: [],
waitForLoad: 0,
DOMLoaded: false,
next: 1,
anims: {},

moduleQueue: [],
assetQueue: [],
audioQueue: {},
audioQueue: [],

/**
Get JSON data.
@method getJSON
@param {String} id
**/
getJSON: function(id) {
return this.json[this.assets[id]];
return this.json[this.paths[id]];
},

/**
Expand Down Expand Up @@ -236,11 +235,7 @@ var core = {
@return {String} id
**/
addAsset: function(path, id) {
id = id || path;
path = this.config.mediaFolder + path + this.nocache;
this.assets[id] = path;
if (this.assetQueue.indexOf(path) === -1) this.assetQueue.push(path);
return id;
this.addFileToQueue(path, id, 'assetQueue');
},

/**
Expand All @@ -251,9 +246,15 @@ var core = {
@return {String} id
**/
addAudio: function(path, id) {
this.addFileToQueue(path, id, 'audioQueue');
},

addFileToQueue: function(path, id, queue) {
id = id || path;
path = this.config.mediaFolder + path + this.nocache;
this.audioQueue[path] = id;
if (this.paths[id]) throw('Id ' + id + ' already found');
this.paths[id] = path;
if (this[queue].indexOf(path) === -1) this[queue].push(path);
return id;
},

Expand All @@ -270,7 +271,7 @@ var core = {
this.current = { name: name, requires: [], loaded: false, body: null, version: version };
if (name === 'game.main') this.current.requires.push('engine.core');
this.modules[name] = this.current;
this.loadQueue.push(this.current);
this.moduleQueue.push(this.current);

if (this.current.name === 'engine.core') {
if (this.config.ignoreModules) {
Expand Down Expand Up @@ -318,7 +319,7 @@ var core = {
@param {String} [canvasId] Id of canvas element.
**/
start: function(scene, width, height, loaderClass, canvasId) {
if (this.loadQueue.length > 0) throw('Core not ready');
if (this.moduleQueue.length > 0) throw('Core not ready');

this.system = new this.System(width, height, canvasId);

Expand Down Expand Up @@ -359,8 +360,8 @@ var core = {

loadModules: function() {
var moduleLoaded, i, j, module, name, dependenciesLoaded;
for (i = 0; i < this.loadQueue.length; i++) {
module = this.loadQueue[i];
for (i = 0; i < this.moduleQueue.length; i++) {
module = this.moduleQueue[i];
dependenciesLoaded = true;

for (j = 0; j < module.requires.length; j++) {
Expand All @@ -375,8 +376,8 @@ var core = {
}

if (dependenciesLoaded && module.body) {
this.loadQueue.splice(i, 1);
if (this.loadQueue.length === 0) {
this.moduleQueue.splice(i, 1);
if (this.moduleQueue.length === 0) {
// Last module loaded, parse config
for (var c in this.config) {
var m = c.ucfirst();
Expand All @@ -394,21 +395,21 @@ var core = {
}
}

if (moduleLoaded && this.loadQueue.length > 0) {
if (moduleLoaded && this.moduleQueue.length > 0) {
this.loadModules();
}
else if (this.waitForLoad === 0 && this.loadQueue.length !== 0) {
else if (this.waitForLoad === 0 && this.moduleQueue.length !== 0) {
var unresolved = [];
for (i = 0; i < this.loadQueue.length; i++) {
for (i = 0; i < this.moduleQueue.length; i++) {
var unloaded = [];
var requires = this.loadQueue[i].requires;
var requires = this.moduleQueue[i].requires;
for (j = 0; j < requires.length; j++) {
module = this.modules[requires[j]];
if (!module || !module.loaded) {
unloaded.push(requires[j]);
}
}
unresolved.push(this.loadQueue[i].name + ' (requires: ' + unloaded.join(', ') + ')');
unresolved.push(this.moduleQueue[i].name + ' (requires: ' + unloaded.join(', ') + ')');
}
throw('Unresolved modules:\n' + unresolved.join('\n'));
}
Expand Down
8 changes: 3 additions & 5 deletions src/engine/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ game.Loader = game.Class.extend({
this.assetQueue.push(this.getPath(game.assetQueue[i]));
}

if (game.Audio) {
for (var name in game.audioQueue) {
this.soundQueue.push(name);
}
for (var i = 0; i < game.audioQueue.length; i++) {
this.soundQueue.push(game.audioQueue[i]);
}

if (this.assetQueue.length > 0) {
Expand Down Expand Up @@ -223,7 +221,7 @@ game.Loader = game.Class.extend({
}

game.assetQueue.length = 0;
if (game.Audio) game.audioQueue = {};
game.audioQueue.length = 0;

if (!this.dynamic) return this.setScene();
if (typeof this.callback === 'function') this.callback();
Expand Down
10 changes: 5 additions & 5 deletions src/engine/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ game.Sprite = PIXI.Sprite.extend({

init: function(id, x, y, settings) {
if (typeof id === 'string') {
id = game.assets[id] || id;
id = game.paths[id] || id;
id = game.Texture.fromFrame(id);
}
this._super(id);
Expand All @@ -45,7 +45,7 @@ game.Sprite = PIXI.Sprite.extend({

setTexture: function(id) {
if (typeof id === 'string') {
id = game.assets[id] || id;
id = game.paths[id] || id;
id = game.Texture.fromFrame(id);
}
this._super(id);
Expand Down Expand Up @@ -84,7 +84,7 @@ game.Sprite = PIXI.Sprite.extend({
**/
game.Spine = PIXI.Spine.extend({
init: function(id, settings) {
this._super(game.assets[id] || id);
this._super(game.paths[id] || id);
game.merge(this, settings);
},

Expand Down Expand Up @@ -145,7 +145,7 @@ game.Container = PIXI.DisplayObjectContainer.extend({

game.Texture = PIXI.Texture.extend();
game.Texture.fromImage = function(id, crossorigin) {
id = game.assets[id] || id;
id = game.paths[id] || id;
return PIXI.Texture.fromImage(id, crossorigin);
};
game.Texture.fromCanvas = PIXI.Texture.fromCanvas;
Expand All @@ -165,7 +165,7 @@ game.TilingSprite = PIXI.TilingSprite.extend({
speed: { x: 0, y: 0 },

init: function(path, width, height, settings) {
path = game.assets[path] || path;
path = game.paths[path] || path;
var texture = path instanceof PIXI.Texture ? path : PIXI.Texture.fromFrame(this.path || path);
this._super(texture, width || texture.width, height || texture.height);
game.merge(this, settings);
Expand Down

0 comments on commit caf82d5

Please sign in to comment.