// // glassShatterZone.js // An entity script on a zone that makes the sound of shattering glass upon entering // // 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 GLASS_SOUNDS = []; // sounds from http://www.grsites.com var GLASS_SOUND_URLS = [ "http://static1.grsites.com/archive/sounds/household/household038.wav", "http://static1.grsites.com/archive/sounds/household/household039.wav", "http://static1.grsites.com/archive/sounds/household/household040.wav", "http://static1.grsites.com/archive/sounds/household/household042.wav" ]; var zoneProperties; this.preload = function(entityID) { zoneProperties = Entities.getEntityProperties(entityID, ["position", "dimensions", "rotation"]); GLASS_SOUND_URLS.forEach(function(url) { GLASS_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 = GLASS_SOUNDS.length - 1; var index = Math.round(Math.random() * size); var sound = GLASS_SOUNDS[index]; Audio.playSound(sound, { volume: VOLUME, position: zoneProperties.position }); } this.enterEntity = function(entityID) { if (!isSomeAvatarOtherThanMeStillInsideTheObject(zoneProperties)) { playSound(); } }; });