Update stopwatchServer.js to use self instead of this

This commit is contained in:
Ryan Huffman 2017-01-27 15:30:05 -08:00
parent 47235d24aa
commit 8acb3f9b53

View file

@ -9,88 +9,89 @@
// //
(function() { (function() {
this.equipped = false; var self = this;
this.isActive = false;
this.secondHandID = null; self.equipped = false;
this.minuteHandID = null; self.isActive = false;
var tickSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/tick.wav"); self.secondHandID = null;
var tickInjector = null; self.minuteHandID = null;
var tickIntervalID = null;
var chimeSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/chime.wav"); self.tickSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/tick.wav");
self.tickInjector = null;
self.tickIntervalID = null;
this.preload = function(entityID) { self.chimeSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/chime.wav");
self.preload = function(entityID) {
print("Preloading stopwatch: ", entityID); print("Preloading stopwatch: ", entityID);
this.entityID = entityID; self.entityID = entityID;
this.messageChannel = "STOPWATCH-" + entityID; self.messageChannel = "STOPWATCH-" + entityID;
var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; var userData = Entities.getEntityProperties(self.entityID, 'userData').userData;
var data = JSON.parse(userData); var data = JSON.parse(userData);
this.secondHandID = data.secondHandID; self.secondHandID = data.secondHandID;
this.minuteHandID = data.minuteHandID; self.minuteHandID = data.minuteHandID;
this.resetTimer(); self.resetTimer();
Messages.subscribe(this.messageChannel); Messages.subscribe(self.messageChannel);
Messages.messageReceived.connect(this, this.messageReceived); Messages.messageReceived.connect(this, self.messageReceived);
}; };
this.unload = function() { self.unload = function() {
print("Unloading stopwatch:", this.entityID); print("Unloading stopwatch:", self.entityID);
this.resetTimer(); self.resetTimer();
Messages.unsubscribe(this.messageChannel); Messages.unsubscribe(self.messageChannel);
Messages.messageReceived.disconnect(this, this.messageReceived); Messages.messageReceived.disconnect(this, self.messageReceived);
}; };
this.messageReceived = function(channel, message, sender) { self.messageReceived = function(channel, message, sender) {
print("Message received", channel, sender, message); print("Message received", channel, sender, message);
if (channel === this.messageChannel && message === 'click') { if (channel === self.messageChannel && message === 'click') {
if (this.isActive) { if (self.isActive) {
this.resetTimer(); self.resetTimer();
} else { } else {
this.startTimer(); self.startTimer();
} }
} }
}; };
this.getStopwatchPosition = function() { self.getStopwatchPosition = function() {
return Entities.getEntityProperties(this.entityID, "position").position; return Entities.getEntityProperties(self.entityID, "position").position;
}; };
this.resetTimer = function() { self.resetTimer = function() {
print("Stopping stopwatch"); print("Stopping stopwatch");
if (tickInjector) { if (self.tickInjector) {
tickInjector.stop(); self.tickInjector.stop();
} }
if (tickIntervalID !== null) { if (self.tickIntervalID !== null) {
Script.clearInterval(tickIntervalID); Script.clearInterval(self.tickIntervalID);
tickIntervalID = null; self.tickIntervalID = null;
} }
Entities.editEntity(this.secondHandID, { Entities.editEntity(self.secondHandID, {
rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), rotation: Quat.fromPitchYawRollDegrees(0, 0, 0),
angularVelocity: { x: 0, y: 0, z: 0 }, angularVelocity: { x: 0, y: 0, z: 0 },
}); });
Entities.editEntity(this.minuteHandID, { Entities.editEntity(self.minuteHandID, {
rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), rotation: Quat.fromPitchYawRollDegrees(0, 0, 0),
angularVelocity: { x: 0, y: 0, z: 0 }, angularVelocity: { x: 0, y: 0, z: 0 },
}); });
this.isActive = false; self.isActive = false;
}; };
this.startTimer = function() { self.startTimer = function() {
print("Starting stopwatch"); print("Starting stopwatch");
if (!tickInjector) { if (!self.tickInjector) {
tickInjector = Audio.playSound(tickSound, { self.tickInjector = Audio.playSound(self.tickSound, {
position: this.getStopwatchPosition(), position: self.getStopwatchPosition(),
volume: 0.7, volume: 0.7,
loop: true loop: true
}); });
} else { } else {
tickInjector.restart(); self.tickInjector.restart();
} }
var self = this;
var seconds = 0; var seconds = 0;
tickIntervalID = Script.setInterval(function() { self.tickIntervalID = Script.setInterval(function() {
if (tickInjector) { if (self.tickInjector) {
tickInjector.setOptions({ self.tickInjector.setOptions({
position: self.getStopwatchPosition(), position: self.getStopwatchPosition(),
volume: 0.7, volume: 0.7,
loop: true loop: true
@ -105,7 +106,7 @@
Entities.editEntity(self.minuteHandID, { Entities.editEntity(self.minuteHandID, {
rotation: Quat.fromPitchYawRollDegrees(0, (seconds / 60) * degreesPerTick, 0), rotation: Quat.fromPitchYawRollDegrees(0, (seconds / 60) * degreesPerTick, 0),
}); });
Audio.playSound(chimeSound, { Audio.playSound(self.chimeSound, {
position: self.getStopwatchPosition(), position: self.getStopwatchPosition(),
volume: 1.0, volume: 1.0,
loop: false loop: false
@ -113,6 +114,6 @@
} }
}, 1000); }, 1000);
this.isActive = true; self.isActive = true;
}; };
}); });