69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
//
|
|
// faucetServer.js
|
|
//
|
|
// created by Rebecca Stankus on 04/27/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 ESPRESSO_SOUND_URL = SoundCache.getSound(Script.resolvePath("sounds/espresso.mp3"));
|
|
var AUDIO_VOLUME_LEVEL = 0.1;
|
|
|
|
var sound;
|
|
var injector;
|
|
|
|
function Faucet() {
|
|
_this = this;
|
|
}
|
|
|
|
Faucet.prototype = {
|
|
on: null,
|
|
remotelyCallable: ['toggle'],
|
|
preload: function(entityID) {
|
|
_this.entityID = entityID;
|
|
sound = SoundCache.getSound(SOUND_URL);
|
|
_this.on = Entities.getEntityProperties(_this.entityID, 'visible').visible;
|
|
},
|
|
toggle: function(){
|
|
print("TOGGLE");
|
|
_this.on ? _this.turnOff() : _this.turnOn();
|
|
},
|
|
playSound: function(sound) {
|
|
if (sound.downloaded) {
|
|
if (injector) {
|
|
injector.stop();
|
|
}
|
|
injector = Audio.playSound(sound, {
|
|
position: Entities.getEntityProperties(_this.entityID, 'position').position,
|
|
volume: AUDIO_VOLUME_LEVEL
|
|
});
|
|
Script.setTimeout(function() {
|
|
_this.turnOff();
|
|
}, ESPRESSO_SOUND_URL.duration * 1000);
|
|
}
|
|
},
|
|
turnOn: function() {
|
|
Entities.editEntity(_this.entityID, {isEmitting: true});
|
|
_this.on = true;
|
|
_this.playSound(ESPRESSO_SOUND_URL);
|
|
},
|
|
turnOff: function() {
|
|
Entities.editEntity(_this.entityID, {isEmitting: false});
|
|
_this.on = false;
|
|
if (injector) {
|
|
injector.stop();
|
|
}
|
|
},
|
|
unload: function() {
|
|
if (injector) {
|
|
injector.stop();
|
|
}
|
|
}
|
|
};
|
|
|
|
return new Faucet();
|
|
});
|