diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index 7634d74..6d55f4d 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -10,6 +10,7 @@ GameLib.API.EntityManager = function( id, name, entities, + // systems, parentEntity ) { GameLib.Component.call( @@ -17,6 +18,7 @@ GameLib.API.EntityManager = function( GameLib.Component.COMPONENT_ENTITY_MANAGER, { 'entities' : [GameLib.Entity] + // 'systems' : [GameLib.System] }, null, parentEntity @@ -36,6 +38,11 @@ GameLib.API.EntityManager = function( entities = []; } this.entities = entities; + + // if (GameLib.Utils.UndefinedOrNull(systems)) { + // systems = []; + // } + // this.systems = systems; }; GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype); @@ -54,10 +61,17 @@ GameLib.API.EntityManager.FromObjectEntityManager = function(objectEntityManager } ); + // var apiSystems = objectEntityManager.systems.map( + // function (objectSystem) { + // return GameLib.API.System.FromObjectComponent(objectSystem); + // } + // ); + return new GameLib.API.EntityManager( objectEntityManager.id, objectEntityManager.name, apiEntities, +// apiSystems, objectEntityManager.parentEntity ); }; diff --git a/src/game-lib-d3-api-input-editor.js b/src/game-lib-d3-api-input-editor.js index 1b77b7f..08ac365 100644 --- a/src/game-lib-d3-api-input-editor.js +++ b/src/game-lib-d3-api-input-editor.js @@ -3,13 +3,7 @@ * @param id String * @param name String * @param domElement - * @param domContainer - * @param editor GameLib.D3.API.Editor * @param camera - * @param widthOffset - * @param heightOffset - * @param containerWidthOffset - * @param containerHeightOffset * @param selectDelayMs * @param parentEntity * @constructor @@ -18,13 +12,7 @@ GameLib.D3.API.Input.Editor = function ( id, name, domElement, -// domContainer, - editor, camera, - widthOffset, - heightOffset, - containerWidthOffset, - containerHeightOffset, selectDelayMs, parentEntity ) { @@ -32,7 +20,6 @@ GameLib.D3.API.Input.Editor = function ( this, GameLib.Component.COMPONENT_INPUT_EDITOR, { - 'editor' : GameLib.D3.Editor, 'camera' : GameLib.D3.Camera }, null, @@ -53,42 +40,12 @@ GameLib.D3.API.Input.Editor = function ( domElement = null; } this.domElement = domElement; - // - // if (GameLib.Utils.UndefinedOrNull(domContainer)) { - // domContainer = null; - // } - // this.domContainer = domContainer; - - if (GameLib.Utils.UndefinedOrNull(editor)) { - editor = null; - } - this.editor = editor; if (GameLib.Utils.UndefinedOrNull(camera)) { camera = null; } this.camera = camera; - if (GameLib.Utils.UndefinedOrNull(widthOffset)) { - widthOffset = 400; - } - this.widthOffset = widthOffset; - - if (GameLib.Utils.UndefinedOrNull(heightOffset)) { - heightOffset = 0; - } - this.heightOffset = heightOffset; - - if (GameLib.Utils.UndefinedOrNull(containerWidthOffset)) { - containerWidthOffset = 0; - } - this.containerWidthOffset = containerWidthOffset; - - if (GameLib.Utils.UndefinedOrNull(containerHeightOffset)) { - containerHeightOffset = 80; - } - this.containerHeightOffset = containerHeightOffset; - if (GameLib.Utils.UndefinedOrNull(selectDelayMs)) { selectDelayMs = 300; } @@ -109,13 +66,7 @@ GameLib.D3.API.Input.Editor.FromObjectComponent = function(objectComponent) { objectComponent.id, objectComponent.name, objectComponent.domElement, -// objectComponent.domContainer, - objectComponent.editor, objectComponent.camera, - objectComponent.widthOffset, - objectComponent.heightOffset, - objectComponent.containerWidthOffset, - objectComponent.containerHeightOffset, objectComponent.selectDelayMs, objectComponent.parentEntity ); diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js index 7525a50..cb3b203 100644 --- a/src/game-lib-d3-api-light.js +++ b/src/game-lib-d3-api-light.js @@ -69,52 +69,52 @@ GameLib.D3.API.Light = function( } this.intensity = intensity; - if (typeof position == 'undefined') { + if (GameLib.Utils.UndefinedOrNull(position)) { position = new GameLib.API.Vector3(0,10,0); } this.position = position; - if (typeof targetPosition == 'undefined') { + if (GameLib.Utils.UndefinedOrNull(targetPosition)) { targetPosition = new GameLib.API.Vector3(0,0,0); } this.targetPosition = targetPosition; - if (typeof quaternion == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(quaternion)){ quaternion = new GameLib.API.Quaternion(); } this.quaternion = quaternion; - if (typeof rotation == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(rotation)){ rotation = new GameLib.API.Vector3(0,0,0); } this.rotation = rotation; - if (typeof scale == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(scale)){ scale = new GameLib.API.Vector3(1,1,1); } this.scale = scale; - if (typeof distance == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(distance)){ distance = 0; } this.distance = distance; - if (typeof decay == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(decay)){ decay = 1; } this.decay = decay; - if (typeof power == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(power)){ power = 4 * Math.PI; } this.power = power; - if (typeof angle == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(angle)){ angle = Math.PI / 3; } this.angle = angle; - if (typeof penumbra == 'undefined'){ + if (GameLib.Utils.UndefinedOrNull(penumbra)){ penumbra = 0; } this.penumbra = penumbra; diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 46bda52..0e91d60 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -25,50 +25,17 @@ GameLib.D3.Input.Editor = function ( apiInputEditor.id, apiInputEditor.name, apiInputEditor.domElement, - // apiInputEditor.domContainer, - apiInputEditor.editor, apiInputEditor.camera, - apiInputEditor.widthOffset, - apiInputEditor.heightOffset, - apiInputEditor.containerWidthOffset, - apiInputEditor.containerHeightOffset, apiInputEditor.selectDelayMs, apiInputEditor.parentEntity ); - // if (GameLib.Utils.UndefinedOrNull(dom)) { - // console.warn('Cannot create window resize event without handle to the DOM'); - // dom = null; - // // throw new Error('Cannot create Input without an handle to the DOM'); - // } - // this.dom = dom; - if (this.domElement instanceof GameLib.API.DomElement) { this.domElement = new GameLib.DomElement( this.domElement ) } - // if (this.domContainer instanceof GameLib.API.DomElement) { - // this.domContainer = new GameLib.DomElement( - // this.domContainer - // ) - // } - - /** - * Cannot create a new Editor object here - it will cause an infinite loop - * - * Parent runtime objects can create their child runtime objects - but child runtime objects should not - * create their parent runtime objects. - * - if (this.editor instanceof GameLib.D3.API.Editor) { - this.editor = new GameLib.D3.Editor( - this.graphics, - this.editor - ) - } - */ - if (this.camera instanceof GameLib.D3.API.Camera) { this.camera = new GameLib.D3.Camera( this.graphics, @@ -123,23 +90,23 @@ GameLib.D3.Input.Editor.prototype.createInstance = function(update) { ) } - this.domElement.instance.addEventListener( - 'mousemove', - this.mouseMove, - false - ); - - this.domElement.instance.addEventListener( - 'contextmenu', - this.contextMenu, - false - ); - - this.domElement.instance.addEventListener( - 'mousedown', - this.mouseDown, - false - ); + // this.domElement.instance.addEventListener( + // 'mousemove', + // this.mouseMove, + // false + // ); + // + // this.domElement.instance.addEventListener( + // 'contextmenu', + // this.contextMenu, + // false + // ); + // + // this.domElement.instance.addEventListener( + // 'mousedown', + // this.mouseDown, + // false + // ); this.domElement.instance.addEventListener( 'keydown', @@ -171,13 +138,7 @@ GameLib.D3.Input.Editor.prototype.toApiComponent = function() { this.id, this.name, this.domElementId, - // this.domContainerId, - GameLib.Utils.IdOrNull(this.editor), GameLib.Utils.IdOrNull(this.camera), - this.widthOffset, - this.heightOffset, - this.containerWidthOffset, - this.containerHeightOffset, this.selectDelayMs, GameLib.Utils.IdOrNull(this.parentEntity) ); @@ -236,6 +197,12 @@ GameLib.D3.Input.Editor.prototype.onWindowResize = function() { */ GameLib.D3.Input.Editor.prototype.onKeyPress = function(event) { + console.log('keypressed ' + event.code); + + if (event.code === 'KeyV') { + //todo - change view + }; + if (event.code == "KeyQ") { this.editor.allSelected = !this.editor.allSelected; diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index ac3b875..f177b5e 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -4,25 +4,25 @@ * @param apiLight GameLib.D3.API.Light * @constructor */ -GameLib.D3.Light = function Light( - graphics, - apiLight +GameLib.D3.Light = function( + graphics, + apiLight ) { - this.graphics = graphics; - this.graphics.isNotThreeThrow(); + this.graphics = graphics; + this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(apiLight)) { - apiLight = {}; - } + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = {}; + } if (apiLight instanceof GameLib.D3.Light) { return apiLight; } - GameLib.D3.API.Light.call( - this, - apiLight.id, - apiLight.lightType, + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.lightType, apiLight.name, apiLight.color, apiLight.intensity, @@ -35,41 +35,47 @@ GameLib.D3.Light = function Light( apiLight.decay, apiLight.power, apiLight.angle, - apiLight.penumbra, - apiLight.parentEntity - ); + apiLight.penumbra, + apiLight.parentEntity + ); - this.color = new GameLib.Color( - graphics, - this.color, - this - ); + this.color = new GameLib.Color( + graphics, + this.color, + this + ); - this.position = new GameLib.Vector3( - graphics, - this.position, - this - ); + this.position = new GameLib.Vector3( + graphics, + this.position, + this + ); - this.targetPosition = new GameLib.Vector3( - graphics, - this.targetPosition, - this - ); + this.targetPosition = new GameLib.Vector3( + graphics, + this.targetPosition, + this + ); - this.scale = new GameLib.Vector3( - graphics, - this.scale, - this - ); + this.scale = new GameLib.Vector3( + graphics, + this.scale, + this + ); - this.quaternion = new GameLib.Quaternion( - graphics, - this.quaternion, - this - ); + this.quaternion = new GameLib.Quaternion( + graphics, + this.quaternion, + this + ); - this.instance = this.createInstance(); + this.rotation = new GameLib.Vector3( + graphics, + this.rotation, + this + ); + + this.instance = this.createInstance(); }; GameLib.D3.Light.prototype = Object.create(GameLib.D3.API.Light.prototype); @@ -90,106 +96,105 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; */ GameLib.D3.Light.prototype.createInstance = function(update) { + var instance = null; - var instance = null; + if (update) { + instance = this.instance; + } - if (update) { - instance = this.instance; - } + if (!instance) { - if (!instance) { + if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT || + this.lightType === 'AmbientLight' + ) { + instance = new THREE.AmbientLight( + this.color.instance, + this.intensity + ); + } - if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT || - this.lightType === 'AmbientLight' - ) { - instance = new THREE.AmbientLight( - this.color.instance, - this.intensity - ); - } + if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || + this.lightType === 'DirectionalLight' + ) { + instance = new THREE.DirectionalLight( + this.color.instance, + this.intensity + ); + } - if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || - this.lightType === 'DirectionalLight' - ) { - instance = new THREE.DirectionalLight( - this.color.instance, - this.intensity - ); - } + if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT || + this.lightType === 'PointLight' + ) { + instance = new THREE.PointLight( + this.color.instance, + this.intensity + ); + instance.distance = this.distance; + instance.decay = this.decay; + } - if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT || - this.lightType === 'PointLight' - ) { - instance = new THREE.PointLight( - this.color.instance, - this.intensity - ); - instance.distance = this.distance; - instance.decay = this.decay; - } + if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT || + this.lightType === 'SpotLight' + ) { + instance = new THREE.SpotLight( + this.color.instance, + this.intensity + ); + instance.distance = this.distance; + instance.angle = this.angle; + instance.penumbra = this.penumbra; + instance.decay = this.decay; + } + } - if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT || - this.lightType === 'SpotLight' - ) { - instance = new THREE.SpotLight( - this.color.instance, - this.intensity - ); - instance.distance = this.distance; - instance.angle = this.angle; - instance.penumbra = this.penumbra; - instance.decay = this.decay; - } - } + if (!instance) { + console.warn('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?'); + throw new Error('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?'); + } - if (!instance) { - console.warn('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?'); - throw new Error('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?'); - } + instance.gameLibObject = this; - instance.gameLibObject = this; + instance.name = this.name; - instance.name = this.name; + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + instance.scale.x = this.scale.x; + instance.scale.y = this.scale.y; + instance.scale.z = this.scale.z; - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; + if (instance.target) { + instance.target.position.x = this.targetPosition.x; + instance.target.position.y = this.targetPosition.y; + instance.target.position.z = this.targetPosition.z; + } - if (instance.target) { - instance.target.position.x = this.targetPosition.x; - instance.target.position.y = this.targetPosition.y; - instance.target.position.z = this.targetPosition.z; - } + instance.quaternion.x = this.quaternion.x; + instance.quaternion.y = this.quaternion.y; + instance.quaternion.z = this.quaternion.z; + instance.quaternion.w = this.quaternion.w; - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; + instance.intensity = this.intensity; - instance.intensity = this.intensity; + instance.color.set(this.color.toHex()); - instance.color.set(this.color.toHex()); - - return instance; + return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Light.prototype.updateInstance = function() { - this.instance = this.createInstance(true); + this.instance = this.createInstance(true); }; GameLib.D3.Light.prototype.clone = function() { - return _.cloneDeep(this); + return _.cloneDeep(this); }; /** @@ -197,24 +202,24 @@ GameLib.D3.Light.prototype.clone = function() { * @returns {GameLib.D3.API.Light} */ GameLib.D3.Light.prototype.toApiLight = function() { - return new GameLib.D3.API.Light( - this.id, - this.lightType, - this.name, - this.color.toApiColor(), - this.intensity, - this.position.toApiVector(), - this.targetPosition.toApiVector(), - this.quaternion.toApiQuaternion(), - this.rotation, - this.scale.toApiVector(), - this.distance, - this.decay, - this.power, - this.angle, - this.penumbra, - GameLib.Utils.IdOrNull(this.parentEntity) - ); + return new GameLib.D3.API.Light( + this.id, + this.lightType, + this.name, + this.color.toApiColor(), + this.intensity, + this.position.toApiVector(), + this.targetPosition.toApiVector(), + this.quaternion.toApiQuaternion(), + this.rotation, + this.scale.toApiVector(), + this.distance, + this.decay, + this.power, + this.angle, + this.penumbra, + GameLib.Utils.IdOrNull(this.parentEntity) + ); }; /** @@ -225,9 +230,9 @@ GameLib.D3.Light.prototype.toApiLight = function() { */ GameLib.D3.Light.FromObjectLight = function(graphics, objectLight) { - return new GameLib.D3.Light( - graphics, - GameLib.D3.API.Light.FromObjectLight(objectLight) - ); + return new GameLib.D3.Light( + graphics, + GameLib.D3.API.Light.FromObjectLight(objectLight) + ); }; \ No newline at end of file diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index 544c03f..2b2c690 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -57,7 +57,9 @@ GameLib.D3.Renderer.prototype.createInstance = function(update) { if (update) { instance = this.instance; } else { - instance = new THREE.WebGLRenderer(); + instance = new THREE.WebGLRenderer({ + canvas : this.domElement.instance + }); } instance.localClippingEnabled = this.localClipping; @@ -121,7 +123,6 @@ GameLib.D3.Renderer.prototype.render = function() { GameLib.D3.Renderer.prototype.setSize = function(width, height) { this.width = width; this.height = height; - this.updateInstance(); }; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 6ece800..a45f58a 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -25,6 +25,7 @@ GameLib.EntityManager = function( apiEntityManager.id, apiEntityManager.name, apiEntityManager.entities, + // apiEntityManager.systems, apiEntityManager.parentEntity ); @@ -44,7 +45,23 @@ GameLib.EntityManager = function( }.bind(this) ); - this.buildIdToObject(); + // this.systems = this.systems.map( + // function(apiSystem) { + // + // if (apiSystem instanceof GameLib.API.System) { + // return new GameLib.System( + // this.graphics, + // apiSystem + // ) + // } else { + // console.warn('System not of type API.System'); + // throw new Error('System not of type API.System'); + // } + // + // }.bind(this) + // ); + + this.buildIdToObject(); this.instance = this.createInstance(); }; @@ -64,7 +81,7 @@ GameLib.EntityManager.prototype.createInstance = function() { }; /** - * Creates an GameLib.Entity + * Creates an GameLib.Entity and adds it to entities array * @returns {*} */ GameLib.EntityManager.prototype.createEntity = function(name) { @@ -86,12 +103,20 @@ GameLib.EntityManager.prototype.createEntity = function(name) { /** * Adds an entity to this manager - * @param entity + * @param entity GameLib.Entity */ GameLib.EntityManager.prototype.addEntity = function(entity) { this.entities.push(entity); }; +// /** +// * Adds a system to this manager +// * @param system GameLib.System +// */ +// GameLib.EntityManager.prototype.addSystem = function(system) { +// this.systems.push(system); +// }; + /** * Returns entity by name * @param name @@ -156,72 +181,6 @@ GameLib.EntityManager.prototype.query = function(components) { ); return entities; - - // var result = this.entities.reduce( - // - // function(__queryComponents) { - // - // return function(result, entity) { - // - // var results = __queryComponents.reduce( - // - // function(__entity) { - // - // return function(componentResult, queryComponent) { - // - // var components = __entity.components.reduce( - // - // function(__queryComponent) { - // - // return function(__components, entityComponent) { - // - // if (__queryComponent === entityComponent.constructor) { - // // arrow should point towards a window or sergej ---> - // __components[entityComponent.id] = entityComponent; - // } - // - // return __components; - // - // } - // }(queryComponent), - // {} - // ); - // - // for (var property in components) { - // if (components.hasOwnProperty(property)) { - // componentResult[property] = components[property]; - // } - // } - // - // return componentResult; - // } - // }(entity), - // {} - // - // ); - // - // for (var property in results) { - // if (results.hasOwnProperty(property)) { - // result[property] = results[property]; - // } - // } - // - // return result; - // - // } - // }(components), - // {} - // ); - // - // var array = []; - // - // for (var property in result) { - // if (result.hasOwnProperty(property)) { - // array.push(result[property]); - // } - // } - // - // return array; }; /** @@ -258,10 +217,17 @@ GameLib.EntityManager.prototype.toApiEntityManager = function() { } ); + // var apiSystems = this.systems.map( + // function (system) { + // return system.toApiSystem(); + // } + // ); + var apiEntityManager = new GameLib.API.EntityManager( this.id, this.name, apiEntities, + // apiSystems, this.parentEntity ); diff --git a/src/game-lib-system.js b/src/game-lib-system.js index 011c8b0..ad12aea 100644 --- a/src/game-lib-system.js +++ b/src/game-lib-system.js @@ -76,13 +76,13 @@ GameLib.System.prototype.start = function() { //this.domElement.instance.appendChild(this.domStats.instance); } - this.renderers = this.entityManager.queryComponents(GameLib.D3.Renderer); + // this.renderers = this.entityManager.queryComponents(GameLib.D3.Renderer); - this.renderers.forEach( - function (renderer) { - renderer.domElement.instance.appendChild(renderer.instance.domElement); - }.bind(this) - ); + // this.renderers.forEach( + // function (renderer) { + // renderer.domElement.instance.appendChild(renderer.instance.domElement); + // }.bind(this) + // ); this.renderEntities = this.entityManager.query( [ @@ -180,11 +180,18 @@ GameLib.System.prototype.update = function(deltaTime) { var scenes = renderEntity.getComponent(GameLib.D3.Scene); + if (scenes.length > 1) { + renderer.instance.autoClear = false; + } + + renderer.instance.clear(); + scenes.map(function(scene){ renderer.instance.render( scene.instance, camera.instance ); + renderer.instance.clearDepth(); }); } );