Fixed up blocks scripts

This commit is contained in:
Menithal 2017-03-04 21:55:21 +02:00
parent ea3f7f0274
commit fe19b5511c
2 changed files with 193 additions and 126 deletions

View file

@ -1,7 +1,18 @@
//
// magneticBlock.js
//
// Created by Matti Lahtinen 4/3/2017
// Copyright 2017 High Fidelity, Inc.
//
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Makes the entity the script is bound to connect to nearby, similarly sized entities, like a magnet.
(function() {
const SNAPSOUND = SoundCache.getSound(Script.resolvePath("../../system/assets/sounds/entitySnap.wav?xrs"));
const SNAPSOUND_SOURCE = SoundCache.getSound(Script.resolvePath("../../system/assets/sounds/entitySnap.wav?xrs"));
// Preload trick for faster playback
const RANGE_MULTIPLER = 1.5;
// Helper for detecting nearby objects
@ -40,28 +51,32 @@
Only retrieving userData
*/
var val = Entities.getEntityProperties(id, ['userData'])
var userData = {};
var userData = {grabbableKey: {}};
if (val.userData && val.userData.length > 0) {
try {
userData = JSON.parse(val.userData);
} catch (e) {}
} catch (e) {
}
}
// Object must be triggerable inorder to bind events.
userData.grabbableKey = {grabbable: true};
// Apply the new properties to entity of id
Entities.editEntity(id, {userData: JSON.stringify(userData)});
this.held = false;
userData.grabbableKey.grabbable = true;
// Apply the new properties to entity of id
Entities.editEntity(id, {
userData: JSON.stringify(userData)
});
this.held = false;
// We will now create a custom binding, to keep the 'current' context as these are callbacks called without context
var t = this;
this.callbacks = {};
this.callbacks["releaseGrab"] = function () {
this.releaseGrab = function() {
var released = Entities.getEntityProperties(id, ["position", "rotation", "dimensions"]);
var target = getNearestValidEntityProperties(released);
if (target !== null) {
// We found nearest, now lets do the snap calculations
// Plays the snap sound between the two objects.
Audio.playSound(SNAPSOUND, {
Audio.playSound(SNAPSOUND_SOURCE, {
volume: 1,
position: Vec3.mix(target.position, released.position, 0.5)
});
@ -74,7 +89,7 @@
y: Math.abs(relativeDifference.y),
z: Math.abs(relativeDifference.z)
};
// Check what value is greater. Simplified.
if (abs.x >= abs.y && abs.x >= abs.z) {
relativeDifference.y = 0;
relativeDifference.z = 0;
@ -100,14 +115,17 @@
relativeDifference.z = -target.dimensions.z / 2 - released.dimensions.z / 2;
}
}
// Can be expanded upon to work in nearest rotation as well, but was not in spec.
var newPosition = Vec3.multiplyQbyV(target.rotation, relativeDifference);
Entities.editEntity(id, {rotation: target.rotation, position: Vec3.sum(target.position, newPosition)})
Entities.editEntity(id, {
rotation: target.rotation,
position: Vec3.sum(target.position, newPosition)
})
}
}
this.releaseGrab = this.callbacks["releaseGrab"];
Script.scriptEnding.connect(function() {
Script.removeEventHandler(id, "releaseGrab", this.callbacks["releaseGrab"]); //continueNearGrab
Script.removeEventHandler(id, "releaseGrab", this.releaseGrab);
})
}
}

View file

@ -1,6 +1,23 @@
// Toy
//
// makeBlocks.js
//
// Created by Matti Lahtinen 4/3/2017
// Copyright 2017 High Fidelity, Inc.
//
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Creates multiple "magnetic" blocks with random colors that users clones of and snap together.
(function() {
const MAX_RGB_COMPONENT_VALUE = 256 / 2; // Limit the values to half the maximum.
const MIN_COLOR_VALUE = 127;
const SIZE = 0.3;
const LIFETIME = 600;
// Random Pastel Generator based on Piper's script
function newColor() {
color = {
red: randomPastelRGBComponent(),
@ -13,3 +30,35 @@ function newColor() {
function randomPastelRGBComponent() {
return Math.floor(Math.random() * MAX_RGB_COMPONENT_VALUE) + MIN_COLOR_VALUE;
}
var SCRIPT_URL = Script.resolvePath("./entity_scripts/magneticBlock.js");
var frontVector = Quat.getFront(MyAvatar.orientation);
frontVector.y -=.25;
for(var x =0; x < 3; x++) {
for (var y = 0; y < 3; y++) {
var frontOffset = {
x: 0,
y: SIZE * y + SIZE,
z: SIZE * x + SIZE
};
Entities.addEntity({
type: "Box",
name: "MagneticBlock-" + y +'-' + x,
dimensions: {
x: SIZE,
y: SIZE,
z: SIZE
},
userData: JSON.stringify({grabbableKey: { cloneable: true, grabbable: true, cloneLifetime : LIFETIME, cloneLimit: 9999}}),
position: Vec3.sum(MyAvatar.position, Vec3.sum(frontOffset, frontVector)),
color: newColor(),
script: SCRIPT_URL
});
}
}
Script.stop();
})();