content/hifi-content/DomainContent/Home/cuckooClock/Script/original script with sound URLs and other stuff.js
2022-02-13 22:49:05 +01:00

171 lines
No EOL
5.7 KiB
JavaScript

(function() {
Script.include("http://hifi-public.s3.amazonaws.com/scripts/libraries/utils.js");//../../libraries/utils.js");
// Constants
const CLOCKFACE_UID = "50a4580f-3bf8-4bf5-8af8-0bc6865f7608"; //; Set to match the UID of the clock face please
const CLOCKBODY_UID = "50a4580f-3bf8-4bf5-8af8-0bc6865f7608"; //; Set to match the UID of the clock please
const SND_TICKING_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/Sounds/cuckoo_clock/cuckoo_tick.L.wav";
const SND_CUCKOO_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/Sounds/cuckoo_clock/cuckoo%20mix.wav"; //://hifi-content.s3.amazonaws.com/DomainContent/Home/Sounds/cuckoo_clock/cuckoo_strike1.L.wav";
const ANIM_CUCKOO_URL = "http://hifi-content.s3.amazonaws.com/caitlyn/dev/cuckoo/clockFaceTest3.fbx";
// Timekeeping
var secPos = 0;
var minPos = 0;
var hourPos = 0;
var d = new Date();
// Clock hands
var secondJointIndex = Entities.getJointIndex(CLOCKFACE_UID, "jointSecond");
var minuteJointIndex = Entities.getJointIndex(CLOCKFACE_UID, "jointMinute");
var hourJointIndex = Entities.getJointIndex(CLOCKFACE_UID, "jointHour");
// Chime and cuckooing logistics
var isCuckooing = false;
var cuckooSoundTimer;
var cuckooCount = 0; // Cuckoos called on the hour
var chimeCount = 0; // Chimes called on the quarter hour: 4, 1, 2, 3
var cuckooingTime = 0;
var ChimeTypes = {
HOUR: 0,
QUARTER: 1,
HALF: 2,
THREEQUARTER: 3
};
// Sound caching
var tickerSound = SoundCache.getSound(SND_TICKING_URL);
var cuckooSound = SoundCache.getSound(SND_CUCKOO_URL);
var sndPosition = Entities.getEntityProperties(CLOCKBODY_UID, "position").position;
// Accept a parameter and chime appropriately
function ChimeClock(chimeType) {
var d = new Date();
h = d.getHours();
h = h % 12;
if (isCuckooing) return;
cuckooCount = 0;
if (chimeType == ChimeTypes.QUARTER) chimeCount = 1; // DEPENDING ON CHIME TYPE, PLAY THE APPROPRIATE NUMBER OF TIMES
if (chimeType == ChimeTypes.HALF) chimeCount = 2;
if (chimeType == ChimeTypes.THREEQUARTER) chimeCount = 3;
if (chimeType == ChimeTypes.HOUR) {
isCuckooing = true;
cuckooCount = h;
cuckooingTime = d.getTime() + (cuckooCount * 3000);
chimeCount = 4;
PlayCuckooSound();
cuckooSoundTimer = Script.setInterval(function() {
PlayCuckooSound();
}, 3000);
Entities.editEntity(CLOCKBODY_UID, {
animation: {
url: ANIM_CUCKOO_URL,
fps: 30,
firstFrame: 0,
lastFrame: 90,
currentFrame: 0,
loop: true,
running: true
}
});
}
}
// Move the clock hand. Actually we are rotating the joint to which each child is parented
function SetClockHand(entityID, jointIndex) { // pass current time as parameter
resetChime = true;
var d = new Date();
h = d.getHours();
h = h % 12;
jointRot = Entities.getAbsoluteJointRotationInObjectFrame(entityID, jointIndex); // get current rotation of the joint
entRot = Quat.safeEulerAngles(jointRot);
parentRot = Quat.safeEulerAngles(Entities.getEntityProperties(entityID, "rotation"));
entRot.x = parentRot.x;
entRot.y = parentRot.y;
if (jointIndex == secondJointIndex) {
secPos = 360 - 360 / (60 / d.getSeconds());
entRot.z = secPos;
}
if (jointIndex == minuteJointIndex) {
minRot = 60 / d.getMinutes();
minRot = 360 / minRot;
entRot.z = 360 - minRot;
//print("minRot "+minRot)
if (minRot == 0) ChimeClock(ChimeTypes.HOUR);
if (minRot == 180) ChimeClock(ChimeTypes.HALF);
if (minRot == 90) ChimeClock(ChimeTypes.QUARTER);
if (minRot == 270) ChimeClock(ChimeTypes.THREEQUARTER);
}
if (jointIndex == hourJointIndex) {
hourPos = 360 - 360 / (12 / h);
entRot.z = hourPos;
}
newRot = Quat.fromVec3Degrees(entRot);
Entities.setAbsoluteJointRotationInObjectFrame(entityID, jointIndex, newRot);
}
// Continuously monitor the state of the cuckooing in order to turn it off.
function Update(deltaTime) {
var dt = new Date();
s = dt.getTime();
// print(s);
if (s > cuckooingTime - 3000) Script.clearTimeout(cuckooSoundTimer);
if (cuckooingTime > 0 && s > cuckooingTime) { // turn off cuckoo
isCuckooing = false;
cuckooingTime = 0;
//Script.clearTimeout(cuckooSoundTimer);
Entities.editEntity(CLOCKBODY_UID, {
animation: {
url: ANIM_CUCKOO_URL,
currentFrame: 0,
loop: true,
running: false
}
});
}
}
// Cuckoo!
function PlayCuckooSound() {
var options = {
loop: false,
position: sndPosition,
volume: 0.11
}
var cuckooInjector = Audio.playSound(cuckooSound, options);
}
// Make sure the clock doors are closed and fire up the ticker
function InitClock() {
cuckooingTime = d.getTime() + (5 * 3000); // 3000 = 3 seconds
Entities.editEntity(CLOCKBODY_UID, {
animation: {
url: ANIM_CUCKOO_URL,
fps: 30,
firstFrame: 0,
lastFrame: 90,
currentFrame: 0,
loop: true,
running: false
}
});
var options = {
loop: true,
position: sndPosition,
volume: 0.11
}
var tickingInjector = Audio.playSound(tickerSound, options);
}
InitClock();
ChimeClock(ChimeTypes.HOUR);
//var secondHandTimer = Script.setInterval(function() { SetClockHand(CLOCKFACE_UID, secondJointIndex); }, 1000);
//var minuteHandTimer = Script.setInterval(function() { SetClockHand(CLOCKFACE_UID, minuteJointIndex); }, 1000);
//var hourHandTimer = Script.setInterval(function() { SetClockHand(CLOCKFACE_UID, hourJointIndex); }, 1000);
// Connect a call back that happens every frame.
Script.update.connect(Update);