From 8e5c3d1d04ab0b0078ce516672473ffa6806c5e4 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Sat, 17 Jun 2017 19:27:01 +0200 Subject: [PATCH] first loading seems ok --- src/game-lib-a-2-utils.js | 21 ++-- src/game-lib-a-component-a.js | 9 ++ src/game-lib-d3-image.js | 1 + src/game-lib-d3-input-editor.js | 4 +- src/game-lib-d3-scene.js | 32 ++++- src/game-lib-entity-manager.js | 211 +++++++++++++++++++++++--------- src/game-lib-gui.js | 2 +- src/game-lib-system-0.js | 24 +++- 8 files changed, 225 insertions(+), 79 deletions(-) diff --git a/src/game-lib-a-2-utils.js b/src/game-lib-a-2-utils.js index b2c343e..cd1643b 100644 --- a/src/game-lib-a-2-utils.js +++ b/src/game-lib-a-2-utils.js @@ -93,7 +93,7 @@ GameLib.Utils.LoadIdsFromArrayToIdObject = function(array, idToObject) { console.warn('Object with no ID passed: ' + object) } - } else if (typeof object == 'string') { + } else if (typeof object === 'string') { console.warn('Linked object found:' + object); } else { console.warn('Unhandled type of object: ', object); @@ -124,7 +124,7 @@ GameLib.Utils.LoadIdsFromObjectToIdObject = function(object, idToObject) { } else { console.warn('Object with no ID passed: ' + object) } - } else if (typeof object == 'string') { + } else if (typeof object === 'string') { console.warn('Linked object found:' + object); } else { console.warn('Unhandled type of object: ', object); @@ -133,10 +133,16 @@ GameLib.Utils.LoadIdsFromObjectToIdObject = function(object, idToObject) { return idToObject; }; +/** + * Undefined or null check + * @param variable + * @returns {boolean} + * @constructor + */ GameLib.Utils.UndefinedOrNull = function ( variable ) { - return typeof variable == 'undefined' || variable == null; + return typeof variable === 'undefined' || variable === null; }; /** @@ -199,15 +205,6 @@ GameLib.Utils.Link = function(propertyString, idToObject, parentObject, id) { } }; -GameLib.Utils.Raycast = function ( - from, - to, - options, - world -) { - console.log("not implemented yet"); -}; - /** * Generates a random ID * @returns {string} diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index e161972..7b49ca0 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -57,6 +57,15 @@ GameLib.Component.prototype.getDependencies = function() { var dependencies = []; for (var property in this.linkedObjects) { + + // if ( + // property === 'parentMesh' || + // property === 'parentScene' || + // property === 'parentEntity' + // ) { + // continue; + // } + //TODO array linked objects if (this.linkedObjects.hasOwnProperty(property)){ if (this.hasOwnProperty(property)) { diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index b7bebe3..64a4554 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -30,6 +30,7 @@ GameLib.D3.Image = function( this, GameLib.Component.COMPONENT_IMAGE ); + }; GameLib.D3.Image.prototype = Object.create(GameLib.D3.API.Image.prototype); diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 1f14e34..81c8a39 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -147,13 +147,13 @@ GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity, entityManager) { if (mesh.selected) { this.removeHelper(mesh, entity); entity.removeComponent(mesh); - entity.buildIdToObject(); + console.log('entity.buildIdToObject()'); gui.removeObject(mesh); var scene = mesh.parentScene; scene.removeObject(mesh); - scene.buildIdToObject(); + console.log('scene.buildIdToObject()'); } }.bind(this) ); diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 6411d7e..2915d65 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -208,13 +208,29 @@ GameLib.D3.Scene.prototype.createInstance = function() { instance.quaternion = this.quaternion.instance; - for (var im = 0; im < this.meshes.length; im++) { - instance.add(this.meshes[im].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'); + } + }); - for (var l = 0; l < this.lights.length; l++) { - instance.add(this.lights[l].instance); - } + 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; }; @@ -327,6 +343,8 @@ GameLib.D3.Scene.prototype.addObject = function(object) { if (this.instance) { this.instance.add(object.instance); + } else { + console.warn('no scene instance'); } @@ -360,6 +378,8 @@ GameLib.D3.Scene.prototype.removeObject = function(object) { if (this.instance) { this.instance.remove(object.instance); + } else { + console.warn('no scene instance'); } if (object.parentScene === this) { diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index cefc552..ad0618f 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -25,6 +25,8 @@ GameLib.EntityManager = function() { this.subscriptions = []; + this.register = {}; + this.registerCallbacks(); }; @@ -378,80 +380,175 @@ GameLib.EntityManager.prototype.onParentEntityChange = function(data) { }; + +GameLib.EntityManager.prototype.link = function(component, data) { + for (var property in component.linkedObjects) { + if (component.linkedObjects.hasOwnProperty(property)) { + if (component.linkedObjects[property] instanceof Array) { + component[property] = component[property].map(function (entry) { + if (entry === data.component.id) { + return data.component; + } else { + return entry; + } + }); + + } else { + if (component[property] && + component[property] === data.component.id) { + component[property] = data.component; + } + } + } + } +}; + GameLib.EntityManager.prototype.componentCreated = function(data) { - console.log('component created : ' + data.component.name); + console.log('component created : ' + data.component.name); - /** - * If we notify ourselves - ignore it - */ - if (data.component === this) { - return; - } + /** + * If we notify ourselves - ignore it + */ + if (data.component === this) { + return; + } - /** - * Store this component into our 'loaded' list - */ - this.loaded.push(data.component); + /** + * Store this component into our 'loaded' list + */ + this.loaded.push(data.component); - /** - * Store the dependencies too - */ - data.component.dependencies.map(function(id) { + /** + * Store the dependencies too + */ + data.component.dependencies.map(function(id) { - if (GameLib.Utils.UndefinedOrNull(this.dependencies[id])) { - this.dependencies[id] = []; - } + /** + * Check if we already processed a component on which this component is dependent + */ + if (this.register.hasOwnProperty(id)) { - this.dependencies[id].push(data.component); + console.log('found a component here'); - }.bind(this)); + /** + * First add this to the 'idToObject' + */ + data.component.idToObject[id] = this.register[id]; - /** - * Now find all the dependencies of this component - */ - var dependencies = this.dependencies[data.component.id]; - if (GameLib.Utils.UndefinedOrNull(dependencies)) { + /** + * Remove this dependency from the dependency list + */ + var index = data.component.dependencies.indexOf(id); + if (index === -1) { + console.log('failed to locate dependency which should exist'); + } + data.component.dependencies.splice(index, 1); - /** - * We have no dependencies, so mark our component as loaded and create an instance - */ - data.component.loaded = true; - data.component.instance = data.component.createInstance(); + /** + * Now link the component + */ + this.link(data.component, {component:this.register[id]}); - /** - * If we have none, we are done and can exit - */ - return; - } + } else { - /** - * Otherwise, now - for each dependency - update 'idToObject' and check if its loaded - */ - dependencies.map(function(component){ - component.idToObject[data.component.id] = data.component; + if (GameLib.Utils.UndefinedOrNull(this.dependencies[id])) { + this.dependencies[id] = []; + } - var loaded = true; + /** + * Don't store duplicate dependencies + */ + if (this.dependencies[id].indexOf(data.component === -1)) { + this.dependencies[id].push(data.component); + } + } - for (var property in component.idToObject) { - if ( - component.idToObject.hasOwnProperty(property) && - component.idToObject[property] === null - ) { - loaded = false - } - } + }.bind(this)); - component.loaded = loaded; + /** + * Now find all the components which depend on this component + */ + if (GameLib.Utils.UndefinedOrNull(this.dependencies[data.component.id])) { - if (component.loaded) { - /** - * Our component is fully loaded, time to create an instance of it - */ - component.instance = component.createInstance(); - } + /** + * We don't know about any dependencies on this object - but maybe a component still + * has to load which has dependencies to this object + */ + this.register[data.component.id] = data.component; + } else { - }) + /** + * Otherwise, now - for each dependency - update 'idToObject' and check if its loaded + */ + this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce( + function (result, component) { + + /** + * Link the object to the component + */ + component.idToObject[data.component.id] = data.component; + + /** + * Remove the actual dependency + */ + var index = component.dependencies.indexOf(data.component.id); + if (index === -1) { + console.warn('dependency mismatch'); + } else { + component.dependencies.splice(index, 1); + } + + /** + * Find the actual place where this object should be linked - and link them + */ + this.link(component, data); + + /** + * If we now managed to link the objects, and this object has no more dependencies + */ + if (component.dependencies.length === 0) { + component.loaded = true; + } + + /** + * Also remove this from the current dependency list + */ + return result; + }.bind(this), + [] + ); + + delete this.dependencies[data.component.id]; + } + + /** + * Now if this new component has no dependencies, load it + */ + if (data.component.dependencies.length === 0) { + data.component.loaded = true; + } + + /** + * Now check if all components are loaded, i.e., no more dependencies - if so - create their instance objects + */ + var loaded = true; + for (var i = 0; i < this.loaded.length; i++) { + if (!this.loaded[i].loaded) { + loaded = false; + break + } + } + + /** + * All components loaded + */ + if (loaded) { + this.loaded.map(function(component){ + component.instance = component.createInstance(); + }); + this.loaded = []; + } }; diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index 8002967..37c461e 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -878,7 +878,7 @@ GameLib.GUI.prototype.buildSelectControl = function(folder, object, property, en /** * Properties changed - rebuild the object list in the parent */ - parentObject.buildIdToObject(); + console.log('parentObject.buildIdToObject();'); /** * Properties changed - rebuild GUI diff --git a/src/game-lib-system-0.js b/src/game-lib-system-0.js index 1f2d3bc..6a264fe 100644 --- a/src/game-lib-system-0.js +++ b/src/game-lib-system-0.js @@ -245,13 +245,20 @@ GameLib.System.prototype.update = function(deltaTime) { function(renderEntity) { var stats = renderEntity.getFirstComponent(GameLib.D3.Stats); - stats.instance.begin(); + + 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; } @@ -273,12 +280,27 @@ GameLib.System.prototype.update = function(deltaTime) { ); 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