This commit is contained in:
James B. Pollack 2015-12-10 18:16:01 -08:00
parent b16822ed8a
commit 99bc47867a
2 changed files with 147 additions and 142 deletions

View file

@ -127,9 +127,15 @@ var target = Entities.addEntity(targetProperties);
function addRat() {
var rat = Entities.addEntity(modelRatProperties);
return rat
return rat;
}
//every sixth rat will play a sound
var RAT_SOUND_RATE = 6;
//spawn rate will be multiplied by this to clear any sounds hanging around
var RAT_SOUND_CLEAR_RATE = 3;
var rats = [];
var metaRats = [];
var ratCount = 0;
@ -257,11 +263,11 @@ function createRatSoundInjector() {
var audioOptions = {
volume: 0.05,
loop: false
}
};
var injector = Audio.playSound(ratRunningSound, audioOptions);
return injector
return injector;
}
function moveRats() {
@ -270,8 +276,6 @@ function moveRats() {
//remove the rat if its near the nest
checkDistanceFromNest(rat);
//otherwise figure out where to send it
//see if there are avatars to run from
var avatarFlightVectors = steer.fleeAllAvatars(rat);
var averageAvatarFlight;
@ -280,7 +284,7 @@ function moveRats() {
if (i === 0) {
averageAvatarFlight = avatarFlightVectors[0];
} else {
averageAvatarFlight = Vec3.sum(avatarFlightVectors[i - 1], avatarFlightVectors[i])
averageAvatarFlight = Vec3.sum(avatarFlightVectors[i - 1], avatarFlightVectors[i]);
}
}
averageAvatarFlight = Vec3.multiply(averageAvatarFlight, 1 / avatarFlightVectors.length);
@ -325,12 +329,12 @@ function moveRats() {
var eulerAngle = Quat.safeEulerAngles(ratRotation);
eulerAngle.x = 0;
eulerAngle.z = 0;
var constrainedRotation = Quat.fromVec3Degrees(eulerAngle)
var constrainedRotation = Quat.fromVec3Degrees(eulerAngle);
Entities.editEntity(rat, {
velocity: averageVector,
rotation: constrainedRotation,
})
});
//have to make a 'meta' rat object to keep track of rats for updating sound injector locations. parenting sounds would make this easy.
var metaRat = getMetaRatByRat(rat);
@ -452,23 +456,23 @@ var ratSpawnerInterval;
if (USE_CONSTANT_SPAWNER === true) {
ratSpawnerInterval = Script.setInterval(function() {
var rat = addRat();
playRatRunningAnimation(rat);
rats.push(rat);
ratCount++;
if (ratCount % 6 === 0) {
var metaRat = {
rat: rat,
injector: createRatSoundInjector()
}
metaRats.push(metaRat);
var rat = addRat();
playRatRunningAnimation(rat);
rats.push(rat);
ratCount++;
if (ratCount % RAT_SOUND_RATE === 0) {
var metaRat = {
rat: rat,
injector: createRatSoundInjector()
}
metaRats.push(metaRat);
Script.setTimeout(function() {
//if we have too many injectors hanging around there are problems
metaRat.injector.stop();
delete metaRat.injector;
}, RAT_SPAWN_RATE * 3);
}
Script.setTimeout(function() {
//if we have too many injectors hanging around there are problems
metaRat.injector.stop();
delete metaRat.injector;
}, RAT_SPAWN_RATE * RAT_SOUND_CLEAR_RATE;
}
}, RAT_SPAWN_RATE);
}
}, RAT_SPAWN_RATE);
}

View file

@ -11,171 +11,172 @@
//
function flee(thisEntity, target) {
var targetPosition = Entities.getEntityProperties(target, "position").position;
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 1;
var MAX_FORCE = 1;
var targetPosition = Entities.getEntityProperties(target, "position").position;
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 1;
var MAX_FORCE = 1;
var FLEE_RANGE = 2;
var desired = Vec3.subtract(location, targetPosition);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired);
if (d < FLEE_RANGE) {
var steer = Vec3.subtract(desired, velocity);
var desired = Vec3.subtract(location, targetPosition);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired);
if (d < 2) {
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
return steer;
} else {
// print('target too far away to flee' + d);
return
}
return steer;
} else {
// print('target too far away to flee' + d);
return
}
}
function fleeAllAvatars(thisEntity) {
//print('FLEE AVATARS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
//print('FLEE AVATARS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var nearbyEntities = Entities.findEntities(location, 3);
var flightVectors = [];
for (var entityIndex = 0; entityIndex < nearbyEntities.length; entityIndex++) {
var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Avatar-Detector') {
//print('found an avatar to flee')
var nearbyEntities = Entities.findEntities(location, 3);
var flightVectors = [];
for (var entityIndex = 0; entityIndex < nearbyEntities.length; entityIndex++) {
var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Avatar-Detector') {
//print('found an avatar to flee')
var MAX_SPEED = 8;
var MAX_FORCE = 8;
var MAX_SPEED = 8;
var MAX_FORCE = 8;
var FLEE_AVATAR_RANGE = 3;
var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired);
if (d < FLEE_AVATAR_RANGE) {
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
flightVectors.push(steer);
} else {
// print('target too far away from this avatar to flee' + d);
}
}
var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired);
if (d < 3) {
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
flightVectors.push(steer);
} else {
// print('target too far away from this avatar to flee' + d);
}
}
}
return flightVectors;
return flightVectors;
}
function fleeAvoiderBlocks(thisEntity) {
// print('FLEE AVOIDER BLOCKS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
// print('FLEE AVOIDER BLOCKS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var nearbyEntities = Entities.findEntities(location, 2);
var flightVectors = [];
for (var entityIndex = 0; entityIndex < nearbyEntities.length; entityIndex++) {
var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Rat-Avoider') {
// print('found an avoiderblock to flee');
var MAX_SPEED = 11;
var MAX_FORCE = 6;
var nearbyEntities = Entities.findEntities(location, 2);
var flightVectors = [];
for (var entityIndex = 0; entityIndex < nearbyEntities.length; entityIndex++) {
var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Rat-Avoider') {
// print('found an avoiderblock to flee');
var MAX_SPEED = 11;
var MAX_FORCE = 6;
var FLEE_AVOIDER_RANGE = 5;
var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired);
var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired);
if (d < 5) {
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE)
flightVectors.push(steer);
} else {
//print('target too far away from this avoider to flee' + d);
}
if (d < FLEE_AVOIDER_RANGE) {
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
flightVectors.push(steer);
} else {
//print('target too far away from this avoider to flee' + d);
}
}
}
}
return flightVectors;
return flightVectors;
}
function arrive(thisEntity, target) {
var targetPosition = Entities.getEntityProperties(target, "position").position;
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 10;
var MAX_FORCE = 6;
var ARRIVAL_DISTANCE = 2;
var targetPosition = Entities.getEntityProperties(target, "position").position;
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 10;
var MAX_FORCE = 6;
var ARRIVAL_DISTANCE = 2;
var desired = Vec3.subtract(targetPosition, location);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
var desired = Vec3.subtract(targetPosition, location);
var d = Vec3.length(desired);
desired = Vec3.normalize(desired);
if (d < ARRIVAL_DISTANCE) {
var m = scale(d, 0, ARRIVAL_DISTANCE, 0, MAX_SPEED);
} else {
desired = Vec3.multiply(MAX_SPEED, desired);
if (d < ARRIVAL_DISTANCE) {
var m = scale(d, 0, ARRIVAL_DISTANCE, 0, MAX_SPEED);
} else {
desired = Vec3.multiply(MAX_SPEED, desired);
}
}
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, 0, desired.z);
steer = steerVector.limit(MAX_FORCE);
return steer;
return steer;
}
function V3(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return
this.x = x;
this.y = y;
this.z = z;
return
}
V3.prototype.length = function() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};
V3.prototype.limit = function(s) {
var len = this.length();
var len = this.length();
if (len > s && len > 0) {
this.scale(s / len);
}
if (len > s && len > 0) {
this.scale(s / len);
}
return this;
return this;
};
V3.prototype.scale = function(f) {
this.x *= f;
this.y *= f;
this.z *= f;
return this;
this.x *= f;
this.y *= f;
this.z *= f;
return this;
};
var v3 = new V3();
var scale = function(value, min1, max1, min2, max2) {
return min2 + (max2 - min2) * ((value - min1) / (max1 - min1));
return min2 + (max2 - min2) * ((value - min1) / (max1 - min1));
}
loadSteer = function() {
return {
flee: flee,
fleeAllAvatars: fleeAllAvatars,
fleeAvoiderBlocks: fleeAvoiderBlocks,
arrive: arrive
}
return {
flee: flee,
fleeAllAvatars: fleeAllAvatars,
fleeAvoiderBlocks: fleeAvoiderBlocks,
arrive: arrive
};
}