Implement start/stop and reset button actions

This commit is contained in:
David Rowe 2017-05-26 13:02:05 +12:00
parent f25a9dcede
commit b1280ff810
4 changed files with 93 additions and 21 deletions

View file

@ -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()

View file

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

View file

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

View file

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