content/hifi-content/zfox/triggerToTalk/triggerToTalk.js
2022-02-14 02:04:11 +01:00

100 lines
3.2 KiB
JavaScript

//
// triggerToTalk.js
//
// by Zach Fox on 06/22/18
// Copyright 2018 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
(function() {
var _this;
var TRIGGER_CONTROLS = [Controller.Standard.LT, Controller.Standard.RT];
var TRIGGER_THRESHOLD = 0.95;
var MIC_MUTED_SOUND = SoundCache.getSound(Script.resolvePath("micMuted.wav"));
var MIC_UNMUTED_SOUND = SoundCache.getSound(Script.resolvePath("micUnmuted.wav"));
var initialMutedStatus = false;
var equipped = false;
var currentHand = null;
var injector;
var TriggerToTalk = function() {
_this = this;
};
TriggerToTalk.prototype = {
// START PRELOAD AND UNLOAD FUNCTIONS
preload: function(entityID) {
_this.entityID = entityID;
},
unload: function() {
if (equipped) {
Audio.muted = initialMutedStatus;
}
},
// END PRELOAD AND UNLOAD FUNCTIONS
// START EQUIP-RELATED FUNCTIONS
startEquip: function(id, params) {
equipped = true;
currentHand = params[0] === "left" ? 0 : 1;
},
continueEquip: function(id, params) {
if (!equipped) {
var parentJointIndex = Entities.getEntityProperties(_this.entityID, 'parentJointIndex').parentJointIndex;
currentHand = (parentJointIndex === MyAvatar.getJointIndex("LeftHand")) ? 0 : 1;
initialMutedStatus = Audio.muted;
equipped = true;
} else {
var triggerValue = Controller.getValue(TRIGGER_CONTROLS[currentHand]);
if (triggerValue < TRIGGER_THRESHOLD) {
if (!Audio.muted) {
Entities.editEntity(_this.entityID, {
color: { red: 255, green: 0, blue: 0 }
});
Audio.muted = true;
_this.playSound(MIC_MUTED_SOUND);
}
} else {
if (Audio.muted) {
Entities.editEntity(_this.entityID, {
color: { red: 0, green: 255, blue: 0 }
});
Audio.muted = false;
_this.playSound(MIC_UNMUTED_SOUND);
}
}
}
},
releaseEquip: function(id, params) {
currentHand = null;
equipped = false;
Audio.muted = initialMutedStatus;
},
// END EQUIP-RELATED FUNCTIONS
// START UTILITY FUNCTIONS
playSound: function(sound) {
if (sound.downloaded) {
if (injector) {
injector.stop();
}
injector = Audio.playSound(sound, {
position: MyAvatar.position,
volume: 0.3,
localOnly: true
});
}
}
// END UTILITY FUNCTIONS
};
return new TriggerToTalk();
});