From 35dd45ddf9f3829bb2707555b5d7805006b7cd81 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 15 Mar 2017 17:39:38 -0700 Subject: [PATCH] Capture drive keys while avatar is seated --- scripts/tutorials/entity_scripts/sit.js | 99 +++++++++++++------------ 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/scripts/tutorials/entity_scripts/sit.js b/scripts/tutorials/entity_scripts/sit.js index ffce196572..42cf7685ad 100644 --- a/scripts/tutorials/entity_scripts/sit.js +++ b/scripts/tutorials/entity_scripts/sit.js @@ -6,26 +6,28 @@ 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 var MAX_IK_ERROR = 30; + var IK_SETTLE_TIME = 250; // ms var DESKTOP_UI_CHECK_INTERVAL = 100; var DESKTOP_MAX_DISTANCE = 5; - var SIT_DELAY = 25 - var MAX_RESET_DISTANCE = 0.5 + var SIT_DELAY = 25; + var MAX_RESET_DISTANCE = 0.5; // meters + var OVERRIDEN_DRIVE_KEYS = [0, 1, 2, 4, 5, 6]; this.entityID = null; - this.timers = {}; this.animStateHandlerID = null; this.interval = null; + this.sitDownTimestamp = null; + this.lastTimeNoDriveKeys = null; this.preload = function(entityID) { this.entityID = entityID; } this.unload = function() { if (Settings.getValue(SETTING_KEY) === this.entityID) { - this.sitUp(); + this.standUp(); } if (this.interval !== null) { Script.clearInterval(this.interval); @@ -96,6 +98,10 @@ print("Someone is already sitting in that chair."); return; } + print("Sitting down (" + this.entityID + ")"); + + this.sitDownTimestamp = Date.now(); + this.lastTimeNoDriveKeys = this.sitDownTimestamp; var previousValue = Settings.getValue(SETTING_KEY); Settings.setValue(SETTING_KEY, this.entityID); @@ -118,20 +124,17 @@ return { headType: 0 }; }, ["headType"]); Script.update.connect(this, this.update); - Controller.keyPressEvent.connect(this, this.keyPressed); - Controller.keyReleaseEvent.connect(this, this.keyReleased); - for (var i in RELEASE_KEYS) { - Controller.captureKeyEvents({ text: RELEASE_KEYS[i] }); + for (var i in OVERRIDEN_DRIVE_KEYS) { + MyAvatar.captureDriveKey(OVERRIDEN_DRIVE_KEYS[i]); } } - this.sitUp = function() { + this.standUp = function() { + print("Standing up (" + this.entityID + ")"); MyAvatar.removeAnimationStateHandler(this.animStateHandlerID); 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] }); + for (var i in OVERRIDEN_DRIVE_KEYS) { + MyAvatar.releaseDriveKey(OVERRIDEN_DRIVE_KEYS[i]); } this.setSeatUser(null); @@ -156,6 +159,7 @@ } } + // function called by teleport.js if it detects the appropriate userData this.sit = function () { this.sitDown(); } @@ -207,7 +211,36 @@ var properties = Entities.getEntityProperties(this.entityID); var avatarDistance = Vec3.distance(MyAvatar.position, properties.position); var ikError = MyAvatar.getIKErrorOnLastSolve(); - if (avatarDistance > RELEASE_DISTANCE || ikError > MAX_IK_ERROR) { + 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) { + var elapsed = now - this.sitDownTimestamp; + if (elapsed > IK_SETTLE_TIME) { + 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 @@ -215,45 +248,13 @@ 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.") } - this.sitUp(); + this.standUp(); } } } - this.keyPressed = function(event) { - if (isInEditMode()) { - return; - } - - if (RELEASE_KEYS.indexOf(event.text) !== -1) { - var that = this; - this.timers[event.text] = Script.setTimeout(function() { - delete that.timers[event.text]; - - var properties = Entities.getEntityProperties(that.entityID); - var avatarDistance = Vec3.distance(MyAvatar.position, properties.position); - - // 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; - } - - 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]; - } - } - } - this.canSitDesktop = function() { var properties = Entities.getEntityProperties(this.entityID, ["position"]); var distanceFromSeat = Vec3.distance(MyAvatar.position, properties.position);