continue new ecs

beta.r3js.org
Theunis J. Botha 2016-12-15 15:28:00 +01:00
parent 05efb8eea0
commit aeee9c4533
22 changed files with 51 additions and 3334 deletions

View File

@ -1,331 +0,0 @@
/**
* Common properties of all Components
* @param id
* @param name
* @param componentType GameLib.Component.COMPONENT_*
* @param parentEntity GameLib.Entity
* @param linkedProperties GameLib.D3.LinkedProperty[]
* @constructor
*/
GameLib.Component = function Component(
id,
name,
componentType,
parentEntity,
linkedProperties
) {
this.id = id || GameLib.Utils.RandomId();
if (GameLib.Utils.UndefinedOrNull(name)) {
name = this.constructor.name;
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(componentType)) {
throw new Error('No Component Type - You should extend from this class not instantiate directly');
}
this.componentType = componentType;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
if (GameLib.Utils.UndefinedOrNull(linkedProperties)) {
linkedProperties = [];
}
this.linkedProperties = linkedProperties;
this.linkedProperties.push(
new GameLib.Component.LinkedProperty(
'parentEntity',
GameLib.Entity
)
)
};
/**
* Each custom component has some properties which are converted to Strind IDs for storing to API,
* and then linked before Runtime (after loading from API) back to their actual objects (through the ID)
* @param propertyName String
* @param objectType GameLib.D3.Object
* @constructor
*/
GameLib.Component.LinkedProperty = function(
propertyName,
objectType
) {
this.propertyName = propertyName;
this.objectType = objectType;
};
GameLib.Component.COMPONENT_INTERFACE = 0x1;
GameLib.Component.COMPONENT_CAMERA = 0x2;
GameLib.Component.COMPONENT_COLOR_FLASH = 0x3;
GameLib.Component.COMPONENT_COLOR_LERP = 0x4;
GameLib.Component.COMPONENT_FLY_CONTROLS = 0x5;
GameLib.Component.COMPONENT_FOLLOW = 0x6;
GameLib.Component.COMPONENT_LOOK_AT = 0x7;
GameLib.Component.COMPONENT_MESH = 0x8;
GameLib.Component.COMPONENT_MESH_PERMUTATION = 0x9;
GameLib.Component.COMPONENT_PATH_AI = 0xA;
GameLib.Component.COMPONENT_PATH_FOLLOWING = 0xB;
GameLib.Component.COMPONENT_PATH_CONTROLS = 0xC;
GameLib.Component.COMPONENT_RAYCAST_VEHICLE_CONTROLS = 0xD;
GameLib.Component.COMPONENT_TRIGGER_BOX_BOX = 0xE;
GameLib.Component.COMPONENT_TRIGGER_BOX_SPHERE = 0xF;
GameLib.Component.COMPONENT_TRIGGER_SPHERE_SPHERE = 0x10;
GameLib.Component.COMPONENT_VEHICLE_AI_OBJECT_AVOIDENCE = 0x11;
GameLib.Component.COMPONENT_VEHICLE_AI_PATH_STEERING = 0x12;
GameLib.Component.prototype.toApiComponent = function() {
var apiComponent = new GameLib.API.Component(
this.id,
this.name,
this.componentType,
GameLib.Utils.IdOrNull(this.parentEntity)
);
var isObjectProperty;
for (var property in apiComponent) {
if (
apiComponent.hasOwnProperty(property) &&
this.hasOwnProperty(property)
) {
isObjectProperty = false;
for (var l = 0; l < this.linkedProperties.length; l++) {
var linkedProperty = this.linkedProperties[l];
if (linkedProperty.propertyName == property) {
isObjectProperty = true;
break;
}
}
if (isObjectProperty) {
apiComponent[property] = GameLib.Utils.IdOrNull(this[property]);
} else if (
this[property] instanceof GameLib.Vector2 ||
this[property] instanceof GameLib.Vector3
) {
apiComponent[property] = this[property].toApiVector();
} else if (this[property] instanceof GameLib.Quaternion) {
apiComponent[property] = this[property].toApiQuaternion();
} else if (
this[property] instanceof GameLib.Matrix3 ||
this[property] instanceof GameLib.Matrix4
) {
apiComponent[property] = this[property].toApiMatrix();
} else {
apiComponent[property] = this[property];
}
} else if (
apiComponent.hasOwnProperty(property)
) {
delete apiComponent[property];
}
}
return apiComponent;
};
/**
* Converts and Object component into a proper GameLib.Component
* @param graphics
* @param objectComponent
* @constructor
*/
GameLib.Component.FromObjectComponent = function(graphics, objectComponent) {
var component = null;
switch (objectComponent.componentType) {
case GameLib.Component.COMPONENT_CAMERA :
component = new GameLib.ComponentCamera();
break;
case GameLib.Component.COMPONENT_FOLLOW :
component = new GameLib.ComponentFollow(null, null, graphics);
break;
case GameLib.Component.COMPONENT_LOOK_AT :
component = new GameLib.ComponentLookAt(null, null, graphics);
break;
case GameLib.Component.COMPONENT_PATH_FOLLOWING :
component = new GameLib.ComponentPathFollowing(null, null, graphics);
break;
case GameLib.Component.COMPONENT_MESH :
component = new GameLib.ComponentMesh(null, null, graphics);
break;
case GameLib.Component.COMPONENT_MESH_PERMUTATION :
component = new GameLib.ComponentMeshPermutation(null, null, graphics);
break;
default:
console.warn('This type of component is not yet read from the database:' + objectComponent.componentType);
}
for (var property in component) {
if (
component.hasOwnProperty(property) &&
objectComponent.hasOwnProperty(property)
) {
if (component[property] instanceof GameLib.Vector2) {
component[property] = new GameLib.Vector2(
graphics,
component,
new GameLib.API.Vector2(
objectComponent[property].x,
objectComponent[property].y
)
)
} else if (component[property] instanceof GameLib.Vector3) {
component[property] = new GameLib.Vector3(
graphics,
component,
new GameLib.API.Vector3(
objectComponent[property].x,
objectComponent[property].y,
objectComponent[property].z
)
)
} else if (component[property] instanceof GameLib.Quaternion) {
component[property] = new GameLib.Quaternion(
graphics,
component,
new GameLib.API.Quaternion(
objectComponent[property].x,
objectComponent[property].y,
objectComponent[property].z,
objectComponent[property].w,
new GameLib.API.Vector3(
objectComponent[property].axis.x,
objectComponent[property].axis.y,
objectComponent[property].axis.z
),
objectComponent[property].angle
)
)
} else if (component[property] instanceof GameLib.D3.Color) {
component[property] = new GameLib.D3.Color(
graphics,
component,
new GameLib.D3.API.Color(
objectComponent[property].r,
objectComponent[property].g,
objectComponent[property].b,
objectComponent[property].a
)
)
} else if (component[property] instanceof GameLib.Matrix4) {
component[property] = new GameLib.Matrix4(
graphics,
component,
new GameLib.API.Matrix4(
new GameLib.API.Quaternion(
objectComponent[property].rows[0].x,
objectComponent[property].rows[0].y,
objectComponent[property].rows[0].z,
objectComponent[property].rows[0].w
),
new GameLib.API.Quaternion(
objectComponent[property].rows[1].x,
objectComponent[property].rows[1].y,
objectComponent[property].rows[1].z,
objectComponent[property].rows[1].w
),
new GameLib.API.Quaternion(
objectComponent[property].rows[2].x,
objectComponent[property].rows[2].y,
objectComponent[property].rows[2].z,
objectComponent[property].rows[2].w
),
new GameLib.API.Quaternion(
objectComponent[property].rows[3].x,
objectComponent[property].rows[3].y,
objectComponent[property].rows[3].z,
objectComponent[property].rows[3].w
)
)
)
} else {
component[property] = objectComponent[property];
}
}
}
return component;
};
/**
* Gets executed from every entity during each game state loop
* Set the callbacks to null initially to prevent unneccesary function calls
* @param deltaTime
* @override
* @callback
* @default null
*/
GameLib.Component.prototype.onUpdate = null;
/**
* Gets executed from every entity during each game loop - after onUpdate has been called
* Set the callbacks to null initially to prevent unneccesary function calls
* @param deltaTime
* @override
* @callback
*/
GameLib.Component.prototype.onLateUpdate = null;
/**
* Gets executed when component is added to an Entity
* @param entity GameLib.Entity
* @override
* @callback
*/
GameLib.Component.prototype.onAdd = function(entity) {};
/**
* Gets executed when a component is removed from an Entity
* @param entity GameLib.Entity
* @override
* @callback
*/
GameLib.Component.prototype.onRemove = function(entity) {};
/**
* links object ids to actual objects
* @param idToObject Object linking object components to objects
*/
GameLib.Component.prototype.linkObjects = function(idToObject) {
this.linkedProperties.forEach(
function (linkedProperty) {
if (this[linkedProperty.propertyName]) {
this[linkedProperty.propertyName] = idToObject[this[linkedProperty.propertyName]];
}
}.bind(this)
);
};
/**
* Clones this component
* @returns {*}
*/
GameLib.Component.prototype.clone = function() {
return _.cloneDeep(this);
};

View File

@ -1,61 +0,0 @@
/**
* 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 parentEntity GameLib.Entity
* @param camera GameLib.D3.Camera
* @constructor
*/
GameLib.ComponentCamera = function ComponentCamera(
id,
name,
parentEntity,
camera
) {
GameLib.Component.call(
this,
id,
name,
GameLib.Component.COMPONENT_CAMERA,
parentEntity,
[
new GameLib.Component.LinkedProperty(
'camera',
GameLib.D3.Camera
)
]
);
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.camera = camera;
};
GameLib.ComponentCamera.prototype = Object.create(GameLib.Component.prototype);
GameLib.ComponentCamera.prototype.constructor = GameLib.ComponentCamera;
/**
* onLateUpdate override method
* @override
* @callback
*/
GameLib.ComponentCamera.prototype.onLateUpdate = function() {
if (this.camera) {
this.camera.quaternion.x = this.parentEntity.quaternion.x;
this.camera.quaternion.y = this.parentEntity.quaternion.y;
this.camera.quaternion.z = this.parentEntity.quaternion.z;
this.camera.quaternion.w = this.parentEntity.quaternion.w;
this.camera.position.x = this.parentEntity.position.x;
this.camera.position.y = this.parentEntity.position.y;
this.camera.position.z = this.parentEntity.position.z;
this.camera.updateInstance();
}
};

View File

@ -1,37 +0,0 @@
/**
*
* @param id
* @param name
* @constructor
*/
GameLib.ComponentColorFlash = function ComponentColorFlash(
id,
name
) {
this.id = id || GameLib.Utils.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.Utils.Extend(GameLib.ComponentColorFlash, GameLib.Component);
};
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentColorFlash.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
this.parentEntity.mesh.material.color = new THREE.Color(Math.random(), Math.random(), Math.random());
};
GameLib.ComponentColorFlash.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
parentEntity.mesh.material = new THREE.MeshBasicMaterial();
};

View File

@ -1,66 +0,0 @@
GameLib.ComponentColorLerp = function(
id,
name,
startColor,
endColor,
lerpSpeed
) {
this.id = id|| GameLib.Utils.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.Utils.Extend(GameLib.ComponentColorLerp, GameLib.Component);
this.startColor = startColor || new GameLib.API.Vector3(0, 0, 0);
this.endColor = endColor || new GameLib.API.Vector3(1, 1, 1);
this.lerpSpeed = lerpSpeed || 1.0;
this.lerpTarget = this.endColor;
};
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentColorLerp.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
var t = deltaTime * this.lerpSpeed;
// t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
parentEntity.mesh.material.color.r = parentEntity.mesh.material.color.r + (this.endColor.x - parentEntity.mesh.material.color.r) * t;
parentEntity.mesh.material.color.g = parentEntity.mesh.material.color.g + (this.endColor.y - parentEntity.mesh.material.color.g) * t;
parentEntity.mesh.material.color.b = parentEntity.mesh.material.color.b + (this.endColor.z - parentEntity.mesh.material.color.b) * t;
/* if( parentEntity.mesh.material.color.r == this.endColor.x
&& parentEntity.mesh.material.color.g == this.endColor.y
&& parentEntity.mesh.material.color.b == this.endColor.z
) {
console.error("switch target");
//this.lerpTarget = this.startColor;
} /!*else if (parentEntity.mesh.material.color.r == this.startColor.x
&& parentEntity.mesh.material.color.g == this.startColor.y
&& parentEntity.mesh.material.color.b == this.startColor.z) {
this.lerpTarget = this.endColor;
}*!/*/
};
GameLib.ComponentColorLerp.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
parentEntity.mesh.material.color = new THREE.Color(
this.startColor.x,
this.startColor.y,
this.startColor.z
);
};

View File

@ -1,117 +0,0 @@
/**
*
* @param id {String}
* @param name {String}
* @param parent {GameLib.Entity}
* @param centerToOrigin {Boolean}
* @constructor
*/
GameLib.ComponentEntityParent = function ComponentEntityParent(
id,
name,
parent,
centerToOrigin
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parent = parent;
this.parentEntity = null;
this.centerToOrigin = centerToOrigin;
this.originPosition = new GameLib.API.Vector3(0, 0, 0);
GameLib.Utils.Extend(GameLib.ComponentEntityParent, GameLib.Component);
};
//#ifdef RUNTIME__
if(typeof THREE !== "undefined") {
ComponentEntityParent_TmpVector = new THREE.Vector3();
ComponentEntityParent_TmpQuaternion = new THREE.Quaternion();
}
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentEntityParent.prototype.onLateUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity && this.parent) {
if(this.centerToOrigin) {
this.parentEntity.position.x = ((this.parent.origin.x - this.originPosition.x) * this.parent.scale.x);
this.parentEntity.position.y = ((this.parent.origin.y - this.originPosition.y) * this.parent.scale.y);
this.parentEntity.position.z = ((this.parent.origin.z - this.originPosition.z) * this.parent.scale.z);
// apply quaternion
//
ComponentEntityParent_TmpQuaternion.set(
this.parent.quaternion.x,
this.parent.quaternion.y,
this.parent.quaternion.z,
this.parent.quaternion.w
);
ComponentEntityParent_TmpVector =
ComponentEntityParent_TmpVector.set(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
).applyQuaternion(
ComponentEntityParent_TmpQuaternion
);
this.parentEntity.position.x = ComponentEntityParent_TmpVector.x + this.parent.position.x;
this.parentEntity.position.y = ComponentEntityParent_TmpVector.y + this.parent.position.y;
this.parentEntity.position.z = ComponentEntityParent_TmpVector.z + this.parent.position.z;
} else {
this.parentEntity.position.x = this.parent.position.x;
this.parentEntity.position.y = this.parent.position.y;
this.parentEntity.position.z = this.parent.position.z;
}
this.parentEntity.quaternion.x = this.parent.quaternion.x;
this.parentEntity.quaternion.y = this.parent.quaternion.y;
this.parentEntity.quaternion.z = this.parent.quaternion.z;
this.parentEntity.quaternion.w = this.parent.quaternion.w;
this.parentEntity.scale.x = this.parent.scale.x;
this.parentEntity.scale.y = this.parent.scale.y;
this.parentEntity.scale.z = this.parent.scale.z;
this.parentEntity.mesh.updateMatrix();
}
};
GameLib.ComponentEntityParent.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
if(parentEntity && parentEntity.mesh) {
// The entities should already be scaled down and stuff
// The matrix transform should already be applied to the geometry.
if(this.centerToOrigin) {
if(GameLib.Utils.UndefinedOrNull(this.parent.origin)) {
this.parent.origin = this.parent.mesh.geometry.center();
parentEntity.position.x = this.parent.position.x - this.parent.origin.x;
parentEntity.position.y = this.parent.position.y - this.parent.origin.y;
parentEntity.position.z = this.parent.position.z - this.parent.origin.z;
}
this.originPosition = parentEntity.mesh.geometry.center();
}
}
};
//#endif

View File

@ -1,90 +0,0 @@
/**
*
* @param id
* @param name
* @param positionOffset
* @param quaternionOffset
* @param scaleOffset
* @constructor
*/
GameLib.ComponentEntityPermutation = function ComponentEntityPermutation(
id,
name,
positionOffset,
quaternionOffset,
scaleOffset
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
if(GameLib.Utils.UndefinedOrNull(positionOffset)) {
positionOffset = new GameLib.API.Vector3(0, 0, 0);
} this.positionOffset = positionOffset;
if(GameLib.Utils.UndefinedOrNull(quaternionOffset)) {
quaternionOffset = new GameLib.API.Quaternion(0, 0, 0, 1);
} this.quaternionOffset = quaternionOffset;
if(GameLib.Utils.UndefinedOrNull(scaleOffset)) {
scaleOffset = new GameLib.API.Vector3(1, 1, 1);
} this.scaleOffset = scaleOffset;
GameLib.Utils.Extend(GameLib.ComponentEntityPermutation, GameLib.Component);
};
//#ifdef RUNTIME__
if(typeof THREE != "undefined") {
ComponentEntityPermutation_quaternion = new THREE.Quaternion();
ComponentEntityPermutation_quaternionCopy = new THREE.Quaternion();
ComponentEntityPermutation_position = new THREE.Vector3();
ComponentEntityPermutation_scale = new THREE.Vector3();
ComponentEntityPermutation_offsetQuaternion = new THREE.Quaternion();
ComponentEntityPermutation_offsetPosition = new THREE.Vector3();
ComponentEntityPermutation_offsetScale = new THREE.Vector3();
}
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentEntityPermutation.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity && parentEntity.mesh) {
ComponentEntityPermutation_quaternion.copy(parentEntity.quaternion);
ComponentEntityPermutation_quaternionCopy.copy(ComponentEntityPermutation_quaternion);
ComponentEntityPermutation_position.copy(parentEntity.position);
ComponentEntityPermutation_offsetQuaternion.copy(this.quaternionOffset);
ComponentEntityPermutation_quaternion = ComponentEntityPermutation_quaternion.multiply(ComponentEntityPermutation_offsetQuaternion).normalize();
ComponentEntityPermutation_offsetPosition.copy(this.positionOffset);
ComponentEntityPermutation_position = ComponentEntityPermutation_position.add(ComponentEntityPermutation_offsetPosition.applyQuaternion(ComponentEntityPermutation_quaternionCopy));
ComponentEntityPermutation_scale.copy(parentEntity.scale);
ComponentEntityPermutation_offsetScale.copy(this.scaleOffset);
ComponentEntityPermutation_scale = ComponentEntityPermutation_scale.multiply(ComponentEntityPermutation_offsetScale);
parentEntity.position.x = ComponentEntityPermutation_position.x;
parentEntity.position.y = ComponentEntityPermutation_position.y;
parentEntity.position.z = ComponentEntityPermutation_position.z;
parentEntity.quaternion.x = ComponentEntityPermutation_quaternion.x;
parentEntity.quaternion.y = ComponentEntityPermutation_quaternion.y;
parentEntity.quaternion.z = ComponentEntityPermutation_quaternion.z;
parentEntity.quaternion.w = ComponentEntityPermutation_quaternion.w;
parentEntity.scale.x = ComponentEntityPermutation_scale.x;
parentEntity.scale.y = ComponentEntityPermutation_scale.y;
parentEntity.scale.z = ComponentEntityPermutation_scale.z;
}
};
//#endif

View File

@ -1,260 +0,0 @@
/**
*
* @param id
* @param name
* @constructor
*/
GameLib.ComponentFlyControls = function ComponentFlyControls(
id,
name
) {
this.id = id || GameLib.Utils.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.Utils.Extend(GameLib.ComponentFlyControls, GameLib.Component);
// Component fields
this.pitch = 0;
this.yaw = 0;
this.canRotate = false;
this.moveSpeed = 22.2;
this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
this.moveRight = false;
this.moveUp = false;
this.moveDown = false;
};
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentFlyControls.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
// Apply rotation
var rotation = new THREE.Euler(
this.pitch,
this.yaw,
0,
"YXZ"
);
var quat = new THREE.Quaternion().setFromEuler(
rotation
);
parentEntity.quaternion.x = quat.x;
parentEntity.quaternion.y = quat.y;
parentEntity.quaternion.z = quat.z;
parentEntity.quaternion.w = quat.w;
// Apply translation
var direction = new THREE.Vector3(0, 0, -1);
direction = direction.applyEuler(rotation).normalize();
if(this.moveForward) {
parentEntity.position.x += direction.x * (deltaTime * this.moveSpeed);
parentEntity.position.y += direction.y * (deltaTime * this.moveSpeed);
parentEntity.position.z += direction.z * (deltaTime * this.moveSpeed);
} else if(this.moveBackward) {
parentEntity.position.x -= direction.x * (deltaTime * this.moveSpeed);
parentEntity.position.y -= direction.y * (deltaTime * this.moveSpeed);
parentEntity.position.z -= direction.z * (deltaTime * this.moveSpeed);
}
if(this.moveLeft) {
var right = direction.cross(new THREE.Vector3(0, 1, 0));
parentEntity.position.x -= right.x * (deltaTime * this.moveSpeed);
parentEntity.position.y -= right.y * (deltaTime * this.moveSpeed);
parentEntity.position.z -= right.z * (deltaTime * this.moveSpeed);
} else if(this.moveRight) {
var right = direction.cross(new THREE.Vector3(0, 1, 0));
parentEntity.position.x += right.x * (deltaTime * this.moveSpeed);
parentEntity.position.y += right.y * (deltaTime * this.moveSpeed);
parentEntity.position.z += right.z * (deltaTime * this.moveSpeed);
}
// Absolute Y-Axis
if(this.moveUp) {
parentEntity.position.y += (deltaTime * this.moveSpeed);
} else if(this.moveDown) {
parentEntity.position.y -= (deltaTime * this.moveSpeed);
}
};
/**
*
* @param parentScene
* @param parentEntity
*/
GameLib.ComponentFlyControls.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
var component = this;
// Lock cursor
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
var element = document.body;
document.addEventListener('click', function(event) {
if(havePointerLock) {
if(event.button == 0) {
component.canRotate = true;
element.requestPointerLock();
} else if(event.button == 2) {
component.canRotate = false;
document.exitPointerLock();
}
}
});
if(havePointerLock) {
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
}
// Swipe
document.addEventListener('touchstart', function(evt) {
component.xDown = evt.touches[0].clientX;
component.yDown = evt.touches[0].clientY;
}, false);
document.addEventListener('touchmove', function(evt) {
if ( ! component.xDown || ! component.yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = component.xDown - xUp;
var yDiff = component.yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* left swipe */
} else {
/* right swipe */
}
} else {
if ( yDiff > 0 ) {
/* up swipe */
} else {
/* down swipe */
}
}
component.yaw -= xDiff * 0.002;
component.pitch -= yDiff * 0.002;
}, false);
// Mouse move
document.addEventListener('mousemove', function (event) {
if(component.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
component.yaw -= movementX * 0.002;
component.pitch -= movementY * 0.002;
}
// Save mouseCoords
sys.mouseCoords.set(
(event.clientX / window.innerWidth) * 2 - 1,
-(event.clientY / window.innerHeight) * 2 + 1
);
}, false);
// Hook up keyboard controls
document.addEventListener('keydown', function (event) {
switch (event.keyCode) {
case 87: // w
component.moveForward = true;
break;
case 65: // a
component.moveLeft = true;
break;
case 83: // s
component.moveBackward = true;
break;
case 68: // d
component.moveRight = true;
break;
case 32: // space
component.moveUp = true;
break;
case 16:
component.moveDown = true;
break;
}
}, false);
document.addEventListener('keyup', function (event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
component.moveForward = false;
break;
case 37: // left
case 65: // a
component.moveLeft = false;
break;
case 40: // down
case 83: // s
component.moveBackward = false;
break;
case 39: // right
case 68: // d
component.moveRight = false;
break;
case 32: // space
component.moveUp = false;
break;
case 16:
component.moveDown = false;
break;
}
}, false);
};

View File

@ -1,189 +0,0 @@
/**
* Adjusts a mesh rotation, scale and translation according to parent entity
* @param id
* @param name
* @param graphics GameLib.D3.Graphics
* @param parentEntity GameLib.Entity
* @param mesh GameLib.D3.Mesh
* @param positionOffset GameLib.Vector3
* @param quaternionOffset GameLib.Quaternion
* @param scaleOffset GameLib.Vector3
* @constructor
*/
GameLib.ComponentMeshPermutation = function ComponentMeshPermutation(
id,
name,
graphics,
parentEntity,
mesh,
positionOffset,
quaternionOffset,
scaleOffset
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
GameLib.Component.call(
this,
id,
name,
GameLib.Component.COMPONENT_MESH_PERMUTATION,
parentEntity,
[
new GameLib.Component.LinkedProperty(
'mesh',
GameLib.D3.Camera
)
]
);
if (GameLib.Utils.UndefinedOrNull(mesh)) {
mesh = null;
}
this.mesh = mesh;
/**
* Position
*/
if (GameLib.Utils.UndefinedOrNull(positionOffset)) {
positionOffset = new GameLib.Vector3(
graphics,
this,
new GameLib.API.Vector3(0, 0, 0)
);
}
this.positionOffset = positionOffset;
/**
* Rotation
*/
if (GameLib.Utils.UndefinedOrNull(quaternionOffset)) {
quaternionOffset = new GameLib.Quaternion(
graphics,
this,
new GameLib.API.Quaternion(0, 0, 0, 1)
);
}
this.quaternionOffset = quaternionOffset;
/**
* Scale
*/
if (GameLib.Utils.UndefinedOrNull(scaleOffset)) {
scaleOffset = new GameLib.Vector3(
graphics,
this,
new GameLib.API.Vector3(1, 1, 1)
);
}
this.scaleOffset = scaleOffset;
/**
* Runtime members
*/
this.quaternion = new GameLib.Quaternion(
graphics,
this,
new GameLib.API.Quaternion()
);
this.position = new GameLib.Vector3(
graphics,
this,
new GameLib.API.Vector3()
);
this.scale = new GameLib.Vector3(
graphics,
this,
new GameLib.API.Vector3()
);
this.customCode = "";
};
GameLib.ComponentMeshPermutation.prototype = Object.create(GameLib.Component.prototype);
GameLib.ComponentMeshPermutation.prototype.constructor = GameLib.ComponentMeshPermutation;
/**
* onLateUpdate
* @param deltaTime
*/
GameLib.ComponentMeshPermutation.prototype.onUpdate = function(deltaTime) {
if (this.parentEntity && this.mesh) {
this.quaternion.x = this.parentEntity.quaternion.x;
this.quaternion.y = this.parentEntity.quaternion.y;
this.quaternion.z = this.parentEntity.quaternion.z;
this.quaternion.w = this.parentEntity.quaternion.w;
this.position.x = this.parentEntity.position.x;
this.position.y = this.parentEntity.position.y;
this.position.z = this.parentEntity.position.z;
this.scale.x = this.parentEntity.scale.x;
this.scale.y = this.parentEntity.scale.y;
this.scale.z = this.parentEntity.scale.z;
//this.quaternion.multiply(this.quaternionOffset).normalize();
//this.position.add(this.positionOffset.applyQuaternion(this.quaternion));
//this.quaternion.updateInstance();
//TODO: ifdef debug this --- start
if (this.customCode) {
eval(this.customCode);
}
//TODO: ifdef debug this --- end
//this.quaternion.updateInstance();
//this.quaternion.setFromAngle(this.quaternionOffset.angle);
//this.quaternion.normalize();
this.mesh.position.x = this.position.x;
this.mesh.position.y = this.position.y;
this.mesh.position.z = this.position.z;
this.mesh.quaternion.x = this.quaternion.x;
this.mesh.quaternion.y = this.quaternion.y;
this.mesh.quaternion.z = this.quaternion.z;
this.mesh.quaternion.w = this.quaternion.w;
this.mesh.scale.x = this.scale.x;
this.mesh.scale.y = this.scale.y;
this.mesh.scale.z = this.scale.z;
this.mesh.updateInstance();
// this.updateInstance();
// quaternion.copy(this.mesh.quaternion);
// quaternionCopy.copy(quaternion);
// position.copy(this.mesh.position);
//
// offsetQuaternion.copy(this.quaternionOffset);
// quaternion = quaternion.multiply(offsetQuaternion).normalize();
//
// offsetPosition.copy(this.positionOffset);
// position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
//
// scale.copy(this.mesh.scale);
//
// offsetScale.copy(this.scaleOffset);
// scale = scale.multiply(offsetScale);
//
// this.mesh.position.copy(position);
// this.mesh.quaternion.copy(quaternion);
// this.mesh.scale.copy(scale);
}
};

View File

@ -1,103 +0,0 @@
/**
* 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 graphics GameLib.D3.Graphics
* @param parentEntity GameLib.Entity
* @param mesh GameLib.D3.Camera
* @param positionOffset
* @param scaleOffset
* @param quaternionOffset
* @constructor
*/
GameLib.ComponentMesh = function ComponentMesh(
id,
name,
graphics,
parentEntity,
mesh,
positionOffset,
scaleOffset,
quaternionOffset
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
GameLib.Component.call(
this,
id,
name,
GameLib.Component.COMPONENT_MESH,
parentEntity,
[
new GameLib.Component.LinkedProperty(
'mesh',
GameLib.D3.Camera
)
]
);
if (GameLib.Utils.UndefinedOrNull(mesh)) {
mesh = null;
}
this.mesh = mesh;
/**
* Position
*/
if (GameLib.Utils.UndefinedOrNull(positionOffset)) {
positionOffset = new GameLib.Vector3(
graphics,
this,
new GameLib.API.Vector3(0, 0, 0)
);
}
this.positionOffset = positionOffset;
/**
* Rotation
*/
if (GameLib.Utils.UndefinedOrNull(quaternionOffset)) {
quaternionOffset = new GameLib.Quaternion(
graphics,
this,
new GameLib.API.Quaternion(1, 0, 0, 0)
);
}
this.quaternionOffset = quaternionOffset;
/**
* Scale
*/
if (GameLib.Utils.UndefinedOrNull(scaleOffset)) {
scaleOffset = new GameLib.Vector3(
graphics,
this,
new GameLib.API.Vector3(0, 0, 0)
);
}
this.scaleOffset = scaleOffset;
};
GameLib.ComponentMesh.prototype = Object.create(GameLib.Component.prototype);
GameLib.ComponentMesh.prototype.constructor = GameLib.ComponentMesh;
/**
* onUpdate override method
* @override
* @callback
*/
GameLib.ComponentMesh.prototype.onUpdate = function() {
if (this.mesh) {
// this.mesh.position = this.parentEntity.position.copy().add(this.positionOffset);
// this.mesh.scale = this.parentEntity.scale.copy().add(this.scaleOffset);
// this.mesh.quaternion = this.parentEntity.quaternion.copy().multiply(this.quaternionOffset).normalize();
// this.mesh.updateInstance();
}
};

View File

@ -1,66 +0,0 @@
/**
*
* @param id
* @param name
* @param axis {GameLib.API.Vector3}
* @param getOffsetFunc {function}
* @param userData {Object}
* @constructor
*/
GameLib.ComponentOffsettor = function ComponentOffsettor(
id,
name,
axis,
getOffsetFunc,
userData
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
this.axis = axis || new GameLib.API.Vector3();
this.offset = 1;
var component = this;
this.getOffsetFunc = getOffsetFunc || function(entity, component, userData){ return component.offset; };
this.userData = userData;
GameLib.Utils.Extend(GameLib.ComponentOffsettor, GameLib.Component);
};
//#ifdef RUNTIME__
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentOffsettor.prototype.onLateUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity) {
var pos = new THREE.Vector3(
this.axis.x,
this.axis.y,
this.axis.z
).normalize().applyQuaternion(
new THREE.Quaternion(
parentEntity.quaternion.x,
parentEntity.quaternion.y,
parentEntity.quaternion.z,
parentEntity.quaternion.w
)
).multiplyScalar(this.getOffsetFunc(
parentEntity,
this,
this.userData
));
parentEntity.position.x += pos.x;
parentEntity.position.y += pos.y;
parentEntity.position.z += pos.z;
parentEntity.mesh.updateMatrix();
}
};
//#endif

View File

@ -1,352 +0,0 @@
/**
*
* @param id
* @param name
* @param sensorLength
* @constructor
*/
GameLib.ComponentPathAI = function ComponentPathAI(
id,
name,
sensorLength
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
this.sensorLength = sensorLength || 5;
GameLib.Utils.Extend(GameLib.ComponentPathAI, GameLib.Component);
//#ifdef RUNTIME__
this.sensors = [];
//#endif
};
//#ifdef RUNTIME__
var componentPathAI_Raycast = function(from, to, settings, world) {
/* var raycastResult = new sys.physicsEngine.instance.RaycastResult();
world.raycastClosest(from, to, settings, raycastResult);
return raycastResult;*/
};
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentPathAI.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
var forward = true;
var backward = false;
var left = false;
var right = false;
// Ray tracing part.
var scaledSensorLengthOffset = this.pathFollowingComponent.currentSpeed;
/*world.raycastClosest(
fromC,
toC,
{},
result
);*/
if(forward && !backward) {
this.pathFollowingComponent.direction = 1;
} else if (backward && !forward) {
this.pathFollowingComponent.direction = -1;
} else {
this.pathFollowingComponent.direction = 0;
}
if(left && !right) {
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 1;
} else if (right && !left) {
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = -1;
} else {
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 0;
}
// - - -- - - - - -- - - - - - - - - - - - - - -
// D E B U G G I N G
// - - - - - - - - - - - - - - - - - -
//#ifdef DEBUG__
if(this.sensorVisualizer) {
for(var i = 0, l = this.sensors.length; i < l; ++i) {
var sensor = this.sensors[i];
if(!this.sensorVisualizer.sensors[i]) {
var emptyGeometry = new THREE.Geometry();
var sensorMesh = new THREE.Mesh(
emptyGeometry,
new THREE.MeshBasicMaterial(
{
color : sensor.sensorColor,
wireframe : true
}
)
);
this.sensorVisualizer.sensors[i] = {
sensorMesh : sensorMesh
};
sys.game.scenes["MainScene"].instance.add(this.sensorVisualizer.sensors[i].sensorMesh);
}
if(this.sensorVisualizer.sensors[i].arrow) {
this.sensorVisualizer.sensors[i].sensorMesh.remove(this.sensorVisualizer.sensors[i].arrow);
}
// create new
this.sensorVisualizer.sensors[i].arrow = new THREE.ArrowHelper(
new THREE.Vector3(
sensor.sensorDirection.x,
sensor.sensorDirection.y,
sensor.sensorDirection.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)).normalize(),
new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
).add(new THREE.Vector3(
sensor.sensorPositionOffset.x,
sensor.sensorPositionOffset.y,
sensor.sensorPositionOffset.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
))),
sensor.sensorLength,
sensor.sensorColor
);
this.sensorVisualizer.sensors[i].sensorMesh.add(this.sensorVisualizer.sensors[i].arrow);
}
}
//#endif
};
GameLib.ComponentPathAI.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.parentEntity = parentEntity;
this.pathFollowingComponent = parentEntity.getComponent(GameLib.ComponentPathFollowing);
if(!this.pathFollowingComponent) {
console.error("ComponentPathAI. NO PATH FOLLOWING COMPONENT");
}
// Compute bounding box
if(!this.parentEntity.mesh.geometry.boundingBox) {
this.parentEntity.mesh.geometry.computeBoundingBox();
}
var boundingBox = this.parentEntity.mesh.geometry.boundingBox;
var sensorLength = this.sensorLength;
var sensorColor = new THREE.Color(0, 1, 0);
// Create sensors
// Front
this.sensors.push
(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
0
)
}
);
var sideSensorLengthMultiplier = 0.5;
// Absolute left
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0,
0,
1
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// Tilted left
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-0.5,
0,
0.5
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// left forward
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// Absolute Right
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0,
0,
-1
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
-boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// right forward
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
-boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// Tilted right
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-0.5,
0,
-0.5
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
-boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// debug code
this.sensorVisualizer = {
sensors : []
};
};
//#endif

View File

@ -1,143 +0,0 @@
/**
*
* @param id
* @param name
* @constructor
*/
GameLib.ComponentPathControls = function ComponentPathControls(
id,
name
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
// runtime
this.pathFollowingComponent = null;
this.keyLeftPressed = false;
this.keyRightPressed = false;
this.keyForwardPressed = false;
this.keyBackPressed = false;
this.keyBreakPressed = false;
GameLib.Utils.Extend(GameLib.ComponentPathControls, GameLib.Component);
};
//#ifdef RUNTIME__
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentPathControls.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if (this.keyForwardPressed) { // Forward [i]
this.pathFollowingComponent.direction = 1;
} else if (this.keyBackPressed){
this.pathFollowingComponent.direction = 0;
// this.pathFollowingComponent.direction = -1;
} else {
this.pathFollowingComponent.direction = 0;
}
// left right
if (this.keyLeftPressed) { // Left [j]
this.pathFollowingComponent.offset.x = -1;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 0;
} else if (this.keyRightPressed) { // Right [l]
this.pathFollowingComponent.offset.x = 1;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 0;
} else if(!this.keyRightPressed && !this.keyLeftPressed){
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 0;
}
};
GameLib.ComponentPathControls.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.pathFollowingComponent = parentEntity.getComponent(GameLib.ComponentPathFollowing);
if(!this.pathFollowingComponent) {
console.error("ComponentPathControls. NO PATH FOLLOWING COMPONENT");
}
var component = this;
document.addEventListener('keydown', function(event) {
if (event.keyCode == 73) { // Forward [i]
component.keyForwardPressed = true;
} else if (event.keyCode == 75) { // Back [k]
component.keyBackPressed = true;
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = true;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = true;
}
if (event.keyCode == 66) {
component.keyBreakPressed = true;
}
}, false);
document.addEventListener('keyup', function(event) {
if (event.keyCode == 73) { // Forward [i]
component.keyForwardPressed = false;
} else if (event.keyCode == 75) { // Back [k]
component.keyBackPressed = false;
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = false;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = false;
}
if (event.keyCode == 66) {
component.keyBreakPressed = false;
}
}, false);
};
//#endif

View File

@ -1,190 +0,0 @@
GameLib.ComponentRaycastVehicleControls = function ComponentRaycastVehicleControls(
id,
name,
frontLWheelIndex,
frontRWheelIndex,
backLWheelIndex,
backRWheelIndex,
maxForce,
steering
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
// maybe we pass this in the constructor
this.raycastVehicleComponent = null;
this.frontLWheelIndex = frontLWheelIndex || 0;
this.frontRWheelIndex = frontRWheelIndex || 1;
this.backLWheelIndex = backLWheelIndex || 2;
this.backRWheelIndex = backRWheelIndex || 3;
this.maxForce = maxForce || 400;
this.steering = steering || 0.5;
this.brakeForce = 1000000;
this.currentSteering = 0;
this.steeringSpeed = 8.0;
this.keyLeftPressed = false;
this.keyRightPressed = false;
this.keyForwardPressed = false;
this.keyBackPressed = false;
this.keyBreakPressed = false;
// Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.
GameLib.Utils.Extend(GameLib.ComponentRaycastVehicleControls, GameLib.Component);
};
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentRaycastVehicleControls.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if (this.keyForwardPressed) { // Forward [i]
this.raycastVehicleComponent.instance.applyEngineForce(-this.maxForce, this.backLWheelIndex);
this.raycastVehicleComponent.instance.applyEngineForce(-this.maxForce, this.backRWheelIndex);
} else if (this.keyBackPressed) { // Back [k]
this.raycastVehicleComponent.instance.applyEngineForce(this.maxForce, this.backLWheelIndex);
this.raycastVehicleComponent.instance.applyEngineForce(this.maxForce, this.backRWheelIndex);
} else if (!this.keyForwardPressed && !this.keyBackPressed) {
this.raycastVehicleComponent.instance.applyEngineForce(0, this.backLWheelIndex);
this.raycastVehicleComponent.instance.applyEngineForce(0, this.backRWheelIndex);
}
if (this.keyLeftPressed) { // Left [j]
var t = deltaTime * this.steeringSpeed;
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
this.currentSteering = this.currentSteering + (this.steering - this.currentSteering) * t;
this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex);
this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex);
} else if (this.keyRightPressed) { // Right [l]
var t = deltaTime * this.steeringSpeed;
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
this.currentSteering = this.currentSteering + ((-this.steering) - this.currentSteering) * t;
this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex);
this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex);
} else if (!this.keyLeftPressed && !this.keyRightPressed) {
var t = deltaTime * this.steeringSpeed * 2;
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
this.currentSteering = this.currentSteering + (0 - this.currentSteering) * t;
this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex);
this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex);
}
if(this.keyBreakPressed) {
this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.frontLWheelIndex);
this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.frontRWheelIndex);
this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.backLWheelIndex);
this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.backRWheelIndex);
} else {
this.raycastVehicleComponent.instance.setBrake(0, this.frontLWheelIndex);
this.raycastVehicleComponent.instance.setBrake(0, this.frontRWheelIndex);
this.raycastVehicleComponent.instance.setBrake(0, this.backLWheelIndex);
this.raycastVehicleComponent.instance.setBrake(0, this.backRWheelIndex);
}
};
GameLib.ComponentRaycastVehicleControls.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
console.log("Set parent!");
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
if(!this.raycastVehicleComponent) {
console.warn("NO RAYCAST VEHICLE FOUND!");
} else {
var component = this;
document.addEventListener('keydown', function(event) {
if (event.keyCode == 73) { // Forward [i]
component.keyForwardPressed = true;
} else if (event.keyCode == 75) { // Back [k]
component.keyBackPressed = true;
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = true;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = true;
}
if (event.keyCode == 66) {
component.keyBreakPressed = true;
}
}, false);
document.addEventListener('keyup', function(event) {
if (event.keyCode == 73) { // Forward [i]
component.keyForwardPressed = false;
} else if (event.keyCode == 75) { // Back [k]
component.keyBackPressed = false;
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = false;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = false;
}
if (event.keyCode == 66) {
component.keyBreakPressed = false;
}
}, false);
}
};

View File

@ -1,71 +0,0 @@
/**
*
* @param id
* @param name
* @param axis {GameLib.API.Vector3}
* @param getRotationFunc {Function}
* @param userData {Object}
* @constructor
*/
GameLib.ComponentRotator = function ComponentRotator(
id,
name,
axis,
getRotationFunc,
userData
) {
this.id = id || GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
this.rotation = 0;
var component = this;
this.axis = axis || new GameLib.API.Vector3();
this.getRotationFunc = getRotationFunc || function(entity, component, userData){ return component.rotation; };
this.userData = userData;
GameLib.Utils.Extend(GameLib.ComponentRotator, GameLib.Component);
};
//#ifdef RUNTIME__
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentRotator.prototype.onLateUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity) {
var quat = new THREE.Quaternion(
parentEntity.quaternion.x,
parentEntity.quaternion.y,
parentEntity.quaternion.z,
parentEntity.quaternion.w
).multiply(
new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(
this.axis.x,
this.axis.y,
this.axis.z
),
this.getRotationFunc(
parentEntity,
this,
this.userData
)
)
);
parentEntity.quaternion.x = quat.x;
parentEntity.quaternion.y = quat.y;
parentEntity.quaternion.z = quat.z;
parentEntity.quaternion.w = quat.w;
parentEntity.mesh.updateMatrix();
}
};
//#endif

View File

@ -1,104 +0,0 @@
//
// this component operates on it's parent entity's bounding box.
// so, you can't use this component with a null-mesh.
//
GameLib.ComponentTriggerBoxBox = function ComponentTriggerBoxBox(
id,
name,
entitiesToCheck,
onInside,
onEnter,
onLeave,
onSetParent
) {
console.log('triggerbox box!');
this.id = id || GameLib.Utils.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.Utils.Extend(GameLib.ComponentTriggerBoxBox, GameLib.Component);
this.entitiesToCheck = entitiesToCheck || [];
this.onInside = onInside || null;
this.onEnter = onEnter || null;
this.onLeave = onLeave || null;
this.onSetParent = onSetParent || null;
// defines code
this.entitiesInside = [];
};
//#ifdef RUNTIME__
if(typeof THREE != "undefined") {
ComponentTriggerBoxBox_BB = new THREE.Box3();
ComponentTriggerBoxBox_BBEntity = new THREE.Box3();
}
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentTriggerBoxBox.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity.mesh) {
// Calculate the current transform here, using the position, orientation and scale of the entity.
ComponentTriggerBoxBox_BB.setFromObject(parentEntity.mesh);
for(var e in this.entitiesToCheck) {
var entityToCheck = this.entitiesToCheck[e];
ComponentTriggerBoxBox_BBEntity.setFromObject(entityToCheck.mesh);
if(ComponentTriggerBoxBox_BB.intersectsBox(ComponentTriggerBoxBox_BBEntity)) {
if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger
this.entitiesInside.push(entityToCheck);
if(this.onEnter) {
if(this.onEnter(parentEntity, entityToCheck)) {
break;
}
}
}
if(this.onInside) {
if(this.onInside(parentEntity, entityToCheck)) {
break;
}
}
} else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger
this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1);
if(this.onLeave) {
if(this.onLeave(parentEntity, entityToCheck)) {
break;
}
}
}
}
}
};
//#endif
GameLib.ComponentTriggerBoxBox.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
if(this.onSetParent) {
this.onSetParent(parentScene, parentEntity);
}
};

View File

@ -1,128 +0,0 @@
//
// this component operates on it's parent entity's bounding box.
// so, you can't use this component with a null-mesh.
//
GameLib.ComponentTriggerBoxSphere = function ComponentTriggerBoxSphere(
id,
name,
entitiesToCheck,
onInside,
onEnter,
onLeave,
onSetParent
) {
this.id = id || GameLib.Utils.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.Utils.Extend(GameLib.ComponentTriggerBoxSphere, GameLib.Component);
this.entitiesToCheck = entitiesToCheck || [];
this.onInside = onInside || null;
this.onEnter = onEnter || null;
this.onLeave = onLeave || null;
this.onSetParent = onSetParent || null;
// defines code
this.entitiesInside = [];
};
//#ifdef RUNTIME__
if(typeof THREE != "undefined") {
ComponentTriggerBoxSphere_TargetPosition_Vec3 = new THREE.Vector3();
ComponentTriggerBoxSphere_TargetRadius = 0.0;
}
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentTriggerBoxSphere.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity.mesh) {
// Calculate the current transform here, using the position, orientation and scale of the entity.
var bb = new THREE.Box3();
bb.setFromObject(parentEntity.mesh);
for(var e in this.entitiesToCheck) {
var entityToCheck = this.entitiesToCheck[e];
if(entityToCheck.mesh) {
if (!entityToCheck.mesh.geometry.boundingSphere) {
entityToCheck.mesh.geometry.computeBoundingSphere();
}
}
var spherePosition = new THREE.Vector3(
entityToCheck.position.x,
entityToCheck.position.y,
entityToCheck.position.z
);
var maxScale = Math.max(
entityToCheck.scale.x,
Math.max(
entityToCheck.scale.y,
entityToCheck.scale.z
)
);
var sphere = new THREE.Sphere(
spherePosition,
entityToCheck.mesh
? entityToCheck.mesh.geometry.boundingSphere.radius * maxScale
: 1
);
if(bb.intersectsSphere(sphere)) {
if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger
this.entitiesInside.push(entityToCheck);
if(this.onEnter) {
if(this.onEnter(parentEntity, entityToCheck)) {
break;
}
}
}
if(this.onInside) {
if(this.onInside(parentEntity, entityToCheck)) {
break;
}
}
} else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger
this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1);
if(this.onLeave) {
if(this.onLeave(parentEntity, entityToCheck)) {
break;
}
}
}
}
}
};
//#endif
GameLib.ComponentTriggerBoxSphere.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
if(this.onSetParent) {
this.onSetParent(parentScene, parentEntity);
}
};

View File

@ -1,139 +0,0 @@
/**
* Uses the entity position + mesh bounding sphere to check if intersections
*
* @param id
* @param name
* @param sphereRadius
* @param entitiesToCheck
* @param onInside
* @param onEnter
* @param onLeave
* @param onSetParent
* @constructor
*/
GameLib.ComponentTriggerSphereSphere = function ComponentTriggerSphereSphere(
id,
name,
sphereRadius,
entitiesToCheck,
onInside,
onEnter,
onLeave,
onSetParent
) {
this.id = id || GameLib.Utils.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.Utils.Extend(GameLib.ComponentTriggerSphereSphere, GameLib.Component);
this.entitiesToCheck = entitiesToCheck || [];
this.sphereRadius = sphereRadius || 1.0;
this.onInside = onInside || null;
this.onEnter = onEnter || null;
this.onLeave = onLeave || null;
this.onSetParent = onSetParent || null;
// defines code
this.entitiesInside = [];
};
//#ifdef RUNTIME__
if(typeof THREE != "undefined") {
ComponentTriggerSphereSphere_spherePosition_Vector3 = new THREE.Vector3();
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3 = new THREE.Vector3();
ComponentTriggerSphereSphere_targetBoundingSphereRadius = 0.0;
ComponentTriggerSphereSphere_sphereToSphere_Vector3 = new THREE.Vector3();
}
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentTriggerSphereSphere.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
ComponentTriggerSphereSphere_spherePosition_Vector3.set(
parentEntity.position.x,
parentEntity.position.y,
parentEntity.position.z
);
for(var e in this.entitiesToCheck) {
var entityToCheck = this.entitiesToCheck[e];
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.set(
entityToCheck.position.x,
entityToCheck.position.y,
entityToCheck.position.z
);
if(entityToCheck.mesh && entityToCheck.mesh.geometry.boundingSphere) {
var maxScale = Math.max(
entityToCheck.scale.x,
Math.max(
entityToCheck.scale.y,
entityToCheck.scale.z
)
);
ComponentTriggerSphereSphere_targetBoundingSphereRadius = entityToCheck.mesh.geometry.boundingSphere.radius * maxScale;
} else {
ComponentTriggerSphereSphere_targetBoundingSphereRadius = 1.0;
}
// do sphere sphere collision
ComponentTriggerSphereSphere_sphereToSphere_Vector3.set(
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.x - ComponentTriggerSphereSphere_spherePosition_Vector3.x,
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.y - ComponentTriggerSphereSphere_spherePosition_Vector3.y,
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.z - ComponentTriggerSphereSphere_spherePosition_Vector3.z
);
var length = ComponentTriggerSphereSphere_sphereToSphere_Vector3.length();
if((length - (ComponentTriggerSphereSphere_targetBoundingSphereRadius + this.sphereRadius)) < 0) {
if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger
this.entitiesInside.push(entityToCheck);
if(this.onEnter) {
if(this.onEnter(parentEntity, entityToCheck)) {
break;
}
}
}
if(this.onInside) {
if(this.onInside(parentEntity, entityToCheck)) {
break;
}
}
} else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger
this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1);
if(this.onLeave) {
if(this.onLeave(parentEntity, entityToCheck)) {
break;
}
}
}
}
};
//#endif
GameLib.ComponentTriggerSphereSphere.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
if(this.onSetParent) {
this.onSetParent(parentScene, parentEntity);
}
};

View File

@ -1,570 +0,0 @@
/**
*
* @param id
* @param name
* @param world GameLib.D3.World
* @constructor
*/
GameLib.ComponentVehicleAIObjectAvoidance = function(
id,
name,
world
) {
this.id = id|| GameLib.Utils.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
GameLib.Utils.Extend(GameLib.ComponentVehicleAIObjectAvoidance, GameLib.Component);
this.raycastVehicleComponent = null;
this.physicsWorld = world || null;
this.sensors = [];
// debug
this.debugArrows = {};
console.log("constructor for : ComponentVehicleAIObjectAvoidance");
};
//#ifdef RUNTIME__
/////////////////////////////////////////////////////////////////////////
///////////////////////// Methods to override ///////////////////////////
/////////////////////////////////////////////////////////////////////////
GameLib.ComponentVehicleAIObjectAvoidance.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.parentEntity = parentEntity;
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
console.log("onSetParentEntity for : ComponentVehicleAIObjectAvoidance");
if(!this.raycastVehicleComponent) {
console.warn("NO RAYCAST VEHICLE FOUND!");
}
// create sensors
var boundingBox = this.parentEntity.mesh.geometry.boundingBox;
// this is taken from the main.js.
// this should be configurable inside the editor
var carBoxScaleModifier = { x : 3 / 4, y : 1 / 2, z : 0.90 };
var sensorLength = 7.0;
var sensorColor = new THREE.Color(0, 0, 1);
// . . . . . . . . . . FRONT . . . . . . . . . .
// right
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// left
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// center
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
0,
0
)
}
);
// . . . . . . DIAGONAL FRONT . . . . . . . .
// right
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0,
1,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// left
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0,
-1,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// right
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0.5,
0.5,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// left
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0.5,
-0.5,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// right
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0.75,
0.25,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
// left
this.sensors.push(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0.75,
-0.25,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
0//boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
)
}
);
console.log("pushed sensors", this.sensors.length);
};
GameLib.ComponentVehicleAIObjectAvoidance.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(this.raycastVehicleComponent && this.physicsWorld) {
// debug
this.debugArrows.avgDirection = this.debugArrows.avgDirection || {};
this.debugArrows.sensors = this.debugArrows.sensors || [];
var vehicleVelocity = this.parentEntity.getComponent(GameLib.D3.RigidBody).instance.velocity;
var vehicleVelocityLength = 12;
// shoot rays for each sensor & check collisions.
var world = this.physicsWorld.instance;
var result = new CANNON.RaycastResult();
this.debugArrows.avgDirection.vector = new THREE.Vector3();
for(var s = 0, l = this.sensors.length; s < l; ++s) {
// debug
if(!this.debugArrows.sensors[s]) {
this.debugArrows.sensors[s] = {};
}
var sensor = this.sensors[s];
var from = new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
).add(new THREE.Vector3(
sensor.sensorPositionOffset.x,
sensor.sensorPositionOffset.y,
sensor.sensorPositionOffset.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)));
var fromC = new CANNON.Vec3(
from.x,
from.y,
from.z
);
var to = new THREE.Vector3(
sensor.sensorDirection.x,
sensor.sensorDirection.y,
sensor.sensorDirection.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)).normalize().multiplyScalar(sensor.sensorLength);
var toC = new CANNON.Vec3(
from.x + to.x,
from.y + to.y,
from.z + to.z
);
world.raycastClosest(
fromC,
toC,
{
collisionFilterMask : 2 // check only group 2 (track)
},
result
);
if(result.hasHit) {
sensor.sensorColor = new THREE.Color(1, 0, 0);
var direction = new THREE.Vector3(
sensor.sensorDirection.x,
sensor.sensorDirection.y,
sensor.sensorDirection.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)).normalize();
/*var reflection = direction.reflect(
new THREE.Vector3(
result.hitNormalWorld.x,
result.hitNormalWorld.y,
result.hitNormalWorld.z
)
).normalize();*/
var reflection = direction.reflect(
new THREE.Vector3(
result.hitPointWorld.x,
result.hitPointWorld.y,
result.hitPointWorld.z
).normalize().cross(direction)
).normalize().negate();
var origin = new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
).add(new THREE.Vector3(
sensor.sensorPositionOffset.x,
sensor.sensorPositionOffset.y,
sensor.sensorPositionOffset.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)));
var moveDirection = direction.add(reflection).setY(0).normalize();
this.debugArrows.avgDirection.vector = this.debugArrows.avgDirection.vector.add(moveDirection);
if(this.debugArrows.sensors[s].intersectionArrow && this.debugArrows.sensors[s].mesh) {
this.debugArrows.sensors[s].mesh.remove(this.debugArrows.sensors[s].intersectionArrow);
}
if(this.debugArrows.sensors[s].mesh){
var arrow = new THREE.ArrowHelper(
moveDirection,
origin,
22,
new THREE.Color(1, 1, 0)
);
this.debugArrows.sensors[s].intersectionArrow = arrow;
this.debugArrows.sensors[s].mesh.add(this.debugArrows.sensors[s].intersectionArrow);
}
} else {
sensor.sensorColor = new THREE.Color(0, 0, 1);
if(this.debugArrows.sensors[s].intersectionArrow && this.debugArrows.sensors[s].mesh) {
this.debugArrows.sensors[s].mesh.remove(this.debugArrows.sensors[s].intersectionArrow);
}
}
}
this.debugArrows.avgDirection.vector = this.debugArrows.avgDirection.vector.normalize();
// draw the avg move direction
/*if(!this.debugArrows.avgDirection.mesh) {
this.debugArrows.avgDirection.mesh = new THREE.Mesh(
new THREE.Geometry(),
new THREE.MeshBasicMaterial(
{
color : 0x00ffff,
wireframe : true
}
)
);
sys.game.scenes["MainScene"].instance.add(this.debugArrows.avgDirection.mesh);
}
if(this.debugArrows.avgDirection.arrow && this.debugArrows.avgDirection.mesh) {
this.debugArrows.avgDirection.mesh.remove(this.debugArrows.avgDirection.arrow);
}*/
/*this.debugArrows.avgDirection.arrow = new THREE.ArrowHelper(
this.debugArrows.avgDirection.vector,
new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
),
12,
this.debugArrows.avgDirection.mesh.material.color
);*/
//this.debugArrows.avgDirection.mesh.add(this.debugArrows.avgDirection.arrow);
// draw sensors
/*{
for(var s = 0, l = this.sensors.length; s < l; ++s) {
if(!this.debugArrows.sensors[s].mesh) {
var geometry = new THREE.Geometry();
var mesh = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial(
{
color : 0x000000,
wireframe : true
}
)
);
this.debugArrows.sensors[s].mesh = mesh;
sys.game.scenes["MainScene"].instance.add(this.debugArrows.sensors[s].mesh);
}
// remove old arrow, if we have one
if(this.debugArrows.sensors[s].arrow) {
this.debugArrows.sensors[s].mesh.remove(this.debugArrows.sensors[s].arrow);
}
var sensor = this.sensors[s];
var sensorLength = sensor.sensorLength; // should get this from the sensor itself
this.debugArrows.sensors[s].arrow = new THREE.ArrowHelper(
new THREE.Vector3(
sensor.sensorDirection.x,
sensor.sensorDirection.y,
sensor.sensorDirection.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)).normalize(),
new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
).add(new THREE.Vector3(
sensor.sensorPositionOffset.x,
sensor.sensorPositionOffset.y,
sensor.sensorPositionOffset.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
))),
sensorLength,
sensor.sensorColor
);
this.debugArrows.sensors[s].mesh.add(this.debugArrows.sensors[s].arrow);
}
}*/
// . . . . . . . . . . . . correct the path . . . . . . . . . . . . . .
if( this.debugArrows.avgDirection.vector.x != 0
|| this.debugArrows.avgDirection.vector.y != 0
|| this.debugArrows.avgDirection.vector.z != 0
) {
// get forward direction OR better the velocity angle of the car
var avgMoveVector = this.debugArrows.avgDirection.vector;
var forward = new THREE.Vector3(
1,
0,
0
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)).setY(0).normalize();
// get angle and steer.
var cos = avgMoveVector.dot(forward);
var angleRadians = Math.acos(cos);
var steerAngleModifier = GameLib.API.Vector3.AngleDirection(
forward,
avgMoveVector,
new GameLib.API.Vector3(
0,
1,
0
)
);
var steerAngle = Math.min(angleRadians, 0.5) * steerAngleModifier;
this.raycastVehicleComponent.instance.setSteeringValue(steerAngle, 0);
this.raycastVehicleComponent.instance.setSteeringValue(steerAngle, 1);
}
}
};
//#endif

View File

@ -1,244 +0,0 @@
/**
*
* @param id
* @param name
* @param targetEntity GameLib.Entity
* @param steeringSpeed
* @param maxSteerAngle
* @constructor
*/
GameLib.ComponentVehicleAIPathSteering = function(
id,
name,
targetEntity,
steeringSpeed,
maxSteerAngle
) {
this.id = id|| GameLib.Utils.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.Utils.Extend(GameLib.ComponentVehicleAIPathSteering, GameLib.Component);
//
this.targetEntity = targetEntity;
this.steeringSpeed = steeringSpeed || 2.5;
this.maxSteerAngle = maxSteerAngle || 0.5;
this.raycastVehicleComponent = null;
this.debugArrows = {};
};
///////////////////////// Methods to override //////////////////////////
GameLib.ComponentVehicleAIPathSteering.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
this.parentEntity = parentEntity;
if(!this.raycastVehicleComponent) {
console.warn("NO RAYCAST VEHICLE FOUND!");
}
};
//#ifdef RUNTIME__
GameLib.ComponentVehicleAIPathSteering.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(this.targetEntity && this.raycastVehicleComponent) {
var v1 = new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
);
var v2 = new THREE.Vector3(
this.targetEntity.position.x,
this.targetEntity.position.y,
this.targetEntity.position.z
);
// get forward vector of the car.
var v3 = new THREE.Vector3(
1,
0,
0
);
var q1 = new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
);
var q2 = new THREE.Quaternion(
this.targetEntity.quaternion.x,
this.targetEntity.quaternion.y,
this.targetEntity.quaternion.z,
this.targetEntity.quaternion.w
);
var v1ToV2 = v2.sub(v1);
var forward = v3.applyQuaternion(q1).normalize();
var v1v2cpy = new THREE.Vector3(
v1ToV2.x,
v1ToV2.y,
v1ToV2.z
).applyQuaternion(q2).normalize();
v1v2cpy.y = 0;
v1v2cpy = v1v2cpy.normalize();
var forwardcpy = new THREE.Vector3(
forward.x,
0,
forward.z
).normalize();
var cos = v1v2cpy.dot(forwardcpy);
var angleRadians = Math.acos(cos);
var steerAngleModifier = GameLib.API.Vector3.AngleDirection(
forwardcpy,
v1v2cpy,
new GameLib.API.Vector3(
0,
1,
0
)
);
var steerAngle = Math.min(angleRadians, this.maxSteerAngle) * steerAngleModifier;
if(isNaN(steerAngle)) {
console.log("NOT A NUMBER", steerAngle);
}
// check if forward & movedirection are pointing in two different directions
/* if(cos < 0) {
console.log("movedirection & forward are pointing in different directions");
}*/
//if(steerAngle > 0.002 || steerAngle < -0.002) {
this.raycastVehicleComponent.instance.setSteeringValue(steerAngle, 0);
this.raycastVehicleComponent.instance.setSteeringValue(steerAngle, 1);
//}
/*// - - - - - - - - - - - DEBUG ARROW 1 - - - - - - - - -
{
if(!this.debugArrows.v1) {
this.debugArrows.v1 = this.debugArrows.v1 || {};
}
if(!this.debugArrows.v1.mesh) {
var geometryTHREE = new THREE.Geometry();
var wireframeMesh = new THREE.Mesh(
geometryTHREE,
new THREE.MeshBasicMaterial(
{
color: 0xff0000,
wireframe: true,
opacity: 1
}
)
);
this.debugArrows.v1.mesh = wireframeMesh;
sys.game.scenes["MainScene"].instance.add(this.debugArrows.v1.mesh);
}
if(this.debugArrows.v1.arrow) {
this.debugArrows.v1.mesh.remove(this.debugArrows.v1.arrow);
}
var len = v1ToV2.length();
this.debugArrows.v1.arrow = new THREE.ArrowHelper(
v1ToV2.normalize(),
new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
),
len || 100,
new THREE.Color(
0,
1,
0
)
);
this.debugArrows.v1.mesh.add( this.debugArrows.v1.arrow );
}
// - - - - - - - - - - - DEBUG ARROW 2 - - - - - - - - -
{
if(!this.debugArrows.v2) {
this.debugArrows.v2 = this.debugArrows.v2 || {};
}
if(!this.debugArrows.v2.mesh) {
var geometryTHREE = new THREE.Geometry();
var wireframeMesh = new THREE.Mesh(
geometryTHREE,
new THREE.MeshBasicMaterial(
{
color: 0xff0000,
wireframe: true,
opacity: 1
}
)
);
this.debugArrows.v2.mesh = wireframeMesh;
sys.game.scenes["MainScene"].instance.add(this.debugArrows.v2.mesh);
}
if(this.debugArrows.v2.arrow) {
this.debugArrows.v2.mesh.remove(this.debugArrows.v2.arrow);
}
this.debugArrows.v2.arrow = new THREE.ArrowHelper(
forward,
new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
),
12,
new THREE.Color(
1.0,
0.0,
0.0
)
);
this.debugArrows.v2.mesh.add( this.debugArrows.v2.arrow );
}*/
}
this.raycastVehicleComponent.instance.applyEngineForce(-3500, 2);
this.raycastVehicleComponent.instance.applyEngineForce(-3500, 3);
};
//#endif

View File

@ -4,15 +4,13 @@
* @param path String
* @param name String
* @param meshes GameLib.D3.API.Mesh []
* @param quaternion GameLib.API.Quaternion
* @param position GameLib.API.Vector3
* @param rotation GameLib.API.Vector3
* @param quaternion GameLib.API.Quaternion
* @param scale GameLib.API.Vector3
* @param parentSceneId
* @param lights GameLib.D3.API.Light[]
* @param worlds GameLib.D3.API.World[]
* @param entities GameLib.API.Entity[]
* @param components GameLib.API.Component[]
* @param entityManager GameLib.EntityManager
* @param shapes GameLib.D3.API.Shape[]
* @param cameras
* @param activeCameraIndex
@ -24,14 +22,13 @@ GameLib.D3.API.Scene = function(
path,
name,
meshes,
quaternion,
position,
quaternion,
scale,
parentSceneId,
lights,
worlds,
entities,
components,
entityManager,
shapes,
cameras,
activeCameraIndex,
@ -57,16 +54,16 @@ GameLib.D3.API.Scene = function(
}
this.meshes = meshes;
if (GameLib.Utils.UndefinedOrNull(quaternion)) {
quaternion = new GameLib.API.Quaternion();
}
this.quaternion = quaternion;
if (GameLib.Utils.UndefinedOrNull(position)) {
position = new GameLib.API.Vector3();
}
this.position = position;
if (GameLib.Utils.UndefinedOrNull(quaternion)) {
quaternion = new GameLib.API.Quaternion();
}
this.quaternion = quaternion;
if (GameLib.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.API.Vector3(1,1,1);
}
@ -87,15 +84,10 @@ GameLib.D3.API.Scene = function(
}
this.worlds = worlds;
if (GameLib.Utils.UndefinedOrNull(entities)) {
entities = [];
if (GameLib.Utils.UndefinedOrNull(entityManager)) {
entityManager = null;
}
this.entities = entities;
if (GameLib.Utils.UndefinedOrNull(components)) {
components = [];
}
this.components = components;
this.entityManager = entityManager;
if (GameLib.Utils.UndefinedOrNull(shapes)) {
shapes = [];

View File

@ -13,36 +13,46 @@ GameLib.D3.Scene = function Scene(
apiScene,
imageFactory
) {
var property;
for (property in apiScene) {
if (apiScene.hasOwnProperty(property)) {
this[property] = apiScene[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
GameLib.D3.API.Scene.call(
this,
apiScene.id,
apiScene.path,
apiScene.name,
apiScene.meshes,
apiScene.position,
apiScene.quaternion,
apiScene.scale,
apiScene.parentSceneId,
apiScene.lights,
apiScene.worlds,
apiScene.entityManager,
apiScene.shapes,
apiScene.cameras,
apiScene.activeCameraIndex,
apiScene.splines
);
this.position = new GameLib.Vector3(
graphics,
this,
this.position
);
this.scale = new GameLib.Vector3(
graphics,
this,
this.scale
);
this.quaternion = new GameLib.Quaternion(
graphics,
this,
this.quaternion
);
this.scale = new GameLib.Vector3(
graphics,
this,
this.scale
);
this.progressCallback = progressCallback;
this.instance = this.createInstance();
@ -53,8 +63,6 @@ GameLib.D3.Scene = function Scene(
"cameras",
"meshes",
"lights",
"components",
"entities",
"splines"
];
@ -71,10 +79,11 @@ GameLib.D3.Scene = function Scene(
}
}
}
this.linkObjects();
};
GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype);
GameLib.D3.Scene.prototype.constructor = GameLib.D3.Scene;
/**
* Creates an instance scene
* @returns {GameLib.D3.Scene|THREE.Scene|ApiLib.Scene|*|Scene}
@ -122,17 +131,7 @@ GameLib.D3.Scene.prototype.toApiScene = function() {
}
);
var apiComponents = this.components.map(
function(component) {
return component.toApiComponent();
}
);
var apiEntities = this.entities.map(
function(entity) {
return entity.toApiEntity();
}
);
var apiEntityManager = this.entityManager.toApiEntityManager();
var apiCameras = this.cameras.map(
function(camera) {
@ -169,8 +168,7 @@ GameLib.D3.Scene.prototype.toApiScene = function() {
this.parentSceneId,
apiLights,
apiWorlds,
apiEntities,
apiComponents,
apiEntityManager,
apiShapes,
apiCameras,
this.activeCameraIndex,
@ -209,6 +207,11 @@ GameLib.D3.Scene.FromObjectScene = function(
)
}
),
new GameLib.API.Vector3(
objectScene.position.x,
objectScene.position.y,
objectScene.position.z
),
new GameLib.API.Quaternion(
objectScene.quaternion.x,
objectScene.quaternion.y,
@ -221,11 +224,6 @@ GameLib.D3.Scene.FromObjectScene = function(
),
objectScene.quaternion.angle
),
new GameLib.API.Vector3(
objectScene.position.x,
objectScene.position.y,
objectScene.position.z
),
new GameLib.API.Vector3(
objectScene.scale.x,
objectScene.scale.y,
@ -248,21 +246,9 @@ GameLib.D3.Scene.FromObjectScene = function(
);
}
),
objectScene.entities.map(
function (objectEntity) {
return GameLib.Entity.FromObjectEntity(
graphics,
objectEntity
);
}
),
objectScene.components.map(
function (objectComponent) {
return GameLib.Component.FromObjectComponent(
graphics,
objectComponent
);
}
GameLib.EntityManager.FromObjectEntityManager(
graphics,
objectScene.entityManager
),
objectScene.shapes.map(
function (objectShape) {

View File

@ -88,7 +88,7 @@ GameLib.EntityManager.prototype.queryComponents = function(components) {
* Converts a GameLib.Entity to GameLib.API.Entity
* @returns {GameLib.API.Entity}
*/
GameLib.EntityManager.prototype.toApiEntity = function() {
GameLib.EntityManager.prototype.toApiEntityManager = function() {
//TODO: refactor / fix
// var apiEntity = new GameLib.API.Entity(