API components
parent
93f90964a2
commit
50a3abf1ae
|
@ -24,12 +24,11 @@ GameLib.D3.ComponentCamera = function ComponentCamera(
|
||||||
GameLib.D3.Utils.Extend(GameLib.D3.ComponentCamera, GameLib.D3.ComponentInterface);
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentCamera, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
if (typeof camera == 'undefined') {
|
if (typeof camera == 'undefined') {
|
||||||
var graphics = new GameLib.D3.Graphics(GameLib.D3.Graphics.GRAPHICS_TYPE_THREE, THREE);
|
camera = null;
|
||||||
camera = new GameLib.D3.Camera(graphics);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Component fields
|
// Component fields
|
||||||
this.camera = camera.instance;
|
this.camera = camera;
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////// Methods to override //////////////////////////
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
@ -37,6 +36,11 @@ GameLib.D3.ComponentCamera.prototype.onLateUpdate = function(
|
||||||
deltaTime,
|
deltaTime,
|
||||||
parentEntity
|
parentEntity
|
||||||
) {
|
) {
|
||||||
this.camera.quaternion.copy(parentEntity.quaternion);
|
if (this.camera) {
|
||||||
this.camera.position.copy(parentEntity.position);
|
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!');
|
||||||
|
}
|
||||||
};
|
};
|
|
@ -19,6 +19,291 @@ GameLib.D3.ComponentInterface = function ComponentInterface(
|
||||||
this.parentEntity = null;
|
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
|
// will be not used
|
||||||
GameLib.D3.ComponentInterface.prototype.setParentEntity = function(
|
GameLib.D3.ComponentInterface.prototype.setParentEntity = function(
|
||||||
parentScene,
|
parentScene,
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
*
|
*
|
||||||
* @param id
|
* @param id
|
||||||
* @param name
|
* @param name
|
||||||
* @param splineCurve3
|
* @param spline
|
||||||
* @param accel
|
* @param acceleration
|
||||||
* @param maxSpeed
|
* @param maxSpeed
|
||||||
* @param baseOffset
|
* @param baseOffset
|
||||||
* @param maxOffset
|
* @param maxOffset
|
||||||
|
@ -13,9 +13,9 @@
|
||||||
GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
|
GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
splineCurve3,
|
spline,
|
||||||
normalSplineCurve3,
|
normalSpline,
|
||||||
accel,
|
acceleration,
|
||||||
maxSpeed,
|
maxSpeed,
|
||||||
baseOffset,
|
baseOffset,
|
||||||
maxOffset,
|
maxOffset,
|
||||||
|
@ -30,11 +30,11 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.parentEntity = null;
|
this.parentEntity = null;
|
||||||
|
|
||||||
this.splineCurve3 = splineCurve3;
|
this.spline = spline;
|
||||||
this.normalSplineCurve3 = normalSplineCurve3;
|
this.normalSpline = normalSpline;
|
||||||
|
|
||||||
this.maxSpeed = maxSpeed || 10.0;
|
this.maxSpeed = maxSpeed || 10.0;
|
||||||
this.accel = accel || 2.0;
|
this.acceleration = acceleration || 2.0;
|
||||||
|
|
||||||
this.baseOffset = baseOffset || new GameLib.D3.Vector3();
|
this.baseOffset = baseOffset || new GameLib.D3.Vector3();
|
||||||
this.maxOffset = maxOffset || new GameLib.D3.Vector3(10, 10, 10);
|
this.maxOffset = maxOffset || new GameLib.D3.Vector3(10, 10, 10);
|
||||||
|
@ -59,7 +59,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
|
||||||
parentEntity
|
parentEntity
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if(this.splineCurve3 && this.normalSplineCurve3) {
|
if(this.spline && this.normalSpline) {
|
||||||
|
|
||||||
if(this.currentPathValue >= 1 || this.currentPathValue < 0) {
|
if(this.currentPathValue >= 1 || this.currentPathValue < 0) {
|
||||||
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 ).
|
//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
|
//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;
|
var nextIndex = this.currentPathValue + 0.005;
|
||||||
if(nextIndex > 1.0) {
|
if(nextIndex > 1.0) {
|
||||||
nextIndex = 0;
|
nextIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nextPoint = this.splineCurve3.getPointAt(nextIndex);
|
var nextPoint = this.spline.getPointAt(nextIndex);
|
||||||
var normal = this.normalSplineCurve3.getPointAt(this.currentPathValue).normalize();
|
var normal = this.normalSpline.getPointAt(this.currentPathValue).normalize();
|
||||||
|
|
||||||
var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal);
|
var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal);
|
||||||
var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix);
|
var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix);
|
||||||
|
|
||||||
{ // update current speed
|
{ // 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);
|
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
|
||||||
this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t;
|
this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t;
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function(
|
||||||
parentScene,
|
parentScene,
|
||||||
parentEntity
|
parentEntity
|
||||||
) {
|
) {
|
||||||
if(!this.splineCurve3) {
|
if(!this.spline) {
|
||||||
console.error("NO PATH GIVEN");
|
console.error("NO PATH GIVEN");
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -2,13 +2,13 @@
|
||||||
*
|
*
|
||||||
* @param id
|
* @param id
|
||||||
* @param name
|
* @param name
|
||||||
* @param physicsWorld
|
* @param world GameLib.D3.World
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
GameLib.D3.ComponentVehicleAIObjectAvoidance = function(
|
GameLib.D3.ComponentVehicleAIObjectAvoidance = function(
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
physicsWorld
|
world
|
||||||
) {
|
) {
|
||||||
this.id = id|| GameLib.D3.Tools.RandomId();
|
this.id = id|| GameLib.D3.Tools.RandomId();
|
||||||
if (typeof name == 'undefined') {
|
if (typeof name == 'undefined') {
|
||||||
|
@ -20,7 +20,7 @@ GameLib.D3.ComponentVehicleAIObjectAvoidance = function(
|
||||||
GameLib.D3.Utils.Extend(GameLib.D3.ComponentVehicleAIObjectAvoidance, GameLib.D3.ComponentInterface);
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentVehicleAIObjectAvoidance, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
this.raycastVehicleComponent = null;
|
this.raycastVehicleComponent = null;
|
||||||
this.physicsWorld = physicsWorld || null;
|
this.physicsWorld = world || null;
|
||||||
this.sensors = [];
|
this.sensors = [];
|
||||||
|
|
||||||
// debug
|
// debug
|
||||||
|
|
|
@ -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,
|
id,
|
||||||
name,
|
name,
|
||||||
ids,
|
ids,
|
||||||
|
@ -6,6 +46,9 @@ GameLib.D3.Entity = function Entity(
|
||||||
quaternion,
|
quaternion,
|
||||||
scale
|
scale
|
||||||
) {
|
) {
|
||||||
|
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
|
||||||
|
id = GameLib.D3.Tools.RandomId();
|
||||||
|
}
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|
||||||
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
|
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
|
||||||
|
@ -18,17 +61,13 @@ GameLib.D3.Entity = function Entity(
|
||||||
}
|
}
|
||||||
this.ids = ids;
|
this.ids = ids;
|
||||||
|
|
||||||
// constructed at runtime
|
|
||||||
this.parentScene = null;
|
|
||||||
this.mesh = null;
|
|
||||||
|
|
||||||
if(GameLib.D3.Utils.UndefinedOrNull(position)) {
|
if(GameLib.D3.Utils.UndefinedOrNull(position)) {
|
||||||
position = new GameLib.D3.Vector3(0, 0, 0);
|
position = new GameLib.D3.Vector3();
|
||||||
}
|
}
|
||||||
this.position = position;
|
this.position = position;
|
||||||
|
|
||||||
if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) {
|
if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) {
|
||||||
quaternion = new GameLib.D3.Vector4(0, 0, 0, 1);
|
quaternion = new GameLib.D3.Vector4();
|
||||||
}
|
}
|
||||||
this.quaternion = quaternion;
|
this.quaternion = quaternion;
|
||||||
|
|
||||||
|
@ -45,18 +84,21 @@ GameLib.D3.Entity = function Entity(
|
||||||
GameLib.D3.Entity.prototype.update = function(
|
GameLib.D3.Entity.prototype.update = function(
|
||||||
deltaTime
|
deltaTime
|
||||||
) {
|
) {
|
||||||
for(var c in this.ids) {
|
for (var componentId in this.ids) {
|
||||||
var id = this.ids[c];
|
if (this.ids.hasOwnProperty(componentId)) {
|
||||||
var component = this.parentScene.idToComponent[id];
|
var id = this.ids[componentId];
|
||||||
if(component && component.onUpdate) {
|
var component = this.parentScene.idToComponent[id];
|
||||||
component.onUpdate(deltaTime, this);
|
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.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.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.scale.set(this.scale.x, this.scale.y, this.scale.z);
|
||||||
|
this.mesh.updateInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.onUpdate(deltaTime);
|
this.onUpdate(deltaTime);
|
||||||
|
@ -69,11 +111,13 @@ 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.ids) {
|
for (var componentId in this.ids) {
|
||||||
var id = this.ids[c];
|
if (this.ids.hasOwnProperty(componentId)) {
|
||||||
var component = this.parentScene.idToComponent[id];
|
var id = this.ids[componentId];
|
||||||
if(component && component.onLateUpdate) {
|
var component = this.parentScene.idToComponent[id];
|
||||||
component.onLateUpdate(deltaTime, this);
|
if (component && component.onLateUpdate) {
|
||||||
|
component.onLateUpdate(deltaTime, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,14 +131,14 @@ GameLib.D3.Entity.prototype.lateUpdate = function(
|
||||||
GameLib.D3.Entity.prototype.register = function(
|
GameLib.D3.Entity.prototype.register = function(
|
||||||
parentScene
|
parentScene
|
||||||
) {
|
) {
|
||||||
this.parentScene = parentScene;
|
// this.parentScene = parentScene;
|
||||||
|
|
||||||
if(this.id != null && parentScene.meshIdToMesh[this.id]) {
|
// if (this.id != null && parentScene.meshIdToMesh[this.id]) {
|
||||||
parentScene.instance.add(parentScene.meshIdToMesh[this.id]);
|
// parentScene.instance.add(parentScene.meshIdToMesh[this.id]);
|
||||||
this.mesh = 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(
|
GameLib.D3.Entity.prototype.getComponent = function(
|
||||||
componentType
|
componentType
|
||||||
) {
|
) {
|
||||||
for(var c in this.ids) {
|
for (var componentId in this.ids) {
|
||||||
var id = this.ids[c];
|
if (this.ids.hasOwnProperty(componentId)) {
|
||||||
var component = this.parentScene.idToComponent[id];
|
var id = this.ids[componentId];
|
||||||
if (component instanceof componentType) {
|
var component = this.parentScene.idToComponent[id];
|
||||||
return component;
|
if (component instanceof componentType) {
|
||||||
|
return component;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,18 @@
|
||||||
* @param graphics
|
* @param graphics
|
||||||
* @param progressCallback
|
* @param progressCallback
|
||||||
* @param apiScene GameLib.D3.Scene.API
|
* @param apiScene GameLib.D3.Scene.API
|
||||||
|
* @param imageFactory
|
||||||
|
* @param meshIdToMesh
|
||||||
|
* @param idToComponent
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
GameLib.D3.Scene = function Scene(
|
GameLib.D3.Scene = function Scene(
|
||||||
graphics,
|
graphics,
|
||||||
progressCallback,
|
progressCallback,
|
||||||
apiScene,
|
apiScene,
|
||||||
imageFactory
|
imageFactory,
|
||||||
|
meshIdToMesh,
|
||||||
|
idToComponent
|
||||||
) {
|
) {
|
||||||
|
|
||||||
for (var property in apiScene) {
|
for (var property in apiScene) {
|
||||||
|
@ -22,11 +27,15 @@ GameLib.D3.Scene = function Scene(
|
||||||
this.graphics = graphics;
|
this.graphics = graphics;
|
||||||
this.graphics.isNotThreeThrow();
|
this.graphics.isNotThreeThrow();
|
||||||
|
|
||||||
// assoc array
|
if (GameLib.D3.Utils.UndefinedOrNull(meshIdToMesh)) {
|
||||||
this.meshIdToMesh = {};
|
meshIdToMesh = {};
|
||||||
|
}
|
||||||
|
this.meshIdToMesh = meshIdToMesh;
|
||||||
|
|
||||||
// assoc array
|
if (GameLib.D3.Utils.UndefinedOrNull(idToComponent)) {
|
||||||
this.idToComponent = {};
|
idToComponent = {};
|
||||||
|
}
|
||||||
|
this.idToComponent = idToComponent;
|
||||||
|
|
||||||
this.progressCallback = progressCallback;
|
this.progressCallback = progressCallback;
|
||||||
|
|
||||||
|
@ -287,6 +296,8 @@ GameLib.D3.Scene.LoadScene = function(
|
||||||
|
|
||||||
var gameLibMeshes = [];
|
var gameLibMeshes = [];
|
||||||
|
|
||||||
|
var meshIdToMesh = {};
|
||||||
|
|
||||||
for (var m = 0; m < scene.meshes.length; m++) {
|
for (var m = 0; m < scene.meshes.length; m++) {
|
||||||
|
|
||||||
var apiMesh = scene.meshes[m];
|
var apiMesh = scene.meshes[m];
|
||||||
|
@ -519,8 +530,42 @@ GameLib.D3.Scene.LoadScene = function(
|
||||||
);
|
);
|
||||||
|
|
||||||
gameLibMeshes.push(gameLibMesh);
|
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(
|
var scene3d = new GameLib.D3.Scene(
|
||||||
graphics,
|
graphics,
|
||||||
progressCallback,
|
progressCallback,
|
||||||
|
@ -555,9 +600,12 @@ GameLib.D3.Scene.LoadScene = function(
|
||||||
worlds,
|
worlds,
|
||||||
entities,
|
entities,
|
||||||
components,
|
components,
|
||||||
shapes
|
shapes,
|
||||||
|
cameras,
|
||||||
|
activeCameraIndex
|
||||||
),
|
),
|
||||||
imageFactory
|
imageFactory,
|
||||||
|
meshIdToMesh
|
||||||
);
|
);
|
||||||
|
|
||||||
onLoaded(scene3d);
|
onLoaded(scene3d);
|
||||||
|
@ -623,7 +671,7 @@ GameLib.D3.Scene.prototype.intitializeComponents = function() {
|
||||||
|
|
||||||
var component = this.components[c];
|
var component = this.components[c];
|
||||||
|
|
||||||
this.scene3d.registerComponent(component);
|
this.registerComponent(component);
|
||||||
|
|
||||||
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
|
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
|
||||||
component.setParentEntity(this.parentScene, this);
|
component.setParentEntity(this.parentScene, this);
|
||||||
|
@ -682,6 +730,7 @@ GameLib.D3.Scene.prototype.registerEntity = function(
|
||||||
entity
|
entity
|
||||||
) {
|
) {
|
||||||
this.entities.push(entity);
|
this.entities.push(entity);
|
||||||
|
|
||||||
entity.register(this);
|
entity.register(this);
|
||||||
|
|
||||||
for(var c = 0; c < entity.ids.length; c++) {
|
for(var c = 0; c < entity.ids.length; c++) {
|
||||||
|
|
|
@ -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);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue