Merge pull request #5882 from imgntn/flashlight_updates_2

[Scripts] Flashlight Updates
This commit is contained in:
Brad Hefta-Gaub 2015-09-24 15:56:22 -07:00
commit be4fda3493
2 changed files with 69 additions and 91 deletions

View file

@ -11,7 +11,7 @@
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // 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"); Script.include("https://hifi-public.s3.amazonaws.com/scripts/utilities.js");
@ -19,34 +19,14 @@ var scriptURL = Script.resolvePath('flashlight.js?123123');
var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx"; var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx";
var center = Vec3.sum(Vec3.sum(MyAvatar.position, { var center = Vec3.sum(Vec3.sum(MyAvatar.position, {x: 0, y: 0.5, z: 0}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
x: 0,
y: 0.5,
z: 0
}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
var flashlight = Entities.addEntity({ var flashlight = Entities.addEntity({
type: "Model", type: "Model",
modelURL: modelURL, modelURL: modelURL,
position: center, position: center,
dimensions: { dimensions: { x: 0.08, y: 0.30, z: 0.08},
x: 0.08, collisionsWillMove: true,
y: 0.30, shapeType: 'box',
z: 0.08 script: scriptURL
},
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);
// Close script after running once to prevent a flashlight from being created each time you start Interface
Script.stop();

View file

@ -14,39 +14,28 @@
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // 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"); Script.include("../../libraries/utils.js");
var _this; 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';
// this is the "constructor" for the entity as a JS object we don't do much here, but we do want to remember // 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) // 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() { function Flashlight() {
_this = this; return;
}; }
//if the trigger value goes below this while held, the flashlight will turn off. if it goes above, it will //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; var DISABLE_LIGHT_THRESHOLD = 0.7;
// These constants define the Spotlight position and orientation relative to the model // These constants define the Spotlight position and orientation relative to the model
var MODEL_LIGHT_POSITION = { var MODEL_LIGHT_POSITION = { x: 0, y: -0.3, z: 0 };
x: 0, var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { x: 1, y: 0, z: 0 });
y: -0.3,
z: 0
};
var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, {
x: 1,
y: 0,
z: 0
});
var GLOW_LIGHT_POSITION = { var GLOW_LIGHT_POSITION = { x: 0, y: -0.1, z: 0};
x: 0,
y: -0.1,
z: 0
}
// Evaluate the world light entity positions and orientations from the model ones // Evaluate the world light entity positions and orientations from the model ones
function evalLightWorldTransform(modelPos, modelRot) { function evalLightWorldTransform(modelPos, modelRot) {
@ -55,15 +44,14 @@
p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)), p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)),
q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION)
}; };
}; }
function glowLightWorldTransform(modelPos, modelRot) { function glowLightWorldTransform(modelPos, modelRot) {
return { return {
p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, GLOW_LIGHT_POSITION)), p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, GLOW_LIGHT_POSITION)),
q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION)
}; };
}; }
Flashlight.prototype = { Flashlight.prototype = {
lightOn: false, lightOn: false,
@ -74,11 +62,13 @@
setRightHand: function() { setRightHand: function() {
this.hand = 'RIGHT'; this.hand = 'RIGHT';
}, },
setLeftHand: function() { setLeftHand: function() {
this.hand = 'LEFT'; this.hand = 'LEFT';
}, },
startNearGrab: function() { startNearGrab: function() {
if (!_this.hasSpotlight) { if (!this.hasSpotlight) {
//this light casts the beam //this light casts the beam
this.spotlight = Entities.addEntity({ this.spotlight = Entities.addEntity({
@ -117,29 +107,16 @@
cutoff: 90, // in degrees 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; this.hasSpotlight = true;
} }
}, },
setWhichHand: function() { setWhichHand: function() {
this.whichHand = this.hand; this.whichHand = this.hand;
}, },
continueNearGrab: function() { continueNearGrab: function() {
if (this.whichHand === null) { if (this.whichHand === null) {
//only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten //only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten
@ -149,6 +126,7 @@
this.changeLightWithTriggerPressure(this.whichHand); this.changeLightWithTriggerPressure(this.whichHand);
} }
}, },
releaseGrab: function() { releaseGrab: function() {
//delete the lights and reset state //delete the lights and reset state
if (this.hasSpotlight) { if (this.hasSpotlight) {
@ -158,9 +136,10 @@
this.glowLight = null; this.glowLight = null;
this.spotlight = null; this.spotlight = null;
this.whichHand = null; this.whichHand = null;
this.lightOn = false;
} }
}, },
updateLightPositions: function() { updateLightPositions: function() {
var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']);
@ -168,27 +147,20 @@
var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation); var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation);
var glowLightTransform = glowLightWorldTransform(modelProperties.position, modelProperties.rotation); var glowLightTransform = glowLightWorldTransform(modelProperties.position, modelProperties.rotation);
//move them with the entity model //move them with the entity model
Entities.editEntity(this.spotlight, { Entities.editEntity(this.spotlight, {
position: lightTransform.p, position: lightTransform.p,
rotation: lightTransform.q, rotation: lightTransform.q,
}) });
Entities.editEntity(this.glowLight, { Entities.editEntity(this.glowLight, {
position: glowLightTransform.p, position: glowLightTransform.p,
rotation: glowLightTransform.q, rotation: glowLightTransform.q,
}) });
// Entities.editEntity(this.debugBox, {
// position: lightTransform.p,
// rotation: lightTransform.q,
// })
}, },
changeLightWithTriggerPressure: function(flashLightHand) {
changeLightWithTriggerPressure: function(flashLightHand) {
var handClickString = flashLightHand + "_HAND_CLICK"; var handClickString = flashLightHand + "_HAND_CLICK";
var handClick = Controller.findAction(handClickString); var handClick = Controller.findAction(handClickString);
@ -200,37 +172,62 @@
} else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) { } else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) {
this.turnLightOn(); this.turnLightOn();
} }
return return;
}, },
turnLightOff: function() { turnLightOff: function() {
this.playSoundAtCurrentPosition(false);
Entities.editEntity(this.spotlight, { Entities.editEntity(this.spotlight, {
intensity: 0 intensity: 0
}); });
Entities.editEntity(this.glowLight, { Entities.editEntity(this.glowLight, {
intensity: 0 intensity: 0
}); });
this.lightOn = false this.lightOn = false;
}, },
turnLightOn: function() { turnLightOn: function() {
this.playSoundAtCurrentPosition(true);
Entities.editEntity(this.glowLight, { Entities.editEntity(this.glowLight, {
intensity: 2 intensity: 2
}); });
Entities.editEntity(this.spotlight, { Entities.editEntity(this.spotlight, {
intensity: 2 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: playSoundAtCurrentPosition: function(playOnSound) {
// * remembering our entityID, so we can access it in cases where we're called without an entityID var position = Entities.getEntityProperties(this.entityID, "position").position;
var audioProperties = {
volume: 0.25,
position: position
};
if (playOnSound) {
Audio.playSound(this.ON_SOUND, audioProperties);
} else {
Audio.playSound(this.OFF_SOUND, audioProperties);
}
},
preload: function(entityID) { 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.entityID = entityID;
this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL);
this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL);
}, },
// unload() will be called when our entity is no longer available. It may be because we were deleted, unload: function() {
// or because we've left the domain or quit the application. // unload() will be called when our entity is no longer available. It may be because we were deleted,
unload: function(entityID) { // or because we've left the domain or quit the application.
if (this.hasSpotlight) { if (this.hasSpotlight) {
Entities.deleteEntity(this.spotlight); Entities.deleteEntity(this.spotlight);
Entities.deleteEntity(this.glowLight); Entities.deleteEntity(this.glowLight);
@ -238,12 +235,13 @@
this.glowLight = null; this.glowLight = null;
this.spotlight = null; this.spotlight = null;
this.whichHand = null; this.whichHand = null;
this.lightOn = false;
} }
}, },
}; };
// entity scripts always need to return a newly constructed object of our type // entity scripts always need to return a newly constructed object of our type
return new Flashlight(); return new Flashlight();
}) });