123 lines
No EOL
3.3 KiB
JavaScript
123 lines
No EOL
3.3 KiB
JavaScript
//
|
|
// Maraca.js
|
|
//
|
|
// Created by Philip Rosedale on May 9, 2016
|
|
// Copyright 2016 High Fidelity, Inc.
|
|
//
|
|
// reates a maraca sound when shaken
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
|
|
function Timer() {
|
|
var time;
|
|
var count = 0;
|
|
var totalTime = 0;
|
|
this.reset = function() {
|
|
count = 0;
|
|
totalTime = 0;
|
|
}
|
|
this.start = function() {
|
|
time = new Date().getTime();
|
|
}
|
|
this.record = function() {
|
|
var elapsed = new Date().getTime() - time;
|
|
totalTime += elapsed;
|
|
count++;
|
|
return elapsed;
|
|
}
|
|
this.count = function() {
|
|
return count;
|
|
}
|
|
this.average = function() {
|
|
return (count == 0) ? 0 : totalTime / count;
|
|
}
|
|
this.elapsed = function() {
|
|
return new Date().getTime() - time;
|
|
}
|
|
}
|
|
|
|
(function () {
|
|
var entityID,
|
|
wantDebug = true,
|
|
timer = new Timer(),
|
|
playTimer = new Timer(),
|
|
TRIGGER_SPEED = 0.06,
|
|
moving = false,
|
|
RED = { red: 255, green: 0, blue: 0 },
|
|
GRAY = { red: 128, green: 128, blue: 128 },
|
|
averageVelocity = { x: 0, y: 0, z: 0 },
|
|
VELOCITY_AVERAGE_RATE = 0.5,
|
|
VOLUME = 2.0,
|
|
MIN_TIME_BETWEEN_SOUNDS = 0.1,
|
|
soundUp = SoundCache.getSound("http://hifi-content.s3.amazonaws.com/Examples%20Content/production/maracas/maraca.wav"),
|
|
soundDown = SoundCache.getSound("http://hifi-content.s3.amazonaws.com/Examples%20Content/production/maracas/maraca3.wav")
|
|
|
|
function printDebug(message) {
|
|
if (wantDebug) {
|
|
print(message);
|
|
}
|
|
}
|
|
|
|
function maybePlaySound() {
|
|
|
|
var deltaTime = timer.elapsed() / 1000.0;
|
|
if (deltaTime == 0.0) {
|
|
return;
|
|
}
|
|
|
|
properties = Entities.getEntityProperties(entityID);
|
|
|
|
// Compute the pseudoAcceleration of the object as the instantaneous speed dotted with its own history
|
|
var relativeSpeed = Vec3.dot(properties.velocity, averageVelocity);
|
|
var speed = Vec3.length(properties.velocity);
|
|
|
|
if (relativeSpeed > TRIGGER_SPEED) {
|
|
moving = true;
|
|
Entities.editEntity(entityID, { color: GRAY });
|
|
}
|
|
if (moving && (relativeSpeed < TRIGGER_SPEED)) {
|
|
moving = false;
|
|
var volume = Math.min(Vec3.length(averageVelocity) * VOLUME, 1.0);
|
|
if (playTimer.elapsed() / 1000 > MIN_TIME_BETWEEN_SOUNDS) {
|
|
Entities.editEntity(entityID, { color: RED });
|
|
var up = Vec3.dot(averageVelocity, { x: 0, y: 1, z: 0 });
|
|
Audio.playSound( up > 0 ? soundUp : soundDown, {
|
|
position: properties.position,
|
|
volume: volume,
|
|
loop: false } );
|
|
|
|
|
|
playTimer.start();
|
|
}
|
|
}
|
|
averageVelocity = Vec3.sum( Vec3.multiply(VELOCITY_AVERAGE_RATE, properties.velocity),
|
|
Vec3.multiply(1.0 - VELOCITY_AVERAGE_RATE, averageVelocity) );
|
|
timer.start();
|
|
}
|
|
|
|
this.startNearGrab = function() {
|
|
timer.start();
|
|
playTimer.start();
|
|
}
|
|
|
|
this.continueNearGrab = function() {
|
|
// Check object velocity
|
|
maybePlaySound();
|
|
}
|
|
|
|
this.releaseGrab = function() {
|
|
printDebug("released maraca...");
|
|
}
|
|
|
|
this.preload = function (givenEntityID) {
|
|
printDebug("load maraca...");
|
|
entityID = givenEntityID;
|
|
};
|
|
|
|
this.unload = function () {
|
|
printDebug("Unload maraca...");
|
|
};
|
|
|
|
});
|