77 lines
No EOL
2.5 KiB
JavaScript
77 lines
No EOL
2.5 KiB
JavaScript
(function() {
|
|
var _entityID = null;
|
|
/*
|
|
{
|
|
"Door": {
|
|
"closePosition": {
|
|
"x": -84.6295,
|
|
"y": 151.7094,
|
|
"z": 302.5855
|
|
},
|
|
"openPosition": {
|
|
"x": -84.6295,
|
|
"y": 155.2094,
|
|
"z": 302.5855
|
|
},
|
|
"moveTime": 1.0,
|
|
"open": false
|
|
}
|
|
}
|
|
*/
|
|
var DOOR_SOUND = SoundCache.getSound(Script.resolvePath('386073__speakingmusic__door-future-open.wav'));
|
|
var DOOR_SOUND_PROPERTIES = {
|
|
volume: 0.5
|
|
};
|
|
|
|
var getUserData = function() {
|
|
try {
|
|
return JSON.parse(Entities.getEntityProperties(_entityID, 'userData').userData);
|
|
} catch (e) {
|
|
// e
|
|
print('Could not retrieve valid userData')
|
|
}
|
|
return null;
|
|
};
|
|
|
|
var setDoorOpenState = function(userDataObject, open) {
|
|
userDataObject.Door.open = open;
|
|
Entities.editEntity(_entityID, {userData: JSON.stringify(userDataObject)})
|
|
}
|
|
|
|
this.preload = function(entityID) {
|
|
_entityID = entityID;
|
|
};
|
|
|
|
this.trigger = function(entityID, args) {
|
|
var userDataObject = getUserData();
|
|
if (userDataObject === null || userDataObject.Door === undefined) {
|
|
return;
|
|
}
|
|
|
|
if (DOOR_SOUND.downloaded) {
|
|
var soundProperties = DOOR_SOUND_PROPERTIES;
|
|
soundProperties.position = Entities.getEntityProperties(_entityID, 'position').position;
|
|
Audio.playSound(DOOR_SOUND, soundProperties);
|
|
}
|
|
var opening = !userDataObject.Door.open;
|
|
var from = opening ? userDataObject.Door.closePosition : userDataObject.Door.openPosition;
|
|
var to = opening ? userDataObject.Door.openPosition : userDataObject.Door.closePosition;
|
|
var moveTime = userDataObject.Door.moveTime;
|
|
var moveDirection = Vec3.subtract(to , from);
|
|
var moveVelocity = Vec3.multiply(moveDirection, 1 / moveTime);
|
|
Entities.editEntity(_entityID, {
|
|
velocity: moveVelocity,
|
|
position: from,
|
|
damping: 0
|
|
})
|
|
|
|
Script.setTimeout(function() {
|
|
Entities.editEntity(_entityID, {
|
|
velocity: {x: 0, y: 0, z: 0},
|
|
position: to
|
|
});
|
|
}, moveTime * 1000);
|
|
|
|
setDoorOpenState(userDataObject, opening);
|
|
};
|
|
}) |