API components

beta.r3js.org
Theunis J. Botha 2016-11-28 15:05:02 +01:00
parent 93f90964a2
commit 50a3abf1ae
7 changed files with 519 additions and 59 deletions

View File

@ -24,12 +24,11 @@ GameLib.D3.ComponentCamera = function ComponentCamera(
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);
camera = new GameLib.D3.Camera(graphics);
camera = null;
}
// Component fields
this.camera = camera.instance;
this.camera = camera;
};
///////////////////////// Methods to override //////////////////////////
@ -37,6 +36,11 @@ GameLib.D3.ComponentCamera.prototype.onLateUpdate = function(
deltaTime,
parentEntity
) {
this.camera.quaternion.copy(parentEntity.quaternion);
this.camera.position.copy(parentEntity.position);
if (this.camera) {
this.camera.instance.quaternion.copy(parentEntity.quaternion);
this.camera.instance.position.copy(parentEntity.position);
} else {
console.warn('Camera Component Camera not set!');
throw new Error('Camera Component Camera not set!');
}
};

View File

@ -19,6 +19,291 @@ GameLib.D3.ComponentInterface = function ComponentInterface(
this.parentEntity = null;
};
GameLib.D3.ComponentInterface.COMPONENT_INTERFACE = 0x1;
GameLib.D3.ComponentInterface.COMPONENT_CAMERA = 0x2;
GameLib.D3.ComponentInterface.COMPONENT_COLOR_FLASH = 0x3;
GameLib.D3.ComponentInterface.COMPONENT_COLOR_LERP = 0x4;
GameLib.D3.ComponentInterface.COMPONENT_FLY_CONTROLS = 0x5;
GameLib.D3.ComponentInterface.COMPONENT_FOLLOW = 0x6;
GameLib.D3.ComponentInterface.COMPONENT_LOOK_AT = 0x7;
GameLib.D3.ComponentInterface.COMPONENT_MESH_PERMUTATION = 0x8;
GameLib.D3.ComponentInterface.COMPONENT_PATH_AI = 0x9;
GameLib.D3.ComponentInterface.COMPONENT_PATH_CONTROLS = 0xA;
GameLib.D3.ComponentInterface.COMPONENT_PATH_FOLLOWING = 0xB;
GameLib.D3.ComponentInterface.COMPONENT_RAYCAST_VEHICLE_CONTROLS = 0xC;
GameLib.D3.ComponentInterface.COMPONENT_TRIGGER_BOX_BOX = 0xD;
GameLib.D3.ComponentInterface.COMPONENT_TRIGGER_BOX_SPHERE = 0xE;
GameLib.D3.ComponentInterface.COMPONENT_TRIGGER_SPHERE_SPHERE = 0xF;
GameLib.D3.ComponentInterface.COMPONENT_VEHICLE_AI_OBJECT_AVOIDENCE = 0x10;
GameLib.D3.ComponentInterface.COMPONENT_VEHICLE_AI_PATH_STEERING = 0x11;
/**
* Superset for storing Components to API, and generating them from componentType
* @param id
* @param name
* @param componentType
* @param camera
* @param startColor
* @param endColor
* @param lerpSpeed
* @param targetEntity
* @param targetOffset
* @param minDistance
* @param moveSpeed
* @param rotationSpeed
* @param positionOffset
* @param quaternionOffset
* @param scaleOffset
* @param sensorLength
* @param spline
* @param normalSpline
* @param acceleration
* @param maxSpeed
* @param baseOffset
* @param maxOffset
* @param steeringSpeed
* @param frontLWheelIndex
* @param frontRWheelIndex
* @param backLWheelIndex
* @param backRWheelIndex
* @param maxForce
* @param steering
* @param entitiesToCheck
* @param onInside
* @param onEnter
* @param onLeave
* @param onSetParent
* @param sphereRadius
* @param world
* @param maxSteerAngle
* @constructor
*/
GameLib.D3.ComponentInterface.API = function(
id,
name,
componentType,
camera,
startColor,
endColor,
lerpSpeed,
targetEntity,
targetOffset,
minDistance,
moveSpeed,
rotationSpeed,
positionOffset,
quaternionOffset,
scaleOffset,
sensorLength,
spline,
normalSpline,
acceleration,
maxSpeed,
baseOffset,
maxOffset,
steeringSpeed,
frontLWheelIndex,
frontRWheelIndex,
backLWheelIndex,
backRWheelIndex,
maxForce,
steering,
entitiesToCheck,
onInside,
onEnter,
onLeave,
onSetParent,
sphereRadius,
world,
maxSteerAngle
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'component-unnamed';
}
this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(componentType)) {
componentType = GameLib.D3.ComponentInterface.COMPONENT_INTERFACE;
}
this.componentType = componentType;
if (GameLib.D3.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.camera = camera;
if (GameLib.D3.Utils.UndefinedOrNull(startColor)) {
startColor = new GameLib.D3.Vector3(0, 0, 0);
}
this.startColor = startColor;
if (GameLib.D3.Utils.UndefinedOrNull(endColor)) {
endColor = new GameLib.D3.Vector3(1, 1, 1);
}
this.endColor = endColor;
if (GameLib.D3.Utils.UndefinedOrNull(lerpSpeed)) {
lerpSpeed = 1.0;
}
this.lerpSpeed = lerpSpeed;
if (GameLib.D3.Utils.UndefinedOrNull(targetEntity)) {
targetEntity = null;
}
this.targetEntity = targetEntity;
if (GameLib.D3.Utils.UndefinedOrNull(targetOffset)) {
targetOffset = new GameLib.D3.Vector3(0, 0, 0);
}
this.targetOffset = targetOffset;
if (GameLib.D3.Utils.UndefinedOrNull(minDistance)) {
minDistance = 0;
}
this.minDistance = minDistance;
if (GameLib.D3.Utils.UndefinedOrNull(moveSpeed)) {
moveSpeed = 12.5;
}
this.moveSpeed = moveSpeed;
if (GameLib.D3.Utils.UndefinedOrNull(rotationSpeed)) {
rotationSpeed = 22.0;
}
this.rotationSpeed = rotationSpeed;
if (GameLib.D3.Utils.UndefinedOrNull(positionOffset)) {
positionOffset = new GameLib.D3.Vector3();
}
this.positionOffset = positionOffset;
if (GameLib.D3.Utils.UndefinedOrNull(quaternionOffset)) {
quaternionOffset = new GameLib.D3.Vector4();
}
this.quaternionOffset = quaternionOffset;
if (GameLib.D3.Utils.UndefinedOrNull(scaleOffset)) {
scaleOffset = new GameLib.D3.Vector3();
}
this.scaleOffset = scaleOffset;
if (GameLib.D3.Utils.UndefinedOrNull(sensorLength)) {
sensorLength = 5;
}
this.sensorLength = sensorLength;
if (GameLib.D3.Utils.UndefinedOrNull(spline)) {
spline = new GameLib.D3.Spline.API(null, 'spline');
}
this.spline = spline;
if (GameLib.D3.Utils.UndefinedOrNull(normalSpline)) {
normalSpline = new GameLib.D3.Spline.API(null, 'normal-spline');
}
this.normalSpline = normalSpline;
if (GameLib.D3.Utils.UndefinedOrNull(acceleration)) {
acceleration = 2.0;
}
this.acceleration = acceleration;
if (GameLib.D3.Utils.UndefinedOrNull(maxSpeed)) {
maxSpeed = 10.0;
}
this.maxSpeed = maxSpeed;
if (GameLib.D3.Utils.UndefinedOrNull(baseOffset)) {
baseOffset = new GameLib.D3.Vector3();
}
this.baseOffset = baseOffset;
if (GameLib.D3.Utils.UndefinedOrNull(maxOffset)) {
maxOffset = new GameLib.D3.Vector3(10, 10, 10);
}
this.maxOffset = maxOffset;
if (GameLib.D3.Utils.UndefinedOrNull(steeringSpeed)) {
steeringSpeed = 1.0;
}
this.steeringSpeed = steeringSpeed;
if (GameLib.D3.Utils.UndefinedOrNull(frontLWheelIndex)) {
frontLWheelIndex = 1;
}
this.frontLWheelIndex = frontLWheelIndex;
if (GameLib.D3.Utils.UndefinedOrNull(frontRWheelIndex)) {
frontRWheelIndex = 2;
}
this.frontRWheelIndex = frontRWheelIndex;
if (GameLib.D3.Utils.UndefinedOrNull(backLWheelIndex)) {
backLWheelIndex = 3;
}
this.backLWheelIndex = backLWheelIndex;
if (GameLib.D3.Utils.UndefinedOrNull(backRWheelIndex)) {
backRWheelIndex = 4;
}
this.backRWheelIndex = backRWheelIndex;
if (GameLib.D3.Utils.UndefinedOrNull(maxForce)) {
maxForce = 400;
}
this.maxForce = maxForce;
if (GameLib.D3.Utils.UndefinedOrNull(steering)) {
steering = 0.5;
}
this.steering = steering;
if (GameLib.D3.Utils.UndefinedOrNull(entitiesToCheck)) {
entitiesToCheck = [];
}
this.entitiesToCheck = entitiesToCheck;
if (GameLib.D3.Utils.UndefinedOrNull(onInside)) {
onInside = null;
}
this.onInside = onInside;
if (GameLib.D3.Utils.UndefinedOrNull(onEnter)) {
onEnter = null;
}
this.onEnter = onEnter;
if (GameLib.D3.Utils.UndefinedOrNull(onLeave)) {
onLeave = null;
}
this.onLeave = onLeave;
if (GameLib.D3.Utils.UndefinedOrNull(onSetParent)) {
onSetParent = null;
}
this.onSetParent = onSetParent;
if (GameLib.D3.Utils.UndefinedOrNull(sphereRadius)) {
sphereRadius = 1.0;
}
this.sphereRadius = sphereRadius;
if (GameLib.D3.Utils.UndefinedOrNull(world)) {
world = null;
}
this.world = world;
if (GameLib.D3.Utils.UndefinedOrNull(maxSteerAngle)) {
maxSteerAngle = 0.5;
}
this.maxSteerAngle = maxSteerAngle;
};
// will be not used
GameLib.D3.ComponentInterface.prototype.setParentEntity = function(
parentScene,

View File

@ -2,8 +2,8 @@
*
* @param id
* @param name
* @param splineCurve3
* @param accel
* @param spline
* @param acceleration
* @param maxSpeed
* @param baseOffset
* @param maxOffset
@ -13,9 +13,9 @@
GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
id,
name,
splineCurve3,
normalSplineCurve3,
accel,
spline,
normalSpline,
acceleration,
maxSpeed,
baseOffset,
maxOffset,
@ -30,11 +30,11 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
this.name = name;
this.parentEntity = null;
this.splineCurve3 = splineCurve3;
this.normalSplineCurve3 = normalSplineCurve3;
this.spline = spline;
this.normalSpline = normalSpline;
this.maxSpeed = maxSpeed || 10.0;
this.accel = accel || 2.0;
this.acceleration = acceleration || 2.0;
this.baseOffset = baseOffset || new GameLib.D3.Vector3();
this.maxOffset = maxOffset || new GameLib.D3.Vector3(10, 10, 10);
@ -59,7 +59,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
parentEntity
) {
if(this.splineCurve3 && this.normalSplineCurve3) {
if(this.spline && this.normalSpline) {
if(this.currentPathValue >= 1 || this.currentPathValue < 0) {
this.currentPathValue = 0;
@ -67,21 +67,21 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
//To maintain a constant speed, you use .getPointAt( t ) instead of .getPoint( t ).
//http://stackoverflow.com/questions/18400667/three-js-object-following-a-spline-path-rotation-tanget-issues-constant-sp
var position = this.splineCurve3.getPointAt(this.currentPathValue);
var position = this.spline.getPointAt(this.currentPathValue);
var nextIndex = this.currentPathValue + 0.005;
if(nextIndex > 1.0) {
nextIndex = 0;
}
var nextPoint = this.splineCurve3.getPointAt(nextIndex);
var normal = this.normalSplineCurve3.getPointAt(this.currentPathValue).normalize();
var nextPoint = this.spline.getPointAt(nextIndex);
var normal = this.normalSpline.getPointAt(this.currentPathValue).normalize();
var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal);
var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix);
{ // update current speed
var t = deltaTime * this.accel;
var t = deltaTime * this.acceleration;
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t;
}
@ -143,7 +143,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
if(!this.splineCurve3) {
if(!this.spline) {
console.error("NO PATH GIVEN");
}
};

View File

@ -2,13 +2,13 @@
*
* @param id
* @param name
* @param physicsWorld
* @param world GameLib.D3.World
* @constructor
*/
GameLib.D3.ComponentVehicleAIObjectAvoidance = function(
id,
name,
physicsWorld
world
) {
this.id = id|| GameLib.D3.Tools.RandomId();
if (typeof name == 'undefined') {
@ -20,7 +20,7 @@ GameLib.D3.ComponentVehicleAIObjectAvoidance = function(
GameLib.D3.Utils.Extend(GameLib.D3.ComponentVehicleAIObjectAvoidance, GameLib.D3.ComponentInterface);
this.raycastVehicleComponent = null;
this.physicsWorld = physicsWorld || null;
this.physicsWorld = world || null;
this.sensors = [];
// debug

View File

@ -1,4 +1,44 @@
GameLib.D3.Entity = function Entity(
/**
* Entity Runtime
* @param apiEntity
* @param parentScene
* @param mesh
* @constructor
*/
GameLib.D3.Entity = function(
apiEntity,
parentScene,
mesh
) {
for (var property in apiEntity) {
if (apiEntity.hasOwnProperty(property)) {
this[property] = apiEntity[property];
}
}
if (GameLib.D3.Utils.UndefinedOrNull(parentScene)) {
parentScene = null;
}
this.parentScene = parentScene;
if (GameLib.D3.Utils.UndefinedOrNull(mesh)) {
mesh = null;
}
this.mesh = mesh;
};
/**
* Entity API
* @param id
* @param name
* @param ids
* @param position
* @param quaternion
* @param scale
* @constructor
*/
GameLib.D3.Entity.API = function Entity(
id,
name,
ids,
@ -6,6 +46,9 @@ GameLib.D3.Entity = function Entity(
quaternion,
scale
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
@ -18,17 +61,13 @@ GameLib.D3.Entity = function Entity(
}
this.ids = ids;
// constructed at runtime
this.parentScene = null;
this.mesh = null;
if(GameLib.D3.Utils.UndefinedOrNull(position)) {
position = new GameLib.D3.Vector3(0, 0, 0);
position = new GameLib.D3.Vector3();
}
this.position = position;
if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) {
quaternion = new GameLib.D3.Vector4(0, 0, 0, 1);
quaternion = new GameLib.D3.Vector4();
}
this.quaternion = quaternion;
@ -45,18 +84,21 @@ GameLib.D3.Entity = function Entity(
GameLib.D3.Entity.prototype.update = function(
deltaTime
) {
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);
for (var componentId in this.ids) {
if (this.ids.hasOwnProperty(componentId)) {
var id = this.ids[componentId];
var component = this.parentScene.idToComponent[id];
if (component && component.onUpdate) {
component.onUpdate(deltaTime, this);
}
}
}
if(this.mesh) {
if (this.mesh) {
this.mesh.position.set(this.position.x, this.position.y, this.position.z);
this.mesh.quaternion.set(this.quaternion.x, this.quaternion.y, this.quaternion.z, this.quaternion.w);
this.mesh.scale.set(this.scale.x, this.scale.y, this.scale.z);
this.mesh.updateInstance();
}
this.onUpdate(deltaTime);
@ -69,11 +111,13 @@ GameLib.D3.Entity.prototype.update = function(
GameLib.D3.Entity.prototype.lateUpdate = function(
deltaTime
) {
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);
for (var componentId in this.ids) {
if (this.ids.hasOwnProperty(componentId)) {
var id = this.ids[componentId];
var component = this.parentScene.idToComponent[id];
if (component && component.onLateUpdate) {
component.onLateUpdate(deltaTime, this);
}
}
}
@ -87,14 +131,14 @@ GameLib.D3.Entity.prototype.lateUpdate = function(
GameLib.D3.Entity.prototype.register = function(
parentScene
) {
this.parentScene = parentScene;
// this.parentScene = parentScene;
if(this.id != null && parentScene.meshIdToMesh[this.id]) {
parentScene.instance.add(parentScene.meshIdToMesh[this.id]);
this.mesh = parentScene.meshIdToMesh[this.id];
}
// if (this.id != null && parentScene.meshIdToMesh[this.id]) {
// parentScene.instance.add(parentScene.meshIdToMesh[this.id]);
// this.mesh = parentScene.meshIdToMesh[this.id];
// }
this.onRegistered(parentScene);
this.onRegistered(this.parentScene);
};
/**
@ -127,11 +171,13 @@ GameLib.D3.Entity.prototype.addComponent = function(
GameLib.D3.Entity.prototype.getComponent = function(
componentType
) {
for(var c in this.ids) {
var id = this.ids[c];
var component = this.parentScene.idToComponent[id];
if (component instanceof componentType) {
return component;
for (var componentId in this.ids) {
if (this.ids.hasOwnProperty(componentId)) {
var id = this.ids[componentId];
var component = this.parentScene.idToComponent[id];
if (component instanceof componentType) {
return component;
}
}
}

View File

@ -4,13 +4,18 @@
* @param graphics
* @param progressCallback
* @param apiScene GameLib.D3.Scene.API
* @param imageFactory
* @param meshIdToMesh
* @param idToComponent
* @constructor
*/
GameLib.D3.Scene = function Scene(
graphics,
progressCallback,
apiScene,
imageFactory
imageFactory,
meshIdToMesh,
idToComponent
) {
for (var property in apiScene) {
@ -22,11 +27,15 @@ GameLib.D3.Scene = function Scene(
this.graphics = graphics;
this.graphics.isNotThreeThrow();
// assoc array
this.meshIdToMesh = {};
if (GameLib.D3.Utils.UndefinedOrNull(meshIdToMesh)) {
meshIdToMesh = {};
}
this.meshIdToMesh = meshIdToMesh;
// assoc array
this.idToComponent = {};
if (GameLib.D3.Utils.UndefinedOrNull(idToComponent)) {
idToComponent = {};
}
this.idToComponent = idToComponent;
this.progressCallback = progressCallback;
@ -287,6 +296,8 @@ GameLib.D3.Scene.LoadScene = function(
var gameLibMeshes = [];
var meshIdToMesh = {};
for (var m = 0; m < scene.meshes.length; m++) {
var apiMesh = scene.meshes[m];
@ -519,8 +530,42 @@ GameLib.D3.Scene.LoadScene = function(
);
gameLibMeshes.push(gameLibMesh);
meshIdToMesh[gameLibMesh.id] = gameLibMesh;
}
var cameras = [];
for (var c = 0; c < scene.cameras.length; c++) {
var apiCamera = scene.cameras[c];
var camera = new GameLib.D3.Camera(
graphics,
new GameLib.D3.Camera.API(
apiCamera.id,
apiCamera.cameraType,
apiCamera.name,
apiCamera.fov,
apiCamera.aspect,
apiCamera.near,
apiCamera.far,
apiCamera.position,
apiCamera.lookAt,
apiCamera.minX,
apiCamera.maxX,
apiCamera.minY,
apiCamera.maxY,
apiCamera.minZ,
apiCamera.maxZ
)
);
cameras.push(camera);
}
var activeCameraIndex = scene.activeCameraIndex;
var scene3d = new GameLib.D3.Scene(
graphics,
progressCallback,
@ -555,9 +600,12 @@ GameLib.D3.Scene.LoadScene = function(
worlds,
entities,
components,
shapes
shapes,
cameras,
activeCameraIndex
),
imageFactory
imageFactory,
meshIdToMesh
);
onLoaded(scene3d);
@ -623,7 +671,7 @@ GameLib.D3.Scene.prototype.intitializeComponents = function() {
var component = this.components[c];
this.scene3d.registerComponent(component);
this.registerComponent(component);
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
component.setParentEntity(this.parentScene, this);
@ -682,6 +730,7 @@ GameLib.D3.Scene.prototype.registerEntity = function(
entity
) {
this.entities.push(entity);
entity.register(this);
for(var c = 0; c < entity.ids.length; c++) {

76
src/game-lib-spline.js Normal file
View File

@ -0,0 +1,76 @@
/**
* Spline constructor
* @param graphics GameLib.D3.Graphics
* @param apiSpline GameLib.D3.Spline.API
* @constructor
*/
GameLib.D3.Spline = function(
graphics,
apiSpline
) {
for (var property in apiSpline) {
if (apiSpline.hasOwnProperty(property)) {
this[property] = apiSpline[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* API Spline
* @constructor
*/
GameLib.D3.Spline.API = function(
id,
name,
vertices
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'spline-unnamed';
}
this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(vertices)) {
vertices = [];
}
this.vertices = vertices;
};
/**
* Creates an instance spline
* @param update boolean
*/
GameLib.D3.Spline.prototype.createInstance = function(update) {
var vertices = [];
for (var v = 0; v < this.vertices.length; v++) {
vertices.push(new this.graphics.instance.Vector3(
this.vertices[v].x,
this.vertices[v].y,
this.vertices[v].z
));
}
return new this.graphics.instance.CatmullRomCurve3(vertices);
};
/**
* Updates the instance
*/
GameLib.D3.Spline.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};