//batAttackTrigger.js (function(){ var BAT_ORBIT_RADIUS = 50; var batData; var zoneProperties; var HALF_MULTIPLIER = 0.5; this.preload = function(entityID) { // Get the Bat_tornado_position entity userData fields, and note the position of the entity. //debugPrint("Bat Zone preload " + entityID); entity = entityID; _this = this; zoneProperties = Entities.getEntityProperties(entityID, ["position", "dimensions", "rotation", "userData"]); var position = Entities.getEntityProperties(entityID, "position").position; batData = getPropertiesFromNamedObjects("bat", position, BAT_ORBIT_RADIUS*2, ["position"]); }; function isPositionInsideBox(position, boxProperties) { var localPosition = Vec3.multiplyQbyV(Quat.inverse(boxProperties.rotation), Vec3.subtract(position, boxProperties.position)); var halfDimensions = Vec3.multiply(boxProperties.dimensions, HALF_MULTIPLIER); return -halfDimensions.x <= localPosition.x && halfDimensions.x >= localPosition.x && -halfDimensions.y <= localPosition.y && halfDimensions.y >= localPosition.y && -halfDimensions.z <= localPosition.z && halfDimensions.z >= localPosition.z; } function isSomeAvatarOtherThanMeStillInsideTheObject(objectProperties) { var result = false; AvatarList.getAvatarIdentifiers().forEach(function(avatarID) { var avatar = AvatarList.getAvatar(avatarID); if (avatar.sessionUUID !== MyAvatar.sessionUUID) { if (isPositionInsideBox(avatar.position, objectProperties)) { result = true; } } }); return result; } function getPropertiesFromNamedObjects(entityName, searchOriginPosition, searchRadius, arrayOfProperties) { var entityList = Entities.findEntitiesByName( entityName, searchOriginPosition, searchRadius ); var returnedObjects = []; if (entityList.length > 0) { //return Entities.getEntityProperties(entityList.forEach, arrayOfProperties); entityList.forEach(function(entity){ var properties = Entities.getEntityProperties(entity, arrayOfProperties); returnedObjects.push(properties); }); return returnedObjects; } else { console.log("GOT NOTHING"); return null; } } this.enterEntity = function(entityID) { print("bat zone entered"); if (!isSomeAvatarOtherThanMeStillInsideTheObject(zoneProperties) && batData.length > 0) { batData.forEach(function(element){ var delay = 1; Script.setTimeout(function(){ console.log("showing bats"); Entities.callEntityServerMethod(element.id, "apparateBats", [true]); }, delay*100); delay += 1; }); } }; this.leaveEntity = function(entityID) { if (!isSomeAvatarOtherThanMeStillInsideTheObject(zoneProperties) && batData.length > 0) { Script.setTimeout(function(){ batData.forEach(function(element){ console.log("hiding bats"); Entities.callEntityServerMethod(element.id, "apparateBats", [false]); }); }, 6000); } } this.unload = function(entityID) { this.cleanup(); }; this.cleanup = function() { Script.stop(); }; }) // this.enterEntity = function(entityID) { // print("bat zone entered"); // if (!isSomeAvatarOtherThanMeStillInsideTheObject(zoneProperties)) { // // Collect data on the original parents of the bats for resetting them after the event has completed // var runOnce = false; // var checkDistOnce = false; // var endSwarmCheck = []; // var oldParents = []; // if(runOnce === false){ // batData.forEach(function(element){ // oldParents.push(element.parentID); // endSwarmCheck.push(false); // console.log("ORIGINAL BAT PARENT: ", JSON.stringify(element.parentID), " and OLD PARENT: ", oldParents[oldParents.length - 1]); // }); // console.log("oldParents got data", oldParents.length); // var angularVelocityToPass = { // x: Math.PI, // y: 0, // z: 0 // }; // // Initiate the swoop // swooperData.forEach(function(element){ // // console.log("Swooper Element ID ", JSON.stringify(element.id)); // Entities.callEntityServerMethod(element.id, "swoopTheBats", [JSON.stringify(angularVelocityToPass)]); // }); // runOnce = true; // } // // Start an interval to distance check bats from the tornado center. // batIntervalCheck = Script.setInterval(function(){ // batData = null; // batData = getPropertiesFromNamedObjects("bat", tornadoData.position, BAT_ORBIT_RADIUS*2, ["position"]); // var distance = []; // batData.forEach(function(element){ // // console.log("bat data is: ", JSON.stringify(element)); // distance.push(Vec3.distance(element.position, tornadoData.position)); // }); // for(var i=0; i