content/hifi-content/caitlyn/scratch/clockScriptForPhoton.js
2022-02-13 22:19:19 +01:00

50 lines
2.2 KiB
JavaScript

const CLOCKFACE_UID = "c02753ba-0b0f-4917-8c43-a9f84a03412d"; //; Set to match the UID of the clock please
const CHIME_HOUR = 0;
const CHIME_HALFHOUR = 1;
const CHIME_QUARTER = 2;
var secPos = 0;
var minPos = 0;
var hourPos = 0;
var secondJointIndex = Entities.getJointIndex(CLOCKFACE_UID, "jointSecond");
var minuteJointIndex = Entities.getJointIndex(CLOCKFACE_UID, "jointMinute");
var hourJointIndex = Entities.getJointIndex(CLOCKFACE_UID, "jointHour");
var lastChime = 0;
function ChimeClock(chimeType){
var d = new Date();
if (lastChime>d.getMilliseconds()) return;
lastChime = d.getMilliseconds()+5;
print("CHIMING "+chimeType);
}
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); // convert to Euler
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;}//+= 6;
if (jointIndex==minuteJointIndex) {
minRot = 60/d.getMinutes();
minRot = 360/minRot;//360 - 360/(60/d.getMinutes());
entRot.z = 360-minRot;
print("minRot "+minRot)
if (minRot == 0) ChimeClock(CHIME_HOUR);
if (minRot == 180) ChimeClock(CHIME_HALFHOUR);
if (minRot == 90) ChimeClock(CHIME_QUARTER);
if (minRot == 270) ChimeClock(CHIME_QUARTER);
}//
if (jointIndex==hourJointIndex) {hourPos =360 - 360/(12/h); entRot.z = hourPos;}
newRot = Quat.fromVec3Degrees(entRot); // generate a quaternion from the euler
Entities.setAbsoluteJointRotationInObjectFrame(entityID, jointIndex, newRot);
}
// Start the timers for each clock hand
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);