names and ids important for editor

beta.r3js.org
Theunis J. Botha 2016-11-14 14:48:37 +01:00
parent 431797fdaa
commit 7c62a686a8
20 changed files with 240 additions and 84 deletions

20
src/game-lib-camera.js Normal file
View File

@ -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();
};

View File

@ -1,21 +1,35 @@
/** /**
* * Creates a camera component which just assigns the position and rotation to whatever its parent position and rotation
* @param componentId * is.
* @param threeCamera * @param id
* @param name String
* @param camera GameLib.D3.Camera
* @constructor * @constructor
*/ */
GameLib.D3.ComponentCamera = function( GameLib.D3.ComponentCamera = function ComponentCamera(
componentId, id,
threeCamera 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; 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. // 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); 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 // Component fields
this.threeCamera = threeCamera; this.camera = camera.instance;
}; };
///////////////////////// Methods to override ////////////////////////// ///////////////////////// Methods to override //////////////////////////
@ -23,6 +37,6 @@ GameLib.D3.ComponentCamera.prototype.onLateUpdate = function(
deltaTime, deltaTime,
parentEntity parentEntity
) { ) {
this.threeCamera.quaternion.copy(parentEntity.quaternion); this.camera.quaternion.copy(parentEntity.quaternion);
this.threeCamera.position.copy(parentEntity.position); this.camera.position.copy(parentEntity.position);
}; };

View File

@ -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; 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. // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.

View File

@ -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; 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. // 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( GameLib.D3.ComponentFlyControls.prototype.onSetParentEntity = function(
parentScene, parentScene,
parentEntity parentEntity

View File

@ -1,35 +1,42 @@
/** /**
* *
* @param componentId * @param id
* @param name
* @param targetEntity GameLib.D3.Entity * @param targetEntity GameLib.D3.Entity
* @param targetOffset GameLib.D3.Vector3 * @param targetOffset GameLib.D3.Vector3
* @param minDistance * @param minDistance
* @param moveSpeed * @param moveSpeed
* @constructor * @constructor
*/ */
GameLib.D3.ComponentFollow = function( GameLib.D3.ComponentFollow = function ComponentFollow(
componentId, id,
name,
targetEntity, targetEntity,
targetOffset, targetOffset,
minDistance, minDistance,
moveSpeed moveSpeed
) { ) {
this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.id = id || GameLib.D3.Tools.RandomId();
this.parentEntity = null;
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. // 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); 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.targetEntity = targetEntity;
this.moveSpeed = moveSpeed || 12.5;
if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) {
targetOffset = new GameLib.D3.Vector3(0, 0, 0); targetOffset = new GameLib.D3.Vector3(0, 0, 0);
} }
this.targetOffset = targetOffset; this.targetOffset = targetOffset;
this.moveSpeed = moveSpeed || 12.5;
this.minDistance = minDistance || 0; this.minDistance = minDistance || 0;
}; };

View File

@ -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 will maybe not used
this.parentEntity = null; this.parentEntity = null;

View File

@ -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, targetEntity,
targetOffset, targetOffset,
rotationSpeed 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; 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. // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.

View File

@ -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, positionOffset,
quaternionOffset, quaternionOffset,
scaleOffset 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.parentEntity = null;
this.positionOffset = positionOffset || new GameLib.D3.Vector3(0, 0, 0); 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); 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. // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.

View File

@ -1,5 +1,6 @@
GameLib.D3.ComponentRaycastVehicleControls = function( GameLib.D3.ComponentRaycastVehicleControls = function ComponentRaycastVehicleControls(
componentId, id,
name,
frontLWheelIndex, frontLWheelIndex,
frontRWheelIndex, frontRWheelIndex,
backLWheelIndex, backLWheelIndex,
@ -7,7 +8,13 @@ GameLib.D3.ComponentRaycastVehicleControls = function(
maxForce, maxForce,
steering 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; this.parentEntity = null;
// maybe we pass this in the constructor // maybe we pass this in the constructor

View File

@ -2,12 +2,19 @@
// this component operates on it's parent entity's bounding box. // this component operates on it's parent entity's bounding box.
// so, you can't use this component with a null-mesh. // so, you can't use this component with a null-mesh.
// //
GameLib.D3.ComponentTriggerBoxSphere = function( GameLib.D3.ComponentTriggerBoxSphere = function ComponentTriggerBoxSphere(
componentId, id,
name,
entitiesToCheck, entitiesToCheck,
callback 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; 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. // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.

View File

@ -1,13 +1,27 @@
// /**
// uses the entity position + mesh bounding sphere to check if intersections * Uses the entity position + mesh bounding sphere to check if intersections
// *
GameLib.D3.ComponentTriggerSphereSphere = function( * @param id
componentId, * @param name
* @param sphereRadius
* @param entitiesToCheck
* @param callback
* @constructor
*/
GameLib.D3.ComponentTriggerSphereSphere = function ComponentTriggerSphereSphere(
id,
name,
sphereRadius, sphereRadius,
entitiesToCheck, entitiesToCheck,
callback 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; 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. // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.

View File

@ -1,17 +1,22 @@
GameLib.D3.Entity = function( GameLib.D3.Entity = function Entity(
meshId, id,
componentIds, name,
ids,
position, position,
quaternion, quaternion,
scale scale
) { ) {
this.meshId = meshId; this.id = id;
if (typeof componentIds == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(name)) {
componentIds = []; name = this.constructor.name;
} }
this.name = name;
this.componentIds = componentIds; if (GameLib.D3.Utils.UndefinedOrNull(ids)) {
ids = [];
}
this.ids = ids;
// constructed at runtime // constructed at runtime
this.parentScene = null; this.parentScene = null;
@ -40,9 +45,9 @@ GameLib.D3.Entity = function(
GameLib.D3.Entity.prototype.update = function( GameLib.D3.Entity.prototype.update = function(
deltaTime deltaTime
) { ) {
for(var c in this.componentIds) { for(var c in this.ids) {
var componentId = this.componentIds[c]; var id = this.ids[c];
var component = this.parentScene.componentIdToComponent[componentId]; var component = this.parentScene.idToComponent[id];
if(component && component.onUpdate) { if(component && component.onUpdate) {
component.onUpdate(deltaTime, this); component.onUpdate(deltaTime, this);
} }
@ -64,9 +69,9 @@ GameLib.D3.Entity.prototype.update = function(
GameLib.D3.Entity.prototype.lateUpdate = function( GameLib.D3.Entity.prototype.lateUpdate = function(
deltaTime deltaTime
) { ) {
for(var c in this.componentIds) { for(var c in this.ids) {
var componentId = this.componentIds[c]; var id = this.ids[c];
var component = this.parentScene.componentIdToComponent[componentId]; var component = this.parentScene.idToComponent[id];
if(component && component.onLateUpdate) { if(component && component.onLateUpdate) {
component.onLateUpdate(deltaTime, this); component.onLateUpdate(deltaTime, this);
} }
@ -84,9 +89,9 @@ GameLib.D3.Entity.prototype.register = function(
) { ) {
this.parentScene = parentScene; this.parentScene = parentScene;
if(this.meshId != null && parentScene.meshIdToMesh[this.meshId]) { if(this.id != null && parentScene.meshIdToMesh[this.id]) {
parentScene.instance.add(parentScene.meshIdToMesh[this.meshId]); parentScene.instance.add(parentScene.meshIdToMesh[this.id]);
this.mesh = parentScene.meshIdToMesh[this.meshId]; this.mesh = parentScene.meshIdToMesh[this.id];
} }
this.onRegistered(parentScene); this.onRegistered(parentScene);
@ -94,12 +99,12 @@ GameLib.D3.Entity.prototype.register = function(
/** /**
* Add an already registered component to the entity * Add an already registered component to the entity
* @param componentId Number * @param id Number
*/ */
GameLib.D3.Entity.prototype.addComponentId = function( 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.parentScene.registerComponent(component);
this.componentIds.push(component.componentId); this.ids.push(component.id);
if(component.setParentEntity && typeof component.setParentEntity == 'function') { if(component.setParentEntity && typeof component.setParentEntity == 'function') {
component.setParentEntity(this.parentScene, this); component.setParentEntity(this.parentScene, this);
@ -122,9 +127,9 @@ GameLib.D3.Entity.prototype.addComponent = function(
GameLib.D3.Entity.prototype.getComponent = function( GameLib.D3.Entity.prototype.getComponent = function(
componentType componentType
) { ) {
for(var c in this.componentIds) { for(var c in this.ids) {
var componentId = this.componentIds[c]; var id = this.ids[c];
var component = this.parentScene.componentIdToComponent[componentId]; var component = this.parentScene.idToComponent[id];
if (component instanceof componentType) { if (component instanceof componentType) {
return component; return component;
} }

View File

@ -34,7 +34,7 @@ GameLib.D3.Light = function(
angle, angle,
penumbra penumbra
) { ) {
this.id = id; this.id = GameLib.D3.Tools.RandomId();
this.lightType = lightType; this.lightType = lightType;
this.name = name; this.name = name;
this.color = color; this.color = color;

View File

@ -42,8 +42,7 @@ GameLib.D3.Mesh = function(
parentMeshId, parentMeshId,
parentSceneId parentSceneId
) { ) {
this.id = id; this.id = GameLib.D3.Tools.RandomId();
this.meshId = GameLib.D3.Tools.RandomId();
this.path = path; this.path = path;
this.name = name; this.name = name;
this.meshType = meshType; this.meshType = meshType;

View File

@ -14,7 +14,7 @@ GameLib.D3.RaycastVehicle = function(
this.engine = engine; this.engine = engine;
this.engine.isNotCannonThrow(); this.engine.isNotCannonThrow();
this.componentId = GameLib.D3.Tools.RandomId(); this.id = GameLib.D3.Tools.RandomId();
this.chassisBody = chassisBody; this.chassisBody = chassisBody;

View File

@ -30,7 +30,7 @@ GameLib.D3.RaycastWheel = function(
this.engine = engine; this.engine = engine;
this.engine.isNotCannonThrow(); this.engine.isNotCannonThrow();
this.componentId = GameLib.D3.Tools.RandomId(); this.id = GameLib.D3.Tools.RandomId();
if(typeof chassisConnectionPointLocal == 'undefined' || chassisConnectionPointLocal == null) { if(typeof chassisConnectionPointLocal == 'undefined' || chassisConnectionPointLocal == null) {
chassisConnectionPointLocal = new this.engine.instance.Vec3(); chassisConnectionPointLocal = new this.engine.instance.Vec3();

View File

@ -10,7 +10,7 @@ GameLib.D3.RigidBodyVehicle = function(
chassisBody, chassisBody,
wheels wheels
) { ) {
this.componentId = GameLib.D3.Tools.RandomId(); this.id = GameLib.D3.Tools.RandomId();
this.engine = engine; this.engine = engine;
this.engine.isNotCannonThrow(); this.engine.isNotCannonThrow();

View File

@ -37,7 +37,7 @@ GameLib.D3.RigidBody = function(
fixedRotation, fixedRotation,
shape shape
) { ) {
this.componentId = GameLib.D3.Tools.RandomId(); this.id = GameLib.D3.Tools.RandomId();
this.position = position || new GameLib.D3.Vector3(); this.position = position || new GameLib.D3.Vector3();
this.velocity = velocity || new GameLib.D3.Vector3(); this.velocity = velocity || new GameLib.D3.Vector3();
this.angularVelocity = angularVelocity || new GameLib.D3.Vector3(); this.angularVelocity = angularVelocity || new GameLib.D3.Vector3();

View File

@ -12,7 +12,7 @@ GameLib.D3.RigidWheel = function(
axis, axis,
direction direction
) { ) {
this.componentId = GameLib.D3.Tools.RandomId(); this.id = GameLib.D3.Tools.RandomId();
this.body = body; this.body = body;
this.position = position; this.position = position;
this.axis = axis; this.axis = axis;

View File

@ -109,7 +109,7 @@ GameLib.D3.Scene = function(
this.meshIdToMesh = {}; this.meshIdToMesh = {};
// assoc array // assoc array
this.componentIdToComponent = {}; this.idToComponent = {};
this.uploadUrl = uploadUrl; this.uploadUrl = uploadUrl;
@ -448,6 +448,10 @@ GameLib.D3.Scene.LoadScene = function(
var mesh = gameLibScene.meshes[m]; var mesh = gameLibScene.meshes[m];
if (!mesh.id) {
mesh.id = GameLib.D3.Tools.RandomId();
}
console.log("loading mesh " + mesh.name); console.log("loading mesh " + mesh.name);
var geometry = new graphics.instance.Geometry(); var geometry = new graphics.instance.Geometry();
@ -594,6 +598,8 @@ GameLib.D3.Scene.LoadScene = function(
instanceMesh.name = mesh.name; instanceMesh.name = mesh.name;
instanceMesh.gameLibObject = mesh;
instanceMesh.position.x = mesh.position.x; instanceMesh.position.x = mesh.position.x;
instanceMesh.position.y = mesh.position.y; instanceMesh.position.y = mesh.position.y;
instanceMesh.position.z = mesh.position.z; instanceMesh.position.z = mesh.position.z;
@ -648,6 +654,8 @@ GameLib.D3.Scene.LoadScene = function(
light.decay = gameLibLight.decay; light.decay = gameLibLight.decay;
} }
light.gameLibObject = gameLibLight;
light.position.x = gameLibLight.position.x; light.position.x = gameLibLight.position.x;
light.position.y = gameLibLight.position.y; light.position.y = gameLibLight.position.y;
light.position.z = gameLibLight.position.z; light.position.z = gameLibLight.position.z;
@ -776,7 +784,7 @@ GameLib.D3.Scene.prototype.render = function(
/** /**
* Registers an entity to the scene. * Registers an entity to the scene.
* This method also links the entity and it's components, * 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. * The setParentEntity and onSetParentEntity methods are being called here.
* @param entity GameLib.D3.Entity * @param entity GameLib.D3.Entity
*/ */
@ -787,10 +795,10 @@ GameLib.D3.Scene.prototype.registerEntity = function(
entity.register(this); entity.register(this);
// link components to entity // link components to entity
for(var c in entity.componentIds) { for(var c in entity.ids) {
var componentId = entity.componentIds[c]; var id = entity.ids[c];
var component = this.componentIdToComponent[componentId]; var component = this.idToComponent[id];
if(component) { if(component) {
component.setParentEntity(this, entity); component.setParentEntity(this, entity);
} }
@ -805,7 +813,7 @@ GameLib.D3.Scene.prototype.registerEntity = function(
GameLib.D3.Scene.prototype.registerComponent = function( GameLib.D3.Scene.prototype.registerComponent = function(
component component
) { ) {
this.componentIdToComponent[component.componentId] = component; this.idToComponent[component.id] = component;
if(component.onRegistered && typeof component.onRegistered == 'function') { if(component.onRegistered && typeof component.onRegistered == 'function') {
component.onRegistered(this); component.onRegistered(this);
} }