mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-13 22:49:57 +02:00
JS Linting and Coding Standards
This commit is contained in:
parent
561b4ca8e1
commit
3e3d7a8bfa
2 changed files with 60 additions and 74 deletions
|
@ -8,6 +8,7 @@
|
|||
// Loads a wand model and attaches the bubble wand behavior.
|
||||
// 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("../../utilities.js");
|
||||
Script.include("../../libraries/utils.js");
|
||||
|
@ -17,37 +18,26 @@ var WAND_COLLISION_SHAPE = 'http://hifi-public.s3.amazonaws.com/james/bubblewand
|
|||
var WAND_SCRIPT_URL = Script.resolvePath("wand.js");
|
||||
|
||||
//create the wand in front of the avatar
|
||||
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 wand = Entities.addEntity({
|
||||
name: 'Bubble Wand',
|
||||
type: "Model",
|
||||
modelURL: WAND_MODEL,
|
||||
position: center,
|
||||
gravity: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0,
|
||||
},
|
||||
dimensions: {
|
||||
x: 0.05,
|
||||
y: 0.25,
|
||||
z: 0.05
|
||||
},
|
||||
//must be enabled to be grabbable in the physics engine
|
||||
collisionsWillMove: true,
|
||||
compoundShapeURL: WAND_COLLISION_SHAPE,
|
||||
script: WAND_SCRIPT_URL
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
// the line below this is commented out to make the wand that you create persistent.
|
||||
// Entities.deleteEntity(wand);
|
||||
}
|
||||
|
||||
|
||||
Script.scriptEnding.connect(cleanup);
|
||||
name: 'Bubble Wand',
|
||||
type: "Model",
|
||||
modelURL: WAND_MODEL,
|
||||
position: center,
|
||||
gravity: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0,
|
||||
},
|
||||
dimensions: {
|
||||
x: 0.05,
|
||||
y: 0.25,
|
||||
z: 0.05
|
||||
},
|
||||
//must be enabled to be grabbable in the physics engine
|
||||
collisionsWillMove: true,
|
||||
compoundShapeURL: WAND_COLLISION_SHAPE,
|
||||
script: WAND_SCRIPT_URL
|
||||
});
|
|
@ -10,8 +10,9 @@
|
|||
// 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 */
|
||||
|
||||
(function() {
|
||||
(function () {
|
||||
|
||||
Script.include("../../utilities.js");
|
||||
Script.include("../../libraries/utils.js");
|
||||
|
@ -22,20 +23,22 @@
|
|||
x: 0.01,
|
||||
y: 0.01,
|
||||
z: 0.01
|
||||
}
|
||||
};
|
||||
|
||||
var BUBBLE_LIFETIME_MIN = 3;
|
||||
var BUBBLE_LIFETIME_MAX = 8;
|
||||
var BUBBLE_SIZE_MIN = 1;
|
||||
var BUBBLE_SIZE_MAX = 5;
|
||||
var BUBBLE_DIVISOR = 50;
|
||||
var BUBBLE_SIZE_MIN = 0.02;
|
||||
var BUBBLE_SIZE_MAX = 0.1;
|
||||
var BUBBLE_LINEAR_DAMPING = 0.4;
|
||||
var BUBBLE_GRAVITY_MIN = 0.1;
|
||||
var BUBBLE_GRAVITY_MAX = 0.3;
|
||||
var GROWTH_FACTOR = 0.005;
|
||||
var SHRINK_FACTOR = 0.001;
|
||||
var SHRINK_LOWER_LIMIT = 0.02;
|
||||
var WAND_TIP_OFFSET = 0.095;
|
||||
var VELOCITY_THRESHOLD = 0.5;
|
||||
|
||||
//this helps us get the time passed since the last function call, for use in velocity calculations
|
||||
function interval() {
|
||||
var lastTime = new Date().getTime() / 1000;
|
||||
|
||||
|
@ -49,53 +52,48 @@
|
|||
|
||||
var checkInterval = interval();
|
||||
|
||||
var _this;
|
||||
|
||||
var BubbleWand = function() {
|
||||
_this = this;
|
||||
function BubbleWand() {
|
||||
return;
|
||||
}
|
||||
|
||||
BubbleWand.prototype = {
|
||||
timePassed: null,
|
||||
currentBubble: null,
|
||||
preload: function(entityID) {
|
||||
preload: function (entityID) {
|
||||
this.entityID = entityID;
|
||||
},
|
||||
getWandTipPosition: function(properties) {
|
||||
getWandTipPosition: function (properties) {
|
||||
//the tip of the wand is going to be in a different place than the center, so we move in space relative to the model to find that position
|
||||
var upVector = Quat.getUp(properties.rotation);
|
||||
var frontVector = Quat.getFront(properties.rotation);
|
||||
var upOffset = Vec3.multiply(upVector, WAND_TIP_OFFSET);
|
||||
var wandTipPosition = Vec3.sum(properties.position, upOffset);
|
||||
return wandTipPosition
|
||||
return wandTipPosition;
|
||||
},
|
||||
addCollisionsToBubbleAfterCreation: function(bubble) {
|
||||
addCollisionsToBubbleAfterCreation: function (bubble) {
|
||||
//if the bubble collide immediately, we get weird effects. so we add collisions after release
|
||||
Entities.editEntity(bubble, {
|
||||
collisionsWillMove: true
|
||||
})
|
||||
});
|
||||
},
|
||||
randomizeBubbleGravity: function() {
|
||||
randomizeBubbleGravity: function () {
|
||||
//change up the gravity a little bit for variation in floating effects
|
||||
var randomNumber = randInt(0, 3);
|
||||
var randomNumber = randFloat(BUBBLE_GRAVITY_MIN, BUBBLE_GRAVITY_MAX);
|
||||
var gravity = {
|
||||
x: 0,
|
||||
y: -randomNumber / 10,
|
||||
y: -randomNumber,
|
||||
z: 0
|
||||
}
|
||||
return gravity
|
||||
};
|
||||
return gravity;
|
||||
},
|
||||
growBubbleWithWandVelocity: function(properties, deltaTime) {
|
||||
growBubbleWithWandVelocity: function (properties, deltaTime) {
|
||||
//get the wand and tip position for calculations
|
||||
var wandPosition = properties.position;
|
||||
var wandTipPosition = this.getWandTipPosition(properties)
|
||||
this.getWandTipPosition(properties);
|
||||
// velocity = change in position / time
|
||||
var velocity = Vec3.multiply(Vec3.subtract(wandPosition, this.lastPosition), 1 / deltaTime);
|
||||
|
||||
// velocity = distance / time
|
||||
var distance = Vec3.subtract(wandPosition, this.lastPosition);
|
||||
var velocity = Vec3.multiply(distance, 1 / deltaTime);
|
||||
|
||||
var velocityStrength = Vec3.length(velocity);
|
||||
velocityStrength = velocityStrength;
|
||||
|
||||
//store the last position of the wand for velocity calculations
|
||||
this.lastPosition = wandPosition;
|
||||
|
@ -105,9 +103,7 @@
|
|||
|
||||
if (velocityStrength > VELOCITY_THRESHOLD) {
|
||||
//add some variation in bubble sizes
|
||||
var bubbleSize = randInt(BUBBLE_SIZE_MIN, BUBBLE_SIZE_MAX);
|
||||
bubbleSize = bubbleSize / BUBBLE_DIVISOR;
|
||||
|
||||
var bubbleSize = randFloat(BUBBLE_SIZE_MIN, BUBBLE_SIZE_MAX);
|
||||
//release the bubble if its dimensions are bigger than the bubble size
|
||||
if (dimensions.x > bubbleSize) {
|
||||
|
||||
|
@ -126,7 +122,7 @@
|
|||
|
||||
//release the bubble -- when we create a new bubble, it will carry on and this update loop will affect the new bubble
|
||||
this.createBubbleAtTipOfWand();
|
||||
return
|
||||
return;
|
||||
} else {
|
||||
//grow small bubbles
|
||||
dimensions.x += GROWTH_FACTOR * velocityStrength;
|
||||
|
@ -134,6 +130,7 @@
|
|||
dimensions.z += GROWTH_FACTOR * velocityStrength;
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
// if the wand is not moving, make the current bubble smaller
|
||||
if (dimensions.x >= SHRINK_LOWER_LIMIT) {
|
||||
|
@ -149,14 +146,12 @@
|
|||
dimensions: dimensions
|
||||
});
|
||||
},
|
||||
createBubbleAtTipOfWand: function() {
|
||||
createBubbleAtTipOfWand: function () {
|
||||
|
||||
//create a new bubble at the tip of the wand
|
||||
var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
|
||||
var wandPosition = properties.position;
|
||||
|
||||
wandTipPosition = this.getWandTipPosition(properties);
|
||||
|
||||
//store the position of the tip for use in velocity calculations
|
||||
this.lastPosition = wandPosition;
|
||||
|
||||
|
@ -165,7 +160,7 @@
|
|||
name: 'Bubble',
|
||||
type: 'Model',
|
||||
modelURL: BUBBLE_MODEL,
|
||||
position: wandTipPosition,
|
||||
position: this.getWandTipPosition(properties),
|
||||
dimensions: BUBBLE_INITIAL_DIMENSIONS,
|
||||
collisionsWillMove: false,
|
||||
ignoreForCollisions: false,
|
||||
|
@ -174,18 +169,17 @@
|
|||
});
|
||||
|
||||
},
|
||||
startNearGrab: function() {
|
||||
startNearGrab: function () {
|
||||
//create a bubble to grow at the start of the grab
|
||||
if (this.currentBubble === null) {
|
||||
this.createBubbleAtTipOfWand();
|
||||
}
|
||||
},
|
||||
continueNearGrab: function() {
|
||||
var deltaTime = checkInterval()
|
||||
continueNearGrab: function () {
|
||||
var deltaTime = checkInterval();
|
||||
//only get the properties that we need
|
||||
var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
|
||||
|
||||
this.growBubbleWithWandVelocity(properties, deltaTime);
|
||||
|
||||
var wandTipPosition = this.getWandTipPosition(properties);
|
||||
|
||||
|
@ -193,15 +187,17 @@
|
|||
Entities.editEntity(this.currentBubble, {
|
||||
position: wandTipPosition,
|
||||
});
|
||||
this.growBubbleWithWandVelocity(properties, deltaTime);
|
||||
|
||||
},
|
||||
releaseGrab: function() {
|
||||
releaseGrab: function () {
|
||||
//delete the current buble and reset state when the wand is released
|
||||
Entities.deleteEntity(this.currentBubble);
|
||||
this.currentBubble = null
|
||||
this.currentBubble = null;
|
||||
},
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
return new BubbleWand();
|
||||
|
||||
})
|
||||
});
|
Loading…
Reference in a new issue