content/hifi-content/jimi/json/dvaBirdScript3.js
2022-02-13 23:57:50 +01:00

203 lines
No EOL
7.1 KiB
JavaScript

(function() {
var dvaID = "{4d37c172-9315-4076-a091-a322d5428535}"; // ID number in edit entity
var birdSound = SoundCache.getSound("https://hifi-content.s3.amazonaws.com/jimi/personal/sounds/birdSounds.wav");
//var crowID = "{https://hifi-content.s3.amazonaws.com/jimi/personal/props/crow.fbx}";
// Array of butterflies
var butterflies = [];
this.enterEntity = function(entityID) { // If you enter the zone, this will trigger
print("YAY I WALKED INTO THE CUBE");
// Generate the butterflies
for (var i = 0; i < numButterflies; i++) {
addButterfly();
};
Audio.playSound(birdSound, {
Volume: 1
});
// dva appears
//var props = {
// position: {x: -1719.9800, y: -1353.7898, z: -3153.9185
//Entities.editEntity(dvaID, props);
// for (var bird = 0; bird < 10; bird++) {
// var randX = Math.random()-0.5 //randInteger(0.1); //Math.random(0.1)
// var randZ = Math.random()-0.5 //randInteger(0.1);
// var randY = Math.random()-0.5;
// Entities.addEntity("model", {
// velocity: {x:0+randX,y:10+randY,z:0+randZ},
// position: { x: -1740.27,y: -1346.5,z: -3141.54},
// modelURL: "https://hifi-content.s3.amazonaws.com/jimi/personal/props/crow.fbx",
// //animationURL: "http://bird.fbx"
// lifetime: 5,
// });
// };
// Audio.playSound(birdSound, { position: { x: -1740.27,y: -1346.5,z: -3141.54}, volume: 5});
Script.setTimeout(function() {
// show dva...
print ("Hey it's working!");
Entities.editEntity(dvaID, {visible: true});
}, 500);
Script.update.connect(updateButterflies);
};
this.leaveEntity = function(entityID) { // If you leave the zone, this will trigger
print("YAY I LEFT THE CUBE");
// dva disappears
// Entities.editEntity(dvaID, {visible: false});
Script.update.disconnect(updateButterflies);
for (var i = 0; i < numButterflies; i++) {
Entities.deleteEntity(butterflies[i]);
}
// Clear the array
butterflies = [];
};
// Adapting from butterflies.js. WE'll be modifying it to spawn crows instead
print("BUTTERFLIES START");
var numButterflies = 25;
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
// Create a random vector with individual lengths between a,b
function randVector(a, b) {
var rval = { x: a + Math.random() * (b - a), y: a + Math.random() * (b - a), z: a + Math.random() * (b - a) };
return rval;
}
var startTimeInSeconds = new Date().getTime() / 1000;
var NATURAL_SIZE_OF_BUTTERFLY = { x:0.7, y: 0.2, z: 0.47 };
var lifeTime = 60; // One minute lifespan
var range = 7.0; // Over what distance in meters do you want the flock to fly around
var frame = 0;
var DISTANCE_IN_FRONT_OF_ME = 1.5;
var DISTANCE_ABOVE_ME = 1.5;
var FIXED_LOCATION = false;
var flockPosition = { x: -1738.8, y: -1343.5, z: -3141.54};
// if (!FIXED_LOCATION) {
// var flockPosition = Vec3.sum(MyAvatar.position,Vec3.sum(
// Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_ABOVE_ME),
// Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_IN_FRONT_OF_ME)));
// } else {
// var flockPosition = { x: 4999.6, y: 4986.5, z: 5003.5 };
// }
// This is our butterfly object
function defineButterfly(entityID, targetPosition) {
this.entityID = entityID;
this.targetPosition = targetPosition;
}
function addButterfly() {
// Decide the size of butterfly
var color = { red: 100, green: 100, blue: 100 };
var size = 0;
var MINSIZE = 0.01;
var RANGESIZE = 0.05;
var maxSize = MINSIZE + RANGESIZE;
size = MINSIZE + Math.random() * RANGESIZE;
var dimensions = Vec3.multiply(NATURAL_SIZE_OF_BUTTERFLY, (size / maxSize));
var GRAVITY = -0.2;
var newFrameRate = 29 + Math.random() * 30;
var properties = {
type: "Model",
lifetime: lifeTime,
position: flockPosition,
rotation: Quat.fromPitchYawRollDegrees(-80 + Math.random() * 20, Math.random() * 360.0, 0.0),
velocity: { x: 0, y: 0, z: 0 },
gravity: { x: 0, y: GRAVITY, z: 0 },
damping: 0.00001,
dimensions: dimensions,
color: color,
animation: {
url: "https://hifi-content.s3.amazonaws.com/jimi/personal/props/crow_Anim3.fbx",
fps: newFrameRate,
loop: true,
running: true,
startAutomatically:true
},
modelURL: "https://hifi-content.s3.amazonaws.com/jimi/personal/props/crow_Anim3.fbx"
};
butterflies.push(Entities.addEntity(properties));
}
// Main update function
function updateButterflies(deltaTime) {
frame++;
// Only update every third frame because we don't need to do it too quickly
if ((frame % 3) == 0) {
// Update all the butterflies
var CHANCE_OF_IMPULSE = 0.04;
for (var i = 0; i < numButterflies; i++) {
if (Math.random() < CHANCE_OF_IMPULSE) {
var properties = Entities.getEntityProperties(butterflies[i]);
if (Vec3.length(Vec3.subtract(properties.position, flockPosition)) > range) {
Entities.editEntity(butterflies[i], { position: flockPosition } );
} else if (properties.velocity.y <= 0.0) {
// If falling, Create a new direction and impulse
var HORIZ_SCALE = 0.50;
var VERT_SCALE = 0.50;
var newHeading = Math.random() * 360.0;
var newVelocity = Vec3.multiply(HORIZ_SCALE, Quat.getFront(Quat.fromPitchYawRollDegrees(0.0, newHeading, 0.0)));
newVelocity.y = (Math.random() + 0.5) * VERT_SCALE;
Entities.editEntity(butterflies[i], { rotation: Quat.fromPitchYawRollDegrees(-80 + Math.random() * 20, newHeading, (Math.random() - 0.5) * 10),
velocity: newVelocity } );
}
}
}
// Check to see if we've been running long enough that our butterflies are dead
var nowTimeInSeconds = new Date().getTime() / 1000;
if ((nowTimeInSeconds - startTimeInSeconds) >= lifeTime) {
Script.stop();
return;
}
}
}
// register the call back so it fires before each data send
Script.update.connect(updateButterflies);
// Delete our little friends if script is stopped
Script.scriptEnding.connect(function() {
for (var i = 0; i < numButterflies; i++) {
Entities.deleteEntity(butterflies[i]);
}
});
print("BUTTERFLIES END");
});
// For (var) = Making things loop
// Bird++ means add 1 more bird
// Have to look up the codes, this is all currently pseudo code, look into docs
// Script setTimeout means once the script is running, wait x amount of time before running code.
// RandX and RandZ means Random x or z, randInteger(0.1) means it will vary between 0 - 0.1
// Audio.playSound(nameofSoundID, {props}) Can set properties like position of sound, volume, range