2016-11-09 14:17:13 +01:00
|
|
|
//
|
|
|
|
// uses the entity position + mesh bounding sphere to check if intersections
|
2016-11-14 10:58:30 +01:00
|
|
|
// this is a bit jerky to use.
|
|
|
|
// so please feel free to use the box-sphere & box-box triggers instead.
|
2016-11-09 14:17:13 +01:00
|
|
|
//
|
|
|
|
GameLib.D3.ComponentTriggerSphereSphere = function(
|
|
|
|
componentId,
|
|
|
|
sphereRadius,
|
|
|
|
entitiesToCheck,
|
2016-11-11 16:25:39 +01:00
|
|
|
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;
|
2016-11-11 16:25:39 +01:00
|
|
|
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) {
|
2016-11-14 10:58:30 +01:00
|
|
|
var maxScale = Math.max(
|
|
|
|
entityToCheck.scale.x,
|
|
|
|
Math.max(
|
|
|
|
entityToCheck.scale.y,
|
|
|
|
entityToCheck.scale.z
|
|
|
|
)
|
|
|
|
);
|
|
|
|
ComponentTriggerSphereSphere_targetBoundingSphereRadius = entityToCheck.mesh.geometry.boundingSphere.radius * maxScale;
|
2016-11-09 14:17:13 +01:00
|
|
|
} else {
|
2016-11-14 10:58:30 +01:00
|
|
|
ComponentTriggerSphereSphere_targetBoundingSphereRadius = 1.0;
|
2016-11-09 14:17:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-11-11 16:25:39 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|