Merge pull request #9538 from huffman/feat/stopwatch

Add stopwatch example script
This commit is contained in:
Ryan Huffman 2017-02-06 08:32:19 -08:00 committed by GitHub
commit f0238ec4d7
5 changed files with 189 additions and 0 deletions

Binary file not shown.

View file

@ -0,0 +1,48 @@
//
// spawnStopwatch.js
//
// Created by Ryan Huffman on 1/20/17.
// 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
//
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")
});

View file

@ -0,0 +1,22 @@
//
// stopwatchServer.js
//
// Created by Ryan Huffman on 1/20/17.
// 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) {
this.messageChannel = "STOPWATCH-" + entityID;
};
function click() {
Messages.sendMessage(this.messageChannel, 'click');
}
this.startNearTrigger = click;
this.startFarTrigger = click;
this.clickDownOnEntity = click;
});

View file

@ -0,0 +1,119 @@
//
// stopwatchServer.js
//
// Created by Ryan Huffman on 1/20/17.
// 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 self = this;
self.equipped = false;
self.isActive = false;
self.secondHandID = null;
self.minuteHandID = null;
self.tickSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/tick.wav");
self.tickInjector = null;
self.tickIntervalID = null;
self.chimeSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/huffman/chime.wav");
self.preload = function(entityID) {
print("Preloading stopwatch: ", entityID);
self.entityID = entityID;
self.messageChannel = "STOPWATCH-" + entityID;
var userData = Entities.getEntityProperties(self.entityID, 'userData').userData;
var data = JSON.parse(userData);
self.secondHandID = data.secondHandID;
self.minuteHandID = data.minuteHandID;
self.resetTimer();
Messages.subscribe(self.messageChannel);
Messages.messageReceived.connect(this, self.messageReceived);
};
self.unload = function() {
print("Unloading stopwatch:", self.entityID);
self.resetTimer();
Messages.unsubscribe(self.messageChannel);
Messages.messageReceived.disconnect(this, self.messageReceived);
};
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();
}
}
};
self.getStopwatchPosition = function() {
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;
}
Entities.editEntity(self.secondHandID, {
rotation: Quat.fromPitchYawRollDegrees(0, 0, 0),
angularVelocity: { x: 0, y: 0, z: 0 },
});
Entities.editEntity(self.minuteHandID, {
rotation: Quat.fromPitchYawRollDegrees(0, 0, 0),
angularVelocity: { x: 0, y: 0, z: 0 },
});
self.isActive = false;
};
self.startTimer = function() {
print("Starting stopwatch");
if (!self.tickInjector) {
self.tickInjector = Audio.playSound(self.tickSound, {
position: self.getStopwatchPosition(),
volume: 0.7,
loop: true
});
} else {
self.tickInjector.restart();
}
var seconds = 0;
self.tickIntervalID = Script.setInterval(function() {
if (self.tickInjector) {
self.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(self.chimeSound, {
position: self.getStopwatchPosition(),
volume: 1.0,
loop: false
});
}
}, 1000);
self.isActive = true;
};
});

Binary file not shown.