258 lines
8.9 KiB
JavaScript
Raw Normal View History

2017-02-15 15:15:17 -08:00
(function() {
2017-02-17 11:43:09 -08:00
Script.include("/~/system/libraries/utils.js");
var SETTING_KEY = "com.highfidelity.avatar.isSitting";
2017-02-17 11:43:09 -08:00
var ROLE = "fly";
var ANIMATION_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/animations/sitting_idle.fbx";
var ANIMATION_FPS = 30;
var ANIMATION_FIRST_FRAME = 1;
var ANIMATION_LAST_FRAME = 10;
var RELEASE_KEYS = ['w', 'a', 's', 'd', 'UP', 'LEFT', 'DOWN', 'RIGHT'];
var RELEASE_TIME = 500; // ms
var RELEASE_DISTANCE = 0.2; // meters
2017-02-17 18:17:54 -08:00
var MAX_IK_ERROR = 20;
2017-02-17 11:43:09 -08:00
var DESKTOP_UI_CHECK_INTERVAL = 250;
var DESKTOP_MAX_DISTANCE = 5;
2017-02-24 18:40:36 -08:00
var SIT_DELAY = 25
2017-02-17 11:43:09 -08:00
this.entityID = null;
this.timers = {};
this.animStateHandlerID = null;
this.preload = function(entityID) {
this.entityID = entityID;
2017-02-15 18:10:33 -08:00
}
2017-02-17 11:43:09 -08:00
this.unload = function() {
2017-02-22 11:32:07 -08:00
if (MyAvatar.sessionUUID === this.getSeatUser()) {
2017-02-17 11:43:09 -08:00
this.sitUp(this.entityID);
}
if (this.interval) {
Script.clearInterval(this.interval);
this.interval = null;
}
this.cleanupOverlay();
}
2017-02-22 11:32:07 -08:00
this.setSeatUser = function(user) {
var userData = Entities.getEntityProperties(this.entityID, ["userData"]).userData;
userData = JSON.parse(userData);
if (user) {
userData.seat.user = user;
} else {
delete userData.seat.user;
}
Entities.editEntity(this.entityID, {
userData: JSON.stringify(userData)
});
}
this.getSeatUser = function() {
var properties = Entities.getEntityProperties(this.entityID, ["userData", "position"]);
var userData = JSON.parse(properties.userData);
if (userData.seat.user && userData.seat.user !== MyAvatar.sessionUUID) {
var avatar = AvatarList.getAvatar(userData.seat.user);
if (avatar && Vec3.distance(avatar.position, properties.position) > RELEASE_DISTANCE) {
return null;
}
}
return userData.seat.user;
}
2017-02-17 11:43:09 -08:00
this.checkSeatForAvatar = function() {
2017-02-22 11:32:07 -08:00
var seatUser = this.getSeatUser();
2017-02-17 11:43:09 -08:00
var avatarIdentifiers = AvatarList.getAvatarIdentifiers();
for (var i in avatarIdentifiers) {
var avatar = AvatarList.getAvatar(avatarIdentifiers[i]);
2017-02-22 11:32:07 -08:00
if (avatar && avatar.sessionUUID === seatUser) {
2017-02-17 11:43:09 -08:00
return true;
}
}
return false;
}
2017-02-17 11:43:09 -08:00
this.sitDown = function() {
if (this.checkSeatForAvatar()) {
print("Someone is already sitting in that chair.");
return;
}
this.setSeatUser(MyAvatar.sessionUUID);
2017-02-17 11:43:09 -08:00
var previousValue = Settings.getValue(SETTING_KEY);
Settings.setValue(SETTING_KEY, this.entityID);
if (previousValue === "") {
MyAvatar.characterControllerEnabled = false;
MyAvatar.hmdLeanRecenterEnabled = false;
MyAvatar.overrideRoleAnimation(ROLE, ANIMATION_URL, ANIMATION_FPS, true, ANIMATION_FIRST_FRAME, ANIMATION_LAST_FRAME);
2017-02-27 17:33:26 -08:00
MyAvatar.resetSensorsAndBody();
}
2017-02-27 15:03:15 -08:00
2017-02-21 18:39:41 -08:00
var that = this;
Script.setTimeout(function() {
var properties = Entities.getEntityProperties(that.entityID, ["position", "rotation"]);
var index = MyAvatar.getJointIndex("Hips");
2017-02-21 18:39:41 -08:00
MyAvatar.pinJoint(index, properties.position, properties.rotation);
2017-02-17 11:43:09 -08:00
2017-02-22 11:32:07 -08:00
that.animStateHandlerID = MyAvatar.addAnimationStateHandler(function(properties) {
2017-02-21 18:39:41 -08:00
return { headType: 0 };
}, ["headType"]);
Script.update.connect(that, that.update);
Controller.keyPressEvent.connect(that, that.keyPressed);
Controller.keyReleaseEvent.connect(that, that.keyReleased);
for (var i in RELEASE_KEYS) {
Controller.captureKeyEvents({ text: RELEASE_KEYS[i] });
}
}, SIT_DELAY);
2017-02-15 15:15:17 -08:00
}
2017-02-17 11:43:09 -08:00
this.sitUp = function() {
2017-02-22 11:32:07 -08:00
this.setSeatUser(null);
2017-02-17 11:43:09 -08:00
if (Settings.getValue(SETTING_KEY) === this.entityID) {
MyAvatar.restoreRoleAnimation(ROLE);
MyAvatar.characterControllerEnabled = true;
MyAvatar.hmdLeanRecenterEnabled = true;
2017-02-17 11:43:09 -08:00
var index = MyAvatar.getJointIndex("Hips");
MyAvatar.clearPinOnJoint(index);
2017-02-17 11:43:09 -08:00
2017-02-22 12:13:22 -08:00
MyAvatar.resetSensorsAndBody();
Script.setTimeout(function() {
MyAvatar.bodyPitch = 0.0;
MyAvatar.bodyRoll = 0.0;
}, SIT_DELAY);
Settings.setValue(SETTING_KEY, "");
}
MyAvatar.removeAnimationStateHandler(this.animStateHandlerID);
2017-02-17 11:43:09 -08:00
Script.update.disconnect(this, this.update);
Controller.keyPressEvent.disconnect(this, this.keyPressed);
Controller.keyReleaseEvent.disconnect(this, this.keyReleased);
for (var i in RELEASE_KEYS) {
Controller.releaseKeyEvents({ text: RELEASE_KEYS[i] });
}
2017-02-15 15:15:17 -08:00
}
2017-02-17 11:43:09 -08:00
this.sit = function () {
this.sitDown();
}
2017-02-17 11:43:09 -08:00
this.createOverlay = function() {
var text = "Click to sit";
var textMargin = 0.05;
var lineHeight = 0.15;
2017-02-15 15:15:17 -08:00
2017-02-17 11:43:09 -08:00
this.overlay = Overlays.addOverlay("text3d", {
position: { x: 0.0, y: 0.0, z: 0.0},
dimensions: { x: 0.1, y: 0.1 },
backgroundColor: { red: 0, green: 0, blue: 0 },
color: { red: 255, green: 255, blue: 255 },
topMargin: textMargin,
leftMargin: textMargin,
bottomMargin: textMargin,
rightMargin: textMargin,
text: text,
lineHeight: lineHeight,
alpha: 0.9,
backgroundAlpha: 0.9,
ignoreRayIntersection: true,
visible: true,
isFacingAvatar: true
});
var textSize = Overlays.textSize(this.overlay, text);
var overlayDimensions = {
x: textSize.width + 2 * textMargin,
y: textSize.height + 2 * textMargin
}
2017-02-22 11:32:07 -08:00
var properties = Entities.getEntityProperties(this.entityID, ["position", "registrationPoint", "dimensions"]);
var yOffset = (1.0 - properties.registrationPoint.y) * properties.dimensions.y + (overlayDimensions.y / 2.0);
var overlayPosition = Vec3.sum(properties.position, { x: 0, y: yOffset, z: 0 });
2017-02-17 11:43:09 -08:00
Overlays.editOverlay(this.overlay, {
position: overlayPosition,
dimensions: overlayDimensions
});
}
this.cleanupOverlay = function() {
if (this.overlay !== null) {
Overlays.deleteOverlay(this.overlay);
this.overlay = null;
}
}
2017-02-15 18:10:33 -08:00
2017-02-17 11:43:09 -08:00
this.update = function(dt) {
2017-02-22 11:32:07 -08:00
if (MyAvatar.sessionUUID === this.getSeatUser()) {
2017-02-17 11:43:09 -08:00
var properties = Entities.getEntityProperties(this.entityID, ["position"]);
var avatarDistance = Vec3.distance(MyAvatar.position, properties.position);
var ikError = MyAvatar.getIKErrorOnLastSolve();
if (avatarDistance > RELEASE_DISTANCE || ikError > MAX_IK_ERROR) {
2017-02-17 18:17:54 -08:00
print("IK error: " + ikError + ", distance from chair: " + avatarDistance);
2017-02-17 11:43:09 -08:00
this.sitUp(this.entityID);
}
}
2017-02-15 18:10:33 -08:00
}
2017-02-17 11:43:09 -08:00
this.keyPressed = function(event) {
if (isInEditMode()) {
return;
}
2017-02-15 15:15:17 -08:00
2017-02-17 11:43:09 -08:00
if (RELEASE_KEYS.indexOf(event.text) !== -1) {
var that = this;
this.timers[event.text] = Script.setTimeout(function() {
that.sitUp();
}, RELEASE_TIME);
}
}
this.keyReleased = function(event) {
if (RELEASE_KEYS.indexOf(event.text) !== -1) {
if (this.timers[event.text]) {
Script.clearTimeout(this.timers[event.text]);
delete this.timers[event.text];
}
}
}
2017-02-15 15:15:17 -08:00
2017-02-17 11:43:09 -08:00
this.canSitDesktop = function() {
2017-02-22 11:32:07 -08:00
var properties = Entities.getEntityProperties(this.entityID, ["position"]);
var distanceFromSeat = Vec3.distance(MyAvatar.position, properties.position);
2017-02-17 11:43:09 -08:00
return distanceFromSeat < DESKTOP_MAX_DISTANCE && !this.checkSeatForAvatar();
}
2017-02-17 11:43:09 -08:00
this.hoverEnterEntity = function(event) {
2017-02-22 11:32:07 -08:00
if (isInEditMode() || (MyAvatar.sessionUUID === this.getSeatUser())) {
2017-02-17 11:43:09 -08:00
return;
}
2017-02-15 18:10:33 -08:00
2017-02-17 11:43:09 -08:00
var that = this;
this.interval = Script.setInterval(function() {
if (that.overlay === null) {
if (that.canSitDesktop()) {
that.createOverlay();
}
} else if (!that.canSitDesktop()) {
that.cleanupOverlay();
}
}, DESKTOP_UI_CHECK_INTERVAL);
}
this.hoverLeaveEntity = function(event) {
if (this.interval) {
Script.clearInterval(this.interval);
this.interval = null;
}
this.cleanupOverlay();
2017-02-15 18:10:33 -08:00
}
2017-02-15 15:15:17 -08:00
2017-02-17 11:43:09 -08:00
this.clickDownOnEntity = function () {
2017-02-22 11:32:07 -08:00
if (isInEditMode() || (MyAvatar.sessionUUID === this.getSeatUser())) {
2017-02-17 11:43:09 -08:00
return;
}
if (this.canSitDesktop()) {
this.sitDown();
}
2017-02-15 15:15:17 -08:00
}
});