r3-legacy/src/game-lib-component-trigger-...

94 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-11-09 14:17:13 +01:00
//
// uses the entity position + mesh bounding sphere to check if intersections
//
GameLib.D3.ComponentTriggerSphereSphere = function(
componentId,
sphereRadius,
entitiesToCheck,
onInside,
onEnter,
onLeave
2016-11-09 14:17:13 +01:00
) {
this.componentId = componentId || GameLib.D3.Tools.RandomId();
this.parentEntity = null;
// Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.
GameLib.D3.Utils.Extend(GameLib.D3.ComponentTriggerSphereSphere, GameLib.D3.ComponentInterface);
this.entitiesToCheck = entitiesToCheck || [];
this.sphereRadius = sphereRadius || 1.0;
this.onInside = onInside || null;
this.onEnter = onEnter || null;
this.onLeave = onLeave || null;
// runtime code
this.entitiesInside = [];
2016-11-09 14:17:13 +01:00
};
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.D3.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) {
ComponentTriggerSphereSphere_targetBoundingSphereRadius = entityToCheck.mesh.geometry.boundingSphere.radius;
} else {
ComponentTriggerSphereSphere_targetBoundingSphereRadius = 0.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) {
this.onEnter(parentEntity, entityToCheck);
}
}
if(this.onInside) {
this.onInside(parentEntity, entityToCheck);
}
} else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger
this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1);
if(this.onLeave) {
this.onLeave(parentEntity, entityToCheck);
2016-11-09 14:17:13 +01:00
}
}
}
};