content/hifi-content/caitlyn/dev/Scripts/JunkyardPhysics.js
2022-02-13 22:19:19 +01:00

55 lines
No EOL
2 KiB
JavaScript

( function() {
const distributionWidth = 25; // Change these three constants to adjust the width and length of the spread of junk
const distributionHeight = 0.5;
const distributionLength = 25;
const scanDiameter = 15000;
var arePhysicsOn = false;
var arrayFound = Entities.findEntities(MyAvatar.position, scanDiameter);
this.preload = function (entityID) {
this.entityID = entityID;
InitializeJunkyard();
}
function ShowJunkyardStateText(textEntity){
Entities.editEntity(textEntity, { text: "Dynamics "+arePhysicsOn});
if (arePhysicsOn) Entities.editEntity(textEntity, { backgroundColor: {red:0,green:255,blue:0}});
else Entities.editEntity(textEntity, { backgroundColor: {red:255,green:0,blue:0}});
}
function SetJunkyardPhysicsEntity(junkEntity, turnPhysicsOn){
var myPos = Entities.getEntityProperties(junkEntity, "position").position;
myPos.x = Math.random()*distributionLength + 10;
myPos.y = Math.random()*distributionHeight+1;
myPos.z = Math.random()*distributionWidth;
if (turnPhysicsOn) {
Entities.editEntity(junkEntity, {dynamic : true, position: myPos, velocity:{x:0, y:0.1, z:0}});
} else {
Entities.editEntity(junkEntity, {dynamic : false});
}
}
function InitializeJunkyard( ){
for( i = 0; i < arrayFound.length; i++){
var entProps = Entities.getEntityProperties(arrayFound[i]);
if (entProps.locked) continue;
if (arrayFound[i]==this.entityID) continue;
SetJunkyardPhysicsEntity(arrayFound[i], arePhysicsOn);
}
ShowJunkyardStateText(this.entityID);
}
this.mousePressOnEntity = function(entityID, mouseEvent){
arePhysicsOn = !arePhysicsOn;
for( i = 0; i < arrayFound.length; i++){
var entProps = Entities.getEntityProperties(arrayFound[i]);
if (entProps.locked) continue;
if (arrayFound[i]==this.entityID) continue;
SetJunkyardPhysicsEntity(arrayFound[i], arePhysicsOn);
}
ShowJunkyardStateText(entityID);
};
}
)