139 lines
4.2 KiB
JavaScript
139 lines
4.2 KiB
JavaScript
//
|
|
// daegeum.js
|
|
//
|
|
// Adapted by Robin Wilson 06/28/2018
|
|
//
|
|
// Created from guitarMexico.js by Rebecca Stankus on 01/31/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 MUSIC_URLS = [
|
|
"Sounds/chung_sung_gok_1.wav",
|
|
"Sounds/chung_sung_gok_2.wav",
|
|
"Sounds/chung_sung_gok_3.wav",
|
|
"Sounds/chung_sung_gok_4.wav"
|
|
];
|
|
|
|
var sounds = [];
|
|
|
|
var AUDIO_VOLUME_LEVEL = 0.8;
|
|
var SONG_LENGTH_MS = 50000;
|
|
var UPDATE_POSITION_MS = 50;
|
|
var BOTH_HANDS = 2;
|
|
var ONE_HAND = 1;
|
|
var NO_HANDS = 0;
|
|
var soundIdx = 0;
|
|
|
|
var playing = false;
|
|
var injector;
|
|
var interval;
|
|
var numberHandsGrabbing = 1;
|
|
|
|
var Daegeum = function() {
|
|
_this = this;
|
|
};
|
|
|
|
Daegeum.prototype = {
|
|
preload: function(entityID){
|
|
_this.entityID = entityID;
|
|
_this.homePos = Entities.getEntityProperties(_this.entityID, "position").position;
|
|
|
|
MUSIC_URLS.forEach(function (soundURL, idx) {
|
|
sounds[idx] = SoundCache.getSound(Script.resolvePath(soundURL));
|
|
});
|
|
},
|
|
startNearGrab: function(thisEntity, otherEntity, collision) {
|
|
|
|
if (numberHandsGrabbing === NO_HANDS) {
|
|
numberHandsGrabbing = ONE_HAND;
|
|
this.playSound();
|
|
} else {
|
|
numberHandsGrabbing = BOTH_HANDS;
|
|
if (!playing) {
|
|
this.playSound();
|
|
}
|
|
}
|
|
},
|
|
releaseGrab: function() {
|
|
|
|
if (numberHandsGrabbing === BOTH_HANDS) {
|
|
numberHandsGrabbing = ONE_HAND;
|
|
} else {
|
|
if (injector) {
|
|
injector.stop();
|
|
}
|
|
if (interval) {
|
|
Script.clearInterval(interval);
|
|
}
|
|
playing = false;
|
|
// Entities.editEntity(_this.entityID, {lifetime: LIFETIME_ON_RELEASE});
|
|
numberHandsGrabbing = NO_HANDS;
|
|
}
|
|
},
|
|
playSound: function() {
|
|
var song = this.getSound();
|
|
print("PLAY SONG", soundIdx - 1);
|
|
|
|
if (song.downloaded && !playing) {
|
|
|
|
injector = Audio.playSound(song, {
|
|
position: _this.homePos,
|
|
volume: AUDIO_VOLUME_LEVEL
|
|
});
|
|
playing = true;
|
|
interval = Script.setInterval(function() {
|
|
var newAudioPosition = Entities.getEntityProperties(_this.entityID, 'position').position;
|
|
injector.options = { position: newAudioPosition };
|
|
}, UPDATE_POSITION_MS);
|
|
|
|
Script.setTimeout(function() {
|
|
playing = false;
|
|
if (interval) {
|
|
Script.clearInterval(interval);
|
|
}
|
|
}, SONG_LENGTH_MS);
|
|
}
|
|
},
|
|
getSound: function(){
|
|
|
|
if (soundIdx >= sounds.length){
|
|
soundIdx = 0;
|
|
}
|
|
|
|
var curSoundIdx = soundIdx;
|
|
|
|
if (!sounds[soundIdx].downloaded){
|
|
for (soundIdx = 0; soundIdx < sounds.length; soundIdx++){
|
|
if (sounds[soundIdx].downloaded){
|
|
curSoundIdx = soundIdx;
|
|
soundIdx = soundIdx + 1;
|
|
return sounds[curSoundIdx];
|
|
}
|
|
}
|
|
}
|
|
soundIdx = soundIdx + 1;
|
|
return sounds[curSoundIdx];
|
|
},
|
|
clickReleaseOnEntity: function(entityID, mouseEvent) {
|
|
|
|
if (mouseEvent.isLeftButton) {
|
|
if (!playing) {
|
|
this.playSound();
|
|
} else {
|
|
this.releaseGrab();
|
|
}
|
|
}
|
|
},
|
|
unload: function() {
|
|
this.releaseGrab();
|
|
}
|
|
};
|
|
|
|
return new Daegeum ();
|
|
});
|