diff --git a/src/game-lib-component-trigger-sphere-sphere.js b/src/game-lib-component-trigger-sphere-sphere.js new file mode 100644 index 0000000..f6419db --- /dev/null +++ b/src/game-lib-component-trigger-sphere-sphere.js @@ -0,0 +1,71 @@ +// +// uses the entity position + mesh bounding sphere to check if intersections +// +GameLib.D3.ComponentTriggerSphereSphere = function( + componentId, + sphereRadius, + entitiesToCheck, + callback +) { + 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.callback = callback || null; +}; + +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.callback) { + this.callback(parentEntity, entityToCheck); + } + } + } +}; \ No newline at end of file