208 lines
No EOL
9.2 KiB
JavaScript
208 lines
No EOL
9.2 KiB
JavaScript
//batAttackTrigger.js
|
|
|
|
(function(){
|
|
var BAT_ORBIT_RADIUS = 50;
|
|
var batIntervalCheck = null;
|
|
var CHECK_INTERVAL = 50;
|
|
var swooperData;
|
|
var batData;
|
|
var tornadoData;
|
|
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;
|
|
|
|
tornadoData = getPropertiesFromNamedObj("bat_tornado", position, 30, ["position"]);
|
|
swooperData = getPropertiesFromNamedObjects("bat_swooper", tornadoData.position, BAT_ORBIT_RADIUS, ["position"]);
|
|
batData = getPropertiesFromNamedObjects("bat", tornadoData.position, BAT_ORBIT_RADIUS*2, ["position"]);
|
|
//console.log("swooper data length ", JSON.stringify(swooperData[3].id));
|
|
//console.log(JSON.stringify(batData));
|
|
|
|
// var props = Entities.getEntityProperties(entity, [ "userData" ]);
|
|
// var data = {};
|
|
// if (props.userData) {
|
|
// data = JSON.parse(props.userData);
|
|
// }
|
|
// var changed = false;
|
|
// for(var p in DEFAULT_USERDATA) {
|
|
// if (!(p in data)) {
|
|
// data[p] = DEFAULT_USERDATA[p];
|
|
// changed = true;
|
|
// }
|
|
// }
|
|
// if (changed) {
|
|
// //debugPrint("applying default values to userData");
|
|
// Entities.editEntity(entity, { userData: JSON.stringify(data) });
|
|
// }
|
|
// this.updateSettings();
|
|
|
|
// Subscribe to toggle notifications using entity ID as a channel name
|
|
// Messages.subscribe(entity);
|
|
// Messages.messageReceived.connect(this, "_onMessageReceived");
|
|
};
|
|
|
|
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 getPropertiesFromNamedObj(entityName, searchOriginPosition, searchRadius, arrayOfProperties) {
|
|
|
|
var entityList = Entities.findEntitiesByName(
|
|
entityName,
|
|
searchOriginPosition,
|
|
searchRadius
|
|
);
|
|
|
|
if (entityList.length > 0) {
|
|
return Entities.getEntityProperties(entityList[0], arrayOfProperties);
|
|
} else {
|
|
console.log("GOT NOTHING");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function getPropertiesFromNamedObjects(entityName, searchOriginPosition, searchRadius, arrayOfProperties) {
|
|
|
|
var entityList = Entities.findEntitiesByName(
|
|
entityName,
|
|
searchOriginPosition,
|
|
searchRadius
|
|
);
|
|
var returnedObjects = [];
|
|
|
|
// console.log("getProperties searchOriginPosition ",JSON.stringify(searchOriginPosition));
|
|
// console.log("getProperties searchRadius ",JSON.stringify(searchRadius));
|
|
// console.log("getProperties entityList ",JSON.stringify(entityList));
|
|
|
|
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)) {
|
|
// 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<distance.length; i++){
|
|
if (distance[i] <= 10.0 && batData[i].parentID != tornadoData.id && checkDistOnce === false){
|
|
console.log("distance less than 10!!!!!! ", distance[i]);
|
|
console.log("PRECHANGE: batData parent: ", batData[i].parentID, "tornado ID: ", tornadoData.id)
|
|
// Join the tornado!
|
|
Entities.callEntityServerMethod(batData[i].id, "changeParent", [tornadoData.id]);
|
|
//Entities.callEntityServerMethod(batData[i].id, "changeParent", tornadoData.id);
|
|
console.log("POSTCHANGE: batData parent: ", batData[i].parentID, "tornado ID: ", tornadoData.id)
|
|
checkDistOnce = true;
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < batData.length; i++){
|
|
if (batData[i].parentID == tornadoData.id && endSwarmCheck[i] === false){
|
|
// After tornado-ing for a bit, swoop back out.
|
|
endSwarmCheck[i] = true;
|
|
var index = i;
|
|
Script.setTimeout(function(){
|
|
console.log("tornado id ", tornadoData.id);
|
|
console.log("bat parent before ", batData[index].parentID);
|
|
//Entities.callEntityServerMethod(batData[index].id, "changeParent", [oldParents[index]])
|
|
Entities.callEntityServerMethod(batData[index].id, "changeParent", oldParents[index])
|
|
console.log("bat parent after ", batData[index].parentID);
|
|
var angularVelocityToEnd = {
|
|
x: 0,
|
|
y: 0,
|
|
z: 0
|
|
};
|
|
Script.setTimeout(function(){
|
|
Entities.callEntityServerMethod(swooperData[index].id, "swoopTheBats", [JSON.stringify(angularVelocityToEnd)]);
|
|
runOnce = false;
|
|
checkDistOnce = false;
|
|
console.log("finished swarming, checkdist reset", checkDistOnce);
|
|
}, 1000);
|
|
}, 4000);
|
|
}
|
|
}
|
|
},CHECK_INTERVAL);
|
|
}
|
|
};
|
|
|
|
this.unload = function(entityID) {
|
|
this.cleanup();
|
|
};
|
|
|
|
this.cleanup = function() {
|
|
Script.stop();
|
|
};
|
|
}) |