mirror of
https://github.com/overte-org/overte.git
synced 2025-08-05 16:10:11 +02:00
Cleanup and safeguards
- RegistrationPoint can no longer be set for magnetic blocks, will set it to 0.5 on snap. - Simplified axis lock logic
This commit is contained in:
parent
fe19b5511c
commit
04d3bf0c38
1 changed files with 80 additions and 67 deletions
|
@ -12,13 +12,14 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
const SNAPSOUND_SOURCE = 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;
|
const RANGE_MULTIPLER = 1.5;
|
||||||
|
const MAX_SCALE = 2;
|
||||||
|
const MIN_SCALE = 0.5;
|
||||||
|
|
||||||
// Helper for detecting nearby objects
|
// Helper for detecting nearby objects near entityProperties, with the scale calculated by the dimensions of the object.
|
||||||
function findEntitiesInRange(releasedProperties) {
|
function findEntitiesInRange(entityProperties) {
|
||||||
var dimensions = releasedProperties.dimensions;
|
var dimensions = entityProperties.dimensions;
|
||||||
return Entities.findEntities(releasedProperties.position, ((dimensions.x + dimensions.y + dimensions.z) / 3) * RANGE_MULTIPLER);
|
return Entities.findEntities(entityProperties.position, ((dimensions.x + dimensions.y + dimensions.z) / 3) * RANGE_MULTIPLER);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNearestValidEntityProperties(releasedProperties) {
|
function getNearestValidEntityProperties(releasedProperties) {
|
||||||
|
@ -32,7 +33,7 @@
|
||||||
var distance = Vec3.distance(releasedProperties.position, entity.position);
|
var distance = Vec3.distance(releasedProperties.position, entity.position);
|
||||||
var scale = releaseSize / Vec3.length(entity.dimensions);
|
var scale = releaseSize / Vec3.length(entity.dimensions);
|
||||||
|
|
||||||
if (distance < nearest && (scale >= 0.5 && scale <= 2)) {
|
if (distance < nearest && (scale >= MIN_SCALE && scale <= MAX_SCALE)) {
|
||||||
nearestEntity = entity;
|
nearestEntity = entity;
|
||||||
nearest = distance;
|
nearest = distance;
|
||||||
}
|
}
|
||||||
|
@ -51,27 +52,34 @@
|
||||||
Only retrieving userData
|
Only retrieving userData
|
||||||
*/
|
*/
|
||||||
var val = Entities.getEntityProperties(id, ['userData'])
|
var val = Entities.getEntityProperties(id, ['userData'])
|
||||||
var userData = {grabbableKey: {}};
|
var userData = {
|
||||||
|
grabbableKey: {}
|
||||||
|
};
|
||||||
|
// Check if existing userData field exists.
|
||||||
if (val.userData && val.userData.length > 0) {
|
if (val.userData && val.userData.length > 0) {
|
||||||
try {
|
try {
|
||||||
userData = JSON.parse(val.userData);
|
userData = JSON.parse(val.userData);
|
||||||
|
if (!userData.grabbableKey) {
|
||||||
|
userData.grabbableKey = {}; // If by random change there is no grabbableKey in the userData.
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// if user data is not valid json, we will simply overwrite it.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Object must be triggerable inorder to bind events.
|
// Object must be triggerable inorder to bind releaseGrabEvent
|
||||||
userData.grabbableKey.grabbable = true;
|
userData.grabbableKey.grabbable = true;
|
||||||
|
|
||||||
// Apply the new properties to entity of id
|
// Apply the new properties to entity of id
|
||||||
Entities.editEntity(id, {
|
Entities.editEntity(id, {
|
||||||
userData: JSON.stringify(userData)
|
userData: JSON.stringify(userData)
|
||||||
});
|
});
|
||||||
this.held = false;
|
Script.scriptEnding.connect(function() {
|
||||||
// We will now create a custom binding, to keep the 'current' context as these are callbacks called without context
|
Script.removeEventHandler(id, "releaseGrab", this.releaseGrab);
|
||||||
var t = this;
|
})
|
||||||
this.callbacks = {};
|
},
|
||||||
this.releaseGrab = function() {
|
releaseGrab: function(entityId) {
|
||||||
var released = Entities.getEntityProperties(id, ["position", "rotation", "dimensions"]);
|
// Release grab is called with entityId,
|
||||||
|
var released = Entities.getEntityProperties(entityId, ["position", "rotation", "dimensions"]);
|
||||||
var target = getNearestValidEntityProperties(released);
|
var target = getNearestValidEntityProperties(released);
|
||||||
if (target !== null) {
|
if (target !== null) {
|
||||||
// We found nearest, now lets do the snap calculations
|
// We found nearest, now lets do the snap calculations
|
||||||
|
@ -89,45 +97,50 @@
|
||||||
y: Math.abs(relativeDifference.y),
|
y: Math.abs(relativeDifference.y),
|
||||||
z: Math.abs(relativeDifference.z)
|
z: Math.abs(relativeDifference.z)
|
||||||
};
|
};
|
||||||
// Check what value is greater. Simplified.
|
// Check what value is greater. and lock down to that axis.
|
||||||
|
var newRelative = {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
z: 0
|
||||||
|
}
|
||||||
if (abs.x >= abs.y && abs.x >= abs.z) {
|
if (abs.x >= abs.y && abs.x >= abs.z) {
|
||||||
relativeDifference.y = 0;
|
newRelative.x = target.dimensions.x / 2 + released.dimensions.x / 2;
|
||||||
relativeDifference.z = 0;
|
if (relativeDifference.x < 0) {
|
||||||
if (relativeDifference.x > 0) {
|
newRelative.x = -newRelative.x;
|
||||||
relativeDifference.x = target.dimensions.x / 2 + released.dimensions.x / 2;
|
|
||||||
} else {
|
|
||||||
relativeDifference.x = -target.dimensions.x / 2 - released.dimensions.x / 2;
|
|
||||||
}
|
}
|
||||||
} else if (abs.y >= abs.x && abs.y >= abs.z) {
|
} else if (abs.y >= abs.x && abs.y >= abs.z) {
|
||||||
relativeDifference.x = 0;
|
newRelative.y = target.dimensions.y / 2 + released.dimensions.y / 2;
|
||||||
relativeDifference.z = 0;
|
if (relativeDifference.y < 0) {
|
||||||
if (relativeDifference.y > 0) {
|
newRelative.y = -newRelative.y;
|
||||||
relativeDifference.y = target.dimensions.y / 2 + released.dimensions.y / 2;
|
|
||||||
} else {
|
|
||||||
relativeDifference.y = -target.dimensions.y / 2 - released.dimensions.y / 2;
|
|
||||||
}
|
}
|
||||||
} else if (abs.z >= abs.x && abs.z >= abs.y) {
|
} else if (abs.z >= abs.x && abs.z >= abs.y) {
|
||||||
relativeDifference.x = 0;
|
newRelative.z = target.dimensions.z / 2 + released.dimensions.z / 2;
|
||||||
relativeDifference.y = 0;
|
if (relativeDifference.z < 0) {
|
||||||
if (relativeDifference.z > 0) {
|
newRelative.z = -newRelative.z;
|
||||||
relativeDifference.z = target.dimensions.z / 2 + released.dimensions.z / 2;
|
|
||||||
} else {
|
|
||||||
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.
|
// Can be expanded upon to work in nearest 90 degree rotation as well, but was not in spec.
|
||||||
var newPosition = Vec3.multiplyQbyV(target.rotation, relativeDifference);
|
var newPosition = Vec3.multiplyQbyV(target.rotation, newRelative);
|
||||||
Entities.editEntity(id, {
|
Entities.editEntity(entityId, {
|
||||||
|
// Script relies on the registrationPoint being at the very center of the object. Thus override.
|
||||||
|
registrationPoint: {
|
||||||
|
x: 0.5,
|
||||||
|
y: 0.5,
|
||||||
|
z: 0.5
|
||||||
|
},
|
||||||
rotation: target.rotation,
|
rotation: target.rotation,
|
||||||
position: Vec3.sum(target.position, newPosition)
|
position: Vec3.sum(target.position, newPosition)
|
||||||
|
});
|
||||||
|
// Script relies on the registrationPoint being at the very center of the object. Thus override.
|
||||||
|
Entities.editEntity(target.id, {
|
||||||
|
registrationPoint: {
|
||||||
|
x: 0.5,
|
||||||
|
y: 0.5,
|
||||||
|
z: 0.5
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.scriptEnding.connect(function() {
|
|
||||||
Script.removeEventHandler(id, "releaseGrab", this.releaseGrab);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new MagneticBlock();
|
return new MagneticBlock();
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue