diff --git a/unpublishedScripts/marketplace/stopwatch/chime.wav b/unpublishedScripts/marketplace/stopwatch/chime.wav new file mode 100644 index 0000000000..cd7143eeeb Binary files /dev/null and b/unpublishedScripts/marketplace/stopwatch/chime.wav differ diff --git a/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js b/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js new file mode 100644 index 0000000000..e385f04771 --- /dev/null +++ b/unpublishedScripts/marketplace/stopwatch/spawnStopwatch.js @@ -0,0 +1,38 @@ +var positionToSpawn = Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.rotation)); + +var stopwatchID = Entities.addEntity({ + type: "Model", + name: "stopwatch/base", + position: positionToSpawn, + modelURL: "http://hifi-content.s3.amazonaws.com/alan/dev/Stopwatch.fbx", + dimensions: {"x":4.129462242126465,"y":1.058512806892395,"z":5.773681640625} +}); + +var secondHandID = Entities.addEntity({ + type: "Model", + name: "stopwatch/seconds", + parentID: stopwatchID, + localPosition: {"x":-0.004985813982784748,"y":0.39391064643859863,"z":0.8312804698944092}, + dimensions: {"x":0.14095762372016907,"y":0.02546107769012451,"z":1.6077008247375488}, + registrationPoint: {"x":0.5,"y":0.5,"z":1}, + modelURL: "http://hifi-content.s3.amazonaws.com/alan/dev/Stopwatch-sec-hand.fbx", +}); + +var minuteHandID = Entities.addEntity({ + type: "Model", + name: "stopwatch/minutes", + parentID: stopwatchID, + localPosition: {"x":-0.0023056098725646734,"y":0.3308190703392029,"z":0.21810021996498108}, + dimensions: {"x":0.045471154153347015,"y":0.015412690117955208,"z":0.22930574417114258}, + registrationPoint: {"x":0.5,"y":0.5,"z":1}, + modelURL: "http://hifi-content.s3.amazonaws.com/alan/dev/Stopwatch-min-hand.fbx", +}); + +Entities.editEntity(stopwatchID, { + userData: JSON.stringify({ + secondHandID: secondHandID, + minuteHandID: minuteHandID, + }), + script: Script.resolvePath("stopwatchClient.js"), + serverScripts: Script.resolvePath("stopwatchServer.js") +}); diff --git a/unpublishedScripts/marketplace/stopwatch/stopwatchClient.js b/unpublishedScripts/marketplace/stopwatch/stopwatchClient.js index 2c25231881..786c290a02 100644 --- a/unpublishedScripts/marketplace/stopwatch/stopwatchClient.js +++ b/unpublishedScripts/marketplace/stopwatch/stopwatchClient.js @@ -1,20 +1,12 @@ (function() { - this.equipped = false; + var messageChannel; this.preload = function(entityID) { - this.entityID = entityID; this.messageChannel = "STOPWATCH-" + entityID; - print("In stopwatch client", this.messageChannel); - }; - this.startEquip = function(entityID, args) { - this.equipped = true; - }; - this.continueEquip = function(entityID, args) { - var triggerValue = Controller.getValue(args[0] === 'left' ? Controller.Standard.LT : Controller.Standard.RT); - if (triggerValue > 0.5) { - Messages.sendMessage(messageChannel, 'click'); - } - }; - this.releaseEquip = function(entityID, args) { - this.equipped = false; }; + function click() { + Messages.sendMessage(this.messageChannel, 'click'); + } + this.startNearTrigger = click; + this.startFarTrigger = click; + this.clickDownOnEntity = click; }); diff --git a/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js b/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js index c23813eeaa..8d87241617 100644 --- a/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js +++ b/unpublishedScripts/marketplace/stopwatch/stopwatchServer.js @@ -2,53 +2,107 @@ this.equipped = false; this.isActive = false; - this.secondsEntityID = null; - this.minutesEntityID = null; + this.secondHandID = null; + this.minuteHandID = null; + + var tickSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/tick.wav"); + var tickInjector = null; + var tickIntervalID = null; + + var chimeSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/chime.wav"); this.preload = function(entityID) { - print("Stopwatch Server Preload"); + print("Preloading stopwatch: ", entityID); this.entityID = entityID; this.messageChannel = "STOPWATCH-" + entityID; var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; var data = JSON.parse(userData); - this.secondsEntityID = data.secondsEntityID; - this.minutesEntityID = data.minutesEntityID; + this.secondHandID = data.secondHandID; + this.minuteHandID = data.minuteHandID; this.resetTimer(); Messages.subscribe(this.messageChannel); Messages.messageReceived.connect(this, this.messageReceived); }; - this.unload = function() { - print("Stopwatch Server Unload"); + print("Unloading stopwatch:", this.entityID); + this.resetTimer(); Messages.unsubscribe(this.messageChannel); Messages.messageReceived.disconnect(this, this.messageReceived); }; - + 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(); + } + } + }; + this.getStopwatchPosition = function() { + return Entities.getEntityProperties(this.entityID, "position").position; + }; this.resetTimer = function() { - Entities.editEntity(this.secondsEntityID, { + print("Stopping stopwatch"); + if (tickInjector) { + tickInjector.stop(); + } + if (tickIntervalID !== null) { + Script.clearInterval(tickIntervalID); + tickIntervalID = null; + } + Entities.editEntity(this.secondHandID, { + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + angularVelocity: { x: 0, y: 0, z: 0 }, + }); + Entities.editEntity(this.minuteHandID, { rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: { x: 0, y: 0, z: 0 }, }); this.isActive = false; }; this.startTimer = function() { - Entities.editEntity(this.secondsEntityID, { - angularVelocity: { x: 0, y: -6, z: 0 }, - }); + print("Starting stopwatch"); + if (!tickInjector) { + tickInjector = Audio.playSound(tickSound, { + position: this.getStopwatchPosition(), + volume: 0.7, + loop: true + }); + } else { + tickInjector.restart(); + } + + var self = this; + var seconds = 0; + tickIntervalID = Script.setInterval(function() { + if (tickInjector) { + tickInjector.setOptions({ + position: self.getStopwatchPosition(), + volume: 0.7, + loop: true + }); + } + seconds++; + const degreesPerTick = -360 / 60; + Entities.editEntity(self.secondHandID, { + rotation: Quat.fromPitchYawRollDegrees(0, seconds * degreesPerTick, 0), + }); + if (seconds % 60 == 0) { + Entities.editEntity(self.minuteHandID, { + rotation: Quat.fromPitchYawRollDegrees(0, (seconds / 60) * degreesPerTick, 0), + }); + Audio.playSound(chimeSound, { + position: self.getStopwatchPosition(), + volume: 1.0, + loop: false + }); + } + }, 1000); + this.isActive = true; }; - - this.messageReceived = function(channel, sender, message) { - print("Message received", channel, sender, message); - if (channel == this.messageChannel && message === 'click') { - if (this.isActive) { - resetTimer(); - } else { - startTimer(); - } - } - }; }); diff --git a/unpublishedScripts/marketplace/stopwatch/tick.wav b/unpublishedScripts/marketplace/stopwatch/tick.wav new file mode 100644 index 0000000000..21781f8ce4 Binary files /dev/null and b/unpublishedScripts/marketplace/stopwatch/tick.wav differ