From b7534fbd57c6165e410d493512fd79f3fdd6e4b7 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Mon, 21 Mar 2016 13:52:26 -0700 Subject: [PATCH] basics of virtual baton --- .../batonSoundTestEntityScript.js | 70 +++++++++++++++++++ .../batonSoundTestEntitySpawner.js | 31 ++++++++ 2 files changed, 101 insertions(+) create mode 100644 examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js create mode 100644 examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js new file mode 100644 index 0000000000..ee100a9563 --- /dev/null +++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js @@ -0,0 +1,70 @@ + +(function() { + Script.include("../../libraries/virtualBaton.js"); + + var baton; + var iOwn = false; + var updateLoopConnected = false; + + var _this; + BatonSoundEntity = function() { + _this = this; + _this.drumSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Drums/deepdrum1.wav"); + _this.injectorOptions = {position: MyAvatar.position, loop: false, volume: 0.7}; + }; + + function startUpdate() { + iOwn = true; + updateLoopConnected = true; + print("START UPDATE"); + Script.update.connect(_this.update); + } + + function stopUpdateAndReclaim() { + // when the baton is release + print("CLAIM BATON") + if (updateLoopConnected === true) { + updateLoopConnected = false; + Script.update.disconnect(_this.update); + } + iOwn = false; + + + // hook up callbacks to the baton + baton.claim(startUpdate, stopUpdateAndReclaim); + } + + BatonSoundEntity.prototype = { + + update: function() { + if (iOwn === false) { + return; + } + + print("EBL I AM UPDATING"); + }, + + preload: function(entityID) { + _this.entityID = entityID; + print("PRELOAD ENTITY SCRIPT!!!"); + baton = virtualBaton({ + // One winner for each entity + batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID + }); + Audio.playSound(_this.drumSound, _this.injectorOptions); + stopUpdateAndReclaim(); + }, + + unload: function() { + if (updateLoopConnected === true) { + updateLoopConnected = false; + Script.update.disconnect(_this.update); + } + } + + + }; + + // entity scripts always need to return a newly constructed object of our type + return new BatonSoundEntity(); +}); \ No newline at end of file diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js new file mode 100644 index 0000000000..541eb05baf --- /dev/null +++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js @@ -0,0 +1,31 @@ + var orientation = Camera.getOrientation(); + orientation = Quat.safeEulerAngles(orientation); + orientation.x = 0; + orientation = Quat.fromVec3Degrees(orientation); + var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation))); + + // Math.random ensures no caching of script + var SCRIPT_URL = Script.resolvePath("batonSoundTestEntityScript.js?v1" + Math.random()) + + var soundEntity = Entities.addEntity({ + type: "Box", + color: { + red: 200, + green: 10, + blue: 10 + }, + position: center, + dimensions: { + x: 0.1, + y: 0.1, + z: 0.1 + }, + script: SCRIPT_URL + }); + + + function cleanup() { + Entities.deleteEntity(soundEntity); + } + + Script.scriptEnding.connect(cleanup); \ No newline at end of file