144 lines
6.6 KiB
JavaScript
144 lines
6.6 KiB
JavaScript
//
|
|
// Treasure Detector
|
|
//
|
|
// Created by Caitlyn Meeks on 09/21/2016
|
|
// 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
|
|
//
|
|
// A treasure detector. Attach this script to an entity parented to a joint on the base of a metal detector model. It will
|
|
// vary the sound and speed of a sound emitter based on proximity to any entity with isTreasure=True in its user data field.
|
|
|
|
// get the entity id in the preload
|
|
|
|
|
|
(function () {
|
|
var entityID = null;
|
|
var DETECTOR_POWERUP_SOUND = SoundCache.getSound("http://www.norteclabs.com/HF/resources/RolePull.wav");
|
|
var DETECTOR_DETECTING_SOUND = SoundCache.getSound("http://hifi-content.s3.amazonaws.com/caitlyn/production/treasureDetector/detectorPeep.wav");
|
|
var FOUND_SOUND = SoundCache.getSound("http://hifi-content.s3.amazonaws.com/caitlyn/production/treasureDetector/detectorFindsSomethin2.wav");
|
|
var PING_SPEED_DEFAULT = 1000;
|
|
var soundInjector;
|
|
var foundSound_Injector;
|
|
var scanSoundVolume = 0.1;
|
|
var SEARCH_DISTANCE = 100.0;
|
|
var closestTreasureDistance = 0;
|
|
var pingSpeed = PING_SPEED_DEFAULT; // ping every 500 ms
|
|
var pingVol;
|
|
var treasureList = []; // A list of position coordinates for each treasure
|
|
var treasureItemDistances = []; // A list of distances from treasureList constantly updated as you sweep the detector base
|
|
var jointIndex;
|
|
var entProperties;
|
|
var jointLocInObjectFrame;
|
|
var detectorJointPos;
|
|
|
|
|
|
this.preload = function(pEntityID) {
|
|
entityID = pEntityID;
|
|
print("CAITLYN preload("+entityID+")");
|
|
entProperties = Entities.getEntityProperties(entityID);
|
|
jointIndex = Entities.getJointIndex(entityID, "target_jnt");
|
|
print("JOINT INDEX IS "+jointIndex);
|
|
jointLocInObjectFrame = Entities.getAbsoluteJointTranslationInObjectFrame(entityID, jointIndex);
|
|
detectorJointPos = Vec3.sum(entProperties.position, Vec3.multiplyQbyV(entProperties.rotation, jointLocInObjectFrame));
|
|
|
|
// FindAllTreasure
|
|
// Creates and updates treasureList, which is a list of treasure items so designated by their userdata isTreasure value
|
|
var findAllTreasure = Script.setInterval(function() {
|
|
print("There are "+treasureList.length+" treasures located within "+SEARCH_DISTANCE);
|
|
treasureList = [];
|
|
var foundEntitiesArray = Entities.findEntities(detectorJointPos, SEARCH_DISTANCE);
|
|
for (var i = 0; i < foundEntitiesArray.length; i++) {
|
|
var properties = Entities.getEntityProperties(foundEntitiesArray[i], ["EntityItemID", "userData", "position"]);
|
|
if (!properties.userData) {
|
|
continue;
|
|
} try {
|
|
var treasureData = JSON.parse(properties.userData);
|
|
var treasurePos = properties.position;
|
|
if (treasureData.isTreasure == true) {
|
|
treasureList.push(treasurePos);
|
|
}
|
|
} catch (e){}
|
|
}
|
|
}, 5000);
|
|
|
|
/* rather than get the closest treasure distance, you need an array for multiple treasures in proximity. You need the distance between all treasures and the intensity should be the average of them all.
|
|
Add a list called treasureItemDistances and calculate that distance for each one.
|
|
|
|
|
|
*/
|
|
|
|
// TreasureScan
|
|
// Goes through each item in treasureList and measures the distance to it.
|
|
var treasureScan = Script.setInterval(function(){
|
|
if (!treasureList) return; //targetSensorPos = Vec3.sum(Vec3.sum(detectorJointPos, {x: 0, y: 0.5, z: 0}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
|
|
var averageDistance = 0; // To get the average distance of all items nearby.
|
|
entProperties = Entities.getEntityProperties(entityID);
|
|
jointIndex = Entities.getJointIndex(entityID, "target_jnt");//print("JOINT INDEX IS "+jointIndex);
|
|
jointLocInObjectFrame = Entities.getAbsoluteJointTranslationInObjectFrame(entityID, jointIndex);
|
|
detectorJointPos = Vec3.sum(entProperties.position, Vec3.multiplyQbyV(entProperties.rotation, jointLocInObjectFrame));
|
|
for (var i = 0; i < treasureList.length; i++) {
|
|
var b = treasureList[i];
|
|
var distance = Vec3.distance(detectorJointPos, b);
|
|
distance = Math.min(distance, 100);
|
|
treasureItemDistances[i] = distance;
|
|
averageDistance += distance;
|
|
if (distance > closestTreasureDistance) {
|
|
closestTreasureDistance = distance;
|
|
}
|
|
}
|
|
averageDistance /= treasureList.length;
|
|
var dn = 1 - averageDistance / 100;
|
|
pingVol = Math.pow(dn, 27);
|
|
pingVol = Math.min(Math.max(0, pingVol), 0.75)
|
|
pingSpeed = 1000 * (1 - Math.pow(dn, 255)); //print ("Detector distance is "+dn+" and volume should be "+pingVol);
|
|
}, 30);
|
|
|
|
Script.setTimeout(playPingSound, pingSpeed);
|
|
};
|
|
|
|
this.unload = function() {
|
|
stopFoundSound();
|
|
};
|
|
|
|
var playPingSound = function() {
|
|
entProperties = Entities.getEntityProperties(entityID);
|
|
jointIndex = Entities.getJointIndex(entityID, "target_jnt");
|
|
jointLocInObjectFrame = Entities.getAbsoluteJointTranslationInObjectFrame(entityID, jointIndex);
|
|
detectorJointPos = Vec3.sum(entProperties.position, Vec3.multiplyQbyV(entProperties.rotation, jointLocInObjectFrame));
|
|
Audio.playSound(DETECTOR_DETECTING_SOUND, {
|
|
position: detectorJointPos,
|
|
volume: pingVol,
|
|
localOnly: true
|
|
});
|
|
Script.setTimeout(playPingSound, pingSpeed);
|
|
if (pingSpeed < 120) {
|
|
playFoundSound(detectorJointPos, pingVol);
|
|
}
|
|
else {
|
|
stopFoundSound();
|
|
}
|
|
}
|
|
|
|
function playFoundSound(sndPos, sndVol) {
|
|
if (foundSound_Injector !== undefined && foundSound_Injector.isPlaying) {
|
|
foundSound_Injector.position = sndPos;
|
|
return;
|
|
}
|
|
foundSound_Injector = Audio.playSound(FOUND_SOUND, {
|
|
position: sndPos,
|
|
volume: 1.0,
|
|
loop: true,
|
|
localOnly: true
|
|
});
|
|
}
|
|
|
|
function stopFoundSound() {
|
|
if (foundSound_Injector !== undefined && foundSound_Injector.isPlaying) {
|
|
foundSound_Injector.stop();
|
|
foundSound_Injector = undefined;
|
|
}
|
|
}
|
|
});
|