From 0733cd4bda86e20bac67546ab9e5c7ab5ac3d8e5 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 10 Mar 2016 17:09:16 -0800 Subject: [PATCH] plays sounds and stuff --- examples/controllers/handControllerGrab.js | 141 +++++++++++------- .../Home/musicBox/createMusicBox.js | 8 +- .../DomainContent/Home/musicBox/lid.js | 119 +++++++++++++-- 3 files changed, 204 insertions(+), 64 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 396dd169a0..e63f3b4e3f 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -44,8 +44,8 @@ var PICK_WITH_HAND_RAY = true; var DISTANCE_HOLDING_RADIUS_FACTOR = 3.5; // multiplied by distance between hand and object var DISTANCE_HOLDING_ACTION_TIMEFRAME = 0.1; // how quickly objects move to their new position -var DISTANCE_HOLDING_UNITY_MASS = 1200; // The mass at which the distance holding action timeframe is unmodified -var DISTANCE_HOLDING_UNITY_DISTANCE = 6; // The distance at which the distance holding action timeframe is unmodified +var DISTANCE_HOLDING_UNITY_MASS = 1200; // The mass at which the distance holding action timeframe is unmodified +var DISTANCE_HOLDING_UNITY_DISTANCE = 6; // The distance at which the distance holding action timeframe is unmodified var DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR = 2.0; // object rotates this much more than hand did var MOVE_WITH_HEAD = true; // experimental head-control of distantly held objects var FAR_TO_NEAR_GRAB_PADDING_FACTOR = 1.2; @@ -346,7 +346,7 @@ function MyController(hand) { this.grabSphereOff(); if (WANT_DEBUG || WANT_DEBUG_STATE) { print("STATE (" + this.hand + "): " + stateToName(this.state) + " --> " + - stateToName(newState) + ", hand: " + this.hand); + stateToName(newState) + ", hand: " + this.hand); } this.state = newState; }; @@ -425,11 +425,15 @@ function MyController(hand) { } this.grabSphereOn = function() { - var color = {red: 0, green: 255, blue: 0}; + var color = { + red: 0, + green: 255, + blue: 0 + }; if (this.grabSphere === null) { var sphereProperties = { position: this.getHandPosition(), - size: GRAB_RADIUS*2, + size: GRAB_RADIUS * 2, color: color, alpha: 0.1, solid: true, @@ -439,7 +443,7 @@ function MyController(hand) { } else { Overlays.editOverlay(this.grabSphere, { position: this.getHandPosition(), - size: GRAB_RADIUS*2, + size: GRAB_RADIUS * 2, color: color, alpha: 0.1, solid: true, @@ -492,12 +496,10 @@ function MyController(hand) { } var searchSphereLocation = Vec3.sum(distantPickRay.origin, - Vec3.multiply(distantPickRay.direction, this.searchSphereDistance)); - this.searchSphereOn(searchSphereLocation, SEARCH_SPHERE_SIZE * this.searchSphereDistance, - (this.triggerSmoothedGrab() || this.bumperSqueezed()) ? INTERSECT_COLOR : NO_INTERSECT_COLOR); + Vec3.multiply(distantPickRay.direction, this.searchSphereDistance)); + this.searchSphereOn(searchSphereLocation, SEARCH_SPHERE_SIZE * this.searchSphereDistance, (this.triggerSmoothedGrab() || this.bumperSqueezed()) ? INTERSECT_COLOR : NO_INTERSECT_COLOR); if ((USE_OVERLAY_LINES_FOR_SEARCHING === true) && PICK_WITH_HAND_RAY) { - this.overlayLineOn(handPosition, searchSphereLocation, - (this.triggerSmoothedGrab() || this.bumperSqueezed()) ? INTERSECT_COLOR : NO_INTERSECT_COLOR); + this.overlayLineOn(handPosition, searchSphereLocation, (this.triggerSmoothedGrab() || this.bumperSqueezed()) ? INTERSECT_COLOR : NO_INTERSECT_COLOR); } } @@ -824,8 +826,8 @@ function MyController(hand) { var distantPickRay = { origin: PICK_WITH_HAND_RAY ? handPosition : Camera.position, direction: PICK_WITH_HAND_RAY ? Quat.getUp(this.getHandRotation()) : Vec3.mix(Quat.getUp(this.getHandRotation()), - Quat.getFront(Camera.orientation), - HAND_HEAD_MIX_RATIO), + Quat.getFront(Camera.orientation), + HAND_HEAD_MIX_RATIO), length: PICK_MAX_DISTANCE }; @@ -1149,9 +1151,9 @@ function MyController(hand) { // double delta controller rotation var handChange = Quat.multiply(Quat.slerp(this.previousControllerRotation, - controllerRotation, - DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR), - Quat.inverse(this.previousControllerRotation)); + controllerRotation, + DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR), + Quat.inverse(this.previousControllerRotation)); // update the currentObject position and rotation. this.currentObjectPosition = Vec3.sum(this.currentObjectPosition, handMoved); @@ -1279,7 +1281,9 @@ function MyController(hand) { }; this.hasPresetOffsets = function() { - var wearableData = getEntityCustomData('wearable', this.grabbedEntity, {joints: {}}); + var wearableData = getEntityCustomData('wearable', this.grabbedEntity, { + joints: {} + }); if ("joints" in wearableData) { var allowedJoints = wearableData.joints; var handJointName = this.hand === RIGHT_HAND ? "RightHand" : "LeftHand"; @@ -1291,7 +1295,9 @@ function MyController(hand) { } this.getPresetPosition = function() { - var wearableData = getEntityCustomData('wearable', this.grabbedEntity, {joints: {}}); + var wearableData = getEntityCustomData('wearable', this.grabbedEntity, { + joints: {} + }); var allowedJoints = wearableData.joints; var handJointName = this.hand === RIGHT_HAND ? "RightHand" : "LeftHand"; if (handJointName in allowedJoints) { @@ -1300,7 +1306,9 @@ function MyController(hand) { } this.getPresetRotation = function() { - var wearableData = getEntityCustomData('wearable', this.grabbedEntity, {joints: {}}); + var wearableData = getEntityCustomData('wearable', this.grabbedEntity, { + joints: {} + }); var allowedJoints = wearableData.joints; var handJointName = this.hand === RIGHT_HAND ? "RightHand" : "LeftHand"; if (handJointName in allowedJoints) { @@ -1319,12 +1327,12 @@ function MyController(hand) { roll: false } } - var constraints = getEntityCustomData(GRAB_CONSTRAINTS_USER_DATA_KEY, this.grabbedEntity,defaultConstraints); + var constraints = getEntityCustomData(GRAB_CONSTRAINTS_USER_DATA_KEY, this.grabbedEntity, defaultConstraints); return constraints; } this.nearGrabbing = function() { - print('NEAR GRAB') + print('NEAR GRAB') var now = Date.now(); var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); @@ -1451,7 +1459,7 @@ function MyController(hand) { Vec3.length(props.localPosition) > NEAR_PICK_MAX_DISTANCE * 2.0) { // for whatever reason, the held/equipped entity has been pulled away. ungrab or unequip. print("handControllerGrab -- autoreleasing held or equipped item because it is far from hand." + - props.parentID + " " + vec3toStr(props.position)); + props.parentID + " " + vec3toStr(props.position)); this.setState(STATE_RELEASE); this.callEntityMethodOnGrabbed(this.state == STATE_NEAR_GRABBING ? "releaseGrab" : "releaseEquip"); return; @@ -1483,10 +1491,10 @@ function MyController(hand) { var constraints = this.getGrabConstraints(); - if(constraints.positionLocked===true){ + if (constraints.positionLocked === true) { print('IT HAS ITS POSITION LOCKED!!') } - if(constraints.rotationMod!==false){ + if (constraints.rotationMod !== false) { print('IT HAS A ROTATION MOD!!!') } @@ -1529,7 +1537,7 @@ function MyController(hand) { } this.callEntityMethodOnGrabbed("startNearTrigger"); this.setState(STATE_CONTINUE_NEAR_TRIGGER); - print('START NEAR TRIGGER') + print('START NEAR TRIGGER') }; @@ -1551,8 +1559,8 @@ function MyController(hand) { return; } - - var constraints = this.getGrabConstraints(); + + var constraints = this.getGrabConstraints(); if (constraints.rotationMod !== false) { //implement the rotation modifier var grabbedProps = Entities.getEntityProperties(this.grabbedEntity); @@ -1561,9 +1569,7 @@ function MyController(hand) { var modTypes = []; - print('rotation constrained') if (constraints.rotationMod.pitch !== false) { - print('HAS PITCH MOD') modTypes.push('pitch') } if (constraints.rotationMod.yaw !== false) { @@ -1585,14 +1591,16 @@ function MyController(hand) { var min1 = constraints.rotationMod[modType].startingPoint; var finalAngle = scale(value, min1, constraints.rotationMod[modType].distanceToMax, constraints.rotationMod[modType].min, constraints.rotationMod[modType].max) - print('VARS: ') - print('CONSTRAINTS:: ' + JSON.stringify(constraints)) - print('value: ' + value) - print('min1:' + min1) - print('max1:' + constraints.rotationMod[modType].distanceToMax) - print('min2: ' + constraints.rotationMod[modType].min) - print('max2: ' + constraints.rotationMod[modType].max) - print('FINAL ANGLE::' + finalAngle) + // print('VARS: ') + // print('CONSTRAINTS:: ' + JSON.stringify(constraints)) + // print('value: ' + value) + // print('min1:' + min1) + // print('max1:' + constraints.rotationMod[modType].distanceToMax) + // print('min2: ' + constraints.rotationMod[modType].min) + // print('max2: ' + constraints.rotationMod[modType].max) + // print('FINAL ANGLE::' + finalAngle) + + if (finalAngle < constraints.rotationMod[modType].min) { finalAngle = constraints.rotationMod[modType].min; } @@ -1611,11 +1619,8 @@ function MyController(hand) { finalRotation.z = finalAngle } }); - print('FINAL ROTATION::' + JSON.stringify(finalRotation)) - Entities.editEntity(this.grabbedEntity, { - rotation: Quat.fromPitchYawRollDegrees(finalRotation.x, finalRotation.y, finalRotation.z) - }) + Entities.callEntityMethod(this.grabbedEntity, constraints.callback, [JSON.stringify(finalRotation)]); } this.callEntityMethodOnGrabbed("continueNearTrigger"); @@ -1731,7 +1736,7 @@ function MyController(hand) { // sometimes we want things to stay right where they are when we let go. var grabData = getEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, {}); var releaseVelocityData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); - print('RELEASE DATA::'+ JSON.stringify(releaseVelocityData)) + print('RELEASE DATA::' + JSON.stringify(releaseVelocityData)) if (releaseVelocityData.disableReleaseVelocity === true || // this next line allowed both: // (1) far-grab, pull to self, near grab, then throw @@ -1833,7 +1838,9 @@ function MyController(hand) { // people are holding something and one of them will be able (if the other releases at the right time) to // bootstrap themselves with the held object. This happens because the meaning of "otherAvatar" in // the collision mask hinges on who the physics simulation owner is. - Entities.editEntity(entityID, {"collidesWith": COLLIDES_WITH_WHILE_MULTI_GRABBED}); + Entities.editEntity(entityID, { + "collidesWith": COLLIDES_WITH_WHILE_MULTI_GRABBED + }); } } print('ACTIVATED ENTITY!!!') @@ -1848,7 +1855,9 @@ function MyController(hand) { var children = Entities.getChildrenIDsOfJoint(MyAvatar.sessionUUID, handJointIndex); children.forEach(function(childID) { print("disconnecting stray child of hand: (" + _this.hand + ") " + childID); - Entities.editEntity(childID, {parentID: NULL_UUID}); + Entities.editEntity(childID, { + parentID: NULL_UUID + }); }); } @@ -1876,11 +1885,23 @@ function MyController(hand) { data["dynamic"] && data["parentID"] == NULL_UUID && !data["collisionless"]) { - deactiveProps["velocity"] = {x: 0.0, y: 0.1, z: 0.0}; + deactiveProps["velocity"] = { + x: 0.0, + y: 0.1, + z: 0.0 + }; } if (noVelocity) { - deactiveProps["velocity"] = {x: 0.0, y: 0.0, z: 0.0}; - deactiveProps["angularVelocity"] = {x: 0.0, y: 0.0, z: 0.0}; + deactiveProps["velocity"] = { + x: 0.0, + y: 0.0, + z: 0.0 + }; + deactiveProps["angularVelocity"] = { + x: 0.0, + y: 0.0, + z: 0.0 + }; } Entities.editEntity(entityID, deactiveProps); @@ -1890,13 +1911,31 @@ function MyController(hand) { var deactiveProps = { parentID: this.previousParentID, parentJointIndex: this.previousParentJointIndex, - velocity: {x: 0.0, y: 0.0, z: 0.0}, - angularVelocity: {x: 0.0, y: 0.0, z: 0.0} + velocity: { + x: 0.0, + y: 0.0, + z: 0.0 + }, + angularVelocity: { + x: 0.0, + y: 0.0, + z: 0.0 + } }; Entities.editEntity(entityID, deactiveProps); } else if (noVelocity) { - Entities.editEntity(entityID, {velocity: {x: 0.0, y: 0.0, z: 0.0}, - angularVelocity: {x: 0.0, y: 0.0, z: 0.0}}); + Entities.editEntity(entityID, { + velocity: { + x: 0.0, + y: 0.0, + z: 0.0 + }, + angularVelocity: { + x: 0.0, + y: 0.0, + z: 0.0 + } + }); } } else { data = null; diff --git a/unpublishedScripts/DomainContent/Home/musicBox/createMusicBox.js b/unpublishedScripts/DomainContent/Home/musicBox/createMusicBox.js index c041ff4cef..43abe429bb 100644 --- a/unpublishedScripts/DomainContent/Home/musicBox/createMusicBox.js +++ b/unpublishedScripts/DomainContent/Home/musicBox/createMusicBox.js @@ -56,6 +56,8 @@ var LID_REGISTRATION_POINT = { z: 1 } +var LID_SCRIPT_URL = Script.resolvePath('lid.js?'+Math.random()); + var base, lid; @@ -84,9 +86,11 @@ function createLid() { position: lidPosition, registrationPoint: LID_REGISTRATION_POINT, dynamic: false, - collidesWith: '', + script:LID_SCRIPT_URL, + collidesWith: 'myAvatar,otherAvatar', userData: JSON.stringify({ grabConstraintsKey: { + callback:'rotateLid', positionLocked: true, rotationLocked: false, positionMod: false, @@ -97,8 +101,6 @@ function createLid() { startingAxis:'y', startingPoint:lidPosition.y, distanceToMax:lidPosition.y+0.35, - trigger:20, - triggerFunction:'toggleMusic' }, yaw: false, roll: false diff --git a/unpublishedScripts/DomainContent/Home/musicBox/lid.js b/unpublishedScripts/DomainContent/Home/musicBox/lid.js index 928dfcc1e9..41c59a24be 100644 --- a/unpublishedScripts/DomainContent/Home/musicBox/lid.js +++ b/unpublishedScripts/DomainContent/Home/musicBox/lid.js @@ -7,23 +7,122 @@ return this; } + var MUSIC_URL = 'https://hifi-content.s3.amazonaws.com/DomainContent/Home/Sounds/aquarium_small.L.wav'; + var SHUT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/DomainContent/Home/Sounds/book_fall.L.wav'; + var OPEN_SOUND_URL = 'http://public.highfidelity.io/sounds/Switches%20and%20sliders/lamp_switch_2.wav' + Lid.prototype = { - preload: function() { + preload: function(entityID) { + print('PRELOAD LID') + _this.entityID = entityID; + _this.music = SoundCache.getSound(MUSIC_URL); + _this.shutSound = SoundCache.getSound(SHUT_SOUND_URL); + _this.openSound = SoundCache.getSound(OPEN_SOUND_URL); + _this.musicIsPlaying = false; + _this.shut = true; + _this.shutSoundInjector = { + isPlaying: false + }; + _this.musicInjector = null; + _this.openSoundInjector = { + isPlaying: false + } + }, - initialize: function() { + continueNearTrigger: function() { + var properties = Entities.getEntityProperties(this.entityID); + + }, + playMusic: function() { + if (this.musicIsPlaying !== true) { + + var properties = Entities.getEntityProperties(this.entityID); + + var audioOptions = { + position: properties.position, + volume: 0.75, + loop: true + } + this.musicInjector = Audio.playSound(this.music, audioOptions); + this.musicIsPlaying = true; + } + }, + + stopMusic: function() { + this.musicInjector.stop(); + this.musicIsPlaying = false; + }, + + playOpenSound: function() { + if (this.openSoundInjector.isPlaying !== true) { + + var properties = Entities.getEntityProperties(this.entityID); + + var audioOptions = { + position: properties.position, + volume: 1, + loop: false + } + this.openSoundInjector = Audio.playSound(this.openSound, audioOptions); + } + }, + playShutSound: function() { + if (this.shutSoundInjector.isPlaying !== true) { + + var properties = Entities.getEntityProperties(this.entityID); + + var audioOptions = { + position: properties.position, + volume: 1, + loop: false + } + this.shutSoundInjector = Audio.playSound(this.shutSound, audioOptions); + } + }, + rotateLid: function(myID, paramsArray) { + + var finalRotation; + paramsArray.forEach(function(param) { + var p; + // print('param is:' + param) + try { + p = JSON.parse(param); + finalRotation = p; + + } catch (err) { + // print('not a json param') + return; + p = param; + } + + }); + + if (finalRotation.x > 20 && this.musicIsPlaying === false) { + this.playMusic(); + print('play music!!') + } + if (finalRotation.x <= 20 && this.musicIsPlaying === true) { + print('stop music!!') + this.stopMusic(); + } + if (finalRotation.x > 0 && this.shut === true) { + print('play open sound!!') + this.shut = false; + this.playOpenSound(); + } else if (finalRotation.x <= 0 && this.shut === false) { + print('play shut sound!!') + this.shut = true; + this.playShutSound(); + } + Entities.editEntity(this.entityID, { + rotation: Quat.fromPitchYawRollDegrees(finalRotation.x, finalRotation.y, finalRotation.z) + }) }, - startNearGrab: function() { - - }, - - releaseGrab: function() { - - }, - unload: function() { + this.musicInjector.stop(); }, }