146 lines
5.5 KiB
JavaScript
146 lines
5.5 KiB
JavaScript
//
|
|
// gateServer.js
|
|
//
|
|
// Created by Rebecca Stankus on 3/6/18.
|
|
// Edited for Bingo on 01/15/19
|
|
// Copyright 2018 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
|
|
//
|
|
|
|
(function() {
|
|
var _this;
|
|
|
|
var GAME_AUDIO_POSITION = { x: -79, y: -14, z: 6 };
|
|
var MOVEMENT_VELOCITY_M_PER_SEC = 2.4;
|
|
var POSITION_CHECK_INTERVAL_MS = 25;
|
|
var EPSILON_M = 0.01;
|
|
|
|
var gatePositionCheckInterval;
|
|
var openedLocalPosition;
|
|
var closedLocalPosition;
|
|
|
|
// *************************************
|
|
// START UTILITY FUNCTIONS
|
|
// *************************************
|
|
|
|
/* PLAY A SOUND: Plays the specified sound at specified position and volume */
|
|
|
|
|
|
// *************************************
|
|
// END UTILITY FUNCTIONS
|
|
// *************************************
|
|
|
|
var Gate = function() {
|
|
_this = this;
|
|
};
|
|
|
|
Gate.prototype = {
|
|
remotelyCallable: ['openGate', 'closeGate'],
|
|
preload: function(entityID){
|
|
_this.entityID = entityID;
|
|
var name = Entities.getEntityProperties(_this.entityID, 'name').name;
|
|
if (name === "Today Web Entity 1") {
|
|
closedLocalPosition = { x: 92.6318, y: -3.3 , z: 29.1593 };
|
|
openedLocalPosition = { x: closedLocalPosition.x, y: 5.8, z: closedLocalPosition.z };
|
|
} else if (name === "Today Web Entity 3") {
|
|
closedLocalPosition = { x: 106.0391, y: -3.2742, z: 30.2763 };
|
|
openedLocalPosition = { x: closedLocalPosition.x, y: 5.27, z: closedLocalPosition.z };
|
|
}
|
|
|
|
Entities.editEntity(_this.entityID, { localPosition: closedLocalPosition});
|
|
},
|
|
|
|
// Gate moves up to open
|
|
openGate: function() {
|
|
// Clear check interval if one exists.
|
|
if (gatePositionCheckInterval) {
|
|
Script.clearInterval(gatePositionCheckInterval);
|
|
gatePositionCheckInterval = false;
|
|
}
|
|
|
|
// Check if already open.
|
|
var currentPosition = Entities.getEntityProperties(_this.entityID, 'localPosition').localPosition;
|
|
if (currentPosition.y <= openedLocalPosition.y - EPSILON_M &&
|
|
currentPosition.y >= openedLocalPosition.y + EPSILON_M) {
|
|
return;
|
|
}
|
|
|
|
// Start opening the gate.
|
|
Entities.editEntity(_this.entityID,
|
|
{
|
|
velocity: {x: 0, y: MOVEMENT_VELOCITY_M_PER_SEC, z: 0}
|
|
}
|
|
);
|
|
|
|
// Start the check interval that stops the gate when it's open.
|
|
gatePositionCheckInterval = Script.setInterval(function() {
|
|
var localPosition = Entities.getEntityProperties(_this.entityID, 'localPosition').localPosition;
|
|
if (localPosition.y >= openedLocalPosition.y) {
|
|
Entities.editEntity(_this.entityID,
|
|
{
|
|
localPosition: openedLocalPosition,
|
|
velocity: {x: 0, y: 0, z: 0}
|
|
}
|
|
);
|
|
Script.clearInterval(gatePositionCheckInterval);
|
|
gatePositionCheckInterval = false;
|
|
}
|
|
}, POSITION_CHECK_INTERVAL_MS);
|
|
},
|
|
|
|
// Gate moves UP to close
|
|
closeGate: function() {
|
|
// Clear check interval if one exists.
|
|
if (gatePositionCheckInterval) {
|
|
Script.clearInterval(gatePositionCheckInterval);
|
|
gatePositionCheckInterval = false;
|
|
}
|
|
|
|
// Check if already closed.
|
|
var currentPosition = Entities.getEntityProperties(_this.entityID, 'localPosition').localPosition;
|
|
if (currentPosition.y <= closedLocalPosition.y - EPSILON_M &&
|
|
currentPosition.y >= closedLocalPosition.y + EPSILON_M) {
|
|
return;
|
|
}
|
|
|
|
// Start closing the gate.
|
|
Entities.editEntity(_this.entityID,
|
|
{
|
|
velocity: {x: 0, y: -MOVEMENT_VELOCITY_M_PER_SEC, z: 0}
|
|
}
|
|
);
|
|
|
|
// Start the check interval that stops the gate when it's closed.
|
|
gatePositionCheckInterval = Script.setInterval(function() {
|
|
var localPosition = Entities.getEntityProperties(_this.entityID, 'localPosition').localPosition;
|
|
if (localPosition.y <= closedLocalPosition.y) {
|
|
Entities.editEntity(_this.entityID,
|
|
{
|
|
localPosition: closedLocalPosition,
|
|
velocity: {x: 0, y: 0, z: 0}
|
|
}
|
|
);
|
|
Script.clearInterval(gatePositionCheckInterval);
|
|
gatePositionCheckInterval = false;
|
|
}
|
|
}, POSITION_CHECK_INTERVAL_MS);
|
|
},
|
|
|
|
unload: function() {
|
|
if (gatePositionCheckInterval) {
|
|
Script.clearInterval(gatePositionCheckInterval);
|
|
gatePositionCheckInterval = false;
|
|
}
|
|
Entities.editEntity(_this.entityID,
|
|
{
|
|
localPosition: closedLocalPosition,
|
|
velocity: {x: 0, y: 0, z: 0}
|
|
}
|
|
);
|
|
}
|
|
};
|
|
|
|
return new Gate();
|
|
});
|