135 lines
5.9 KiB
JavaScript
135 lines
5.9 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 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));
|
|
|
|
var multiple_timer = Script.setInterval(function() {
|
|
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){}
|
|
}
|
|
}, 100);
|
|
|
|
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())));
|
|
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);
|
|
if (distance > 100) {
|
|
distance = 100;
|
|
}
|
|
if (distance > closestTreasureDistance) {
|
|
closestTreasureDistance = distance;
|
|
}
|
|
var dn = 1 - distance / 100;
|
|
pingVol = Math.pow(dn, 27);
|
|
pingVol = Math.min(Math.max(0, pingVol), 0.75)
|
|
pingSpeed = 1000 * (1 - Math.pow(dn, 25)); //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);
|
|
//print("PS: "+pingSpeed);
|
|
if (pingSpeed < 35) {
|
|
playFoundSound(detectorJointPos, pingVol);
|
|
}
|
|
else {
|
|
stopFoundSound();
|
|
}
|
|
}
|
|
|
|
function playFoundSound(sndPos, sndVol) {
|
|
print("CAITLYN PLAY FOUND SOUND 1");
|
|
//if (!foundSound_Injector) return;
|
|
print("CAITLYN PLAY FOUND SOUND 2");
|
|
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;
|
|
}
|
|
}
|
|
});
|