diff --git a/src/game-lib-d3-api-graphics.js b/bak/game-lib-d3-api-graphics.js similarity index 66% rename from src/game-lib-d3-api-graphics.js rename to bak/game-lib-d3-api-graphics.js index 1d941a8..e704a7d 100644 --- a/src/game-lib-d3-api-graphics.js +++ b/bak/game-lib-d3-api-graphics.js @@ -7,9 +7,6 @@ * @constructor */ GameLib.D3.API.Graphics = function ( - id, - name, - graphicsType, parentEntity ) { @@ -21,20 +18,7 @@ GameLib.D3.API.Graphics = function ( parentEntity ); - if (GameLib.Utils.UndefinedOrNull(id)) { - id = GameLib.Utils.RandomId(); - } - this.id = id; - if (GameLib.Utils.UndefinedOrNull(name)) { - name = 'Graphics (' + id + ')'; - } - this.name = name; - - if (GameLib.Utils.UndefinedOrNull(graphicsType)) { - graphicsType = GameLib.D3.Graphics.GRAPHICS_TYPE_THREE; - } - this.graphicsType = graphicsType; }; GameLib.D3.API.Graphics.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-component-a.js b/src/game-lib-component-a.js index 4eaa86d..81d021a 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-component-a.js @@ -47,6 +47,7 @@ GameLib.Component.COMPONENT_VIEWPORT = 0x13; GameLib.Component.COMPONENT_SYSTEM = 0x14; GameLib.Component.COMPONENT_GRAPHICS = 0x15; GameLib.Component.COMPONENT_HELPER = 0x16; +GameLib.Component.COMPONENT_CUSTOM_CODE = 0x17; /** * Components are linked at runtime - for storing, we just store the ID diff --git a/src/game-lib-d3-api-custom-code.js b/src/game-lib-d3-api-custom-code.js new file mode 100644 index 0000000..832fc09 --- /dev/null +++ b/src/game-lib-d3-api-custom-code.js @@ -0,0 +1,74 @@ +/** + * This component makes the parentEntity (ex. car) follow the path provided by the spline + * @param id String + * @param name String + * @param code String + * @param domElementId + * @param parentEntity + * @param args + * @constructor + */ +GameLib.D3.API.CustomCode = function ( + id, + name, + code, + domElementId, + parentEntity, + args +) { + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_CUSTOM_CODE, + { + 'args' : [GameLib.Component] + }, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'CustomCode (' + this.id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(code)) { + code = ''; + } + this.code = code; + + if (GameLib.Utils.UndefinedOrNull(domElementId)) { + domElementId = "CustomCode_" + this.id; + } + this.domElementId = domElementId; + + if (GameLib.Utils.UndefinedOrNull(args)) { + args = []; + } + this.args = args; +}; + +GameLib.D3.API.CustomCode.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.CustomCode.prototype.constructor = GameLib.D3.API.CustomCode; + +/** + * Object to GameLib.D3.API.CustomCode + * @param objectComponent + * @returns {GameLib.D3.API.CustomCode} + * @constructor + */ +GameLib.D3.API.CustomCode.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.CustomCode( + objectComponent.id, + objectComponent.name, + objectComponent.code, + objectComponent.domElementId, + objectComponent.parentEntity, + objectComponent.args + ); +}; diff --git a/src/game-lib-d3-api-editor.js b/src/game-lib-d3-api-editor.js index 4ab673d..00e0d88 100644 --- a/src/game-lib-d3-api-editor.js +++ b/src/game-lib-d3-api-editor.js @@ -2,7 +2,7 @@ * Raw Editor API object - should always correspond with the Editor Schema * @param id * @param name - * @param games [GameLib.API.D3.Game] + * @param game [GameLib.API.D3.Game] * @param allSelected * @param selectedObjects * @param viewports @@ -12,7 +12,7 @@ GameLib.D3.API.Editor = function( id, name, - games, + game, allSelected, selectedObjects, viewports, @@ -21,7 +21,9 @@ GameLib.D3.API.Editor = function( GameLib.Component.call( this, GameLib.Component.COMPONENT_EDITOR, - null, + { + 'game' : GameLib.D3.Game + }, null, parentEntity ); @@ -36,10 +38,10 @@ GameLib.D3.API.Editor = function( } this.name = name; - if (GameLib.Utils.UndefinedOrNull(games)) { - games = []; + if (GameLib.Utils.UndefinedOrNull(game)) { + game = null; } - this.games = games; + this.game = game; if (GameLib.Utils.UndefinedOrNull(allSelected)) { allSelected = false; @@ -70,7 +72,7 @@ GameLib.D3.API.Editor.FromObjectEditor = function(objectEditor) { return new GameLib.D3.API.Editor( objectEditor.id, objectEditor.name, - objectEditor.games, + objectEditor.game, objectEditor.allSelected, objectEditor.selectedObjects, objectEditor.viewports, diff --git a/src/game-lib-d3-api-input-editor.js b/src/game-lib-d3-api-input-editor.js index 731b685..29f1c60 100644 --- a/src/game-lib-d3-api-input-editor.js +++ b/src/game-lib-d3-api-input-editor.js @@ -37,18 +37,13 @@ GameLib.D3.API.Input.Editor = function ( parentEntity ); - this.meshMoveMode = false; - this.meshMoveXMode = false; - this.meshMoveYMode = false; - this.meshMoveZMode = false; - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } this.id = id; if (GameLib.Utils.UndefinedOrNull(name)) { - name = this.constructor.name; + name = 'Input Editor (' + this.id + ')'; } this.name = name; @@ -62,6 +57,11 @@ GameLib.D3.API.Input.Editor = function ( } this.domContainerId = domContainerId; + if (GameLib.Utils.UndefinedOrNull(editor)) { + editor = null; + } + this.editor = editor; + if (GameLib.Utils.UndefinedOrNull(camera)) { camera = null; } diff --git a/src/game-lib-d3-coder.js b/src/game-lib-d3-coder.js new file mode 100644 index 0000000..3c75bab --- /dev/null +++ b/src/game-lib-d3-coder.js @@ -0,0 +1,79 @@ +/** + * Coder + * @param id + * @param name + * @param coderType + * @constructor + */ +GameLib.D3.Coder = function Coder( + id, + name, + coderType +) { + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Coder (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(coderType)) { + coderType = GameLib.D3.Coder.CODER_TYPE_CODE_MIRROR; + } + this.coderType = coderType; + + this.instance = this.createInstance(); +}; + +GameLib.D3.Coder.prototype = Object.create(GameLib.D3.API.Coder.prototype); +GameLib.D3.Coder.prototype.constructor = GameLib.D3.Coder; + +/** + * GameLib.D3.Coder Types + * @type {number} + */ +GameLib.D3.Coder.CODER_TYPE_CODE_MIRROR = 0x1; + +/** + * @returns {THREE.Coder} + */ +GameLib.D3.Coder.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } else { + instance = CodeMirror; + } + + return instance; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Coder.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * True if THREE physics + * @returns {boolean} + */ +GameLib.D3.Coder.prototype.isCodeMirror = function() { + return (this.coderType == GameLib.D3.Coder.CODER_TYPE_CODE_MIRROR) +}; + +/** + * Logs a warning and throws an error if not cannon + */ +GameLib.D3.Coder.prototype.isNotCodeMirrorThrow = function() { + if (this.coderType != GameLib.D3.Coder.CODER_TYPE_CODE_MIRROR) { + console.warn('Only CodeMirror supported for this function'); + throw new Error('Only CodeMirror supported for this function'); + } +}; diff --git a/src/game-lib-d3-custom-code.js b/src/game-lib-d3-custom-code.js new file mode 100644 index 0000000..5d587ec --- /dev/null +++ b/src/game-lib-d3-custom-code.js @@ -0,0 +1,98 @@ +/** + * Creates a CustomCode object + * @param apiCustomCode GameLib.D3.API.CustomCode + * @constructor + */ +GameLib.D3.CustomCode = function( + apiCustomCode +) { + + if (GameLib.Utils.UndefinedOrNull(apiCustomCode)) { + apiCustomCode = {}; + } + + GameLib.D3.API.CustomCode.call( + this, + apiCustomCode.id, + apiCustomCode.name, + apiCustomCode.code, + apiCustomCode.domElementId, + apiCustomCode.parentEntity, + apiCustomCode.args + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.CustomCode.prototype = Object.create(GameLib.D3.API.CustomCode.prototype); +GameLib.D3.CustomCode.prototype.constructor = GameLib.D3.CustomCode; + +/** + * Creates a camera instance of 'graphics' type (only THREE for now) + * @returns {THREE.CustomCode} + */ +GameLib.D3.CustomCode.prototype.createInstance = function(update) { + + var instance = function(deltaTime) { + this.args['deltaTime'] = deltaTime; + var f = new Function(this.code).apply(this.parentEntity, this.args); + f(); + }; + + return instance; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.CustomCode.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * Converts a GameLib.D3.CustomCode to a new GameLib.D3.API.CustomCode + * @returns {GameLib.D3.API.CustomCode} + */ +GameLib.D3.CustomCode.prototype.toApiCustomCode = function() { + + var apiArgs = []; + + if (this.args) { + apiArgs = this.args.map( + function(arg) { + return GameLib.Utils.IdOrNull(arg); + } + ) + } + + return new GameLib.D3.API.CustomCode( + this.id, + this.name, + this.code, + this.domElementId, + GameLib.Utils.IdOrNull(this.parentEntity), + apiArgs + ); + +}; + +/** + * Converts from an Object CustomCode to a GameLib.D3.CustomCode + * @param objectCustomCode Object + * @returns {GameLib.D3.CustomCode} + * @constructor + */ +GameLib.D3.CustomCode.FromObjectCustomCode = function(objectCustomCode) { + + var apiCustomCode = GameLib.D3.API.CustomCode.FromObjectCustomCode(objectCustomCode); + + return new GameLib.D3.CustomCode( + apiCustomCode + ); + +}; + + +GameLib.D3.CustomCode.prototype.update = function(deltaTime) { + this.instance(deltaTime); +}; \ No newline at end of file diff --git a/src/game-lib-d3-editor.js b/src/game-lib-d3-editor.js index 4fecbd09..2396d98 100644 --- a/src/game-lib-d3-editor.js +++ b/src/game-lib-d3-editor.js @@ -2,12 +2,13 @@ * Creates a Editor object * @param graphics GameLib.D3.Graphics * @param apiEditor GameLib.D3.API.Editor + * @param onSelectionChanged * @constructor */ GameLib.D3.Editor = function( graphics, apiEditor, - onSelectionChange + onSelectionChanged ) { this.graphics = graphics; @@ -21,29 +22,26 @@ GameLib.D3.Editor = function( this, apiEditor.id, apiEditor.name, - apiEditor.games, + apiEditor.game, apiEditor.allSelected, apiEditor.selectedObjects, apiEditor.viewports, apiEditor.parentEntity ); - this.games = this.games.map( - function(apiGame) { - - if (apiGame instanceof GameLib.D3.API.Game) { - return GameLib.D3.Game( - this.graphics, - apiGame - ) - } else { - console.warn('Game not of type API.Game'); - throw new Error('Game not of type API.Game'); - } - - }.bind(this) - ); - + if (this.game) { + if (this.game instanceof GameLib.D3.API.Game) { + this.game = new GameLib.D3.Game( + this.graphics, + this.game + ) + } + else { + console.warn('Game not of type API.Game'); + throw new Error('Game not of type API.Game'); + } + } + this.viewports = this.viewports.map( function(apiViewport) { @@ -60,10 +58,10 @@ GameLib.D3.Editor = function( }.bind(this) ); - if (GameLib.Utils.UndefinedOrNull(onSelectionChange)) { - onSelectionChange = null; + if (GameLib.Utils.UndefinedOrNull(onSelectionChanged)) { + onSelectionChanged = null; } - this.onSelectionChange = onSelectionChange; + this.onSelectionChanged = onSelectionChanged; this.instance = this.createInstance(); }; @@ -99,18 +97,14 @@ GameLib.D3.Editor.prototype.updateInstance = function() { */ GameLib.D3.Editor.prototype.toApiEditor = function() { - var apiGames = []; - if (this.games) { - apiGames = this.games.map( - function(game) { - if (game instanceof GameLib.D3.Game) { - return game.toApiGame(); - } else { - console.warn('Game not an instance of Game'); - throw new Error('Game not an instance of Game'); - } - } - ); + var apiGame = null; + if (this.game) { + if (this.game instanceof GameLib.D3.Game) { + apiGame = this.game.toApiGame(); + } else { + console.warn('Game not an instance of Game'); + throw new Error('Game not an instance of Game'); + } } var apiViewports = []; @@ -130,7 +124,7 @@ GameLib.D3.Editor.prototype.toApiEditor = function() { return new GameLib.D3.API.Editor( this.id, this.name, - apiGames, + apiGame, this.allSelected, this.selectedObjects, apiViewports, diff --git a/src/game-lib-d3-game.js b/src/game-lib-d3-game.js index 28e683f..5228bee 100644 --- a/src/game-lib-d3-game.js +++ b/src/game-lib-d3-game.js @@ -186,28 +186,28 @@ GameLib.D3.Game.prototype.createInstance = function(update) { return instance; }; -GameLib.D3.Game.prototype.setSize = function(width, height) { - - // var w = 0; - // var h = 0; - - this.viewports.map( - function(viewport) { - // w = viewport.width; - // h = viewport.height; - // - // //TODO : calculate width and height decrease or increase ratio and adjust viewport x and y offset according - // var wx = width / w; - // var hx = height / h; - - viewport.width = width; - viewport.height = height; - - viewport.updateInstance(); - } - ) - -}; +// GameLib.D3.Game.prototype.setSize = function(width, height) { +// +// // var w = 0; +// // var h = 0; +// +// this.viewports.map( +// function(viewport) { +// // w = viewport.width; +// // h = viewport.height; +// // +// // //TODO : calculate width and height decrease or increase ratio and adjust viewport x and y offset according +// // var wx = width / w; +// // var hx = height / h; +// +// viewport.width = width; +// viewport.height = height; +// +// viewport.updateInstance(); +// } +// ) +// +// }; /** * Updates the instance with the current state diff --git a/src/game-lib-d3-graphics.js b/src/game-lib-d3-graphics.js index 731df4d..5d6dd28 100644 --- a/src/game-lib-d3-graphics.js +++ b/src/game-lib-d3-graphics.js @@ -1,23 +1,29 @@ /** - * Graphics Superset - * @param apiGraphics + * Graphics + * @param id + * @param name + * @param graphicsType * @constructor */ GameLib.D3.Graphics = function Graphics( - apiGraphics + id, + name, + graphicsType ) { - - if (GameLib.Utils.UndefinedOrNull(apiGraphics)) { - apiGraphics = {}; + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); } - - GameLib.D3.API.Graphics.call( - this, - apiGraphics.id, - apiGraphics.name, - apiGraphics.graphicsType, - apiGraphics.parentEntity - ); + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Graphics (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(graphicsType)) { + graphicsType = GameLib.D3.Graphics.GRAPHICS_TYPE_THREE; + } + this.graphicsType = graphicsType; this.instance = this.createInstance(); }; @@ -54,37 +60,6 @@ GameLib.D3.Graphics.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; -/** - * Converts a GameLib.D3.Graphics to a new GameLib.D3.API.Graphics - * @returns {GameLib.D3.API.Graphics} - */ -GameLib.D3.Graphics.prototype.toApiGraphics = function() { - return new GameLib.D3.API.Graphics( - this.id, - this.name, - this.graphicsType, - this.parentEntity - ); -}; - -/** - * Converts from an Object Graphics to a GameLib.D3.Graphics - * @param graphics GameLib.D3.Graphics - * @param objectGraphics Object - * @returns {GameLib.D3.Graphics} - * @constructor - */ -GameLib.D3.Graphics.FromObjectGraphics = function(graphics, objectGraphics) { - - var apiGraphics = GameLib.D3.API.Graphics.FromObjectComponent(objectGraphics); - - return new GameLib.D3.Graphics( - graphics, - apiGraphics - ); - -}; - /** * True if THREE physics * @returns {boolean} diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 8a2a5dc..96af077 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -42,6 +42,11 @@ GameLib.D3.Input.Editor = function ( this.element = null; this.container = null; + this.meshMoveMode = false; + this.meshMoveXMode = false; + this.meshMoveYMode = false; + this.meshMoveZMode = false; + /** * We need new function pointers with scope bound to this so we can remove the * window event handlers when we need to @@ -61,48 +66,50 @@ GameLib.D3.Input.Editor.prototype.constructor = GameLib.D3.Input.Editor; GameLib.D3.Input.Editor.prototype.createInstance = function(update) { - var instance = null; - - if (update) { - instance = this.instance; - return instance; - } - - instance = this.dom.document.getElementById(this.domElementId); - - if (!instance) { + this.element = this.dom.document.getElementById(this.domElementId); + if (!this.element) { console.warn('Could not locate DOM element with ID: ' + this.domElementId); throw new Error('Could not locate DOM element with ID: ' + this.domElementId); } - this.element = instance; - this.container = this.dom.document.getElementById(this.domContainerId); - if (!this.container) { console.warn('Could not locate DOM container with ID: ' + this.domContainerId); throw new Error('Could not locate DOM container with ID: ' + this.domContainerId); } - instance.addEventListener( + var instance = null; + + if (update) { + instance = this.instance; + instance.camera = this.camera.instance; + return instance; + } else { + instance = new THREE.EditorControls( + this.camera.instance, + this.element + ) + } + + this.element.addEventListener( 'mousemove', this.mouseMove, false ); - instance.addEventListener( + this.element.addEventListener( 'contextmenu', this.contextMenu, false ); - instance.addEventListener( + this.element.addEventListener( 'mousedown', this.mouseDown, false ); - instance.addEventListener( + this.element.addEventListener( 'keydown', this.keyPress, false @@ -131,8 +138,13 @@ 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, GameLib.Utils.IdOrNull(this.parentEntity) ); @@ -154,14 +166,26 @@ GameLib.D3.Input.Editor.prototype.onWindowResize = function() { this.container.style.height = (this.window.innerHeight - this.containerHeightOffset) + 'px'; this.container.style.width = (this.window.innerWidth - this.containerWidthOffset) + 'px'; - this.games.map( - function(width, height) { - return function(game) { - game.width = width; - game.height = height; - game.updateInstance(); - } - }(this.window.innerWidth - this.widthOffset, this.window.innerHeight - this.heightOffset) + var width = this.window.innerWidth - this.widthOffset; + + var height = this.window.innerHeight - this.heightOffset; + + //TODO: map the relative viewport sizes and offsets with the size differences + + this.editor.viewports.map( + function(viewport) { + viewport.width = width; + viewport.height = height; + viewport.updateInstance(); + } + ); + + this.editor.game.viewports.map( + function(viewport) { + viewport.width = width; + viewport.height = height; + viewport.updateInstance(); + } ); // // this.scene.cameras[this.scene.activeCameraIndex].aspect = () / window.innerHeight; diff --git a/src/game-lib-d3-viewport.js b/src/game-lib-d3-viewport.js index af14f39..0ce61c4 100644 --- a/src/game-lib-d3-viewport.js +++ b/src/game-lib-d3-viewport.js @@ -60,6 +60,8 @@ GameLib.D3.Viewport = function ( GameLib.D3.Viewport.prototype = Object.create(GameLib.D3.API.Viewport.prototype); GameLib.D3.Viewport.prototype.constructor = GameLib.D3.Viewport; +//GameLib.D3.Viewport.VIEWPORT_TYPE_GAME = 0x1; + /** * * @param update diff --git a/src/game-lib-system.js b/src/game-lib-system.js index d9cae52..c0d8ca5 100644 --- a/src/game-lib-system.js +++ b/src/game-lib-system.js @@ -90,7 +90,7 @@ GameLib.System.prototype.start = function() { * @callback * @override */ -GameLib.System.prototype.update = function() { +GameLib.System.prototype.update = function(deltaTime) { if (this.systemType == GameLib.System.SYSTEM_TYPE_INPUT) { this.driveInputObjects.forEach( @@ -100,14 +100,6 @@ GameLib.System.prototype.update = function() { ); } - if (this.systemType == GameLib.System.SYSTEM_TYPE_RENDER) { - this.viewports.forEach( - function (viewport) { - viewport.update(deltaTime); - } - ); - } - if (this.systemType == GameLib.System.SYSTEM_TYPE_ANIMATION) { this.pathFollowingObjects.forEach( function(object) { @@ -146,6 +138,14 @@ GameLib.System.prototype.update = function() { ); } + if (this.systemType == GameLib.System.SYSTEM_TYPE_RENDER) { + this.viewports.forEach( + function (viewport) { + viewport.update(deltaTime); + } + ); + } + }; /**