Merge pull request #6096 from imgntn/resetter_acs

[Scripts] - Move resetting basketballs and targets to an AC script
This commit is contained in:
Eric Levin 2015-10-16 12:18:15 -07:00
commit 9a87f5c574
3 changed files with 2004 additions and 2036 deletions

View file

@ -0,0 +1,217 @@
//
// originalPositionResetter.js
// toybox
//
// Created by James B. Pollack @imgntn 10/16/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
var TARGET_MODEL_URL = HIFI_PUBLIC_BUCKET + "models/ping_pong_gun/target.fbx";
var TARGET_COLLISION_HULL_URL = HIFI_PUBLIC_BUCKET + "models/ping_pong_gun/target_collision_hull.obj";
var TARGET_DIMENSIONS = {
x: 0.06,
y: 0.42,
z: 0.42
};
var TARGET_ROTATION = Quat.fromPitchYawRollDegrees(0, -55.25, 0);
var targetsScriptURL = Script.resolvePath('../ping_pong_gun/wallTarget.js');
var basketballURL = HIFI_PUBLIC_BUCKET + "models/content/basketball2.fbx";
var NUMBER_OF_BALLS = 4;
var BALL_DIAMETER = 0.30;
var RESET_DISTANCE = 1;
var MINIMUM_MOVE_LENGTH = 0.05;
var totalTime = 0;
var lastUpdate = 0;
var UPDATE_INTERVAL = 1 / 5; // 5fps
var Resetter = {
searchForEntitiesToResetToOriginalPosition: function(searchOrigin, objectName) {
var ids = Entities.findEntities(searchOrigin, 5);
var objects = [];
var i;
var entityID;
var name;
for (i = 0; i < ids.length; i++) {
entityID = ids[i];
name = Entities.getEntityProperties(entityID, "name").name;
if (name === objectName) {
//we found an object to reset
objects.push(entityID);
}
}
return objects;
},
deleteObjects: function(objects) {
while (objects.length > 0) {
Entities.deleteEntity(objects.pop());
}
},
createBasketBalls: function() {
var position = {
x: 542.86,
y: 494.84,
z: 475.06
};
var i;
var ballPosition;
var collidingBall;
for (i = 0; i < NUMBER_OF_BALLS; i++) {
ballPosition = {
x: position.x,
y: position.y + BALL_DIAMETER * 2,
z: position.z + (BALL_DIAMETER) - (BALL_DIAMETER * i)
};
collidingBall = Entities.addEntity({
type: "Model",
name: 'Hifi-Basketball',
shapeType: 'Sphere',
position: ballPosition,
dimensions: {
x: BALL_DIAMETER,
y: BALL_DIAMETER,
z: BALL_DIAMETER
},
restitution: 1.0,
linearDamping: 0.00001,
gravity: {
x: 0,
y: -9.8,
z: 0
},
collisionsWillMove: true,
ignoreForCollisions: false,
modelURL: basketballURL,
userData: JSON.stringify({
originalPositionKey: {
originalPosition: ballPosition
},
resetMe: {
resetMe: true
},
grabbable: {
invertSolidWhileHeld: true
}
})
});
}
},
testBallDistanceFromStart: function(balls) {
var resetCount = 0;
balls.forEach(function(ball, index) {
var properties = Entities.getEntityProperties(ball, ["position", "userData"]);
var currentPosition = properties.position;
var originalPosition = properties.userData.originalPositionKey.originalPosition;
var distance = Vec3.subtract(originalPosition, currentPosition);
var length = Vec3.length(distance);
if (length > RESET_DISTANCE) {
Script.setTimeout(function() {
var newPosition = Entities.getEntityProperties(ball, "position").position;
var moving = Vec3.length(Vec3.subtract(currentPosition, newPosition));
if (moving < MINIMUM_MOVE_LENGTH) {
if (resetCount === balls.length) {
this.deleteObjects(balls);
this.createBasketBalls();
}
}
}, 200);
}
});
},
testTargetDistanceFromStart: function(targets) {
targets.forEach(function(target, index) {
var properties = Entities.getEntityProperties(target, ["position", "userData"]);
var currentPosition = properties.position;
var originalPosition = properties.userData.originalPositionKey.originalPosition;
var distance = Vec3.subtract(originalPosition, currentPosition);
var length = Vec3.length(distance);
if (length > RESET_DISTANCE) {
Script.setTimeout(function() {
var newPosition = Entities.getEntityProperties(target, "position").position;
var moving = Vec3.length(Vec3.subtract(currentPosition, newPosition));
if (moving < MINIMUM_MOVE_LENGTH) {
Entities.deleteEntity(target);
var targetProperties = {
name: 'Hifi-Target',
type: 'Model',
modelURL: TARGET_MODEL_URL,
shapeType: 'compound',
collisionsWillMove: true,
dimensions: TARGET_DIMENSIONS,
compoundShapeURL: TARGET_COLLISION_HULL_URL,
position: originalPosition,
rotation: TARGET_ROTATION,
script: targetsScriptURL,
userData: JSON.stringify({
resetMe: {
resetMe: true
},
grabbableKey: {
grabbable: false
},
originalPositionKey: originalPosition
})
};
Entities.addEntity(targetProperties);
}
}, 200);
}
});
}
};
function update(deltaTime) {
if (!Entities.serversExist() || !Entities.canRez()) {
return;
}
totalTime += deltaTime;
// We don't want to edit the entity EVERY update cycle, because that's just a lot
// of wasted bandwidth and extra effort on the server for very little visual gain
if (totalTime - lastUpdate > UPDATE_INTERVAL) {
//do stuff
var balls = Resetter.searchForEntitiesToResetToOriginalPosition({
x: 542.86,
y: 494.84,
z: 475.06
}, "Hifi-Basketball");
var targets = Resetter.searchForEntitiesToResetToOriginalPosition({
x: 548.68,
y: 497.30,
z: 509.74
}, "Hifi-Target");
if (balls.length !== 0) {
Resetter.testBallDistanceFromStart(balls);
}
if (targets.length !== 0) {
Resetter.testTargetDistanceFromStart(targets);
}
lastUpdate = totalTime;
}
}
Script.update.connect(update);

View file

@ -274,12 +274,10 @@
grabbableKey: {
grabbable: false
}
})
});
var collidingBalls = [];
var originalBallPositions = [];
function createCollidingBalls() {
var position = rackStartPosition;
@ -291,16 +289,16 @@
y: position.y + DIAMETER * 2,
z: position.z + (DIAMETER) - (DIAMETER * i)
};
var collidingBall = Entities.addEntity({
type: "Model",
name: 'Colliding Basketball',
shapeType: 'Sphere',
position: {
var newPosition = {
x: position.x + (DIAMETER * 2) - (DIAMETER * i),
y: position.y + DIAMETER * 2,
z: position.z
},
};
var collidingBall = Entities.addEntity({
type: "Model",
name: 'Hifi-Basketball',
shapeType: 'Sphere',
position: newPosition,
dimensions: {
x: DIAMETER,
y: DIAMETER,
@ -317,63 +315,25 @@
ignoreForCollisions: false,
modelURL: basketballURL,
userData: JSON.stringify({
originalPositionKey: {
originalPosition: newPosition
},
resetMe: {
resetMe: true
},
grabbableKey: {
grabbable: {
invertSolidWhileHeld: true
}
})
});
collidingBalls.push(collidingBall);
originalBallPositions.push(position);
}
}
function testBallDistanceFromStart() {
var resetCount = 0;
collidingBalls.forEach(function(ball, index) {
var currentPosition = Entities.getEntityProperties(ball, "position").position;
var originalPosition = originalBallPositions[index];
var distance = Vec3.subtract(originalPosition, currentPosition);
var length = Vec3.length(distance);
if (length > RESET_DISTANCE) {
Script.setTimeout(function() {
var newPosition = Entities.getEntityProperties(ball, "position").position;
var moving = Vec3.length(Vec3.subtract(currentPosition, newPosition));
if (moving < MINIMUM_MOVE_LENGTH) {
resetCount++;
if (resetCount === NUMBER_OF_BALLS) {
deleteCollidingBalls();
createCollidingBalls();
}
}
}, 200);
}
});
}
function deleteEntity(entityID) {
if (entityID === rack) {
deleteCollidingBalls();
Script.clearInterval(distanceCheckInterval);
Entities.deletingEntity.disconnect(deleteEntity);
}
}
function deleteCollidingBalls() {
while (collidingBalls.length > 0) {
Entities.deleteEntity(collidingBalls.pop());
}
}
createCollidingBalls();
Entities.deletingEntity.connect(deleteEntity);
var distanceCheckInterval = Script.setInterval(testBallDistanceFromStart, 1000);
}
function createTargets() {
@ -396,6 +356,7 @@
var VERTICAL_SPACING = TARGET_DIMENSIONS.y + 0.5;
var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.5;
var startPosition = {
x: 548.68,
y: 497.30,
@ -404,31 +365,8 @@
var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0);
var targetIntervalClearer = Entities.addEntity({
name: 'Target Interval Clearer - delete me to clear',
type: 'Box',
position: startPosition,
dimensions: TARGET_DIMENSIONS,
rotation: rotation,
visible: false,
collisionsWillMove: false,
ignoreForCollisions: true,
userData: JSON.stringify({
resetMe: {
resetMe: true
},
grabbableKey: {
grabbable: false
}
})
});
var targets = [];
var originalPositions = [];
var lastPositions = [];
function addTargets() {
var i;
var row = -1;
@ -444,11 +382,8 @@
var position = Vec3.sum(startPosition, multiplier);
position.y = startPosition.y - (row * VERTICAL_SPACING);
originalPositions.push(position);
lastPositions.push(position);
var targetProperties = {
name: 'Target',
name: 'Hifi-Target',
type: 'Model',
modelURL: MODEL_URL,
shapeType: 'compound',
@ -459,6 +394,9 @@
rotation: rotation,
script: targetsScriptURL,
userData: JSON.stringify({
originalPositionKey: {
originalPosition: position
},
resetMe: {
resetMe: true
},
@ -467,73 +405,13 @@
}
})
};
var target = Entities.addEntity(targetProperties);
targets.push(target);
}
}
function testTargetDistanceFromStart() {
targets.forEach(function(target, index) {
var currentPosition = Entities.getEntityProperties(target, "position").position;
var originalPosition = originalPositions[index];
var distance = Vec3.subtract(originalPosition, currentPosition);
var length = Vec3.length(distance);
var moving = Vec3.length(Vec3.subtract(currentPosition, lastPositions[index]));
lastPositions[index] = currentPosition;
if (length > RESET_DISTANCE && moving < MINIMUM_MOVE_LENGTH) {
Entities.deleteEntity(target);
var targetProperties = {
name: 'Target',
type: 'Model',
modelURL: MODEL_URL,
shapeType: 'compound',
collisionsWillMove: true,
dimensions: TARGET_DIMENSIONS,
compoundShapeURL: COLLISION_HULL_URL,
position: originalPositions[index],
rotation: rotation,
script: targetsScriptURL,
userData: JSON.stringify({
resetMe: {
resetMe: true
},
grabbableKey: {
grabbable: false
}
})
};
targets[index] = Entities.addEntity(targetProperties);
}
});
}
function deleteEntity(entityID) {
if (entityID === targetIntervalClearer) {
deleteTargets();
Script.clearInterval(distanceCheckInterval);
Entities.deletingEntity.disconnect(deleteEntity);
}
}
function deleteTargets() {
while (targets.length > 0) {
Entities.deleteEntity(targets.pop());
}
Entities.deleteEntity(targetIntervalClearer);
}
Entities.deletingEntity.connect(deleteEntity);
var distanceCheckInterval = Script.setInterval(testTargetDistanceFromStart, 1000);
addTargets();
}
@ -543,7 +421,7 @@
var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/Dark_Cat.fbx";
var animationURL = "http://hifi-public.s3.amazonaws.com/ryan/sleeping.fbx";
var animationSettings = JSON.stringify({
running: true,
running: true
});
var cat = Entities.addEntity({
type: "Model",
@ -876,7 +754,7 @@
collisionsWillMove: true,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
@ -927,7 +805,7 @@
mass: 10,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -968,7 +846,7 @@
collisionsWillMove: true,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
@ -1007,7 +885,7 @@
compoundShapeURL: hoopCollisionHullURL,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -1045,7 +923,7 @@
script: wandScriptURL,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
@ -1087,7 +965,7 @@
collisionSoundURL: "http://s3.amazonaws.com/hifi-public/sounds/basketball/basketball.wav",
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
@ -1127,7 +1005,7 @@
collisionsWillMove: true,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
@ -1166,7 +1044,7 @@
},
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
@ -1204,7 +1082,7 @@
linearDamping: 0.4,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -1247,7 +1125,7 @@
linearDamping: 0.2,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -1298,7 +1176,7 @@
y: 0.05,
z: 0.25
}
}, ];
} ];
var modelURL, entity;
for (i = 0; i < blockTypes.length; i++) {
@ -1329,7 +1207,7 @@
},
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
}
})
});

View file

@ -251,7 +251,6 @@ MasterReset = function() {
});
var collidingBalls = [];
var originalBallPositions = [];
function createCollidingBalls() {
var position = rackStartPosition;
@ -263,16 +262,16 @@ MasterReset = function() {
y: position.y + DIAMETER * 2,
z: position.z + (DIAMETER) - (DIAMETER * i)
};
var collidingBall = Entities.addEntity({
type: "Model",
name: 'Colliding Basketball',
shapeType: 'Sphere',
position: {
var newPosition = {
x: position.x + (DIAMETER * 2) - (DIAMETER * i),
y: position.y + DIAMETER * 2,
z: position.z
},
};
var collidingBall = Entities.addEntity({
type: "Model",
name: 'Hifi-Basketball',
shapeType: 'Sphere',
position: newPosition,
dimensions: {
x: DIAMETER,
y: DIAMETER,
@ -289,6 +288,9 @@ MasterReset = function() {
ignoreForCollisions: false,
modelURL: basketballURL,
userData: JSON.stringify({
originalPositionKey: {
originalPosition: newPosition
},
resetMe: {
resetMe: true
},
@ -299,53 +301,12 @@ MasterReset = function() {
});
collidingBalls.push(collidingBall);
originalBallPositions.push(position);
}
}
function testBallDistanceFromStart() {
var resetCount = 0;
collidingBalls.forEach(function(ball, index) {
var currentPosition = Entities.getEntityProperties(ball, "position").position;
var originalPosition = originalBallPositions[index];
var distance = Vec3.subtract(originalPosition, currentPosition);
var length = Vec3.length(distance);
if (length > RESET_DISTANCE) {
Script.setTimeout(function() {
var newPosition = Entities.getEntityProperties(ball, "position").position;
var moving = Vec3.length(Vec3.subtract(currentPosition, newPosition));
if (moving < MINIMUM_MOVE_LENGTH) {
resetCount++;
if (resetCount === NUMBER_OF_BALLS) {
deleteCollidingBalls();
createCollidingBalls();
}
}
}, 200);
}
});
}
function deleteEntity(entityID) {
if (entityID === rack) {
deleteCollidingBalls();
Script.clearInterval(distanceCheckInterval);
Entities.deletingEntity.disconnect(deleteEntity);
}
}
function deleteCollidingBalls() {
while (collidingBalls.length > 0) {
Entities.deleteEntity(collidingBalls.pop());
}
}
createCollidingBalls();
Entities.deletingEntity.connect(deleteEntity);
var distanceCheckInterval = Script.setInterval(testBallDistanceFromStart, 1000);
}
function createTargets() {
@ -377,27 +338,8 @@ MasterReset = function() {
var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0);
var targetIntervalClearer = Entities.addEntity({
name: 'Target Interval Clearer - delete me to clear',
type: 'Box',
position: startPosition,
dimensions: TARGET_DIMENSIONS,
rotation: rotation,
visible: false,
collisionsWillMove: false,
ignoreForCollisions: true,
userData: JSON.stringify({
resetMe: {
resetMe: true
}
})
});
var targets = [];
var originalPositions = [];
var lastPositions = [];
function addTargets() {
var i;
var row = -1;
@ -413,11 +355,8 @@ MasterReset = function() {
var position = Vec3.sum(startPosition, multiplier);
position.y = startPosition.y - (row * VERTICAL_SPACING);
originalPositions.push(position);
lastPositions.push(position);
var targetProperties = {
name: 'Target',
name: 'Hifi-Target',
type: 'Model',
modelURL: MODEL_URL,
shapeType: 'compound',
@ -428,6 +367,9 @@ MasterReset = function() {
rotation: rotation,
script: targetsScriptURL,
userData: JSON.stringify({
originalPositionKey: {
originalPosition: position
},
resetMe: {
resetMe: true
},
@ -443,69 +385,6 @@ MasterReset = function() {
}
}
function testTargetDistanceFromStart() {
targets.forEach(function(target, index) {
var currentPosition = Entities.getEntityProperties(target, "position").position;
var originalPosition = originalPositions[index];
var distance = Vec3.subtract(originalPosition, currentPosition);
var length = Vec3.length(distance);
var moving = Vec3.length(Vec3.subtract(currentPosition, lastPositions[index]));
lastPositions[index] = currentPosition;
if (length > RESET_DISTANCE && moving < MINIMUM_MOVE_LENGTH) {
Entities.deleteEntity(target);
var targetProperties = {
name: 'Target',
type: 'Model',
modelURL: MODEL_URL,
shapeType: 'compound',
collisionsWillMove: true,
dimensions: TARGET_DIMENSIONS,
compoundShapeURL: COLLISION_HULL_URL,
position: originalPositions[index],
rotation: rotation,
script: targetsScriptURL,
userData: JSON.stringify({
resetMe: {
resetMe: true
},
grabbableKey: {
grabbable: false
}
}
})
};
targets[index] = Entities.addEntity(targetProperties);
}
});
}
function deleteEntity(entityID) {
if (entityID === targetIntervalClearer) {
deleteTargets();
Script.clearInterval(distanceCheckInterval);
Entities.deletingEntity.disconnect(deleteEntity);
}
}
function deleteTargets() {
while (targets.length > 0) {
Entities.deleteEntity(targets.pop());
}
Entities.deleteEntity(targetIntervalClearer);
}
Entities.deletingEntity.connect(deleteEntity);
var distanceCheckInterval = Script.setInterval(testTargetDistanceFromStart, 1000);
addTargets();
}
@ -515,7 +394,7 @@ function createCat(position) {
var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/Dark_Cat.fbx";
var animationURL = "http://hifi-public.s3.amazonaws.com/ryan/sleeping.fbx";
var animationSettings = JSON.stringify({
running: true,
running: true
});
var cat = Entities.addEntity({
type: "Model",
@ -543,7 +422,6 @@ function createCat(position) {
grabbableKey: {
grabbable: false
}
}
})
});
@ -582,7 +460,6 @@ function createFlashlight(position) {
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
});
@ -849,12 +726,12 @@ function createDice() {
collisionsWillMove: true,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
};
var dice1 = Entities.addEntity(diceProps);
@ -901,7 +778,7 @@ function createGates() {
mass: 10,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -942,12 +819,12 @@ function createPingPongBallGun() {
collisionsWillMove: true,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
});
}
@ -982,7 +859,7 @@ function createBasketballHoop() {
compoundShapeURL: hoopCollisionHullURL,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -1020,12 +897,11 @@ function createWand(position) {
script: wandScriptURL,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
});
@ -1063,12 +939,11 @@ function createBasketBall(position) {
collisionSoundURL: "http://s3.amazonaws.com/hifi-public/sounds/basketball/basketball.wav",
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
});
@ -1104,12 +979,11 @@ function createDoll(position) {
collisionsWillMove: true,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
});
@ -1144,12 +1018,11 @@ function createSprayCan(position) {
},
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
invertSolidWhileHeld: true
}
}
})
});
@ -1183,7 +1056,7 @@ function createPottedPlant(position) {
linearDamping: 0.4,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -1226,7 +1099,7 @@ function createCombinedArmChair(position) {
linearDamping: 0.2,
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
},
grabbableKey: {
grabbable: false
@ -1277,7 +1150,7 @@ function createBlocks(position) {
y: 0.05,
z: 0.25
}
}, ];
}];
var modelURL, entity;
for (i = 0; i < blockTypes.length; i++) {
@ -1308,7 +1181,7 @@ function createBlocks(position) {
},
userData: JSON.stringify({
resetMe: {
resetMe: true,
resetMe: true
}
})
});