118 lines
3.4 KiB
JavaScript
118 lines
3.4 KiB
JavaScript
//
|
|
// button.js
|
|
//
|
|
// Created by Clement Brisset on 10/26/13
|
|
// 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 Entities, Script, AnimationCache, Settings, MyAvatar, DriveKeys, AvatarList,
|
|
Vec3, HMD, Overlays, Camera, isInEditMode */
|
|
|
|
(function () {
|
|
var myEntityID = null;
|
|
var version = 11;
|
|
var myParentID = null;
|
|
var myUserData = null;
|
|
|
|
function debug(text) {
|
|
console.log("[TAG " + version + "] " + text);
|
|
}
|
|
debug("Version: " + version);
|
|
|
|
var signals = {
|
|
list: {},
|
|
connect: function(signal, func) {
|
|
debug("Connecting: " + signal.name);
|
|
signal.connect(func);
|
|
signals.list[signal.name] = function() {
|
|
signal.disconnect(func);
|
|
}
|
|
},
|
|
disconnect: function(signal) {
|
|
debug("Disonnecting: " + signal.name);
|
|
signals.list[signal.name]();
|
|
delete signals.list[signal.name];
|
|
},
|
|
disconnectAll: function() {
|
|
for (signalName in signals.list) {
|
|
debug("DisonnectingAll: " + signalName);
|
|
signals.list[signalName]();
|
|
delete signals.list[signalName];
|
|
}
|
|
}
|
|
};
|
|
|
|
this.preload = function (id) {
|
|
myEntityID = id;
|
|
var props = Entities.getEntityProperties(myEntityID, ["parentID", "userData"]);
|
|
myParentID = props.parentID;
|
|
myUserData = JSON.parse(props.userData);
|
|
debug("Loading " + myEntityID + " (parentID: " + myParentID + ")");
|
|
|
|
signals.connect(Entities.clickDownOnEntity, onClickDown);
|
|
};
|
|
this.unload = function () {
|
|
debug("Unloading " + myEntityID);
|
|
signals.disconnectAll();
|
|
};
|
|
|
|
function trigger() {
|
|
if (isIn()) {
|
|
getOut();
|
|
} else {
|
|
getIn();
|
|
}
|
|
}
|
|
function onUpdate() {
|
|
if (isIn()) {
|
|
var props = Entities.getEntityProperties(myEntityID);
|
|
if (Vec3.distance(MyAvatar.position, props.position) > 5) {
|
|
getOut();
|
|
}
|
|
}
|
|
}
|
|
|
|
function isIn() {
|
|
return MyAvatar.getParentID() == myEntityID;
|
|
}
|
|
function getIn() {
|
|
debug("getIn");
|
|
var props = Entities.getEntityProperties(myEntityID);
|
|
if (Vec3.distance(MyAvatar.position, props.position) > 2) {
|
|
debug("nope")
|
|
return;
|
|
}
|
|
|
|
props.position.y = MyAvatar.position.y;
|
|
MyAvatar.position = props.position;
|
|
MyAvatar.orientation = props.rotation;
|
|
// MyAvatar.setCollisionsEnabled(false);
|
|
MyAvatar.setParentID(myEntityID);
|
|
signals.connect(Script.update, onUpdate);
|
|
}
|
|
function getOut() {
|
|
debug("getOut");
|
|
signals.disconnect(Script.update);
|
|
MyAvatar.setParentID(Uuid.NULL_ID);
|
|
// MyAvatar.setCollisionsEnabled(true);
|
|
MyAvatar.bodyPitch = 0;
|
|
MyAvatar.bodyRoll = 0;
|
|
}
|
|
|
|
function onClickDown(entityID, event) {
|
|
if (myEntityID != entityID || !event.isPrimaryButton) {
|
|
return;
|
|
}
|
|
debug("onClickDown: " + JSON.stringify(myUserData))
|
|
|
|
trigger();
|
|
}
|
|
|
|
this.startFarTrigger = function() {
|
|
debug("startFarTrigger: " + JSON.stringify(myUserData));
|
|
trigger();
|
|
}
|
|
});
|