// // soundZone.js // An entity script on a zone that pulls sound URLS from the zone's user data // // Author: Elisa Lupin-Jimenez // Copyright High Fidelity 2018 // // Licensed under the Apache 2.0 License // See accompanying license file or http://apache.org/ // // All assets are under CC Attribution Non-Commerical // http://creativecommons.org/licenses/ // (function() { var HALF_MULTIPLIER = 0.5; var VOLUME = 0.5; var SOUNDS = []; // sounds from http://www.grsites.com var SOUND_URLS; var zoneProperties; this.preload = function(entityID) { zoneProperties = Entities.getEntityProperties(entityID, ["position", "dimensions", "rotation", "userData"]); SOUND_URLS = JSON.parse(zoneProperties.userData).soundURLs; SOUND_URLS.forEach(function(url) { SOUNDS.push(SoundCache.getSound(Script.resolvePath(url))); }); }; function isPositionInsideBox(position, boxProperties) { var localPosition = Vec3.multiplyQbyV(Quat.inverse(boxProperties.rotation), Vec3.subtract(position, boxProperties.position)); var halfDimensions = Vec3.multiply(boxProperties.dimensions, HALF_MULTIPLIER); return -halfDimensions.x <= localPosition.x && halfDimensions.x >= localPosition.x && -halfDimensions.y <= localPosition.y && halfDimensions.y >= localPosition.y && -halfDimensions.z <= localPosition.z && halfDimensions.z >= localPosition.z; } function isSomeAvatarOtherThanMeStillInsideTheObject(objectProperties) { var result = false; AvatarList.getAvatarIdentifiers().forEach(function(avatarID) { var avatar = AvatarList.getAvatar(avatarID); if (avatar.sessionUUID !== MyAvatar.sessionUUID) { if (isPositionInsideBox(avatar.position, objectProperties)) { result = true; } } }); return result; } function playSound() { var size = SOUNDS.length - 1; var index = Math.round(Math.random() * size); var sound = SOUNDS[index]; Audio.playSound(sound, { volume: VOLUME, position: zoneProperties.position }); } this.enterEntity = function(entityID) { print("sound zone entered"); if (!isSomeAvatarOtherThanMeStillInsideTheObject(zoneProperties)) { playSound(); } }; });