diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 088713b..1b9691d 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -97,7 +97,7 @@ GameLib.Event.DELAYED_INSTANCE_ENCOUNTERED = 0x4f; GameLib.Event.CAST_SOURCE_CHANGED = 0x50; GameLib.Event.ANIMATION_MESH_ADDED = 0x51; GameLib.Event.ANIMATION_MESH_REMOVED = 0x52; -// GameLib.Event.GET_SCENE = 0x53; +GameLib.Event.DOM_ELEMENT_CHANGE = 0x53; GameLib.Event.CUSTOM_CODE_WINDOW_RESIZE = 0x54; GameLib.Event.LOAD_FONT = 0x55; GameLib.Event.FONT_NOT_FOUND = 0x56; @@ -215,7 +215,7 @@ GameLib.Event.GetEventName = function(number) { case 0x50 : return 'cast_source_changed'; case 0x51 : return 'animation_mesh_added'; case 0x52 : return 'animation_mesh_removed'; - case 0x53 : return 'unused';//return 'get_scene'; + case 0x53 : return 'dom_element_change'; case 0x54 : return 'custom_code_window_resize'; case 0x55 : return 'load_font'; case 0x56 : return 'font_not_found'; diff --git a/src/game-lib-a-api-component.js b/src/game-lib-a-api-component.js index f5dfef7..72983dd 100644 --- a/src/game-lib-a-api-component.js +++ b/src/game-lib-a-api-component.js @@ -11,7 +11,30 @@ GameLib.API.Component = function( this.componentType = componentType; if (GameLib.Utils.UndefinedOrNull(parentEntity)) { - parentEntity = null; + + if (this instanceof GameLib.EntityManager) { + parentEntity = this.defaultEntity; + } else { + + parentEntity = GameLib.EntityManager.Instance.defaultEntity; + + if (parentEntity) { + + if (parentEntity.id === this.id) { + + /** + * This is the default entity, it cannot be the parent entity of itself - so terminate here. + */ + console.log('terminating here'); + + parentEntity = null; + + } else { + + parentEntity.addComponent(this); + } + } + } } this.parentEntity = parentEntity; }; diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 35ba133..be6050c 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -1151,13 +1151,9 @@ GameLib.Component.prototype.save = function(remote) { }.bind(this) ); - for (var property in this.idToObject) { - if ( - this.idToObject.hasOwnProperty(property) && - this.idToObject[property] instanceof GameLib.Component - ) { - - var apiObject = this.idToObject[property].toApiObject(); + Object.keys(this.idToObject).map( + function(componentId) { + var apiObject = this.idToObject[componentId].toApiObject(); toSave.push(apiObject); @@ -1166,10 +1162,17 @@ GameLib.Component.prototype.save = function(remote) { { apiObject: apiObject, remote: remote + }, + function success(result) { + console.log(result); + }, + function error(error) { + console.log(error); } ); - } - } + + }.bind(this) + ); }; /** diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index 6bc9b5e..070b5d7 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -4,12 +4,14 @@ * @param id * @param name * @param entities GameLib.API.Entity[] + * @param defaultEntity * @param parentEntity */ GameLib.API.EntityManager = function( id, name, entities, + defaultEntity, parentEntity ) { if (GameLib.Utils.UndefinedOrNull(id)) { @@ -27,6 +29,11 @@ GameLib.API.EntityManager = function( } this.entities = entities; + if (GameLib.Utils.UndefinedOrNull(defaultEntity)) { + defaultEntity = null; + } + this.defaultEntity = defaultEntity; + GameLib.API.Component.call( this, GameLib.Component.ENTITY_MANAGER, @@ -54,6 +61,7 @@ GameLib.API.EntityManager.FromObject = function(objectEntityManager) { objectEntityManager.id, objectEntityManager.name, apiEntities, + objectEntityManager.defaultEntity, objectEntityManager.parentEntity ); }; diff --git a/src/game-lib-controls-0.js b/src/game-lib-controls-0.js index a1e597f..b52ba09 100644 --- a/src/game-lib-controls-0.js +++ b/src/game-lib-controls-0.js @@ -56,7 +56,17 @@ GameLib.Controls.prototype.createInstance = function() { /** * Updates the mesh instance */ -GameLib.Controls.prototype.updateInstance = function() { +GameLib.Controls.prototype.updateInstance = function(property) { + + if (property === 'domElement') { + GameLib.Event.Emit( + GameLib.Event.DOM_ELEMENT_CHANGE, + { + component: this + } + ); + } + console.log('default controls update instance'); }; diff --git a/src/game-lib-controls-mouse.js b/src/game-lib-controls-mouse.js index 4154fdb..a800339 100644 --- a/src/game-lib-controls-mouse.js +++ b/src/game-lib-controls-mouse.js @@ -46,8 +46,8 @@ GameLib.Controls.Mouse.prototype.createInstance = function() { /** * Update Instance */ -GameLib.Controls.Mouse.prototype.updateInstance = function() { - GameLib.Controls.prototype.updateInstance.call(this); +GameLib.Controls.Mouse.prototype.updateInstance = function(property) { + GameLib.Controls.prototype.updateInstance.call(this, property); }; /** diff --git a/src/game-lib-d3-api-mesh-plane.js b/src/game-lib-d3-api-mesh-plane.js index cf3359f..4b0c347 100644 --- a/src/game-lib-d3-api-mesh-plane.js +++ b/src/game-lib-d3-api-mesh-plane.js @@ -115,7 +115,7 @@ GameLib.D3.API.Mesh.Plane.FromObject = function (objectMesh){ objectMesh.heightMapScale, objectMesh.isHeightMap, objectMesh.isDotMap, - GameLib.Utils.IdOrNull(objectMesh.dotObject) + objectMesh.dotObject ); }; diff --git a/src/game-lib-d3-api-renderer.js b/src/game-lib-d3-api-renderer.js index a99ef83..15a1e33 100644 --- a/src/game-lib-d3-api-renderer.js +++ b/src/game-lib-d3-api-renderer.js @@ -122,7 +122,7 @@ GameLib.D3.API.Renderer = function ( this.renderMode = renderMode; if (GameLib.Utils.UndefinedOrNull(lastRenderMode)) { - lastRenderMode = this.renderMode; + lastRenderMode = GameLib.D3.API.Renderer.MODE_CANVAS; } this.lastRenderMode = lastRenderMode; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 64493ce..91406f1 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -142,7 +142,7 @@ GameLib.D3.Mesh = function ( linkedObjects.font = GameLib.D3.Font; } - /** + /** * Runtime meshes have helpers too * @type {null} */ diff --git a/src/game-lib-d3-mesh-plane.js b/src/game-lib-d3-mesh-plane.js index cae6eee..53142f5 100644 --- a/src/game-lib-d3-mesh-plane.js +++ b/src/game-lib-d3-mesh-plane.js @@ -118,7 +118,18 @@ GameLib.D3.Mesh.Plane.prototype.updateInstance = function(property) { property === 'isDotMap' || property === 'dotObject' ) { - this.generateDotMap(); + this.dots.map( + function(dot){ + this.parentScene.instance.remove(dot); + dot.geometry.dispose(); + dot.material.dispose(); + }.bind(this) + ); + + if (this.isDotMap) { + this.generateDotMap(); + } + } GameLib.D3.Mesh.prototype.updateInstance.call(this, property); @@ -139,7 +150,7 @@ GameLib.D3.Mesh.Plane.prototype.toApiObject = function() { this.heightMapScale, this.isHeightMap, this.isDotMap, - this.dotObject + GameLib.Utils.IdOrNull(this.dotObject) ); return apiMeshPlane; @@ -215,13 +226,6 @@ GameLib.D3.Mesh.Plane.prototype.getHeightData = function() { GameLib.D3.Mesh.Plane.prototype.generateDotMap = function() { - this.dots.map( - function(dot){ - // this.parentScene.instance.remove(dot); - // dot.geometry.dispose(); - }.bind(this) - ); - this.dots = []; var data = this.getHeightData(); @@ -232,11 +236,18 @@ GameLib.D3.Mesh.Plane.prototype.generateDotMap = function() { for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++ ) { - var geometry = new THREE.BoxBufferGeometry(0.5,0.5,0.5); + var geometry; + var material; - var materialInstance = this.materials[0].instance; + if (this.dotObject) { + geometry = this.dotObject.instance.geometry.clone(); + material = this.dotObject.materials[0].instance; + } else { + geometry = new THREE.BoxBufferGeometry(0.5, 0.5, 0.5); + material = this.materials[0].instance; + } - var dot = new THREE.Mesh(geometry, materialInstance); + var dot = new THREE.Mesh(geometry, material); dot.position.x = x; dot.position.y = y; dot.position.z = data[(y * width) + x]; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index fc4cf07..f005c88 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -13,6 +13,7 @@ GameLib.EntityManager = function(apiEntityManager) { apiEntityManager.id, apiEntityManager.name, apiEntityManager.entities, + apiEntityManager.defaultEntity, apiEntityManager.parentEntity ); @@ -40,10 +41,17 @@ GameLib.EntityManager = function(apiEntityManager) { this.removeComponent.bind(this) ); + GameLib.Event.Subscribe( + GameLib.Event.ENTITY_LOADED, + this.entityLoaded.bind(this) + ); + + GameLib.Component.call( this, { - 'entities' : [GameLib.Entity] + 'entities' : [GameLib.Entity], + 'defaultEntity' : GameLib.Entity } ); }; @@ -62,6 +70,10 @@ GameLib.EntityManager.prototype.instanceCreated = function(data) { } }; +GameLib.EntityManager.prototype.entityLoaded = function(data) { + this.defaultEntity = data.entity; +}; + GameLib.EntityManager.prototype.registerComponent = function(data) { var updated = false; @@ -380,6 +392,7 @@ GameLib.EntityManager.prototype.toApiObject = function() { this.id, this.name, apiEntities, + this.defaultEntity, GameLib.Utils.IdOrNull(this.parentEntity) ); diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 42cd877..ff251df 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -46,20 +46,23 @@ GameLib.Entity.prototype.createInstance = function() { */ GameLib.Entity.prototype.addComponent = function(component) { - GameLib.Utils.PushUnique(this.components, component); - if (component instanceof GameLib.D3.Mesh) { - /** - * For meshes, simply get the children components - * @type {Array} - */ - component.getChildrenComponents().map(function(childComponent){ - GameLib.Utils.PushUnique(this.components, childComponent); - childComponent.parentEntity = this; - }.bind(this)) + // if (component instanceof GameLib.D3.Mesh) { + // + // /** + // * For meshes, simply get the children components + // * @type {Array} + // */ + // component.getChildrenComponents().map(function(childComponent){ + // GameLib.Utils.PushUnique(this.components, childComponent); + // childComponent.parentEntity = this; + // }.bind(this)) + // + // } else { + + if (component instanceof GameLib.Component) { - } else { /** * Here we will dig into this component - find all its 'parentEntity' members - and update them accordingly */ @@ -68,16 +71,16 @@ GameLib.Entity.prototype.addComponent = function(component) { /** * Also add the child components of this component as components of this entity */ - for (var property in component.idToObject) { - if (component.idToObject.hasOwnProperty(property) && - component.idToObject[property] !== component && - component.idToObject[property] instanceof GameLib.Component - ) { - GameLib.Utils.PushUnique(this.components, component.idToObject[property]); - component.idToObject[property].parentEntity = this; - } - } + Object.keys(component.idToObject).map( + function(componentId) { + GameLib.Utils.PushUnique(this.components, component.idToObject[componentId]); + component.idToObject[componentId].parentEntity = this; + }.bind(this) + ); + + GameLib.Utils.PushUnique(this.components, component); } + // } /** diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js index 8511f29..8118f96 100644 --- a/src/game-lib-system-input.js +++ b/src/game-lib-system-input.js @@ -62,6 +62,7 @@ GameLib.System.Input = function( this.delayedInstanceEncounteredSubscription = null; this.instanceCreatedSubscription = null; this.removeComponentSubscription = null; + this.domElementChangeSubscription = null; this.mouse = new GameLib.Mouse(); }; @@ -91,6 +92,11 @@ GameLib.System.Input.prototype.start = function() { this.delayedInstanceEncountered.bind(this) ); + this.domElementChangeSubscription = GameLib.Event.Subscribe( + GameLib.Event.DOM_ELEMENT_CHANGE, + this.domElementChange.bind(this) + ); + this.editorControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_EDITOR); this.touchControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_TOUCH); @@ -141,6 +147,8 @@ GameLib.System.Input.prototype.stop = function() { this.delayedInstanceEncounteredSubscription.remove(); + this.domElementChangeSubscription.remove(); + this.touchControls.map( function(touchControl){ this.deRegisterTouchControls(touchControl); @@ -175,6 +183,16 @@ GameLib.System.Input.prototype.stop = function() { }; +/** + * + * @param data + */ +GameLib.System.Input.prototype.domElementChange = function(data) { + if (data.component instanceof GameLib.Controls) { + console.log('todo: implement dom element change'); + } +}; + /** * From now on we want to track everything about a component, only from the systems that are active * @param data @@ -312,6 +330,11 @@ GameLib.System.Input.prototype.delayedInstanceEncountered = function(data) { GameLib.System.Input.prototype.registerTouchControls = function(touchControl) { + if (!touchControl.domElement || !touchControl.domElement.instance) { + console.warn('no domElement at time of registration of touch controls - this part will be skipped'); + return; + } + touchControl.domElement.instance.addEventListener( 'touchstart', this.touchStart, @@ -337,6 +360,11 @@ GameLib.System.Input.prototype.registerTouchControls = function(touchControl) { GameLib.System.Input.prototype.registerKeyboardControls = function(keyboardControl) { + if (!keyboardControl.domElement || !keyboardControl.domElement.instance) { + console.warn('no domElement at time of registration of keyboard controls - this part will be skipped'); + return; + } + keyboardControl.domElement.instance.addEventListener( 'keyup', this.keyboardKeyUp, @@ -352,6 +380,11 @@ GameLib.System.Input.prototype.registerKeyboardControls = function(keyboardContr GameLib.System.Input.prototype.registerMouseControls = function(mouseControl) { + if (!mouseControl.domElement || !mouseControl.domElement.instance) { + console.warn('no domElement at time of registration of mouse controls - this part will be skipped'); + return; + } + mouseControl.domElement.instance.addEventListener( 'mousedown', this.mouseDown, @@ -401,6 +434,11 @@ GameLib.System.Input.prototype.registerEditorControls = function(editorControl) // }.bind(this) // ); + if (!editorControl.domElement || !editorControl.domElement.instance) { + console.warn('no domElement at time of registration of editor controls - are you sure you know what you are doing?'); + return; + } + editorControl.domElement.instance.addEventListener( 'mousedown', this.mouseDownEdit, diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index 5a98197..4f4866a 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -287,6 +287,9 @@ GameLib.System.Storage.prototype.save = function(data) { component : data.apiObject, session : this.token })); + }, + function error(error){ + console.log(error); } );