This commit is contained in:
James B. Pollack 2015-11-17 11:33:48 -08:00
parent f669f8ac79
commit 08ca03e0af
2 changed files with 126 additions and 108 deletions

View file

@ -1,39 +1,45 @@
Script.include('steer.js')
Script.include('steer.js');
var steer = loadSteer();
Script.include('tween.js');
Script.include('../libraries/tween.js');
var TWEEN = loadTween();
var USE_CONSTANT_SPAWNER = false;
var USE_CONSTANT_SPAWNER = true;
var RAT_SPAWNER_LOCATION = {
x: 0,
y: 0,
z: 0
x: 1001,
y: 98.5,
z: 1039
};
var RAT_NEST_LOCATION = {
x: 0,
y: 0,
z: 0
x: 1003.5,
y: 99,
z: 964.2
};
var RAT_MODEL_URL = '';
var RAT_RUNNING_ANIMATION_URL = '';
var RAT_DEATH_ANIMATION_URL = '';
var RAT_DIMENSIONS = {
x: 0.22,
y: 0.32,
z: 1.14
};
var RAT_IN_NEST_DISTANCE = 0.25;
var RAT_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/james/rat/ratrotated.fbx';
var RAT_RUNNING_ANIMATION_URL = 'http://hifi-content.s3.amazonaws.com/james/rat/walk.fbx';
var RAT_DEATH_ANIMATION_URL = 'http://hifi-content.s3.amazonaws.com/james/rat/death.fbx';
var RAT_IN_NEST_DISTANCE = 3;
//how many milliseconds between rats
var RAT_SPAWN_RATE = 1000;
var RAT_SPAWN_RATE = 2500;
function playRatRunningAnimation() {
function playRatRunningAnimation(rat) {
var animationSettings = JSON.stringify({
running: true
});
Entities.editEntity(rat, {
animationURL: RAT_RUNNING_ANIMATION_URL,
animationSettings: animationSettings,
animationSettings: animationSettings
// animation: {
// url: RAT_RUNNING_ANIMATION_URL,
// running: true,
@ -42,24 +48,23 @@ function playRatRunningAnimation() {
});
}
function playRatDeathAnimation() {
function playRatDeathAnimation(rat) {
var animationSettings = JSON.stringify({
running: true
});
Entities.editEntity(rat, {
animationURL: RAT_DEATH_ANIMATION_URL,
animationSettings: animationSettings,
animationSettings: animationSettings
// animation: {
// url: RAT_DEATH_ANIMATION_URL,
// running: true,
// fps: 180
// running: true
// },
});
}
var ratProperties = {
name: 'Hifi-Rat',
name: 'Rat',
type: 'Box',
color: {
red: 0,
@ -71,21 +76,28 @@ var ratProperties = {
y: 1,
z: 1
},
collisionsWillMove: true,
gravity: {
x: 0,
y: -9.8,
z: 0
},
position: RAT_SPAWNER_LOCATION
};
var modelRatProperties = {
name: 'Hifi-Rat',
name: 'rat',
type: 'Model',
modelURL: RAT_MODEL_URL,
dimensions: RAT_DIMENSIONS,
position: RAT_SPAWNER_LOCATION,
shapeType: 'Box',
collisionsWillMove: true,
gravity: {
x: 0,
y: -9.8
y: -9.8,
z: 0
},
}
//enable this if for some reason we want grabbable rats
// userData:JSON.stringify({
// grabbableKey:{
@ -112,56 +124,54 @@ var targetProperties = {
// script: Script.resolvePath('rat.js')
};
var target = Entities.addEntity(targetProperties)
var target = Entities.addEntity(targetProperties);
function addRat() {
var rat = Entities.addEntity(ratProperties);
var rat = Entities.addEntity(modelRatProperties);
rats.push(rat);
}
var rats = [];
addRat();
var AVOIDER_Y_HEIGHT = 99;
var FIRST_AVOIDER_START_POSITION = {
x: 0,
y: 0,
z: 0
x: 1004,
y: AVOIDER_Y_HEIGHT,
z: 1019
};
var FIRST_AVOIDER_FINISH_POSITION = {
x: 0,
y: 0,
z: 0
x: 997,
y: AVOIDER_Y_HEIGHT,
z: 1019
};
var SECOND_AVOIDER_START_POSITION = {
x: 0,
y: 0,
z: 0
x: 998,
y: AVOIDER_Y_HEIGHT,
z: 998
};
var SECOND_AVOIDER_FINISH_POSITION = {
x: 0,
y: 0,
z: 0
x: 1005,
y: AVOIDER_Y_HEIGHT,
z: 999
};
var THIRD_AVOIDER_START_POSITION = {
x: 0,
y: 0,
z: 0
x: 1001.5,
y: 100,
z: 978
};
var THIRD_AVOIDER_FINISH_POSITION = {
x: 0,
y: 0,
z: 0
x: 1005,
y: 100,
z: 974
};
var avoiders = [
addAvoiderBlock(FIRST_AVOIDER_START_POSITION),
addAvoiderBlock(SECOND_AVOIDER_START_POSITION),
addAvoiderBlock(THIRD_AVOIDER_START_POSITION)
];
var avoiders = [];
addAvoiderBlock(FIRST_AVOIDER_START_POSITION);
addAvoiderBlock(SECOND_AVOIDER_START_POSITION);
addAvoiderBlock(THIRD_AVOIDER_START_POSITION);
function addAvoiderBlock(position) {
var avoiderProperties = {
name: 'Hifi-Rat-Avoider',
type: 'Box',
@ -175,24 +185,23 @@ function addAvoiderBlock(position) {
y: 1,
z: 1
},
position: {
x: 1,
y: 1,
z: 1
},
position: position,
collisionsWillMove: false,
ignoreForCollisions: true
}
ignoreForCollisions: true,
visible:false
};
var avoider = Entities.addEntity(avoiderProperties);
avoiders.push(avoider);
};
}
addAvoiderBlock();
tweenAvoider(avoiders[0]);
tweenAvoider(avoiders[0], FIRST_AVOIDER_START_POSITION, FIRST_AVOIDER_FINISH_POSITION);
tweenAvoider(avoiders[1], SECOND_AVOIDER_START_POSITION, SECOND_AVOIDER_FINISH_POSITION);
tweenAvoider(avoiders[2], THIRD_AVOIDER_START_POSITION, THIRD_AVOIDER_FINISH_POSITION);
function tweenAvoider(entityID, startPosition, endPosition) {
var ANIMATION_DURATION = 500;
var ANIMATION_DURATION = 4200;
var begin = {
x: startPosition.x,
@ -200,11 +209,11 @@ function tweenAvoider(entityID, startPosition, endPosition) {
z: startPosition.z
};
var target = endPosition;
var end = endPosition;
var original = startPosition;
var tweenHead = new TWEEN.Tween(begin).to(target, ANIMATION_DURATION);
var tweenHead = new TWEEN.Tween(begin).to(end, ANIMATION_DURATION);
function updateTo() {
Entities.editEntity(entityID, {
@ -213,8 +222,8 @@ function tweenAvoider(entityID, startPosition, endPosition) {
y: begin.y,
z: begin.z
}
})
};
});
}
function updateBack() {
Entities.editEntity(entityID, {
@ -224,7 +233,7 @@ function tweenAvoider(entityID, startPosition, endPosition) {
z: begin.z
}
})
};
}
var tweenBack = new TWEEN.Tween(begin).to(original, ANIMATION_DURATION).onUpdate(updateBack);
@ -244,10 +253,10 @@ function updateTweens() {
function moveRats() {
rats.forEach(function(rat) {
checkDistanceFromNest(rat);
// print('debug1')
var avatarFlightVectors = steer.fleeAllAvatars(rat);
print('avatarFlightVectors' + avatarFlightVectors)
// print('avatarFlightVectors' + avatarFlightVectors)
var i, j;
var averageAvatarFlight;
@ -259,7 +268,7 @@ function moveRats() {
}
}
averageAvatarFlight = Vec3.normalize(averageAvatarFlight);
// averageAvatarFlight = Vec3.normalize(averageAvatarFlight);
averageAvatarFlight = Vec3.multiply(averageAvatarFlight, 1 / avatarFlightVectors.length);
@ -276,7 +285,7 @@ function moveRats() {
}
};
avarageAvoiderFlight = Vec3.normalize(averageAvoiderFlight);
// avarageAvoiderFlight = Vec3.normalize(averageAvoiderFlight);
averageAvoiderFlight = Vec3.multiply(averageAvoiderFlight, 1 / avoidBlockVectors.length);
@ -288,19 +297,24 @@ function moveRats() {
divisorCount++;
averageVector = Vec3.sum(averageVector, averageAvatarFlight);
}
if (avoidBlockVectors > 0) {
if (avoidBlockVectors.length > 0) {
divisorCount++;
averageVector = Vec3.sum(averageVector, averageAvoiderFlight);
}
averageVector = Vec3.multiply(averageVector, 1 / divisorCount);
var thisRatProps = Entities.getEntityProperties(rat, ["position", "rotation"]);
var ratPosition = thisRatProps.position;
var ratToNest = Vec3.subtract(RAT_NEST_LOCATION, ratPosition);
var ratRotation = Quat.rotationBetween(Vec3.FRONT, ratToNest);
Entities.editEntity(rat, {
velocity: averageVector
velocity: averageVector,
rotation: ratRotation,
//rotation: Quat.fromPitchYawRollDegrees(0,0,0)
})
// castRay(rat);
})
}
@ -308,10 +322,14 @@ Script.update.connect(moveRats)
Script.update.connect(updateTweens);
function checkDistanceFromNest(rat) {
var ratProps = Entitis.getEntityProperties(rat, "position");
var ratProps = Entities.getEntityProperties(rat, "position");
var distance = Vec3.distance(ratProps.position, RAT_NEST_LOCATION);
if (distance < RAT_IN_NEST_DISTANCE) {
removeRatFromScene();
//print('at nest')
removeRatFromScene(rat);
} else {
// print('not yet at nest:::' + distance)
}
}
@ -323,8 +341,6 @@ function removeRatFromScene(rat) {
Entities.deleteEntity(rat);
}
function cleanup() {
while (rats.length > 0) {
Entities.deleteEntity(rats.pop());
@ -340,14 +356,16 @@ function cleanup() {
Script.clearInterval(ratSpawnerInterval);
}
Script.scriptEnding.connect(cleanup)
Script.scriptEnding.connect(cleanup);
var ratSpawnerInterval;
if (USE_CONSTANT_SPAWNER === true) {
ratSpawnerInterval = Script.setInterval(function() {
addRat();
}, RAT_SPAWN_RATE)
// playRatRunningAnimation(rat);
rats.push(rat);
}, RAT_SPAWN_RATE);
}
//unused for now, to be used for some collision avoidance on walls and stuff?

View file

@ -3,8 +3,8 @@ function seek(thisEntity, target) {
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 1;
var MAX_FORCE = 1;
var MAX_SPEED = 30;
var MAX_FORCE = 20;
var desired = Vec3.subtract(targetPosition, location);
desired = Vec3.normalize(desired);
@ -48,8 +48,8 @@ function fleeAvatar(thisEntity, avatarPosition) {
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 2;
var MAX_FORCE = 2;
var MAX_SPEED = 3;
var MAX_FORCE = 10;
var desired = Vec3.subtract(location, avatarPosition);
var d = Vec3.length(desired);
@ -69,7 +69,7 @@ function fleeAvatar(thisEntity, avatarPosition) {
}
function fleeAllAvatars(thisEntity) {
print('FLEE AVATARS');
//print('FLEE AVATARS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
@ -80,10 +80,10 @@ function fleeAllAvatars(thisEntity) {
var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Avatar-Detector') {
print('found an avatar to flee')
//print('found an avatar to flee')
var MAX_SPEED = 2;
var MAX_FORCE = 2;
var MAX_SPEED = 8;
var MAX_FORCE = 8;
var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired);
@ -95,7 +95,7 @@ function fleeAllAvatars(thisEntity) {
steer = steerVector.limit(MAX_FORCE)
flightVectors.push(steer)
} else {
print('target too far away from this avatar to flee' + d);
// print('target too far away from this avatar to flee' + d);
}
}
@ -105,33 +105,33 @@ function fleeAllAvatars(thisEntity) {
}
function fleeAvoiderBlocks(thisEntity) {
print('FLEE AVOIDER BLOCKS');
// print('FLEE AVOIDER BLOCKS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var nearbyEntities = Entities.findEntities(location, 3);
var nearbyEntities = Entities.findEntities(location, 4);
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');
// print('found an avoiderblock to flee');
var MAX_SPEED = 2;
var MAX_FORCE = 0.8;
var MAX_SPEED = 8;
var MAX_FORCE = 6;
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) {
if (d < 5) {
var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, desired.y, desired.z);
steer = steerVector.limit(MAX_FORCE)
flightVectors.push(steer)
flightVectors.push(steer);
} else {
print('target too far away from this avoider to flee' + d);
//print('target too far away from this avoider to flee' + d);
}
}
@ -146,8 +146,8 @@ function arrive(thisEntity, target) {
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position;
var velocity = properties.velocity;
var MAX_SPEED = 3;
var MAX_FORCE = 2;
var MAX_SPEED = 16;
var MAX_FORCE = 12;
var ARRIVAL_DISTANCE = 3;
var desired = Vec3.subtract(targetPosition, location);