diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index f02a3cd..3775911 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -334,23 +334,62 @@ GameLib.Component.prototype.clone = function() { //var object = this.constructor.call(this, this.graphics, apiObject); }; +GameLib.Component.prototype.getStorageDependencies = function() { + + var dependencies = []; + + for (var property in this.linkedObjects) { + if (this.linkedObjects.hasOwnProperty(property)){ + if (this.hasOwnProperty(property)) { + + if ( + this[property] instanceof Array && + this.linkedObjects[property] instanceof Array + ) { + this[property].map( + function(arrayProperty){ + if (arrayProperty instanceof this.linkedObjects[0]) { + dependencies.push(GameLib.Utils.IdOrNull(arrayProperty)); + } + }.bind(this) + ); + } else if (this[property] instanceof this.linkedObjects[property]) { + dependencies.push(GameLib.Utils.IdOrNull(this[property])); + } + } + } + } + + return dependencies; +}; + GameLib.Component.prototype.save = function() { + // + // if (!this.canSave) { + // return; + // } + // + // this.canSave = false; - if (!this.canSave) { - return; - } + this.buildIdToObject(); - this.canSave = false; + for (var property in this.idToObject) { + if ( + this.idToObject.hasOwnProperty(property) && + this.idToObject[property] instanceof GameLib.Component + ) { + var apiObject = this.idToObject[property].toApiObject(); - var apiObject = this.toApiObject(true); + delete apiObject.linkedObjects; - delete apiObject.linkedObjects; + delete apiObject.idToObject; - delete apiObject.idToObject; + this.publish( + GameLib.Event.SAVE, + apiObject + ); + } + } - this.publish( - GameLib.Event.SAVE, - apiObject - ); }; \ No newline at end of file diff --git a/src/game-lib-system-0.js b/src/game-lib-system-0.js index 6a264fe..ff898f1 100644 --- a/src/game-lib-system-0.js +++ b/src/game-lib-system-0.js @@ -28,9 +28,6 @@ GameLib.System = function( apiSystem.parentEntity ); - this.saveCallback = null; - this.loadCallback = null; - GameLib.Component.call( this, GameLib.Component.COMPONENT_SYSTEM @@ -59,104 +56,9 @@ GameLib.System.prototype.createInstance = function() { */ GameLib.System.prototype.start = function() { - if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { - /** - * Hookup all editor input capabilities - */ - var entities = GameLib.EntityManager.Instance.query([GameLib.D3.Input.Editor]); - entities.map(function(entity){ - var component = entity.getFirstComponent(GameLib.D3.Input.Editor); - - component.mouseDown = component.onMouseDown(entity, GameLib.EntityManager.Instance).bind(component); - component.mouseMove = component.onMouseMove(entity).bind(component); - component.keyDown = component.onKeyDown(entity, GameLib.EntityManager.Instance).bind(component); - component.keyUp = component.onKeyUp(entity, GameLib.EntityManager.Instance).bind(component); - - component.domElement.instance.addEventListener('mousedown', component.mouseDown, false); - component.domElement.instance.addEventListener('mousemove', component.mouseMove, false); - component.domElement.instance.addEventListener('keydown', component.keyDown, false); - component.domElement.instance.addEventListener('keyup', component.keyUp, false); - - component.controls = new THREE.EditorControls( - component.camera.instance, - component.domElement.instance - ); - - /** - * After our mouse 'up' editor controls would have updated our camera - * instance, so we need to update our game-lib camera to reflect these - * changes - we override - */ - component.mouseUp = function(center) { - return function() { - var camera = entity.getFirstComponent(GameLib.D3.Camera); - camera.position.x = camera.instance.position.x; - camera.position.y = camera.instance.position.y; - camera.position.z = camera.instance.position.z; - - camera.quaternion.x = camera.instance.quaternion.x; - camera.quaternion.y = camera.instance.quaternion.y; - camera.quaternion.z = camera.instance.quaternion.z; - camera.quaternion.w = camera.instance.quaternion.w; - - camera.lookAt.x = center.x; - camera.lookAt.y = center.y; - camera.lookAt.z = center.z; - - camera.lookAt.instance.copy(center); - }; - }(component.controls.center).bind(component); - - /** - * Same applies to our mouse 'scroll' event - */ - component.mouseWheel = function() { - return function() { - var camera = entity.getFirstComponent(GameLib.D3.Camera); - camera.position.x = camera.instance.position.x; - camera.position.y = camera.instance.position.y; - camera.position.z = camera.instance.position.z; - }; - }.bind(component); - - component.domElement.instance.addEventListener('mousewheel', component.mouseWheel, false); - component.domElement.instance.addEventListener('mouseup', component.mouseUp, false); - - }.bind(this)) - } - - if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) { - - this.renderEntities = GameLib.EntityManager.Instance.query( - [ - GameLib.D3.Viewport, - GameLib.D3.Scene, - GameLib.D3.Renderer, - GameLib.D3.Camera - ] - ); - - this.renderEntities.map( - function(entity) { - var stats = entity.getFirstComponent(GameLib.D3.Stats); - stats.resize(); - stats.domElement.instance.parentElement.appendChild(stats.instance.dom); - } - ) - } - - if (this.systemType === GameLib.System.SYSTEM_TYPE_GUI) { - - var guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); - - guis.map(function(gui){ - gui.domElement.instance.parentElement.appendChild(gui.instance.domElement); - }) - - } if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { // this.pathFollowingObjects = GameLib.EntityManager.Instance.query([GameLib.D3.PathFollowing]); @@ -167,20 +69,6 @@ GameLib.System.prototype.start = function() { // this.lightObjects = GameLib.EntityManager.Instance.query([GameLib.D3.Light]); } - if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) { - - this.subscribe( - GameLib.Event.SAVE, - this.save - ); - - this.subscribe( - GameLib.Event.LOAD, - this.load - ) - } - - this.update(); }; @@ -191,13 +79,6 @@ GameLib.System.prototype.start = function() { */ GameLib.System.prototype.update = function(deltaTime) { - if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { - // this.driveInputObjects.forEach( - // function(object) { - // object.update(deltaTime); - // } - // ); - } if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { @@ -238,89 +119,7 @@ GameLib.System.prototype.update = function(deltaTime) { ); } - if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) { - this.renderEntities.map( - - function(renderEntity) { - - var stats = renderEntity.getFirstComponent(GameLib.D3.Stats); - - if (!stats.instance) { - return; - } - - var renderer = renderEntity.getFirstComponent(GameLib.D3.Renderer); - var camera = renderEntity.getFirstComponent(GameLib.D3.Camera); - var viewports = renderEntity.getComponents(GameLib.D3.Viewport); - var scenes = renderEntity.getComponents(GameLib.D3.Scene); - - if (!renderer.instance) { - return; - } - - if (viewports.length > 1) { - renderer.instance.autoClear = false; - } - - if (scenes.length > 1) { - renderer.instance.autoClear = false; - } - - renderer.instance.clear(); - - viewports.map( - function(viewport){ - - renderer.instance.setViewport( - viewport.x * renderer.width, - viewport.y * renderer.height, - viewport.width * renderer.width, - viewport.height * renderer.height - ); - - function renderScene(scene) { - - if (!scene.instance) { - return; - } - - if (scene.activeCamera) { - - if (!scene.activeCamera.instance) { - return; - } - - renderer.instance.render( - scene.instance, - scene.activeCamera.instance - ); - } else { - - if (!camera.instance) { - return; - } - - renderer.instance.render( - scene.instance, - camera.instance - ); - } - } - - if (viewport.scenes && viewport.scenes.length > 0) { - viewport.scenes.map(renderScene); - } else { - scenes.map(renderScene); - } - } - ); - - - stats.instance.end(); - } - ); - } }; @@ -330,48 +129,6 @@ GameLib.System.prototype.update = function(deltaTime) { */ GameLib.System.prototype.stop = function() { - if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { - - /** - * Now remove all editor input capabilities - */ - var entities = GameLib.EntityManager.Instance.query([GameLib.D3.Input.Editor]); - - entities.map(function(entity){ - var component = entity.getFirstComponent(GameLib.D3.Input.Editor); - component.domElement.instance.removeEventListener('mousedown', component.mouseDown, false); - component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false); - component.domElement.instance.removeEventListener('keydown', component.keyDown, false); - component.domElement.instance.removeEventListener('keyup', component.keyUp, false); - component.controls.dispose(); - component.domElement.instance.removeEventListener('mouseup', component.mouseUp, false); - component.domElement.instance.removeEventListener('mousewheel', component.mouseWheel, false); - }); - - console.log('stopped all input systems'); - } - - if (this.systemType === GameLib.System.SYSTEM_TYPE_GUI) { - console.log('stopping GUI system'); - var guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); - - guis.map(function(gui){ - gui.domElement.instance.parentElement.removeChild(gui.instance.domElement); - }) - } - - if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) { - - this.renderEntities.map( - function(entity) { - var stats = entity.getFirstComponent(GameLib.D3.Stats); - stats.domElement.instance.parentElement.removeChild(stats.instance.dom); - } - ); - - this.renderEntities = []; - } - if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { // this.pathFollowingObjects = []; // this.followObjects = []; @@ -380,20 +137,6 @@ GameLib.System.prototype.stop = function() { // this.cameraObjects = []; // this.lightObjects = []; } - - if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) { - - this.unsubscribe( - GameLib.Event.SAVE, - this.save - ); - - this.unsubscribe( - GameLib.Event.LOAD, - this.load - ) - } - }; /** @@ -401,7 +144,6 @@ GameLib.System.prototype.stop = function() { * @returns {GameLib.API.System} */ GameLib.System.prototype.toApiObject = function() { - return new GameLib.API.System( this.id, this.name, diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js new file mode 100644 index 0000000..ad51c30 --- /dev/null +++ b/src/game-lib-system-gui.js @@ -0,0 +1,38 @@ +/** + * System takes care of updating all the entities (based on their component data) + * @param graphics + * @param apiSystem GameLib.API.System + * @constructor + */ +GameLib.System.GUI = function( + graphics, + apiSystem +) { + GameLib.System.call( + this, + graphics, + apiSystem + ); +}; + +GameLib.System.GUI.prototype = Object.create(GameLib.System.prototype); +GameLib.System.GUI.prototype.constructor = GameLib.System.GUI; + +GameLib.System.GUI.prototype.start = function() { + + var guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); + + guis.map(function(gui){ + gui.domElement.instance.parentElement.appendChild(gui.instance.domElement); + }) +}; + +GameLib.System.GUI.prototype.stop = function() { + + var guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); + + guis.map(function(gui){ + gui.domElement.instance.parentElement.removeChild(gui.instance.domElement); + }) +}; + diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js new file mode 100644 index 0000000..64f89c2 --- /dev/null +++ b/src/game-lib-system-input.js @@ -0,0 +1,110 @@ +/** + * System takes care of updating all the entities (based on their component data) + * @param graphics + * @param apiSystem GameLib.API.System + * @constructor + */ +GameLib.System.Input = function( + graphics, + apiSystem +) { + GameLib.System.call( + this, + graphics, + apiSystem + ); +}; + +GameLib.System.Input.prototype = Object.create(GameLib.System.prototype); +GameLib.System.Input.prototype.constructor = GameLib.System.Input; + +GameLib.System.Input.prototype.start = function() { + + /** + * Hookup all editor input capabilities + */ + var entities = GameLib.EntityManager.Instance.query([GameLib.D3.Input.Editor]); + + entities.map(function(entity){ + + var component = entity.getFirstComponent(GameLib.D3.Input.Editor); + + component.mouseDown = component.onMouseDown(entity, GameLib.EntityManager.Instance).bind(component); + component.mouseMove = component.onMouseMove(entity).bind(component); + component.keyDown = component.onKeyDown(entity, GameLib.EntityManager.Instance).bind(component); + component.keyUp = component.onKeyUp(entity, GameLib.EntityManager.Instance).bind(component); + + component.domElement.instance.addEventListener('mousedown', component.mouseDown, false); + component.domElement.instance.addEventListener('mousemove', component.mouseMove, false); + component.domElement.instance.addEventListener('keydown', component.keyDown, false); + component.domElement.instance.addEventListener('keyup', component.keyUp, false); + + component.controls = new THREE.EditorControls( + component.camera.instance, + component.domElement.instance + ); + + /** + * After our mouse 'up' editor controls would have updated our camera + * instance, so we need to update our game-lib camera to reflect these + * changes - we override + */ + component.mouseUp = function(center) { + return function() { + var camera = entity.getFirstComponent(GameLib.D3.Camera); + camera.position.x = camera.instance.position.x; + camera.position.y = camera.instance.position.y; + camera.position.z = camera.instance.position.z; + + camera.quaternion.x = camera.instance.quaternion.x; + camera.quaternion.y = camera.instance.quaternion.y; + camera.quaternion.z = camera.instance.quaternion.z; + camera.quaternion.w = camera.instance.quaternion.w; + + camera.lookAt.x = center.x; + camera.lookAt.y = center.y; + camera.lookAt.z = center.z; + + camera.lookAt.instance.copy(center); + }; + }(component.controls.center).bind(component); + + /** + * Same applies to our mouse 'scroll' event + */ + component.mouseWheel = function() { + return function() { + var camera = entity.getFirstComponent(GameLib.D3.Camera); + camera.position.x = camera.instance.position.x; + camera.position.y = camera.instance.position.y; + camera.position.z = camera.instance.position.z; + }; + }.bind(component); + + component.domElement.instance.addEventListener('mousewheel', component.mouseWheel, false); + component.domElement.instance.addEventListener('mouseup', component.mouseUp, false); + + }.bind(this)) + +}; + +GameLib.System.Input.prototype.stop = function() { + + /** + * Now remove all editor input capabilities + */ + var entities = GameLib.EntityManager.Instance.query([GameLib.D3.Input.Editor]); + + entities.map(function(entity){ + var component = entity.getFirstComponent(GameLib.D3.Input.Editor); + component.domElement.instance.removeEventListener('mousedown', component.mouseDown, false); + component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false); + component.domElement.instance.removeEventListener('keydown', component.keyDown, false); + component.domElement.instance.removeEventListener('keyup', component.keyUp, false); + component.controls.dispose(); + component.domElement.instance.removeEventListener('mouseup', component.mouseUp, false); + component.domElement.instance.removeEventListener('mousewheel', component.mouseWheel, false); + }); + +}; + diff --git a/src/game-lib-system-render.js b/src/game-lib-system-render.js new file mode 100644 index 0000000..854ebe5 --- /dev/null +++ b/src/game-lib-system-render.js @@ -0,0 +1,141 @@ +/** + * System takes care of updating all the entities (based on their component data) + * @param graphics + * @param apiSystem GameLib.API.System + * @constructor + */ +GameLib.System.Render = function( + graphics, + apiSystem +) { + + GameLib.System.call( + this, + graphics, + apiSystem + ); + +}; + +GameLib.System.Render.prototype = Object.create(GameLib.System.prototype); +GameLib.System.Render.prototype.constructor = GameLib.System.Render; + +GameLib.System.Render.prototype.start = function() { + + this.renderEntities = GameLib.EntityManager.Instance.query( + [ + GameLib.D3.Viewport, + GameLib.D3.Scene, + GameLib.D3.Renderer, + GameLib.D3.Camera + ] + ); + + this.renderEntities.map( + function(entity) { + var stats = entity.getFirstComponent(GameLib.D3.Stats); + stats.resize(); + stats.domElement.instance.parentElement.appendChild(stats.instance.dom); + } + ) +}; + +/** + * Update script + */ +GameLib.System.Render.prototype.update = function() { + + this.renderEntities.map( + + function (renderEntity) { + + var stats = renderEntity.getFirstComponent(GameLib.D3.Stats); + + if (!stats.instance) { + return; + } + + var renderer = renderEntity.getFirstComponent(GameLib.D3.Renderer); + var camera = renderEntity.getFirstComponent(GameLib.D3.Camera); + var viewports = renderEntity.getComponents(GameLib.D3.Viewport); + var scenes = renderEntity.getComponents(GameLib.D3.Scene); + + if (!renderer.instance) { + return; + } + + if (viewports.length > 1) { + renderer.instance.autoClear = false; + } + + if (scenes.length > 1) { + renderer.instance.autoClear = false; + } + + renderer.instance.clear(); + + viewports.map( + function (viewport) { + + renderer.instance.setViewport( + viewport.x * renderer.width, + viewport.y * renderer.height, + viewport.width * renderer.width, + viewport.height * renderer.height + ); + + function renderScene(scene) { + + if (!scene.instance) { + return; + } + + if (scene.activeCamera) { + + if (!scene.activeCamera.instance) { + return; + } + + renderer.instance.render( + scene.instance, + scene.activeCamera.instance + ); + } else { + + if (!camera.instance) { + return; + } + + renderer.instance.render( + scene.instance, + camera.instance + ); + } + } + + if (viewport.scenes && viewport.scenes.length > 0) { + viewport.scenes.map(renderScene); + } else { + scenes.map(renderScene); + } + } + ); + + stats.instance.end(); + } + ); + +}; + +GameLib.System.Render.prototype.stop = function() { + + this.renderEntities.map( + function(entity) { + var stats = entity.getFirstComponent(GameLib.D3.Stats); + stats.domElement.instance.parentElement.removeChild(stats.instance.dom); + } + ); + + this.renderEntities = []; +}; + diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index 01bab23..2b02615 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -29,18 +29,34 @@ GameLib.System.Storage = function( } this.token = token; - this.subscribe( - GameLib.Event.LOGGED_IN, - function(data) { - this.token = data.token; - } - ) + this.loginSubscription = null; + this.saveSubscription = null; + this.loadSubscription = null; }; GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype); GameLib.System.Storage.prototype.constructor = GameLib.System.Storage; +GameLib.System.Storage.prototype.start = function() { + + this.loginSubscription = this.subscribe( + GameLib.Event.LOGGED_IN, + function(data) { + this.token = data.token; + } + ); + + this.saveSubscription = this.subscribe( + GameLib.Event.SAVE, + this.save + ); + + this.loadSubscription = this.subscribe( + GameLib.Event.LOAD, + this.load + ) +}; /** * 'Saves' data to baseURL @@ -131,4 +147,11 @@ GameLib.System.Storage.prototype.load = function(data) { }(xhr).bind(this); xhr.send(); -}; \ No newline at end of file +}; + +GameLib.System.Storage.prototype.stop = function() { + this.loginSubscription.remove(); + this.loadSubscription.remove(); + this.saveSubscription.remove(); +}; +