// // 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 // (function() { Script.include("/~/system/libraries/utils.js"); if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position){ position = position || 0; return this.substr(position, searchString.length) === searchString; }; } var SETTING_KEY = "com.highfidelity.avatar.isSitting"; var ANIMATION_URL = "http://hifi-content.s3.amazonaws.com/alexia/Sitting_Idle.fbx"; var ANIMATION_FPS = 30; var ANIMATION_FIRST_FRAME = 1; var ANIMATION_LAST_FRAME = 350; var RELEASE_TIME = 500; // ms var RELEASE_DISTANCE = 0.2; // meters var MAX_IK_ERROR = 30; var IK_SETTLE_TIME = 250; // ms var DESKTOP_UI_CHECK_INTERVAL = 500; var DESKTOP_MAX_DISTANCE = 2.8; var SIT_DELAY = 25; var ALPHA_START = 0.7; //for overlays var MAX_RESET_DISTANCE = 0.5; // meters 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, ]; this.transparencyInterval = null; this.entityID = null; this.animStateHandlerID = null; this.interval = null; this.sitDownSettlePeriod = null; this.lastTimeNoDriveKeys = null; this.sittingDown = false; var checkForHover; // Preload the animation file this.animation = AnimationCache.prefetch(ANIMATION_URL); this.preload = function(entityID) { this.entityID = entityID; } this.unload = function() { if (Settings.getValue(SETTING_KEY) === this.entityID) { this.standUp(); } if (this.interval !== null) { Script.clearInterval(this.interval); this.interval = null; } if (this.transparencyInterval !== null) { Script.clearInterval(this.transparencyInterval); this.transparencyInterval = null; } if (checkForHover !== null) { Script.clearInterval(checkForHover); checkForHover = null; } this.cleanupOverlay(); } this.setSeatUser = function(user) { 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; } Entities.editEntity(this.entityID, { userData: JSON.stringify(userData) }); } catch (e) { // Do Nothing } } this.getSeatUser = function() { 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; } // 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; } } } catch (e) { // Do nothing } // Nobody on the seat return null; } // Is the seat used this.checkSeatForAvatar = function() { var seatUser = this.getSeatUser(); // 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; } return seatUser !== null; } this.rolesToOverride = function() { return MyAvatar.getAnimationRoles().filter(function(role) { return !(role.startsWith("right") || role.startsWith("left")); }); } // Handler for user changing the avatar model while sitting. There's currently an issue with changing avatar models while override role animations are applied, // so to avoid that problem, re-apply the role overrides once the model has finished changing. this.modelURLChangeFinished = function () { print("Sitter's model has FINISHED changing. Reapply anim role overrides."); var roles = this.rolesToOverride(); for (i in roles) { MyAvatar.overrideRoleAnimation(roles[i], ANIMATION_URL, ANIMATION_FPS, true, ANIMATION_FIRST_FRAME, ANIMATION_LAST_FRAME); } } this.sitDown = function() { if (this.checkSeatForAvatar()) { print("Someone is already sitting in that chair."); return; } print("Sitting down (" + this.entityID + ")"); this.sittingDown = true; var now = Date.now(); this.sitDownSettlePeriod = now + IK_SETTLE_TIME; this.lastTimeNoDriveKeys = now; var previousValue = Settings.getValue(SETTING_KEY); Settings.setValue(SETTING_KEY, this.entityID); this.setSeatUser(MyAvatar.sessionUUID); if (previousValue === "") { MyAvatar.characterControllerEnabled = false; MyAvatar.hmdLeanRecenterEnabled = false; var roles = this.rolesToOverride(); for (i in roles) { MyAvatar.overrideRoleAnimation(roles[i], ANIMATION_URL, ANIMATION_FPS, true, ANIMATION_FIRST_FRAME, ANIMATION_LAST_FRAME); } for (var i in OVERRIDEN_DRIVE_KEYS) { MyAvatar.disableDriveKey(OVERRIDEN_DRIVE_KEYS[i]); } MyAvatar.resetSensorsAndBody(); } 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); MyAvatar.onLoadComplete.connect(this, this.modelURLChangeFinished); } this.standUp = function() { print("Standing up (" + this.entityID + ")"); MyAvatar.removeAnimationStateHandler(this.animStateHandlerID); Script.update.disconnect(this, this.update); MyAvatar.onLoadComplete.disconnect(this, this.modelURLChangeFinished); if (MyAvatar.sessionUUID === this.getSeatUser()) { this.setSeatUser(null); } if (Settings.getValue(SETTING_KEY) === this.entityID) { Settings.setValue(SETTING_KEY, ""); for (var i in OVERRIDEN_DRIVE_KEYS) { MyAvatar.enableDriveKey(OVERRIDEN_DRIVE_KEYS[i]); } var roles = this.rolesToOverride(); for (i in roles) { MyAvatar.restoreRoleAnimation(roles[i]); } MyAvatar.characterControllerEnabled = true; MyAvatar.hmdLeanRecenterEnabled = true; var index = MyAvatar.getJointIndex("Hips"); MyAvatar.clearPinOnJoint(index); MyAvatar.resetSensorsAndBody(); Script.setTimeout(function() { MyAvatar.bodyPitch = 0.0; MyAvatar.bodyRoll = 0.0; }, SIT_DELAY); } this.sittingDown = false; } //on trigger at a certain distance, sit in the chair (VR) this.startNearTrigger = function() { this.sitDown(); }; //on trigger at a certain distance, sit in the chair (VR) this.startFarTrigger = function() { var properties = Entities.getEntityProperties(this.entityID, ["position"]); if(Vec3.distance(MyAvatar.position, properties.position) < DESKTOP_MAX_DISTANCE){ this.sitDown(); } }; this.createOverlay = function() { var url; if(HMD.active){ //change the image based on what modality the user is in url = "http://hifi-content.s3.amazonaws.com/alexia/TriggerToSit.png"; } else { url = "http://hifi-content.s3.amazonaws.com/alexia/ClickToSit.png"; } this.overlay = Overlays.addOverlay("image3d", { position: { x: 0.0, y: 0.0, z: 0.0}, dimensions: { x: 0.1, y: 0.1 }, url: url, ignoreRayIntersection: false, alpha: ALPHA_START, visible: true, isFacingAvatar: true, emissive: true }); var overlayDimensions = { x: 0.3, y: 0.3 } var properties = Entities.getEntityProperties(this.entityID, ["position", "registrationPoint", "dimensions", "rotation"]); var yOffset = (0.8 - properties.registrationPoint.y) * properties.dimensions.y + (overlayDimensions.y / 2.0); var overlayPosition = Vec3.sum(properties.position, { x: 0, y: yOffset, z: 0 }); Overlays.editOverlay(this.overlay, { position: overlayPosition, dimensions: overlayDimensions, }); } // User can also click on overlay to sit down var that = this; this.mousePressOnOverlay = function (overlayID, pointerEvent) { if (overlayID === that.overlay && pointerEvent.isLeftButton) { that.sitDown(); } }; Overlays.mousePressOnOverlay.connect(this.mousePressOnOverlay); this.cleanupOverlay = function() { if (this.overlay !== null) { Overlays.deleteOverlay(this.overlay); this.overlay = null; } } this.update = function(dt) { if (this.sittingDown === true) { var properties = Entities.getEntityProperties(this.entityID); 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 if (ikError > MAX_IK_ERROR && now > this.sitDownSettlePeriod) { shouldStandUp = true; } if (MyAvatar.sessionUUID !== this.getSeatUser()) { shouldStandUp = true; } 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(); } } } this.canSitDesktop = function() { var properties = Entities.getEntityProperties(this.entityID, ["position"]); var distanceFromSeat = Vec3.distance(MyAvatar.position, properties.position); return distanceFromSeat < DESKTOP_MAX_DISTANCE && !this.checkSeatForAvatar(); } // This part is used to fade out the overlay over time this.lerpTransparency = function() { var startAlpha = ALPHA_START; var changeAlpha = 0.01; var that = this; this.transparencyInterval = Script.setInterval(function(){ startAlpha = startAlpha - changeAlpha; // My new alpha Overlays.editOverlay(that.overlay, {alpha : startAlpha}); // Edit the existing overlay if(startAlpha <= 0){ Script.clearInterval(that.transparencyInterval); // Stop my interval when it's faded out that.transparencyInterval = null; Overlays.editOverlay(that.overlay, { ignoreRayIntersection: true }); } }, 50); } this.showOverlays = function() { // Show overlays when I'm close to the seat if (isInEditMode() || this.interval !== null) { return; } if (this.overlay === null) { // Make an overlay if there isn't one if (this.canSitDesktop()) { this.createOverlay(); if(this.transparencyInterval === null){ this.lerpTransparency(); } } } else if (!this.canSitDesktop()) { this.cleanupOverlay(); } } var that = this; // Run my method to check if we've encountered a chair checkForHover = Script.setInterval(function(){ that.showOverlays(); }, DESKTOP_UI_CHECK_INTERVAL); this.clickDownOnEntity = function (id, event) { if (isInEditMode()) { return; } if (event.isPrimaryButton && this.canSitDesktop()) { this.sitDown(); } } });