diff --git a/examples/airGuitar.js b/examples/airGuitar.js index 08898579a7..b7c4b72948 100644 --- a/examples/airGuitar.js +++ b/examples/airGuitar.js @@ -25,22 +25,24 @@ function vMinus(a, b) { return rval; } -// First, load two percussion sounds to be used on the sticks +// The model file to be used for the guitar +var guitarModel = "https://s3-us-west-1.amazonaws.com/highfidelity-public/models/attachments/guitar.fst"; -var guitarType = 2; +// Load sounds that will be played -if (guitarType == 1) { - var chord1 = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+A.raw"); - var chord2 = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+B.raw"); - var chord3 = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+E.raw"); -} else { - var chord1 = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Metal+A+short.raw"); - var chord2 = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Metal+B+short.raw"); - var chord3 = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Metal+E+short.raw"); -} +var chords = new Array(); +// Nylon string guitar +chords[1] = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+A.raw"); +chords[2] = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+B.raw"); +chords[3] = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+E.raw"); +// Electric guitar +chords[4] = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Metal+A+short.raw"); +chords[5] = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Metal+B+short.raw"); +chords[6] = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Metal+E+short.raw"); +var guitarSelector = 3; -var whichChord = chord1; +var whichChord = chords[guitarSelector + 1]; var leftHanded = false; if (leftHanded) { @@ -55,42 +57,74 @@ var lastPosition = { x: 0.0, y: 0.0, z: 0.0 }; +var soundPlaying = false; +var selectorPressed = false; + +MyAvatar.attach(guitarModel, "Hips", {x: -0.0, y: -0.0, z: 0.0}, Quat.fromPitchYawRollDegrees(0, 0, 0), 1.0); function checkHands(deltaTime) { for (var palm = 0; palm < 2; palm++) { var palmVelocity = Controller.getSpatialControlVelocity(palm * 2 + 1); - var speed = length(palmVelocity); + var speed = length(palmVelocity) / 4.0; var position = Controller.getSpatialControlPosition(palm * 2 + 1); var myPelvis = MyAvatar.position; + var trigger = Controller.getTriggerValue(strumHand); + var chord = Controller.getTriggerValue(chordHand); + + if ((chord > 0.1) && Audio.isInjectorPlaying(soundPlaying)) { + // If chord finger trigger pulled, stop current chord + Audio.stopInjector(soundPlaying); + } + + var BUTTON_COUNT = 6; + + // Change guitars if button FWD (5) pressed + if (Controller.isButtonPressed(chordHand * BUTTON_COUNT + 5)) { + if (!selectorPressed) { + if (guitarSelector == 0) { + guitarSelector = 3; + } else { + guitarSelector = 0; + } + selectorPressed = true; + } + } else { + selectorPressed = false; + } + + if (Controller.isButtonPressed(chordHand * BUTTON_COUNT + 1)) { + whichChord = chords[guitarSelector + 1]; + } else if (Controller.isButtonPressed(chordHand * BUTTON_COUNT + 2)) { + whichChord = chords[guitarSelector + 2]; + } else if (Controller.isButtonPressed(chordHand * BUTTON_COUNT + 3)) { + whichChord = chords[guitarSelector + 3]; + } if (palm == strumHand) { - var STRUM_HEIGHT_ABOVE_PELVIS = -0.30; + var STRUM_HEIGHT_ABOVE_PELVIS = 0.00; var strumTriggerHeight = myPelvis.y + STRUM_HEIGHT_ABOVE_PELVIS; //printVector(position); - if ((position.y < strumTriggerHeight) && (lastPosition.y >= strumTriggerHeight)) { - // If hand passes downward through guitar strings, play a chord! + if ( ( ((position.y < strumTriggerHeight) && (lastPosition.y >= strumTriggerHeight)) || + ((position.y > strumTriggerHeight) && (lastPosition.y <= strumTriggerHeight)) ) && (trigger > 0.1) ){ + // If hand passes downward or upward through 'strings', and finger trigger pulled, play var options = new AudioInjectionOptions(); options.position = position; if (speed > 1.0) { speed = 1.0; } options.volume = speed; - Audio.playSound(whichChord, options); + if (Audio.isInjectorPlaying(soundPlaying)) { + Audio.stopInjector(soundPlaying); + } + soundPlaying = Audio.playSound(whichChord, options); } lastPosition = Controller.getSpatialControlPosition(palm * 2 + 1); - } else { - // This is the chord controller - var distanceFromPelvis = Vec3.length(Vec3.subtract(position, myPelvis)); - //print(distanceFromPelvis); - if (distanceFromPelvis > 0.63) { - whichChord = chord3; - } else if (distanceFromPelvis > 0.55) { - whichChord = chord2; - } else { - whichChord = chord1; - } - } + } } } +function scriptEnding() { + MyAvatar.detachOne(guitarModel); +} // Connect a call back that happens every frame -Script.update.connect(checkHands); \ No newline at end of file +Script.update.connect(checkHands); +Script.scriptEnding.connect(scriptEnding); diff --git a/examples/gun.js b/examples/gun.js index 6cdf634239..e404ae1d4d 100644 --- a/examples/gun.js +++ b/examples/gun.js @@ -28,7 +28,7 @@ var BULLET_VELOCITY = 5.0; var MIN_THROWER_DELAY = 1000; var MAX_THROWER_DELAY = 1000; var LEFT_BUTTON_3 = 3; -var RELOAD_INTERVAL = 9; +var RELOAD_INTERVAL = 5; var showScore = false; @@ -92,12 +92,12 @@ function printVector(string, vector) { } function shootBullet(position, velocity) { - var BULLET_SIZE = 0.02; + var BULLET_SIZE = 0.01; var BULLET_GRAVITY = -0.02; Particles.addParticle( { position: position, radius: BULLET_SIZE, - color: { red: 200, green: 0, blue: 0 }, + color: { red: 10, green: 10, blue: 10 }, velocity: velocity, gravity: { x: 0, y: BULLET_GRAVITY, z: 0 }, damping: 0 }); @@ -187,12 +187,23 @@ function keyPressEvent(event) { if (event.text == "t") { var time = MIN_THROWER_DELAY + Math.random() * MAX_THROWER_DELAY; Script.setTimeout(shootTarget, time); - } if (event.text == ".") { + } else if (event.text == ".") { shootFromMouse(); + } else if (event.text == "r") { + playLoadSound(); } } -MyAvatar.attach(gunModel, "RightHand", {x: -0.10, y: 0.0, z: 0.0}, Quat.fromPitchYawRollDegrees(-90, 180, 0), 0.14); +function playLoadSound() { + audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())); + Audio.playSound(loadSound, audioOptions); +} + +MyAvatar.attach(gunModel, "RightHand", {x: -0.02, y: -.14, z: 0.07}, Quat.fromPitchYawRollDegrees(-70, -151, 72), 0.20); +MyAvatar.attach(gunModel, "LeftHand", {x: -0.02, y: -.14, z: 0.07}, Quat.fromPitchYawRollDegrees(-70, -151, 72), 0.20); + +// Give a bit of time to load before playing sound +Script.setTimeout(playLoadSound, 2000); function update(deltaTime) { // Check for mouseLook movement, update rotation @@ -309,6 +320,7 @@ function scriptEnding() { Overlays.deleteOverlay(reticle); Overlays.deleteOverlay(text); MyAvatar.detachOne(gunModel); + MyAvatar.detachOne(gunModel); } Particles.particleCollisionWithVoxel.connect(particleCollisionWithVoxel);