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

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;
// 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;
// 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

View File

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

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.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,
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.

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,
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.

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

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

View File

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

View File

@ -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;

View File

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