From 7c62a686a896d97a2c15274ab9c7963f8b6287fb Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Mon, 14 Nov 2016 14:48:37 +0100 Subject: [PATCH] names and ids important for editor --- src/game-lib-camera.js | 20 ++++++++ src/game-lib-component-camera.js | 34 +++++++++---- src/game-lib-component-colorflash.js | 19 +++++-- src/game-lib-component-fly-controls.js | 24 +++++++-- src/game-lib-component-follow.js | 25 +++++---- src/game-lib-component-interface.js | 18 +++++-- src/game-lib-component-look-at.js | 22 ++++++-- src/game-lib-component-mesh-permutation.js | 24 +++++++-- ...-lib-component-raycast-vehicle-controls.js | 13 +++-- src/game-lib-component-trigger-box-sphere.js | 13 +++-- ...ame-lib-component-trigger-sphere-sphere.js | 26 +++++++--- src/game-lib-entity.js | 51 ++++++++++--------- src/game-lib-light.js | 2 +- src/game-lib-mesh.js | 3 +- src/game-lib-raycast-vehicle.js | 2 +- src/game-lib-raycast-wheel.js | 2 +- src/game-lib-rigid-body-vehicle.js | 2 +- src/game-lib-rigid-body.js | 2 +- src/game-lib-rigid-wheel.js | 2 +- src/game-lib-scene.js | 20 +++++--- 20 files changed, 240 insertions(+), 84 deletions(-) create mode 100644 src/game-lib-camera.js diff --git a/src/game-lib-camera.js b/src/game-lib-camera.js new file mode 100644 index 0000000..a215802 --- /dev/null +++ b/src/game-lib-camera.js @@ -0,0 +1,20 @@ +/** + * Creates a camera object + * @param graphics + * @constructor + */ +GameLib.D3.Camera = function(graphics) { + this.graphics = graphics; + + this.graphics.isNotThreeThrow(); + + this.instance = this.createInstance(); +} ; + +/** + * Creates a camera instance of 'graphics' type (only THREE for now) + * @returns {THREE.Camera} + */ +GameLib.D3.Camera.prototype.createInstance = function() { + return new this.graphics.instance.Camera(); +}; \ No newline at end of file diff --git a/src/game-lib-component-camera.js b/src/game-lib-component-camera.js index 24ab32d..29b7e82 100644 --- a/src/game-lib-component-camera.js +++ b/src/game-lib-component-camera.js @@ -1,21 +1,35 @@ /** - * - * @param componentId - * @param threeCamera + * Creates a camera component which just assigns the position and rotation to whatever its parent position and rotation + * is. + * @param id + * @param name String + * @param camera GameLib.D3.Camera * @constructor */ -GameLib.D3.ComponentCamera = function( - componentId, - threeCamera +GameLib.D3.ComponentCamera = function ComponentCamera( + id, + name, + camera ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentCamera, GameLib.D3.ComponentInterface); + if (typeof camera == 'undefined') { + var graphics = new GameLib.D3.Graphics(GameLib.D3.Graphics.GRAPHICS_TYPE_THREE, THREE); + var camera = new GameLib.D3.Camera(graphics); + } + // Component fields - this.threeCamera = threeCamera; + this.camera = camera.instance; }; ///////////////////////// Methods to override ////////////////////////// @@ -23,6 +37,6 @@ GameLib.D3.ComponentCamera.prototype.onLateUpdate = function( deltaTime, parentEntity ) { - this.threeCamera.quaternion.copy(parentEntity.quaternion); - this.threeCamera.position.copy(parentEntity.position); + this.camera.quaternion.copy(parentEntity.quaternion); + this.camera.position.copy(parentEntity.position); }; \ No newline at end of file diff --git a/src/game-lib-component-colorflash.js b/src/game-lib-component-colorflash.js index 9e23851..1b6b4ee 100644 --- a/src/game-lib-component-colorflash.js +++ b/src/game-lib-component-colorflash.js @@ -1,7 +1,20 @@ -GameLib.D3.ComponentColorFlash = function( - componentId +/** + * + * @param id + * @param name + * @constructor + */ +GameLib.D3.ComponentColorFlash = function ComponentColorFlash( + id, + name ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. diff --git a/src/game-lib-component-fly-controls.js b/src/game-lib-component-fly-controls.js index 7c919c0..37103b7 100644 --- a/src/game-lib-component-fly-controls.js +++ b/src/game-lib-component-fly-controls.js @@ -1,7 +1,20 @@ -GameLib.D3.ComponentFlyControls = function( - componentId +/** + * + * @param id + * @param name + * @constructor + */ +GameLib.D3.ComponentFlyControls = function ComponentFlyControls( + id, + name ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. @@ -89,6 +102,11 @@ GameLib.D3.ComponentFlyControls.prototype.onUpdate = function( } }; +/** + * + * @param parentScene + * @param parentEntity + */ GameLib.D3.ComponentFlyControls.prototype.onSetParentEntity = function( parentScene, parentEntity diff --git a/src/game-lib-component-follow.js b/src/game-lib-component-follow.js index 38a40a6..08cf098 100644 --- a/src/game-lib-component-follow.js +++ b/src/game-lib-component-follow.js @@ -1,35 +1,42 @@ /** * - * @param componentId + * @param id + * @param name * @param targetEntity GameLib.D3.Entity * @param targetOffset GameLib.D3.Vector3 * @param minDistance * @param moveSpeed * @constructor */ -GameLib.D3.ComponentFollow = function( - componentId, +GameLib.D3.ComponentFollow = function ComponentFollow( + id, + name, targetEntity, targetOffset, minDistance, moveSpeed ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); - this.parentEntity = null; + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentFollow, GameLib.D3.ComponentInterface); - // + if (GameLib.D3.Utils.UndefinedOrNull(targetEntity)) { + targetEntity = new GameLib.D3.Entity(); + } this.targetEntity = targetEntity; - this.moveSpeed = moveSpeed || 12.5; - if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { targetOffset = new GameLib.D3.Vector3(0, 0, 0); } - this.targetOffset = targetOffset; + + this.moveSpeed = moveSpeed || 12.5; this.minDistance = minDistance || 0; }; diff --git a/src/game-lib-component-interface.js b/src/game-lib-component-interface.js index 788d481..3a5e11d 100644 --- a/src/game-lib-component-interface.js +++ b/src/game-lib-component-interface.js @@ -1,7 +1,19 @@ -GameLib.D3.ComponentInterface = function( - componentId +/** + * + * @param id + * @param name + * @constructor + */ +GameLib.D3.ComponentInterface = function ComponentInterface( + id, + name ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; // this will maybe not used this.parentEntity = null; diff --git a/src/game-lib-component-look-at.js b/src/game-lib-component-look-at.js index f64dc7e..258aa26 100644 --- a/src/game-lib-component-look-at.js +++ b/src/game-lib-component-look-at.js @@ -1,10 +1,26 @@ -GameLib.D3.ComponentLookAt = function( - componentId, +/** + * + * @param id + * @param name + * @param targetEntity + * @param targetOffset + * @param rotationSpeed + * @constructor + */ +GameLib.D3.ComponentLookAt = function ComponentLookAt( + id, + name, targetEntity, targetOffset, rotationSpeed ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. diff --git a/src/game-lib-component-mesh-permutation.js b/src/game-lib-component-mesh-permutation.js index c614578..6ca117d 100644 --- a/src/game-lib-component-mesh-permutation.js +++ b/src/game-lib-component-mesh-permutation.js @@ -1,14 +1,30 @@ -GameLib.D3.ComponentMeshPermutation = function( - componentId, +/** + * + * @param id + * @param name + * @param positionOffset + * @param quaternionOffset + * @param scaleOffset + * @constructor + */ +GameLib.D3.ComponentMeshPermutation = function ComponentMeshPermutation( + id, + name, positionOffset, quaternionOffset, scaleOffset ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; this.positionOffset = positionOffset || new GameLib.D3.Vector3(0, 0, 0); - this.quaternionOffset = quaternionOffset || new GameLib.D3.Quaternion(0, 0, 0, 1); + this.quaternionOffset = quaternionOffset || new GameLib.D3.Vector4(0, 0, 0, 1); this.scaleOffset = scaleOffset || new GameLib.D3.Vector3(0, 0, 0); // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. diff --git a/src/game-lib-component-raycast-vehicle-controls.js b/src/game-lib-component-raycast-vehicle-controls.js index 4e2d462..0c1852a 100644 --- a/src/game-lib-component-raycast-vehicle-controls.js +++ b/src/game-lib-component-raycast-vehicle-controls.js @@ -1,5 +1,6 @@ -GameLib.D3.ComponentRaycastVehicleControls = function( - componentId, +GameLib.D3.ComponentRaycastVehicleControls = function ComponentRaycastVehicleControls( + id, + name, frontLWheelIndex, frontRWheelIndex, backLWheelIndex, @@ -7,7 +8,13 @@ GameLib.D3.ComponentRaycastVehicleControls = function( maxForce, steering ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // maybe we pass this in the constructor diff --git a/src/game-lib-component-trigger-box-sphere.js b/src/game-lib-component-trigger-box-sphere.js index 8ac3d7f..e74c4a0 100644 --- a/src/game-lib-component-trigger-box-sphere.js +++ b/src/game-lib-component-trigger-box-sphere.js @@ -2,12 +2,19 @@ // this component operates on it's parent entity's bounding box. // so, you can't use this component with a null-mesh. // -GameLib.D3.ComponentTriggerBoxSphere = function( - componentId, +GameLib.D3.ComponentTriggerBoxSphere = function ComponentTriggerBoxSphere( + id, + name, entitiesToCheck, callback ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. diff --git a/src/game-lib-component-trigger-sphere-sphere.js b/src/game-lib-component-trigger-sphere-sphere.js index f6419db..8902f2a 100644 --- a/src/game-lib-component-trigger-sphere-sphere.js +++ b/src/game-lib-component-trigger-sphere-sphere.js @@ -1,13 +1,27 @@ -// -// uses the entity position + mesh bounding sphere to check if intersections -// -GameLib.D3.ComponentTriggerSphereSphere = function( - componentId, +/** + * Uses the entity position + mesh bounding sphere to check if intersections + * + * @param id + * @param name + * @param sphereRadius + * @param entitiesToCheck + * @param callback + * @constructor + */ +GameLib.D3.ComponentTriggerSphereSphere = function ComponentTriggerSphereSphere( + id, + name, sphereRadius, entitiesToCheck, callback ) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index dc3c4b2..0a5c524 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -1,17 +1,22 @@ -GameLib.D3.Entity = function( - meshId, - componentIds, +GameLib.D3.Entity = function Entity( + id, + name, + ids, position, quaternion, scale ) { - this.meshId = meshId; + this.id = id; - if (typeof componentIds == 'undefined') { - componentIds = []; + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = this.constructor.name; } + this.name = name; - this.componentIds = componentIds; + if (GameLib.D3.Utils.UndefinedOrNull(ids)) { + ids = []; + } + this.ids = ids; // constructed at runtime this.parentScene = null; @@ -40,9 +45,9 @@ GameLib.D3.Entity = function( GameLib.D3.Entity.prototype.update = function( deltaTime ) { - for(var c in this.componentIds) { - var componentId = this.componentIds[c]; - var component = this.parentScene.componentIdToComponent[componentId]; + for(var c in this.ids) { + var id = this.ids[c]; + var component = this.parentScene.idToComponent[id]; if(component && component.onUpdate) { component.onUpdate(deltaTime, this); } @@ -64,9 +69,9 @@ GameLib.D3.Entity.prototype.update = function( GameLib.D3.Entity.prototype.lateUpdate = function( deltaTime ) { - for(var c in this.componentIds) { - var componentId = this.componentIds[c]; - var component = this.parentScene.componentIdToComponent[componentId]; + for(var c in this.ids) { + var id = this.ids[c]; + var component = this.parentScene.idToComponent[id]; if(component && component.onLateUpdate) { component.onLateUpdate(deltaTime, this); } @@ -84,9 +89,9 @@ GameLib.D3.Entity.prototype.register = function( ) { this.parentScene = parentScene; - if(this.meshId != null && parentScene.meshIdToMesh[this.meshId]) { - parentScene.instance.add(parentScene.meshIdToMesh[this.meshId]); - this.mesh = parentScene.meshIdToMesh[this.meshId]; + if(this.id != null && parentScene.meshIdToMesh[this.id]) { + parentScene.instance.add(parentScene.meshIdToMesh[this.id]); + this.mesh = parentScene.meshIdToMesh[this.id]; } this.onRegistered(parentScene); @@ -94,12 +99,12 @@ GameLib.D3.Entity.prototype.register = function( /** * Add an already registered component to the entity - * @param componentId Number + * @param id Number */ GameLib.D3.Entity.prototype.addComponentId = function( - componentId + id ) { - this.componentIds.push(componentId); + this.ids.push(id); }; /** @@ -111,7 +116,7 @@ GameLib.D3.Entity.prototype.addComponent = function( ) { this.parentScene.registerComponent(component); - this.componentIds.push(component.componentId); + this.ids.push(component.id); if(component.setParentEntity && typeof component.setParentEntity == 'function') { component.setParentEntity(this.parentScene, this); @@ -122,9 +127,9 @@ GameLib.D3.Entity.prototype.addComponent = function( GameLib.D3.Entity.prototype.getComponent = function( componentType ) { - for(var c in this.componentIds) { - var componentId = this.componentIds[c]; - var component = this.parentScene.componentIdToComponent[componentId]; + for(var c in this.ids) { + var id = this.ids[c]; + var component = this.parentScene.idToComponent[id]; if (component instanceof componentType) { return component; } diff --git a/src/game-lib-light.js b/src/game-lib-light.js index 4a66fc3..f8fea00 100644 --- a/src/game-lib-light.js +++ b/src/game-lib-light.js @@ -34,7 +34,7 @@ GameLib.D3.Light = function( angle, penumbra ) { - this.id = id; + this.id = GameLib.D3.Tools.RandomId(); this.lightType = lightType; this.name = name; this.color = color; diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index 7cc1786..d4a99bf 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -42,8 +42,7 @@ GameLib.D3.Mesh = function( parentMeshId, parentSceneId ) { - this.id = id; - this.meshId = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Tools.RandomId(); this.path = path; this.name = name; this.meshType = meshType; diff --git a/src/game-lib-raycast-vehicle.js b/src/game-lib-raycast-vehicle.js index f84ce77..d1d4887 100644 --- a/src/game-lib-raycast-vehicle.js +++ b/src/game-lib-raycast-vehicle.js @@ -14,7 +14,7 @@ GameLib.D3.RaycastVehicle = function( this.engine = engine; this.engine.isNotCannonThrow(); - this.componentId = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Tools.RandomId(); this.chassisBody = chassisBody; diff --git a/src/game-lib-raycast-wheel.js b/src/game-lib-raycast-wheel.js index 7bb482d..c239ff0 100644 --- a/src/game-lib-raycast-wheel.js +++ b/src/game-lib-raycast-wheel.js @@ -30,7 +30,7 @@ GameLib.D3.RaycastWheel = function( this.engine = engine; this.engine.isNotCannonThrow(); - this.componentId = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Tools.RandomId(); if(typeof chassisConnectionPointLocal == 'undefined' || chassisConnectionPointLocal == null) { chassisConnectionPointLocal = new this.engine.instance.Vec3(); diff --git a/src/game-lib-rigid-body-vehicle.js b/src/game-lib-rigid-body-vehicle.js index 5a73620..8c4fb09 100644 --- a/src/game-lib-rigid-body-vehicle.js +++ b/src/game-lib-rigid-body-vehicle.js @@ -10,7 +10,7 @@ GameLib.D3.RigidBodyVehicle = function( chassisBody, wheels ) { - this.componentId = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Tools.RandomId(); this.engine = engine; this.engine.isNotCannonThrow(); diff --git a/src/game-lib-rigid-body.js b/src/game-lib-rigid-body.js index 4c94abf..cc6dcef 100644 --- a/src/game-lib-rigid-body.js +++ b/src/game-lib-rigid-body.js @@ -37,7 +37,7 @@ GameLib.D3.RigidBody = function( fixedRotation, shape ) { - this.componentId = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Tools.RandomId(); this.position = position || new GameLib.D3.Vector3(); this.velocity = velocity || new GameLib.D3.Vector3(); this.angularVelocity = angularVelocity || new GameLib.D3.Vector3(); diff --git a/src/game-lib-rigid-wheel.js b/src/game-lib-rigid-wheel.js index 3002ad0..a500ab7 100644 --- a/src/game-lib-rigid-wheel.js +++ b/src/game-lib-rigid-wheel.js @@ -12,7 +12,7 @@ GameLib.D3.RigidWheel = function( axis, direction ) { - this.componentId = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Tools.RandomId(); this.body = body; this.position = position; this.axis = axis; diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index a838638..a2cb4f0 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -109,7 +109,7 @@ GameLib.D3.Scene = function( this.meshIdToMesh = {}; // assoc array - this.componentIdToComponent = {}; + this.idToComponent = {}; this.uploadUrl = uploadUrl; @@ -448,6 +448,10 @@ GameLib.D3.Scene.LoadScene = function( var mesh = gameLibScene.meshes[m]; + if (!mesh.id) { + mesh.id = GameLib.D3.Tools.RandomId(); + } + console.log("loading mesh " + mesh.name); var geometry = new graphics.instance.Geometry(); @@ -594,6 +598,8 @@ GameLib.D3.Scene.LoadScene = function( instanceMesh.name = mesh.name; + instanceMesh.gameLibObject = mesh; + instanceMesh.position.x = mesh.position.x; instanceMesh.position.y = mesh.position.y; instanceMesh.position.z = mesh.position.z; @@ -648,6 +654,8 @@ GameLib.D3.Scene.LoadScene = function( light.decay = gameLibLight.decay; } + light.gameLibObject = gameLibLight; + light.position.x = gameLibLight.position.x; light.position.y = gameLibLight.position.y; light.position.z = gameLibLight.position.z; @@ -776,7 +784,7 @@ GameLib.D3.Scene.prototype.render = function( /** * Registers an entity to the scene. * This method also links the entity and it's components, - * if they are defined inside the componentIds array. + * if they are defined inside the ids array. * The setParentEntity and onSetParentEntity methods are being called here. * @param entity GameLib.D3.Entity */ @@ -787,10 +795,10 @@ GameLib.D3.Scene.prototype.registerEntity = function( entity.register(this); // link components to entity - for(var c in entity.componentIds) { - var componentId = entity.componentIds[c]; + for(var c in entity.ids) { + var id = entity.ids[c]; - var component = this.componentIdToComponent[componentId]; + var component = this.idToComponent[id]; if(component) { component.setParentEntity(this, entity); } @@ -805,7 +813,7 @@ GameLib.D3.Scene.prototype.registerEntity = function( GameLib.D3.Scene.prototype.registerComponent = function( component ) { - this.componentIdToComponent[component.componentId] = component; + this.idToComponent[component.id] = component; if(component.onRegistered && typeof component.onRegistered == 'function') { component.onRegistered(this); }