diff --git a/src/engine/audio.js b/src/engine/audio.js index ebcf5035..08c08262 100644 --- a/src/engine/audio.js +++ b/src/engine/audio.js @@ -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: [], @@ -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) { diff --git a/src/engine/core.js b/src/engine/core.js index e3094b58..b0a38df3 100644 --- a/src/engine/core.js +++ b/src/engine/core.js @@ -102,21 +102,20 @@ 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. @@ -124,7 +123,7 @@ var core = { @param {String} id **/ getJSON: function(id) { - return this.json[this.assets[id]]; + return this.json[this.paths[id]]; }, /** @@ -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'); }, /** @@ -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; }, @@ -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) { @@ -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); @@ -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++) { @@ -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(); @@ -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')); } diff --git a/src/engine/loader.js b/src/engine/loader.js index 5846cefa..0224db56 100644 --- a/src/engine/loader.js +++ b/src/engine/loader.js @@ -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) { @@ -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(); diff --git a/src/engine/sprite.js b/src/engine/sprite.js index 326ee305..9501bb2a 100644 --- a/src/engine/sprite.js +++ b/src/engine/sprite.js @@ -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); @@ -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); @@ -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); }, @@ -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; @@ -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);