CR. Run through Lint ECMAScript 5 standard

Meaning all const are out, non-variable definitions are out, etc
This commit is contained in:
Menithal 2017-03-06 21:07:16 +02:00
parent 04d3bf0c38
commit d50a0e33a9
2 changed files with 41 additions and 28 deletions

View file

@ -11,21 +11,23 @@
// Makes the entity the script is bound to connect to nearby, similarly sized entities, like a magnet. // Makes the entity the script is bound to connect to nearby, similarly sized entities, like a magnet.
(function() { (function() {
const SNAPSOUND_SOURCE = SoundCache.getSound(Script.resolvePath("../../system/assets/sounds/entitySnap.wav?xrs")); var SNAPSOUND_SOURCE = SoundCache.getSound(Script.resolvePath("../../system/assets/sounds/entitySnap.wav?xrs"));
const RANGE_MULTIPLER = 1.5; var RANGE_MULTIPLER = 1.5;
const MAX_SCALE = 2; var MAX_SCALE = 2;
const MIN_SCALE = 0.5; var MIN_SCALE = 0.5;
// Helper for detecting nearby objects near entityProperties, with the scale calculated by the dimensions of the object. // Helper for detecting nearby objects near entityProperties, with the scale calculated by the dimensions of the object.
function findEntitiesInRange(entityProperties) { function findEntitiesInRange(entityProperties) {
var dimensions = entityProperties.dimensions; var dimensions = entityProperties.dimensions;
return Entities.findEntities(entityProperties.position, ((dimensions.x + dimensions.y + dimensions.z) / 3) * RANGE_MULTIPLER); // Average of the dimensions instead of full value.
return Entities.findEntities(entityProperties.position,
((dimensions.x + dimensions.y + dimensions.z) / 3) * RANGE_MULTIPLER);
} }
function getNearestValidEntityProperties(releasedProperties) { function getNearestValidEntityProperties(releasedProperties) {
var entities = findEntitiesInRange(releasedProperties); var entities = findEntitiesInRange(releasedProperties);
var nearestEntity = null; var nearestEntity = null;
var nearest = 9999999999999; var nearest = Number.MAX_SAFE_INTEGER;
var releaseSize = Vec3.length(releasedProperties.dimensions); var releaseSize = Vec3.length(releasedProperties.dimensions);
entities.forEach(function(entityId) { entities.forEach(function(entityId) {
if (entityId !== releasedProperties.id) { if (entityId !== releasedProperties.id) {
@ -38,27 +40,30 @@
nearest = distance; nearest = distance;
} }
} }
}) });
return nearestEntity; return nearestEntity;
} }
// Create the 'class' // Create the 'class'
function MagneticBlock() {} function MagneticBlock() {}
// Bind pre-emptive events // Bind pre-emptive events
MagneticBlock.prototype = { MagneticBlock.prototype = {
// When script is bound to an entity, preload is the first callback called with the entityID. It will behave as the constructor /*
When script is bound to an entity, preload is the first callback called with the entityID.
It will behave as the constructor
*/
preload: function(id) { preload: function(id) {
/* /*
We will now override any existing userdata with the grabbable property. We will now override any existing userdata with the grabbable property.
Only retrieving userData Only retrieving userData
*/ */
var val = Entities.getEntityProperties(id, ['userData']) var entity = Entities.getEntityProperties(id, ['userData']);
var userData = { var userData = {
grabbableKey: {} grabbableKey: {}
}; };
// Check if existing userData field exists. // Check if existing userData field exists.
if (val.userData && val.userData.length > 0) { if (entity.userData && entity.userData.length > 0) {
try { try {
userData = JSON.parse(val.userData); userData = JSON.parse(entity.userData);
if (!userData.grabbableKey) { if (!userData.grabbableKey) {
userData.grabbableKey = {}; // If by random change there is no grabbableKey in the userData. userData.grabbableKey = {}; // If by random change there is no grabbableKey in the userData.
} }
@ -75,7 +80,7 @@
}); });
Script.scriptEnding.connect(function() { Script.scriptEnding.connect(function() {
Script.removeEventHandler(id, "releaseGrab", this.releaseGrab); Script.removeEventHandler(id, "releaseGrab", this.releaseGrab);
}) });
}, },
releaseGrab: function(entityId) { releaseGrab: function(entityId) {
// Release grab is called with entityId, // Release grab is called with entityId,
@ -102,7 +107,7 @@
x: 0, x: 0,
y: 0, y: 0,
z: 0 z: 0
} };
if (abs.x >= abs.y && abs.x >= abs.z) { if (abs.x >= abs.y && abs.x >= abs.z) {
newRelative.x = target.dimensions.x / 2 + released.dimensions.x / 2; newRelative.x = target.dimensions.x / 2 + released.dimensions.x / 2;
if (relativeDifference.x < 0) { if (relativeDifference.x < 0) {
@ -138,9 +143,9 @@
y: 0.5, y: 0.5,
z: 0.5 z: 0.5
} }
}) });
} }
} }
} };
return new MagneticBlock(); return new MagneticBlock();
}) });

View file

@ -12,19 +12,20 @@
(function() { (function() {
const MAX_RGB_COMPONENT_VALUE = 256 / 2; // Limit the values to half the maximum. var MAX_RGB_COMPONENT_VALUE = 256 / 2; // Limit the values to half the maximum.
const MIN_COLOR_VALUE = 127; var MIN_COLOR_VALUE = 127;
const SIZE = 0.3; var SIZE = 0.3;
const LIFETIME = 600; var LIFETIME = 600;
var VERTICAL_OFFSET = -0.25;
var ROWS = 3;
var COLUMNS = 3;
// Random Pastel Generator based on Piper's script // Random Pastel Generator based on Piper's script
function newColor() { function newColor() {
color = { return {
red: randomPastelRGBComponent(), red: randomPastelRGBComponent(),
green: randomPastelRGBComponent(), green: randomPastelRGBComponent(),
blue: randomPastelRGBComponent() blue: randomPastelRGBComponent()
}; };
return color;
} }
// Helper functions. // Helper functions.
function randomPastelRGBComponent() { function randomPastelRGBComponent() {
@ -34,9 +35,9 @@
var SCRIPT_URL = Script.resolvePath("./entity_scripts/magneticBlock.js"); var SCRIPT_URL = Script.resolvePath("./entity_scripts/magneticBlock.js");
var frontVector = Quat.getFront(MyAvatar.orientation); var frontVector = Quat.getFront(MyAvatar.orientation);
frontVector.y -=.25; frontVector.y += VERTICAL_OFFSET;
for(var x =0; x < 3; x++) { for (var x = 0; x < COLUMNS; x++) {
for (var y = 0; y < 3; y++) { for (var y = 0; y < ROWS; y++) {
var frontOffset = { var frontOffset = {
x: 0, x: 0,
@ -46,13 +47,20 @@
Entities.addEntity({ Entities.addEntity({
type: "Box", type: "Box",
name: "MagneticBlock-" + y +'-' + x, name: "MagneticBlock-" + y + '-' + x,
dimensions: { dimensions: {
x: SIZE, x: SIZE,
y: SIZE, y: SIZE,
z: SIZE z: SIZE
}, },
userData: JSON.stringify({grabbableKey: { cloneable: true, grabbable: true, cloneLifetime : LIFETIME, cloneLimit: 9999}}), userData: JSON.stringify({
grabbableKey: {
cloneable: true,
grabbable: true,
cloneLifetime: LIFETIME,
cloneLimit: 9999
}
}),
position: Vec3.sum(MyAvatar.position, Vec3.sum(frontOffset, frontVector)), position: Vec3.sum(MyAvatar.position, Vec3.sum(frontOffset, frontVector)),
color: newColor(), color: newColor(),
script: SCRIPT_URL script: SCRIPT_URL