content/hifi-content/robin/bugFixes/elevator/elevator-reef1.js
2022-02-14 02:04:11 +01:00

128 lines
3.8 KiB
JavaScript

//
// elevator.js
//
// 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
/* globals Script Entities Vec3 */
(function() {
var POINT_A = {x: 86.5248, y: -6.1959, z: 38.0987};
var POINT_B = {x: 86.5248, y: -14.5854, z: 38.0987};
var PAUSE_TIME = 4; // seconds
var MOVE_TIME = 5;
var goingUp = true;
var _entityID;
var pause = false;
var check = false;
var startPauseTime;
var upVelocity;
var downVelocity;
this.preload = function(entityID) {
_entityID = entityID;
var upDirection = Vec3.subtract(POINT_A, POINT_B);
var downDirection = Vec3.subtract(POINT_B, POINT_A);
upVelocity = Vec3.multiply(upDirection, 1 / MOVE_TIME);
downVelocity = Vec3.multiply(downDirection, 1 / MOVE_TIME);
stopVelocity = Vec3.ZERO;
Entities.editEntity(function() {
Entities.editEntity(_entityID, {
position: POINT_B,
});
});
Script.update.connect(update);
startMovement(goingUp);
// Script.setInterval(function() {
// movePlatform(goingUp);
// goingUp = !goingUp;
// }, (PAUSE_TIME + MOVE_TIME) * 1000);
};
// var movePlatform = function(isGoingUp) {
// var from = isGoingUp ? POINT_A : POINT_B;
// var to = isGoingUp ? POINT_B : POINT_A;
// var moveTime = MOVE_TIME;
// 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);
// };
var update = function (dt) {
var position = Entities.getEntityProperties(_entityID, "position").position;
if (pause === true) {
var now = Date.now();
if (startPauseTime) {
var elapsed = now - startPauseTime;
if(elapsed > (PAUSE_TIME * 1000)) {
print("I'm done stopping!")
pause = false;
goingUp = !goingUp;
startMovement(goingUp);
startPauseTime = null;
}
} else {
startPause();
}
} else if (goingUp && position.y >= POINT_A.y ) { // highest position
print("I'm up!")
startPause(POINT_A);
} else if (!goingUp && position.y <= POINT_B.y) { // lowest position
print("I'm low!")
startPause(POINT_B);
}
}
var startPause = function (position) {
print("PAUSE");
pause = true;
Entities.editEntity(function() {
Entities.editEntity(_entityID, {
position: position,
velocity: stopVelocity,
damping: 0
});
});
startPauseTime = Date.now();
}
var startMovement = function (isGoingUp) {
print("STARTMOVE");
var newVelocity = isGoingUp ? upVelocity : downVelocity;
Entities.editEntity(function() {
Entities.editEntity(_entityID, {
velocity: newVelocity,
damping: 0
});
});
}
this.unload = function () {
Script.update.disconnect(update);
}
});