329 lines
11 KiB
JavaScript
Raw Normal View History

2017-03-17 10:45:13 -07:00
//
// sit.js
//
// Created by Clement Brisset on 3/3/17
// 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
//
2017-02-15 15:15:17 -08:00
(function() {
2017-02-17 11:43:09 -08:00
Script.include("/~/system/libraries/utils.js");
2017-03-17 10:45:13 -07:00
var SETTING_KEY = "com.highfidelity.avatar.isSitting";
2017-02-17 11:43:09 -08:00
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_TIME = 500; // ms
var RELEASE_DISTANCE = 0.2; // meters
2017-03-13 17:22:06 -08:00
var MAX_IK_ERROR = 30;
var IK_SETTLE_TIME = 250; // ms
2017-03-13 17:22:06 -08:00
var DESKTOP_UI_CHECK_INTERVAL = 100;
2017-02-17 11:43:09 -08:00
var DESKTOP_MAX_DISTANCE = 5;
var SIT_DELAY = 25;
var MAX_RESET_DISTANCE = 0.5; // meters
2017-03-16 18:37:58 -07:00
var OVERRIDEN_DRIVE_KEYS = [
DriveKeys.TRANSLATE_X,
DriveKeys.TRANSLATE_Y,
DriveKeys.TRANSLATE_Z,
DriveKeys.STEP_TRANSLATE_X,
DriveKeys.STEP_TRANSLATE_Y,
DriveKeys.STEP_TRANSLATE_Z,
];
2017-02-17 11:43:09 -08:00
this.entityID = null;
this.animStateHandlerID = null;
2017-03-13 17:22:06 -08:00
this.interval = null;
2017-03-16 14:30:33 -07:00
this.sitDownSettlePeriod = null;
this.lastTimeNoDriveKeys = null;
2017-03-17 10:45:13 -07:00
this.sittingDown = false;
2017-02-17 11:43:09 -08:00
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-03-13 17:22:06 -08:00
if (Settings.getValue(SETTING_KEY) === this.entityID) {
this.standUp();
2017-02-17 11:43:09 -08:00
}
2017-03-13 17:22:06 -08:00
if (this.interval !== null) {
2017-02-17 11:43:09 -08:00
Script.clearInterval(this.interval);
this.interval = null;
}
this.cleanupOverlay();
}
2017-02-22 11:32:07 -08:00
this.setSeatUser = function(user) {
2017-03-13 17:22:06 -08:00
try {
var userData = Entities.getEntityProperties(this.entityID, ["userData"]).userData;
userData = JSON.parse(userData);
if (user !== null) {
userData.seat.user = user;
} else {
delete userData.seat.user;
}
2017-02-22 11:32:07 -08:00
2017-03-13 17:22:06 -08:00
Entities.editEntity(this.entityID, {
userData: JSON.stringify(userData)
});
} catch (e) {
// Do Nothing
2017-02-22 11:32:07 -08:00
}
}
this.getSeatUser = function() {
2017-03-13 17:22:06 -08:00
try {
var properties = Entities.getEntityProperties(this.entityID, ["userData", "position"]);
var userData = JSON.parse(properties.userData);
// If MyAvatar return my uuid
if (userData.seat.user === MyAvatar.sessionUUID) {
return userData.seat.user;
}
2017-02-22 11:32:07 -08:00
2017-03-13 17:22:06 -08:00
// If Avatar appears to be sitting
if (userData.seat.user) {
var avatar = AvatarList.getAvatar(userData.seat.user);
if (avatar && (Vec3.distance(avatar.position, properties.position) < RELEASE_DISTANCE)) {
return userData.seat.user;
}
2017-02-22 11:32:07 -08:00
}
2017-03-13 17:22:06 -08:00
} catch (e) {
// Do nothing
2017-02-22 11:32:07 -08:00
}
2017-03-13 17:22:06 -08:00
// Nobody on the seat
return null;
2017-02-22 11:32:07 -08:00
}
2017-03-13 17:22:06 -08:00
// Is the seat used
2017-02-17 11:43:09 -08:00
this.checkSeatForAvatar = function() {
2017-02-22 11:32:07 -08:00
var seatUser = this.getSeatUser();
2017-03-13 17:22:06 -08:00
// If MyAvatar appears to be sitting
if (seatUser === MyAvatar.sessionUUID) {
var properties = Entities.getEntityProperties(this.entityID, ["position"]);
return Vec3.distance(MyAvatar.position, properties.position) < RELEASE_DISTANCE;
2017-02-17 11:43:09 -08:00
}
2017-03-13 17:22:06 -08:00
return seatUser !== null;
}
2017-02-17 11:43:09 -08:00
this.sitDown = function() {
if (this.checkSeatForAvatar()) {
print("Someone is already sitting in that chair.");
return;
}
print("Sitting down (" + this.entityID + ")");
2017-03-17 10:45:13 -07:00
this.sittingDown = true;
2017-03-16 14:30:33 -07:00
var now = Date.now();
this.sitDownSettlePeriod = now + IK_SETTLE_TIME;
this.lastTimeNoDriveKeys = now;
2017-02-17 11:43:09 -08:00
var previousValue = Settings.getValue(SETTING_KEY);
Settings.setValue(SETTING_KEY, this.entityID);
2017-03-13 17:22:06 -08:00
this.setSeatUser(MyAvatar.sessionUUID);
if (previousValue === "") {
MyAvatar.characterControllerEnabled = false;
MyAvatar.hmdLeanRecenterEnabled = false;
var ROLES = MyAvatar.getAnimationRoles();
2017-03-01 17:49:52 -08:00
for (i in ROLES) {
MyAvatar.overrideRoleAnimation(ROLES[i], ANIMATION_URL, ANIMATION_FPS, true, ANIMATION_FIRST_FRAME, ANIMATION_LAST_FRAME);
}
2017-03-17 10:45:13 -07:00
for (var i in OVERRIDEN_DRIVE_KEYS) {
MyAvatar.disableDriveKey(OVERRIDEN_DRIVE_KEYS[i]);
}
2017-02-27 17:33:26 -08:00
MyAvatar.resetSensorsAndBody();
}
2017-02-27 15:03:15 -08:00
2017-03-13 17:22:06 -08:00
var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
var index = MyAvatar.getJointIndex("Hips");
MyAvatar.pinJoint(index, properties.position, properties.rotation);
this.animStateHandlerID = MyAvatar.addAnimationStateHandler(function(properties) {
return { headType: 0 };
}, ["headType"]);
Script.update.connect(this, this.update);
2017-02-15 15:15:17 -08:00
}
2017-02-17 11:43:09 -08:00
this.standUp = function() {
print("Standing up (" + this.entityID + ")");
2017-03-13 17:22:06 -08:00
MyAvatar.removeAnimationStateHandler(this.animStateHandlerID);
Script.update.disconnect(this, this.update);
2017-02-17 11:43:09 -08:00
if (MyAvatar.sessionUUID === this.getSeatUser()) {
this.setSeatUser(null);
}
if (Settings.getValue(SETTING_KEY) === this.entityID) {
2017-03-13 17:22:06 -08:00
Settings.setValue(SETTING_KEY, "");
2017-03-17 10:45:13 -07:00
for (var i in OVERRIDEN_DRIVE_KEYS) {
MyAvatar.enableDriveKey(OVERRIDEN_DRIVE_KEYS[i]);
}
var ROLES = MyAvatar.getAnimationRoles();
2017-03-01 17:49:52 -08:00
for (i in ROLES) {
MyAvatar.restoreRoleAnimation(ROLES[i]);
}
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);
2017-02-17 11:43:09 -08:00
}
2017-03-17 10:45:13 -07:00
this.sittingDown = false;
2017-02-15 15:15:17 -08:00
}
2017-02-17 11:43:09 -08:00
// function called by teleport.js if it detects the appropriate userData
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-17 11:43:09 -08:00
this.update = function(dt) {
2017-03-17 10:45:13 -07:00
if (this.sittingDown === true) {
var properties = Entities.getEntityProperties(this.entityID);
2017-02-17 11:43:09 -08:00
var avatarDistance = Vec3.distance(MyAvatar.position, properties.position);
var ikError = MyAvatar.getIKErrorOnLastSolve();
var now = Date.now();
var shouldStandUp = false;
// Check if a drive key is pressed
var hasActiveDriveKey = false;
for (var i in OVERRIDEN_DRIVE_KEYS) {
if (MyAvatar.getRawDriveKey(OVERRIDEN_DRIVE_KEYS[i]) != 0.0) {
hasActiveDriveKey = true;
break;
}
}
// Only standup if user has been pushing a drive key for RELEASE_TIME
if (hasActiveDriveKey) {
var elapsed = now - this.lastTimeNoDriveKeys;
shouldStandUp = elapsed > RELEASE_TIME;
} else {
this.lastTimeNoDriveKeys = Date.now();
}
// Allow some time for the IK to settle
2017-03-16 14:30:33 -07:00
if (ikError > MAX_IK_ERROR && now > this.sitDownSettlePeriod) {
shouldStandUp = true;
2017-02-17 11:43:09 -08:00
}
2017-02-15 15:15:17 -08:00
if (MyAvatar.sessionUUID !== this.getSeatUser()) {
shouldStandUp = true;
}
2017-03-13 17:22:06 -08:00
if (shouldStandUp || avatarDistance > RELEASE_DISTANCE) {
print("IK error: " + ikError + ", distance from chair: " + avatarDistance);
// Move avatar in front of the chair to avoid getting stuck in collision hulls
if (avatarDistance < MAX_RESET_DISTANCE) {
var offset = { x: 0, y: 1.0, z: -0.5 - properties.dimensions.z * properties.registrationPoint.z };
var position = Vec3.sum(properties.position, Vec3.multiplyQbyV(properties.rotation, offset));
MyAvatar.position = position;
print("Moving Avatar in front of the chair.");
// Delay standing up by 1 cycle.
// This leaves times for the avatar to actually move since a lot
// of the stand up operations are threaded
return;
}
this.standUp();
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-03-13 17:22:06 -08:00
if (isInEditMode() || this.interval !== null) {
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) {
2017-03-13 17:22:06 -08:00
if (this.interval !== null) {
2017-02-17 11:43:09 -08:00
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-03-06 16:09:31 -08:00
this.clickDownOnEntity = function (id, event) {
2017-03-13 17:22:06 -08:00
if (isInEditMode()) {
2017-02-17 11:43:09 -08:00
return;
}
2017-03-06 16:09:31 -08:00
if (event.isPrimaryButton && this.canSitDesktop()) {
2017-02-17 11:43:09 -08:00
this.sitDown();
}
2017-02-15 15:15:17 -08:00
}
});