content/hifi-content/elisalj/hub/swallowPill.js
2022-02-13 23:16:46 +01:00

115 lines
No EOL
3.6 KiB
JavaScript

//
// swallowPill.js
// Play a swallow sound and trigger visual effects after a pill has been swallowed
//
// 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 SWALLOW_SOUNDS_URLS = [
"http://www.pacdv.com/sounds/people_sound_effects/gulp-2.wav"
];
var VISUAL_EFFECTS = [
"Wireframe:LightingModel:enableWireframe",
1, // Depth
2, // Albedo
3, // Normal
5, // Metallic
6, // Emissive
16, // Shadow Cascade Indices
26, // Ambient Occlusion
27 // Ambient Occlusion Blurred
];
var VOLUME = 0.5;
var WITHIN_10_CM = 0.1;
var TIMEOUT = 10000;
var playback;
var render;
var renderDebug;
var isInactive = true;
var _this = this;
_this.preload = function(entityID) {
_this.entityID = entityID;
render = Render.getConfig("RenderMainView");
renderDebug = render.getConfig("DebugDeferredBuffer");
renderDebug.size = {x: -1, y: -1, z: 1, w: 1};
playback = {volume: VOLUME, position: Entities.getEntityProperties(_this.entityID).position};
};
var checkIfNearHead = function() {
var pos = Entities.getEntityProperties(_this.entityID).position;
var avatarHeadPosition = MyAvatar.getJointPosition("Head");
if (isInactive) {
if (isWithin10cm(pos.y, avatarHeadPosition.y) &
isWithin10cm(pos.z, avatarHeadPosition.z)) {
isInactive = false;
playSwallowEffect(avatarHeadPosition);
}
}
};
function enableVisualEffects() {
var size = VISUAL_EFFECTS.length - 1;
var index = Math.round(Math.random() * size);
var effect = VISUAL_EFFECTS[index];
print("effect is: " + effect);
if (index === 0) {
render.getConfig(effect.split(":")[1])[effect.split(":")[2]] = true;
} else {
renderDebug.enabled = true;
renderDebug.mode = effect;
}
}
function playSwallowEffect(position) {
var size = SWALLOW_SOUNDS_URLS.length - 1;
var index = Math.round(Math.random() * size);
var sound = SoundCache.getSound(SWALLOW_SOUNDS_URLS[index]);
Audio.playSound(sound, playback);
enableVisualEffects();
var editJSON = {
visible: false,
collisionless: true
};
Entities.editEntity(_this.entityID, editJSON);
Script.setTimeout(function() {
Entities.deleteEntity(_this.entityID);
}, TIMEOUT);
}
// Helper function to see if the object is close to us
var isWithin10cm = function(value1, value2) {
if (Math.abs(Math.abs(value1) - Math.abs(value2)) <= WITHIN_10_CM) {
return true;
}
return false;
};
Script.update.connect(checkIfNearHead);
_this.unload = function(entityID) {
var WIREFRAME = "Wireframe:LightingModel:enableWireframe";
render.getConfig(WIREFRAME.split(":")[1])[WIREFRAME.split(":")[2]] = false;
renderDebug.mode = 0;
renderDebug.size = {x: 0, y: -1, z: 1, w: 1};
renderDebug.enabled = false;
Script.update.disconnect(checkIfNearHead);
Script.update.disconnect(_this.mousePressOnEntity);
};
});