content/hifi-public/huffman/stopwatch/stopwatchServer.js
Dale Glass 0d14e5a379 Initial data.
Needs a lot of cleanup. Data has been de-duplicated, and where identical copies existed, one of them
has been replaced with a symlink.

Some files have been excluded, such as binaries, installers and debug dumps. Some of that may still
be present.
2022-02-13 18:59:11 +01:00

92 lines
3.1 KiB
JavaScript

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();
}
}
};
});