Skip to content

Instantly share code, notes, and snippets.

@PhilipRosedale
Created April 28, 2017 05:15
Show Gist options
  • Save PhilipRosedale/755480dd9677ee16292f93870e98943e to your computer and use it in GitHub Desktop.
Save PhilipRosedale/755480dd9677ee16292f93870e98943e to your computer and use it in GitHub Desktop.
//
// drum.js
//
// Created by Patrick Gosch on 03/28/2017
// Modified / Adapted by Philip Rosedale for sample use
// Copyright 2017 High Fidelity, Inc.
//
// Attach this entity script and either hit it or click it to hear a sound.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var SOUND_URL = "http://hifi-content.s3.amazonaws.com/Examples%20Content/production/xylophone/A4.wav";
(function() {
var DURATION = 100; // Delay in msecs before it can be played again
var HIT_COLOR = { red: 255, green: 0, blue: 0 };
var SHOULD_COLOR = true;
var _this;
var debug = false;
function debugPrint(string) {
if (debug) {
print(string);
}
}
function drum() {
_this = this;
}
drum.prototype = {
sound: null,
isWaiting: false,
injector: null,
preload: function(entityID) {
_this.entityID = entityID;
_this.homePos = Entities.getEntityProperties(_this.entityID, ["position"]).position;
_this.originalColor = Entities.getEntityProperties(_this.entityID, ["color"]).color;
_this.sound = SoundCache.getSound(SOUND_URL);
},
collisionWithEntity: function(thisEntity, otherEntity, collision) {
if (collision.type === 0) {
debugPrint("Hit!");
_this.hit();
}
},
clickDownOnEntity: function() {
debugPrint("Clicked!");
_this.hit();
},
hit: function() {
if (!_this.isWaiting) {
_this.isWaiting = true;
_this.injector = Audio.playSound(_this.sound, {
position: _this.homePos,
volume: 1
});
if (SHOULD_COLOR) {
Entities.editEntity(_this.entityID, { color: HIT_COLOR });
}
_this.timeout();
}
},
timeout: function() {
Script.setTimeout(function() {
if (SHOULD_COLOR) {
Entities.editEntity(_this.entityID, { color: _this.originalColor});
}
_this.isWaiting = false;
}, DURATION);
},
};
return new drum();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment