From db4e22522790207c436fcb4e908d7e2d9bf28b58 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Wed, 18 Jan 2017 16:03:44 +0100 Subject: [PATCH] game objects to API --- src/game-lib-api-matrix4.js | 45 +++++++--------- src/game-lib-api-vector4.js | 2 +- src/game-lib-d3-api-game.js | 12 ++--- src/game-lib-d3-api-helper.js | 2 +- src/game-lib-d3-editor.js | 73 +++++++++++++++++++++++++- src/game-lib-d3-game.js | 99 ++++++++++++++++++++++++++++++++--- src/game-lib-d3-light.js | 20 +++++-- src/game-lib-d3-scene.js | 1 + 8 files changed, 208 insertions(+), 46 deletions(-) diff --git a/src/game-lib-api-matrix4.js b/src/game-lib-api-matrix4.js index 15ea6ab..3a2d4dc 100644 --- a/src/game-lib-api-matrix4.js +++ b/src/game-lib-api-matrix4.js @@ -62,32 +62,25 @@ GameLib.API.Matrix4 = function ApiMatrix4( * @constructor */ GameLib.API.Matrix4.FromObjectMatrix = function(objectMatrix) { - return new GameLib.API.Matrix4( - new GameLib.API.Vector4( - objectMatrix.rows[0].x, - objectMatrix.rows[0].y, - objectMatrix.rows[0].z, - objectMatrix.rows[0].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[1].x, - objectMatrix.rows[1].y, - objectMatrix.rows[1].z, - objectMatrix.rows[1].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[2].x, - objectMatrix.rows[2].y, - objectMatrix.rows[2].z, - objectMatrix.rows[2].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[3].x, - objectMatrix.rows[3].y, - objectMatrix.rows[3].z, - objectMatrix.rows[3].w - ) - ) + + if (objectMatrix.rows) { + return new GameLib.API.Matrix4( + GameLib.API.Vector4.FromObjectVector(objectMatrix.rows[0]), + GameLib.API.Vector4.FromObjectVector(objectMatrix.rows[1]), + GameLib.API.Vector4.FromObjectVector(objectMatrix.rows[2]), + GameLib.API.Vector4.FromObjectVector(objectMatrix.rows[3]) + ); + } else if (objectMatrix instanceof Array) { + return new GameLib.API.Matrix4( + GameLib.API.Vector4.FromObjectVector(objectMatrix[0]), + GameLib.API.Vector4.FromObjectVector(objectMatrix[1]), + GameLib.API.Vector4.FromObjectVector(objectMatrix[2]), + GameLib.API.Vector4.FromObjectVector(objectMatrix[3]) + ); + } else { + console.warn('Unsupported object matrix type - whats your DB version?'); + throw new Error('Unsupported object matrix type - whats your DB version?'); + } }; GameLib.API.Matrix4.prototype.rotationMatrixX = function (radians) { diff --git a/src/game-lib-api-vector4.js b/src/game-lib-api-vector4.js index 2afe8d0..5aad9d5 100644 --- a/src/game-lib-api-vector4.js +++ b/src/game-lib-api-vector4.js @@ -27,7 +27,7 @@ GameLib.API.Vector4 = function (x, y, z, w) { * @constructor */ GameLib.API.Vector4.FromObjectVector = function (objectVector) { - return new GameLib.API.Vector2( + return new GameLib.API.Vector4( objectVector.x, objectVector.y, objectVector.z, diff --git a/src/game-lib-d3-api-game.js b/src/game-lib-d3-api-game.js index 4728421..6beb951 100644 --- a/src/game-lib-d3-api-game.js +++ b/src/game-lib-d3-api-game.js @@ -153,7 +153,7 @@ GameLib.D3.API.Game.FromObjectGame = function(objectGame) { if (objectGame.scenes) { apiScenes = objectGame.scenes.map( function(objectScene){ - return GameLib.API.Scene.FromObjectScene(objectScene); + return GameLib.D3.API.Scene.FromObjectScene(objectScene); } ); } @@ -161,7 +161,7 @@ GameLib.D3.API.Game.FromObjectGame = function(objectGame) { if (objectGame.cameras) { apiCameras = objectGame.cameras.map( function(objectCamera){ - return GameLib.API.Camera.FromObjectCamera(objectCamera); + return GameLib.D3.API.Camera.FromObjectCamera(objectCamera); } ); } @@ -169,7 +169,7 @@ GameLib.D3.API.Game.FromObjectGame = function(objectGame) { if (objectGame.renderers) { apiRenderers = objectGame.renderers.map( function(objectRenderer){ - return GameLib.API.Renderer.FromObjectComponent(objectRenderer); + return GameLib.D3.API.Renderer.FromObjectComponent(objectRenderer); } ); } @@ -177,7 +177,7 @@ GameLib.D3.API.Game.FromObjectGame = function(objectGame) { if (objectGame.composers) { apiComposers = objectGame.composers.map( function(objectComposer){ - return GameLib.API.Composer.FromObjectComponent(objectComposer); + return GameLib.D3.API.Composer.FromObjectComponent(objectComposer); } ); } @@ -193,13 +193,13 @@ GameLib.D3.API.Game.FromObjectGame = function(objectGame) { if (objectGame.viewports) { apiViewports = objectGame.viewports.map( function(objectViewport){ - return GameLib.API.Viewport.FromObjectViewport(objectViewport); + return GameLib.D3.API.Viewport.FromObjectViewport(objectViewport); } ); } if (objectGame.entityManager) { - apiEntityManager = GameLib.API.Entity.FromObjectEntity(objectGame.entityManager); + apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectGame.entityManager); } if (objectGame.mouse) { diff --git a/src/game-lib-d3-api-helper.js b/src/game-lib-d3-api-helper.js index 9984ebb..3bc0c9d 100644 --- a/src/game-lib-d3-api-helper.js +++ b/src/game-lib-d3-api-helper.js @@ -17,7 +17,7 @@ GameLib.D3.API.Helper = function ( GameLib.Component.call( this, - GameLib.Component.COMPONENT_PASS, + GameLib.Component.COMPONENT_HELPER, null, null, parentEntity diff --git a/src/game-lib-d3-editor.js b/src/game-lib-d3-editor.js index 2396d98..059086a 100644 --- a/src/game-lib-d3-editor.js +++ b/src/game-lib-d3-editor.js @@ -8,7 +8,9 @@ GameLib.D3.Editor = function( graphics, apiEditor, - onSelectionChanged + onSelectionChanged, + onSelectObject, + onDeSelectObject ) { this.graphics = graphics; @@ -63,6 +65,16 @@ GameLib.D3.Editor = function( } this.onSelectionChanged = onSelectionChanged; + if (GameLib.Utils.UndefinedOrNull(onSelectObject)) { + onSelectObject = null; + } + this.onSelectObject = onSelectObject; + + if (GameLib.Utils.UndefinedOrNull(onDeSelectObject)) { + onDeSelectObject = null; + } + this.onDeSelectObject = onDeSelectObject; + this.instance = this.createInstance(); }; @@ -150,3 +162,62 @@ GameLib.D3.Editor.FromObjectEditor = function(graphics, objectEditor) { ); }; + +/** + * Selects a GameLib Object + * @param object GameLib.* + */ +GameLib.D3.Editor.prototype.selectObject = function(object) { + + this.selectedObjects.push( + new GameLib.D3.SelectedObject( + this.graphics, + object, + new GameLib.D3.Helper( + this.graphics, + new GameLib.D3.API.Helper( + null, + null, + null, + object + ) + ) + ) + ); + + if (this.onSelectObject) { + this.onSelectObject(); + } + +}; + +/** + * Selects a GameLib Object + * @param object GameLib.* + */ +GameLib.D3.Editor.prototype.deSelectObject = function(object) { + + var results = this.selectedObjects.reduce( + function(results, selectedObject) { + + if (selectedObject.object.id == object.id) { + results.removed = selectedObject; + } else { + results.rest.push(selectedObject); + } + + return results; + }, + { + removed : null, + rest : [] + } + ); + + this.selectedObjects = results.rest; + + if (this.onDeSelectObject) { + this.onDeSelectObject(results); + } + +}; \ No newline at end of file diff --git a/src/game-lib-d3-game.js b/src/game-lib-d3-game.js index 9b99b02..c768893 100644 --- a/src/game-lib-d3-game.js +++ b/src/game-lib-d3-game.js @@ -50,9 +50,11 @@ GameLib.D3.Game = function ( function(apiScene) { if (apiScene instanceof GameLib.D3.API.Scene) { - return GameLib.D3.Scene( + return new GameLib.D3.Scene( this.graphics, - apiScene + apiScene, + this.imageFactory, + true ) } else { console.warn('Scene not of type API.Scene'); @@ -66,7 +68,7 @@ GameLib.D3.Game = function ( function(apiCamera) { if (apiCamera instanceof GameLib.D3.API.Camera) { - return GameLib.D3.Camera( + return new GameLib.D3.Camera( this.graphics, apiCamera ) @@ -82,7 +84,7 @@ GameLib.D3.Game = function ( function(apiRenderer) { if (apiRenderer instanceof GameLib.D3.API.Renderer) { - return GameLib.D3.Renderer( + return new GameLib.D3.Renderer( this.graphics, apiRenderer ) @@ -98,7 +100,7 @@ GameLib.D3.Game = function ( function(apiComposer) { if (apiComposer instanceof GameLib.D3.API.Composer) { - return GameLib.D3.Composer( + return new GameLib.D3.Composer( this.graphics, apiComposer ) @@ -114,7 +116,7 @@ GameLib.D3.Game = function ( function(apiSystem) { if (apiSystem instanceof GameLib.D3.API.System) { - return GameLib.D3.System( + return new GameLib.D3.System( this.graphics, apiSystem ) @@ -130,7 +132,7 @@ GameLib.D3.Game = function ( function(apiViewport) { if (apiViewport instanceof GameLib.D3.API.Viewport) { - return GameLib.D3.Viewport( + return new GameLib.D3.Viewport( this.graphics, apiViewport ) @@ -392,3 +394,86 @@ GameLib.D3.Game.FromObjectGame = function(graphics, objectGame) { ); }; + +/** + * Loads a Game + * @param graphics + * @param objectGame + * @param onLoaded + * @constructor + */ +GameLib.D3.Game.LoadGame = function( + graphics, + objectGame, + onLoaded +) { + var game = GameLib.D3.Game.FromObjectGame( + graphics, + objectGame + ); + + onLoaded(game); +}; + +/** + * Loads a Game from the API + * @param graphics GameLib.D3.Graphics + * @param partialGameObject Object + * @param onLoaded callback + * @param apiUrl + * @returns {*} + * @constructor + */ +GameLib.D3.Game.LoadGameFromApi = function( + graphics, + partialGameObject, + onLoaded, + apiUrl +) { + + /** + * First check if this is a client or server side request + */ + if (typeof XMLHttpRequest == 'undefined') { + console.warn('Implement server side loading from API here'); + return onLoaded( + null, + new Error('Not Implemented') + ); + } + + var xhr = new XMLHttpRequest(); + xhr.open( + 'GET', + apiUrl + '/game/load' + partialGameObject.path + '/' + partialGameObject.name + ); + + xhr.onreadystatechange = function(xhr) { + + return function() { + + if (xhr.readyState == 4) { + + try { + var response = JSON.parse(xhr.responseText); + } catch (e) { + return onLoaded(null, new Error('Could not load game : ' + e.message)); + } + + if (!response.game || response.game.length == 0) { + return onLoaded(null, new Error('Could not load game')); + } + + var objectGame = response.game[0]; + + GameLib.D3.Game.LoadGame( + graphics, + objectGame, + onLoaded + ); + } + } + }(xhr); + + xhr.send(); +}; diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index c9bb59d..cda1c28 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -94,21 +94,30 @@ GameLib.D3.Light.prototype.createInstance = function(update) { if (!instance) { - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { + 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) { + 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) { + if ( + this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT || + this.lightType == 'PointLight' + ) { instance = new THREE.PointLight( this.color.instance, this.intensity @@ -117,7 +126,10 @@ GameLib.D3.Light.prototype.createInstance = function(update) { instance.decay = this.decay; } - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) { + if ( + this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT || + this.lightType == 'SpotLight' + ) { instance = new THREE.SpotLight( this.color.instance, this.intensity diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index d75f5ef..36d4e9c 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -3,6 +3,7 @@ * created * @param graphics * @param apiScene GameLib.D3.API.Scene + * @param imageFactory * @param computeNormals * @constructor */