diff --git a/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js b/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js index c4496865d9..06f437478d 100644 --- a/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js +++ b/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js @@ -51,6 +51,7 @@ var startStopButtonID = Entities.addEntity({ dimensions: Vec3.multiply(scale, { x: 0.8, y: 1.0, z: 0.8 }), localPosition: Vec3.multiply(scale, { x: 0, y: -0.1, z: -2.06 }), localRotation: Quat.fromVec3Degrees({ x: 90, y: 0, z: 0 }), + visible: false }); var resetButtonID = Entities.addEntity({ @@ -61,15 +62,29 @@ var resetButtonID = Entities.addEntity({ dimensions: Vec3.multiply(scale, { x: 0.6, y: 0.8, z: 0.6 }), localPosition: Vec3.multiply(scale, { x: -1.5, y: -0.1, z: -1.2 }), localRotation: Quat.fromVec3Degrees({ x: 90, y: 36, z: 0 }), + visible: false }); Entities.editEntity(stopwatchID, { userData: JSON.stringify({ secondHandID: secondHandID, - minuteHandID: minuteHandID, + minuteHandID: minuteHandID }), - script: Script.resolvePath("stopwatchClient.js"), serverScripts: Script.resolvePath("stopwatchServer.js") }); +Entities.editEntity(startStopButtonID, { + userData: JSON.stringify({ + stopwatchID: stopwatchID + }), + script: Script.resolvePath("stopwatchStartStop.js") +}); + +Entities.editEntity(resetButtonID, { + userData: JSON.stringify({ + stopwatchID: stopwatchID + }), + script: Script.resolvePath("stopwatchReset.js") +}); + Script.stop() diff --git a/unpublishedScripts/marketplace/stopwatch/stopwatchReset.js b/unpublishedScripts/marketplace/stopwatch/stopwatchReset.js new file mode 100644 index 0000000000..b65c1e7340 --- /dev/null +++ b/unpublishedScripts/marketplace/stopwatch/stopwatchReset.js @@ -0,0 +1,22 @@ +// +// stopwatchReset.js +// +// Created by David Rowe on 26 May 2017. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +(function () { + this.preload = function (entityID) { + var properties = Entities.getEntityProperties(entityID, "userData"); + this.messageChannel = "STOPWATCH-" + JSON.parse(properties.userData).stopwatchID; + }; + function click() { + Messages.sendMessage(this.messageChannel, "reset"); + } + this.startNearTrigger = click; + this.startFarTrigger = click; + this.clickDownOnEntity = click; +}); diff --git a/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js b/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js index 925db565c3..6ae1b69087 100644 --- a/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js +++ b/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js @@ -13,6 +13,7 @@ self.equipped = false; self.isActive = false; + self.seconds = 0; self.secondHandID = null; self.minuteHandID = null; @@ -46,11 +47,19 @@ }; self.messageReceived = function(channel, message, sender) { print("Message received", channel, sender, message); - if (channel === self.messageChannel && message === 'click') { - if (self.isActive) { - self.resetTimer(); - } else { - self.startTimer(); + if (channel === self.messageChannel) { + switch (message) { + case "startStop": + if (self.isActive) { + self.stopTimer(); + } else { + self.startTimer(); + } + break; + case "reset": + self.stopTimer(); + self.resetTimer(); + break; } } }; @@ -58,14 +67,7 @@ return Entities.getEntityProperties(self.entityID, "position").position; }; self.resetTimer = function() { - print("Stopping stopwatch"); - if (self.tickInjector) { - self.tickInjector.stop(); - } - if (self.tickIntervalID !== null) { - Script.clearInterval(self.tickIntervalID); - self.tickIntervalID = null; - } + print("Resetting stopwatch"); Entities.editEntity(self.secondHandID, { localRotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: { x: 0, y: 0, z: 0 }, @@ -74,7 +76,7 @@ localRotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: { x: 0, y: 0, z: 0 }, }); - self.isActive = false; + self.seconds = 0; }; self.startTimer = function() { print("Starting stopwatch"); @@ -88,7 +90,6 @@ self.tickInjector.restart(); } - var seconds = 0; self.tickIntervalID = Script.setInterval(function() { if (self.tickInjector) { self.tickInjector.setOptions({ @@ -97,15 +98,15 @@ loop: true }); } - seconds++; + self.seconds++; const degreesPerTick = -360 / 60; Entities.editEntity(self.secondHandID, { - localRotation: Quat.fromPitchYawRollDegrees(0, seconds * degreesPerTick, 0), + localRotation: Quat.fromPitchYawRollDegrees(0, self.seconds * degreesPerTick, 0), }); - if (seconds % 60 == 0) { + if (self.seconds % 60 == 0) { Entities.editEntity(self.minuteHandID, { - localRotation: Quat.fromPitchYawRollDegrees(0, (seconds / 60) * degreesPerTick, 0), + localRotation: Quat.fromPitchYawRollDegrees(0, (self.seconds / 60) * degreesPerTick, 0), }); Audio.playSound(self.chimeSound, { position: self.getStopwatchPosition(), @@ -117,4 +118,15 @@ self.isActive = true; }; + self.stopTimer = function () { + print("Stopping stopwatch"); + if (self.tickInjector) { + self.tickInjector.stop(); + } + if (self.tickIntervalID !== null) { + Script.clearInterval(self.tickIntervalID); + self.tickIntervalID = null; + } + self.isActive = false; + }; }); diff --git a/unpublishedScripts/marketplace/stopwatch/stopwatchStartStop.js b/unpublishedScripts/marketplace/stopwatch/stopwatchStartStop.js new file mode 100644 index 0000000000..88c037ee36 --- /dev/null +++ b/unpublishedScripts/marketplace/stopwatch/stopwatchStartStop.js @@ -0,0 +1,23 @@ +// +// stopwatchStartStop.js +// +// Created by David Rowe on 26 May 2017. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +(function () { + var messageChannel; + this.preload = function (entityID) { + var properties = Entities.getEntityProperties(entityID, "userData"); + this.messageChannel = "STOPWATCH-" + JSON.parse(properties.userData).stopwatchID; + }; + function click() { + Messages.sendMessage(this.messageChannel, "startStop"); + } + this.startNearTrigger = click; + this.startFarTrigger = click; + this.clickDownOnEntity = click; +});