mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-09 21:32:31 +02:00
Addressed PR feedback - now dynamically reads userdata
This commit is contained in:
parent
70942925bc
commit
e2dfbf1b02
1 changed files with 49 additions and 29 deletions
|
@ -3,13 +3,12 @@
|
|||
// This entity script will allow you to create an ambient sound that loops when a person is within a given
|
||||
// range of this entity. Great way to add one or more ambisonic soundfields to your environment.
|
||||
//
|
||||
// In the userData section for the entity, add two values:
|
||||
// In the userData section for the entity, add/edit three values:
|
||||
// userData.SoundURL should be a string giving the URL to the sound file. Defaults to 100 meters if not set.
|
||||
// userData.range should be an integer for the max distance away from the entity where the sound will be audible.
|
||||
// userData.volume is the max volume at which the clip should play.
|
||||
// userData.volume is the max volume at which the clip should play. Defaults to 1.0 full volume)
|
||||
//
|
||||
// Note: When you update the above values, you need to reload the entity script for it to see the changes. Also,
|
||||
// remember that the entity has to be visible to the user for the sound to play at all, so make sure the entity is
|
||||
// Remember that the entity has to be visible to the user for the sound to play at all, so make sure the entity is
|
||||
// large enough to be loaded at the range you set, particularly for large ranges.
|
||||
//
|
||||
// Copyright 2016 High Fidelity, Inc.
|
||||
|
@ -24,7 +23,7 @@
|
|||
var DEFAULT_URL = "http://hifi-content.s3.amazonaws.com/ken/samples/forest_ambiX.wav";
|
||||
var DEFAULT_VOLUME = 1.0;
|
||||
|
||||
var soundURL = DEFAULT_URL;
|
||||
var soundURL = "";
|
||||
var range = DEFAULT_RANGE;
|
||||
var maxVolume = DEFAULT_VOLUME;
|
||||
var UPDATE_INTERVAL_MSECS = 100;
|
||||
|
@ -33,40 +32,61 @@
|
|||
var ambientSound;
|
||||
var center;
|
||||
var soundPlaying = false;
|
||||
var updateTimer = false;
|
||||
|
||||
var WANT_DEBUG = false;
|
||||
var checkTimer = false;
|
||||
var _this;
|
||||
|
||||
var WANT_DEBUG = true;
|
||||
function debugPrint(string) {
|
||||
if (WANT_DEBUG) {
|
||||
print(string);
|
||||
}
|
||||
}
|
||||
|
||||
this.updateSettings = function() {
|
||||
// Check user data on the entity for any changes
|
||||
var oldSoundURL = soundURL;
|
||||
var props = Entities.getEntityProperties(entity, [ "userData" ]);
|
||||
if (props.userData) {
|
||||
var data = JSON.parse(props.userData);
|
||||
if (data.soundURL && !(soundURL === data.soundURL)) {
|
||||
soundURL = data.soundURL;
|
||||
debugPrint("Read ambient sound URL: " + soundURL);
|
||||
} else if (!data.soundURL) {
|
||||
soundURL = DEFAULT_URL;
|
||||
}
|
||||
if (data.range && !(range === data.range)) {
|
||||
range = data.range;
|
||||
debugPrint("Read ambient sound range: " + range);
|
||||
}
|
||||
if (data.volume && !(maxVolume === data.volume)) {
|
||||
maxVolume = data.volume;
|
||||
debugPrint("Read ambient sound volume: " + maxVolume);
|
||||
}
|
||||
}
|
||||
if (!(soundURL === oldSoundURL) || (soundURL === "")) {
|
||||
debugPrint("Loading ambient sound into cache");
|
||||
ambientSound = SoundCache.getSound(soundURL);
|
||||
if (soundPlaying && soundPlaying.playing) {
|
||||
soundPlaying.stop();
|
||||
soundPlaying = false;
|
||||
Entities.editEntity(entity, { color: { red: 128, green: 128, blue: 128 }});
|
||||
debugPrint("Restarting ambient sound");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.preload = function(entityID) {
|
||||
// Load the sound and range from the entity userData fields, and note the position of the entity.
|
||||
debugPrint("Ambient sound preload");
|
||||
entity = entityID;
|
||||
var props = Entities.getEntityProperties(entityID, [ "userData" ]);
|
||||
var data = JSON.parse(props.userData);
|
||||
if (data.soundURL) {
|
||||
soundURL = data.soundURL;
|
||||
debugPrint("Read ambient sound URL: " + soundURL);
|
||||
}
|
||||
ambientSound = SoundCache.getSound(soundURL);
|
||||
if (data.range) {
|
||||
range = data.range;
|
||||
debugPrint("Read ambient sound range: " + range);
|
||||
}
|
||||
if (data.volume) {
|
||||
maxVolume = data.volume;
|
||||
debugPrint("Read ambient sound volume: " + maxVolume);
|
||||
}
|
||||
updateTimer = Script.setInterval(this.update, UPDATE_INTERVAL_MSECS);
|
||||
_this = this;
|
||||
checkTimer = Script.setInterval(this.maybeUpdate, UPDATE_INTERVAL_MSECS);
|
||||
};
|
||||
|
||||
this.update = function() {
|
||||
this.maybeUpdate = function() {
|
||||
// Every UPDATE_INTERVAL_MSECS, update the volume of the ambient sound based on distance from my avatar
|
||||
_this.updateSettings();
|
||||
var props = Entities.getEntityProperties(entity);
|
||||
var HYSTERESIS_FRACTION = 0.1;
|
||||
var props = Entities.getEntityProperties(entity, [ "position" ]);
|
||||
center = props.position;
|
||||
|
@ -74,7 +94,7 @@
|
|||
if (distance <= range) {
|
||||
var volume = (1.0 - distance / range) * maxVolume;
|
||||
if (!soundPlaying && ambientSound.downloaded) {
|
||||
soundPlaying = Audio.playSound(ambientSound, { loop: true, volume: volume });
|
||||
soundPlaying = Audio.playSound(ambientSound, { loop: true, localOnly: true, volume: volume });
|
||||
debugPrint("Starting ambient sound, volume: " + volume);
|
||||
Entities.editEntity(entity, { color: { red: 255, green: 0, blue: 0 }});
|
||||
} else if (soundPlaying && soundPlaying.playing) {
|
||||
|
@ -90,12 +110,12 @@
|
|||
|
||||
this.unload = function(entityID) {
|
||||
debugPrint("Ambient sound unload");
|
||||
if (updateTimer) {
|
||||
Script.clearInterval(updateTimer);
|
||||
if (checkTimer) {
|
||||
Script.clearInterval(checkTimer);
|
||||
}
|
||||
if (soundPlaying && soundPlaying.playing) {
|
||||
soundPlaying.stop();
|
||||
}
|
||||
};
|
||||
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue