diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index ebb8237..dc1553f 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -10,6 +10,7 @@ GameLib.Event = function() { * @type {{}} */ GameLib.Event.Subscriptions = {}; +GameLib.Event.OnceSubscriptions = {}; /** * Events we can subscribe to and publish @@ -67,6 +68,39 @@ GameLib.Event.prototype.subscribe = function( } }; +/** + * Stop listening for this event after the callback returns true + * @param eventName + * @param callback + * @returns {{fn, remove: remove}} + */ +GameLib.Event.prototype.subscribeOnce = function( + eventName, + callback +) { + + var fn = callback.bind(this); + + if (GameLib.Event.OnceSubscriptions.hasOwnProperty(eventName)) { + GameLib.Event.OnceSubscriptions[eventName].push(fn); + } else { + GameLib.Event.OnceSubscriptions[eventName] = []; + GameLib.Event.OnceSubscriptions[eventName].push(fn); + } + + var index = GameLib.Event.OnceSubscriptions[eventName].indexOf(fn); + + /** + * Return a handle to the caller to allow us to unsubscribe to this event + */ + return { + fn : fn, + remove : function() { + GameLib.Event.Subscriptions[eventName].splice(index, 1); + } + } +}; + /** * * @param eventName diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index e8df9e3..d55ee64 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -123,7 +123,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { if (objectScene.meshes) { apiMeshes = objectScene.meshes.map( function(objectMesh) { - return GameLib.D3.API.Mesh.FromObject(objectMesh); + if (objectMesh instanceof Object){ + return GameLib.D3.API.Mesh.FromObject(objectMesh); + } else { + return objectMesh; + } } ) } @@ -131,7 +135,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { if (objectScene.lights) { apiLights = objectScene.lights.map( function(objectLight) { - return GameLib.D3.API.Light.FromObject(objectLight); + if (objectLight instanceof Object) { + return GameLib.D3.API.Light.FromObject(objectLight); + } else { + return objectLight; + } } ) } @@ -139,7 +147,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { if (objectScene.textures) { apiTextures = objectScene.textures.map( function(objectTexture) { - return GameLib.D3.API.Texture.FromObject(objectTexture) + if (objectTexture instanceof Object) { + return GameLib.D3.API.Texture.FromObject(objectTexture) + } else { + return objectTexture; + } } ) } @@ -147,7 +159,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { if (objectScene.materials) { apiMaterials = objectScene.materials.map( function(objectMaterial) { - return GameLib.D3.API.Material.FromObject(objectMaterial) + if (objectMaterial instanceof Object) { + return GameLib.D3.API.Material.FromObject(objectMaterial) + } else { + return objectMaterial; + } } ) } @@ -155,7 +171,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { if (objectScene.images) { apiImages = objectScene.images.map( function(objectImage) { - return GameLib.D3.API.Image.FromObject(objectImage) + if (objectImage instanceof Object) { + return GameLib.D3.API.Image.FromObject(objectImage) + } else { + return objectImage; + } } ) } diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 6d5db2f..49db6ad 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -50,8 +50,7 @@ GameLib.D3.Scene = function ( ); } else { - console.warn('apiMesh not an instance of API.Mesh'); - throw new Error('apiMesh not an instance of API.Mesh'); + return apiMesh; } }.bind(this) @@ -86,8 +85,7 @@ GameLib.D3.Scene = function ( ); } else { - console.warn('apiLight not an instance of API.Light'); - throw new Error('apiLight not an instance of API.Light'); + return apiLight; } }.bind(this) @@ -103,12 +101,9 @@ GameLib.D3.Scene = function ( apiTexture ); - // this.idToObject[texture.id] = texture; - return texture; } else { - console.warn('apiTexture not an instance of API.Texture'); - throw new Error('apiTexture not an instance of API.Texture'); + return apiTexture; } }.bind(this) @@ -124,13 +119,10 @@ GameLib.D3.Scene = function ( apiMaterial ); - // this.idToObject[material.id] = material; - return material; } else { - console.warn('apiMaterial not an instance of API.Material'); - throw new Error('apiMaterial not an instance of API.Material'); + return apiMaterial; } }.bind(this) @@ -146,13 +138,9 @@ GameLib.D3.Scene = function ( apiImage ); - // this.idToObject[image.id] = image; - return image; - } else { - console.warn('apiImage not an instance of API.Image'); - throw new Error('apiImage not an instance of API.Image'); + return apiImage; } }.bind(this) @@ -165,14 +153,6 @@ GameLib.D3.Scene = function ( ); } - /** - * TODO : Refactor (linking ?) - * @type {GameLib.D3.Scene} - */ - // this.idToObject[this.id] = this; - // - // this.linkObjects(this.idToObject); - this.sceneInstanceSubscriptions = []; this.objectInstanceSubscriptions = []; @@ -214,29 +194,30 @@ GameLib.D3.Scene.prototype.createInstance = function() { instance.quaternion = this.quaternion.instance; - this.meshes.map(function(mesh){ - if (mesh instanceof GameLib.D3.Mesh) { - if (GameLib.Utils.UndefinedOrNull(mesh.instance)){ - console.warn('No mesh instance'); - throw new Error('No mesh instance'); - } - instance.add(mesh.instance); - } else { - console.log('Unable to add mesh which should be loaded at this point'); - } - }); - - this.lights.map(function(light){ - if (light instanceof GameLib.D3.Light) { - if (GameLib.Utils.UndefinedOrNull(light.instance)){ - console.warn('No light instance'); - throw new Error('No light instance'); - } - instance.add(light.instance) - } else { - console.log('Unable to add light which should be loaded at this point'); - } - }); + // this.meshes.map( + // function(mesh){ + // if (mesh instanceof GameLib.D3.Mesh) { + // if (GameLib.Utils.UndefinedOrNull(mesh.instance)){ + // this.subscribe() + // } + // instance.add(mesh.instance); + // } else { + // console.log('Unable to add mesh which should be loaded at this point'); + // } + // }.bind(this) + // ); + // + // this.lights.map(function(light){ + // if (light instanceof GameLib.D3.Light) { + // if (GameLib.Utils.UndefinedOrNull(light.instance)){ + // console.warn('No light instance'); + // throw new Error('No light instance'); + // } + // instance.add(light.instance) + // } else { + // console.log('Unable to add light which should be loaded at this point'); + // } + // }); return instance; }; diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index f028203..fd3fa2c 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -608,8 +608,7 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property, entityMa handles.push(folder.add(object, property, 0, 1.0).step(0.001).name(property).listen()); } else if ( property === 'shininess' || - property === 'fov'|| - property === 'envMapIntensity' + property === 'fov' ) { handles.push(folder.add(object, property, -255, 255).step(1).name(property).listen()); } else if ( @@ -626,9 +625,10 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property, entityMa } else if ( property === 'near' || property === 'distanceGrain' || - property === 'bumpScale' + property === 'bumpScale' || + property === 'envMapIntensity' ) { - handles.push(folder.add(object, property, 0, 100).step(0.001).name(property).listen()); + handles.push(folder.add(object, property, -10, 100).step(0.001).name(property).listen()); } else if ( property === 'heightOffset' || property === 'rotationFactor' @@ -797,7 +797,11 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di 'w', -100, 100 - ).name(property + '.w').listen().step(0.1).onChange(function(){object.updateInstance()}); + ).name(property + '.w').listen().step(0.1).onChange( + function() { + object.updateInstance(); + } + ); } folder.add( @@ -806,8 +810,8 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di -100, 100 ).name(property + '.x').listen().step(0.1).onChange( - function(){ - object.updateInstance() + function() { + object.updateInstance(); } ); @@ -816,7 +820,11 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di 'y', -100, 100 - ).name(property + '.y').listen().step(0.1).onChange(function(){object.updateInstance()}); + ).name(property + '.y').listen().step(0.1).onChange( + function() { + object.updateInstance(); + } + ); if ( dimension === 3 || @@ -827,9 +835,11 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di 'z', -100, 100 - ).name(property + '.z').listen().step(0.1).onChange(function () { - object.updateInstance() - }); + ).name(property + '.z').listen().step(0.1).onChange( + function() { + object.updateInstance(); + } + ); } }; @@ -918,24 +928,37 @@ GameLib.GUI.prototype.buildSelectControl = function(folder, object, property, en /** * */ -GameLib.GUI.prototype.buildCurrentSelectionControlFromArray = function( +GameLib.GUI.prototype.buildArrayManager = function( folder, object, property, - name + constructor, + entityManager ) { - name = name || 'current ' + property; + var array = object[property]; - object['current' + property] = null; + var addArrayItem = function(item, index){ - var options = object[property].reduce( - function(result, obj){ - if (obj instanceof Object) { - result[obj.name] = obj; - } else { - console.log('not an object'); - } + var controller = folder.add( + { + 'remove' : function() { + object[property].splice(object[property].indexOf(item), 1); + folder.remove(controller); + } + }, + 'remove' + ).name('remove ' + property + '[' + index + '] - ' + item.name); + }; + + array.map(addArrayItem); + + var idToObject = {}; + + var selectionObject = entityManager.queryComponents(constructor).reduce( + function(result, component) { + result[component.name] = component; + idToObject[component.id] = component; return result; }, { @@ -943,7 +966,35 @@ GameLib.GUI.prototype.buildCurrentSelectionControlFromArray = function( } ); - folder.add(object, 'current' + property, options).name(name).listen() + var activeSelection = function(__object, __property) { + + var object = __object; + var property = __property; + + return { + component : null, + add : function() { + if (object[property].indexOf(activeSelection.component) === -1) { + object[property].push(activeSelection.component); + addArrayItem(activeSelection.component, object[property].length - 1) + } + } + }; + + }(object, property); + + folder.add(activeSelection, 'component', selectionObject).name('select ' + property).onChange( + function(value){ + if (value === 'null') { + activeSelection['component'] = null; + } else { + activeSelection['component'] = idToObject[value]; + } + } + ).listen(); + + folder.add(activeSelection, 'add').name('add to ' + property); + }; GameLib.GUI.prototype.buildEntitySelectionControlFromArray = function( @@ -1081,10 +1132,12 @@ GameLib.GUI.prototype.build = function(entityManager) { ) } else if (object.linkedObjects[property] instanceof Array) { console.log('ignored array : ' + property); - this.buildCurrentSelectionControlFromArray( + this.buildArrayManager( folder, object, - property + property, + object.linkedObjects[property], + entityManager ) } else { this.buildSelectControl(folder, object, property, entityManager, object.linkedObjects[property], parentObject);