From 1e09c2501c9b33f5311be4c7402df89481af4edf Mon Sep 17 00:00:00 2001 From: James Pollack Date: Tue, 22 Sep 2015 14:21:14 -0700 Subject: [PATCH 1/3] This updates the flashlight to have sounds when it turns on and off --- examples/toys/flashlight/flashlight.js | 50 ++++++++++++++------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/examples/toys/flashlight/flashlight.js b/examples/toys/flashlight/flashlight.js index 0465266b1c..5afaed829c 100644 --- a/examples/toys/flashlight/flashlight.js +++ b/examples/toys/flashlight/flashlight.js @@ -18,6 +18,8 @@ (function() { Script.include("../../libraries/utils.js"); + var ON_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_on.wav'; + var OFF_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_off.wav'; var _this; @@ -78,7 +80,7 @@ this.hand = 'LEFT'; }, startNearGrab: function() { - if (!_this.hasSpotlight) { + if (!this.hasSpotlight) { //this light casts the beam this.spotlight = Entities.addEntity({ @@ -117,21 +119,7 @@ cutoff: 90, // in degrees }); - this.debugBox = Entities.addEntity({ - type: 'Box', - color: { - red: 255, - blue: 0, - green: 0 - }, - dimensions: { - x: 0.25, - y: 0.25, - z: 0.25 - }, - ignoreForCollisions:true - }) - + this.hasSpotlight = true; } @@ -158,7 +146,7 @@ this.glowLight = null; this.spotlight = null; this.whichHand = null; - + this.lightOn = false; } }, updateLightPositions: function() { @@ -181,11 +169,6 @@ rotation: glowLightTransform.q, }) - // Entities.editEntity(this.debugBox, { - // position: lightTransform.p, - // rotation: lightTransform.q, - // }) - }, changeLightWithTriggerPressure: function(flashLightHand) { @@ -203,6 +186,7 @@ return }, turnLightOff: function() { + this.playSoundAtCurrentPosition(false); Entities.editEntity(this.spotlight, { intensity: 0 }); @@ -210,8 +194,12 @@ intensity: 0 }); this.lightOn = false + + }, turnLightOn: function() { + this.playSoundAtCurrentPosition(true); + Entities.editEntity(this.glowLight, { intensity: 2 }); @@ -225,6 +213,23 @@ // * remembering our entityID, so we can access it in cases where we're called without an entityID preload: function(entityID) { this.entityID = entityID; + this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL); + this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL); + + }, + playSoundAtCurrentPosition: function(playOnSound) { + var position = Entities.getEntityProperties(this.entityID, "position").position; + + var audioProperties = { + volume: 0.5, + position: position + } + if (playOnSound) { + Audio.playSound(this.ON_SOUND, audioProperties) + } else { + Audio.playSound(this.OFF_SOUND, audioProperties) + + } }, // unload() will be called when our entity is no longer available. It may be because we were deleted, @@ -238,6 +243,7 @@ this.glowLight = null; this.spotlight = null; this.whichHand = null; + this.lightOn = false; } From fa1d5be3100a50b18884eddbbb071764d52179f0 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Tue, 22 Sep 2015 18:20:23 -0700 Subject: [PATCH 2/3] Reduce volume, JSLint, Coding standards --- examples/toys/flashlight/createFlashlight.js | 41 ++++-------- examples/toys/flashlight/flashlight.js | 70 +++++++++----------- 2 files changed, 47 insertions(+), 64 deletions(-) diff --git a/examples/toys/flashlight/createFlashlight.js b/examples/toys/flashlight/createFlashlight.js index 38907efa75..7c04433979 100644 --- a/examples/toys/flashlight/createFlashlight.js +++ b/examples/toys/flashlight/createFlashlight.js @@ -11,7 +11,7 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // - +/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ Script.include("https://hifi-public.s3.amazonaws.com/scripts/utilities.js"); @@ -19,31 +19,18 @@ var scriptURL = Script.resolvePath('flashlight.js?123123'); var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx"; -var center = Vec3.sum(Vec3.sum(MyAvatar.position, { - x: 0, - y: 0.5, - z: 0 -}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation()))); +var center = Vec3.sum(Vec3.sum(MyAvatar.position, {x: 0, y: 0.5, z: 0}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation()))); var flashlight = Entities.addEntity({ - type: "Model", - modelURL: modelURL, - position: center, - dimensions: { - x: 0.08, - y: 0.30, - z: 0.08 - }, - collisionsWillMove: true, - shapeType: 'box', - script: scriptURL -}); - - -function cleanup() { - //commenting out the line below makes this persistent. to delete at cleanup, uncomment - //Entities.deleteEntity(flashlight); -} - - -Script.scriptEnding.connect(cleanup); \ No newline at end of file + type: "Model", + modelURL: modelURL, + position: center, + dimensions: { + x: 0.08, + y: 0.30, + z: 0.08 + }, + collisionsWillMove: true, + shapeType: 'box', + script: scriptURL +}); \ No newline at end of file diff --git a/examples/toys/flashlight/flashlight.js b/examples/toys/flashlight/flashlight.js index 5afaed829c..5563001d07 100644 --- a/examples/toys/flashlight/flashlight.js +++ b/examples/toys/flashlight/flashlight.js @@ -14,20 +14,18 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // - -(function() { +/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ +(function () { Script.include("../../libraries/utils.js"); var ON_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_on.wav'; var OFF_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_off.wav'; - var _this; - // this is the "constructor" for the entity as a JS object we don't do much here, but we do want to remember // our this object, so we can access it in cases where we're called without a this (like in the case of various global signals) - Flashlight = function() { - _this = this; - }; + function Flashlight() { + return; + } //if the trigger value goes below this while held, the flashlight will turn off. if it goes above, it will var DISABLE_LIGHT_THRESHOLD = 0.7; @@ -48,7 +46,7 @@ x: 0, y: -0.1, z: 0 - } + }; // Evaluate the world light entity positions and orientations from the model ones function evalLightWorldTransform(modelPos, modelRot) { @@ -57,14 +55,14 @@ p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)), q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) }; - }; + } function glowLightWorldTransform(modelPos, modelRot) { return { p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, GLOW_LIGHT_POSITION)), q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) }; - }; + } Flashlight.prototype = { @@ -73,13 +71,13 @@ whichHand: null, hasSpotlight: false, spotlight: null, - setRightHand: function() { + setRightHand: function () { this.hand = 'RIGHT'; }, - setLeftHand: function() { + setLeftHand: function () { this.hand = 'LEFT'; }, - startNearGrab: function() { + startNearGrab: function () { if (!this.hasSpotlight) { //this light casts the beam @@ -125,10 +123,10 @@ } }, - setWhichHand: function() { + setWhichHand: function () { this.whichHand = this.hand; }, - continueNearGrab: function() { + continueNearGrab: function () { if (this.whichHand === null) { //only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten this.setWhichHand(); @@ -137,7 +135,7 @@ this.changeLightWithTriggerPressure(this.whichHand); } }, - releaseGrab: function() { + releaseGrab: function () { //delete the lights and reset state if (this.hasSpotlight) { Entities.deleteEntity(this.spotlight); @@ -149,7 +147,7 @@ this.lightOn = false; } }, - updateLightPositions: function() { + updateLightPositions: function () { var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); //move the two lights along the vectors we set above @@ -161,16 +159,16 @@ Entities.editEntity(this.spotlight, { position: lightTransform.p, rotation: lightTransform.q, - }) + }); Entities.editEntity(this.glowLight, { position: glowLightTransform.p, rotation: glowLightTransform.q, - }) + }); }, - changeLightWithTriggerPressure: function(flashLightHand) { + changeLightWithTriggerPressure: function (flashLightHand) { var handClickString = flashLightHand + "_HAND_CLICK"; @@ -183,9 +181,9 @@ } else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) { this.turnLightOn(); } - return + return; }, - turnLightOff: function() { + turnLightOff: function () { this.playSoundAtCurrentPosition(false); Entities.editEntity(this.spotlight, { intensity: 0 @@ -193,11 +191,9 @@ Entities.editEntity(this.glowLight, { intensity: 0 }); - this.lightOn = false - - + this.lightOn = false; }, - turnLightOn: function() { + turnLightOn: function () { this.playSoundAtCurrentPosition(true); Entities.editEntity(this.glowLight, { @@ -206,35 +202,35 @@ Entities.editEntity(this.spotlight, { intensity: 2 }); - this.lightOn = true + this.lightOn = true; }, // preload() will be called when the entity has become visible (or known) to the interface // it gives us a chance to set our local JavaScript object up. In this case it means: // * remembering our entityID, so we can access it in cases where we're called without an entityID - preload: function(entityID) { + preload: function (entityID) { this.entityID = entityID; this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL); this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL); }, - playSoundAtCurrentPosition: function(playOnSound) { + playSoundAtCurrentPosition: function (playOnSound) { var position = Entities.getEntityProperties(this.entityID, "position").position; var audioProperties = { - volume: 0.5, + volume: 0.25, position: position - } - if (playOnSound) { - Audio.playSound(this.ON_SOUND, audioProperties) - } else { - Audio.playSound(this.OFF_SOUND, audioProperties) + }; + if (playOnSound) { + Audio.playSound(this.ON_SOUND, audioProperties); + } else { + Audio.playSound(this.OFF_SOUND, audioProperties); } }, // unload() will be called when our entity is no longer available. It may be because we were deleted, // or because we've left the domain or quit the application. - unload: function(entityID) { + unload: function () { if (this.hasSpotlight) { Entities.deleteEntity(this.spotlight); @@ -252,4 +248,4 @@ // entity scripts always need to return a newly constructed object of our type return new Flashlight(); -}) \ No newline at end of file +}); \ No newline at end of file From c1f8cbd1a3c34b26f69336e34a175065a82047c3 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Thu, 24 Sep 2015 14:07:15 -0700 Subject: [PATCH 3/3] Remove space before function calls, add spaces before functions --- examples/toys/flashlight/flashlight.js | 82 ++++++++++++-------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/examples/toys/flashlight/flashlight.js b/examples/toys/flashlight/flashlight.js index 5563001d07..9836579afd 100644 --- a/examples/toys/flashlight/flashlight.js +++ b/examples/toys/flashlight/flashlight.js @@ -15,9 +15,10 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // /*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ -(function () { +(function() { Script.include("../../libraries/utils.js"); + var ON_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_on.wav'; var OFF_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_off.wav'; @@ -31,22 +32,10 @@ var DISABLE_LIGHT_THRESHOLD = 0.7; // These constants define the Spotlight position and orientation relative to the model - var MODEL_LIGHT_POSITION = { - x: 0, - y: -0.3, - z: 0 - }; - var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { - x: 1, - y: 0, - z: 0 - }); + var MODEL_LIGHT_POSITION = { x: 0, y: -0.3, z: 0 }; + var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { x: 1, y: 0, z: 0 }); - var GLOW_LIGHT_POSITION = { - x: 0, - y: -0.1, - z: 0 - }; + var GLOW_LIGHT_POSITION = { x: 0, y: -0.1, z: 0}; // Evaluate the world light entity positions and orientations from the model ones function evalLightWorldTransform(modelPos, modelRot) { @@ -64,20 +53,21 @@ }; } - Flashlight.prototype = { lightOn: false, hand: null, whichHand: null, hasSpotlight: false, spotlight: null, - setRightHand: function () { + setRightHand: function() { this.hand = 'RIGHT'; }, - setLeftHand: function () { + + setLeftHand: function() { this.hand = 'LEFT'; }, - startNearGrab: function () { + + startNearGrab: function() { if (!this.hasSpotlight) { //this light casts the beam @@ -117,16 +107,17 @@ cutoff: 90, // in degrees }); - this.hasSpotlight = true; } }, - setWhichHand: function () { + + setWhichHand: function() { this.whichHand = this.hand; }, - continueNearGrab: function () { + + continueNearGrab: function() { if (this.whichHand === null) { //only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten this.setWhichHand(); @@ -135,7 +126,8 @@ this.changeLightWithTriggerPressure(this.whichHand); } }, - releaseGrab: function () { + + releaseGrab: function() { //delete the lights and reset state if (this.hasSpotlight) { Entities.deleteEntity(this.spotlight); @@ -147,29 +139,28 @@ this.lightOn = false; } }, - updateLightPositions: function () { + + updateLightPositions: function() { var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); //move the two lights along the vectors we set above var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation); var glowLightTransform = glowLightWorldTransform(modelProperties.position, modelProperties.rotation); - //move them with the entity model Entities.editEntity(this.spotlight, { position: lightTransform.p, rotation: lightTransform.q, }); - Entities.editEntity(this.glowLight, { position: glowLightTransform.p, rotation: glowLightTransform.q, }); }, - changeLightWithTriggerPressure: function (flashLightHand) { + changeLightWithTriggerPressure: function(flashLightHand) { var handClickString = flashLightHand + "_HAND_CLICK"; var handClick = Controller.findAction(handClickString); @@ -183,7 +174,8 @@ } return; }, - turnLightOff: function () { + + turnLightOff: function() { this.playSoundAtCurrentPosition(false); Entities.editEntity(this.spotlight, { intensity: 0 @@ -193,7 +185,8 @@ }); this.lightOn = false; }, - turnLightOn: function () { + + turnLightOn: function() { this.playSoundAtCurrentPosition(true); Entities.editEntity(this.glowLight, { @@ -204,16 +197,8 @@ }); this.lightOn = true; }, - // preload() will be called when the entity has become visible (or known) to the interface - // it gives us a chance to set our local JavaScript object up. In this case it means: - // * remembering our entityID, so we can access it in cases where we're called without an entityID - preload: function (entityID) { - this.entityID = entityID; - this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL); - this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL); - }, - playSoundAtCurrentPosition: function (playOnSound) { + playSoundAtCurrentPosition: function(playOnSound) { var position = Entities.getEntityProperties(this.entityID, "position").position; var audioProperties = { @@ -228,10 +213,21 @@ } }, - // unload() will be called when our entity is no longer available. It may be because we were deleted, - // or because we've left the domain or quit the application. - unload: function () { + preload: function(entityID) { + // preload() will be called when the entity has become visible (or known) to the interface + // it gives us a chance to set our local JavaScript object up. In this case it means: + // * remembering our entityID, so we can access it in cases where we're called without an entityID + // * preloading sounds + this.entityID = entityID; + this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL); + this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL); + + }, + + unload: function() { + // unload() will be called when our entity is no longer available. It may be because we were deleted, + // or because we've left the domain or quit the application. if (this.hasSpotlight) { Entities.deleteEntity(this.spotlight); Entities.deleteEntity(this.glowLight); @@ -242,8 +238,8 @@ this.lightOn = false; } - }, + }; // entity scripts always need to return a newly constructed object of our type