function degreesToRadians(degrees) { return (degrees / 180) * Math.PI; } const SecondsRadiansPerSecond = degreesToRadians(-6.0); const MinutesRadiansPerSecond = degreesToRadians(-6.0 / 60); (function() { this.equipped = false; this.isActive = false; this.secondsEntityID = null; this.minutesEntityID = null; var tickSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/tick.wav"); var tickInjector = null; this.preload = function(entityID) { this.entityID = entityID; this.messageChannel = "STOPWATCH-" + entityID; print("Stopwatch Server Preload", this.messageChannel); var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; var data = JSON.parse(userData); this.secondsEntityID = data.secondsEntityID; this.minutesEntityID = data.minutesEntityID; print(this.secondsEntityID, this.minutesEntityID); this.resetTimer(); Messages.subscribe(this.messageChannel); Messages.messageReceived.connect(this, this.messageReceived); print("Stopwatch Server Preload End"); }; this.unload = function() { print("Stopwatch Server Unload"); this.resetTimer(); Messages.unsubscribe(this.messageChannel); Messages.messageReceived.disconnect(this, this.messageReceived); }; this.getStopwatchPosition = function() { return Entities.getEntityProperties(this.entityID, "position").position; }; this.resetTimer = function() { print("Stopping stopwatch"); if (tickInjector) { tickInjector.stop(); } Entities.editEntity(this.secondsEntityID, { rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: { x: 0, y: 0, z: 0 }, }); Entities.editEntity(this.minutesEntityID, { rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: { x: 0, y: 0, z: 0 }, }); this.isActive = false; }; this.startTimer = function() { print("Starting stopwatch"); if (!tickInjector) { tickInjector = Audio.playSound(tickSound, { position: this.getStopwatchPosition(), volume: 0.7, loop: true }); } else { tickInjector.restart(); } Entities.editEntity(this.secondsEntityID, { rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: { x: 0, y: SecondsRadiansPerSecond, z: 0 }, }); Entities.editEntity(this.minutesEntityID, { angularVelocity: { x: 0, y: MinutesRadiansPerSecond, z: 0 }, }); this.isActive = true; }; this.messageReceived = function(channel, message, sender) { print("Message received", channel, sender, message); if (channel === this.messageChannel && message === 'click') { if (this.isActive) { this.resetTimer(); } else { this.startTimer(); } } }; });