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.
// 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,34 +19,14 @@ 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
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);
// 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.
// 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() {
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
// 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;
// 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) {
@ -55,15 +44,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 = {
lightOn: false,
@ -74,11 +62,13 @@
setRightHand: function() {
this.hand = 'RIGHT';
},
setLeftHand: function() {
this.hand = 'LEFT';
},
startNearGrab: function() {
if (!_this.hasSpotlight) {
if (!this.hasSpotlight) {
//this light casts the beam
this.spotlight = Entities.addEntity({
@ -117,29 +107,16 @@
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;
}
},
setWhichHand: function() {
this.whichHand = this.hand;
},
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
@ -149,6 +126,7 @@
this.changeLightWithTriggerPressure(this.whichHand);
}
},
releaseGrab: function() {
//delete the lights and reset state
if (this.hasSpotlight) {
@ -158,9 +136,10 @@
this.glowLight = null;
this.spotlight = null;
this.whichHand = null;
this.lightOn = false;
}
},
updateLightPositions: function() {
var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']);
@ -168,27 +147,20 @@
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,
})
// Entities.editEntity(this.debugBox, {
// position: lightTransform.p,
// rotation: lightTransform.q,
// })
});
},
changeLightWithTriggerPressure: function(flashLightHand) {
changeLightWithTriggerPressure: function(flashLightHand) {
var handClickString = flashLightHand + "_HAND_CLICK";
var handClick = Controller.findAction(handClickString);
@ -200,37 +172,62 @@
} else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) {
this.turnLightOn();
}
return
return;
},
turnLightOff: function() {
this.playSoundAtCurrentPosition(false);
Entities.editEntity(this.spotlight, {
intensity: 0
});
Entities.editEntity(this.glowLight, {
intensity: 0
});
this.lightOn = false
this.lightOn = false;
},
turnLightOn: function() {
this.playSoundAtCurrentPosition(true);
Entities.editEntity(this.glowLight, {
intensity: 2
});
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
playSoundAtCurrentPosition: function(playOnSound) {
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() 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() 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() {
// 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);
@ -238,12 +235,13 @@
this.glowLight = null;
this.spotlight = null;
this.whichHand = null;
this.lightOn = false;
}
},
};
// entity scripts always need to return a newly constructed object of our type
return new Flashlight();
})
});