From 07ecf604857f2aa5158d985191fa7cbdd33ecdee Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Thu, 5 Jan 2017 13:48:31 -0800 Subject: [PATCH] remove sit when close --- scripts/tutorials/entity_scripts/chair.js | 35 ++++++++++------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js index 379450c1f6..f94f3d76e4 100644 --- a/scripts/tutorials/entity_scripts/chair.js +++ b/scripts/tutorials/entity_scripts/chair.js @@ -13,9 +13,9 @@ (function(){ - var UPDATE_INTERVAL_MSECS = 1000; // Update the spring/object at 45Hz + var CHECK_INTERVAL_MSECS = 250; // When sitting, check for need to stand up var SETTINGS_INTERVAL_MSECS = 1000; // Periodically check user data for updates - var DEFAULT_SIT_DISTANCE = 1.0; // How strong/stiff is the spring? + var DEFAULT_SIT_DISTANCE = 1.0; // How far away from the chair can you sit? var HYSTERESIS = 1.1; var sitTarget = { x: 0, y: 0, z: 0 }; // Offset where your butt should go relative @@ -70,6 +70,7 @@ rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("LeftLeg")); MyAvatar.setJointData("LeftLeg", Quat.fromPitchYawRollDegrees(LOWER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("LeftLeg")); } + function leaveSitPose() { MyAvatar.clearJointData("RightUpLeg"); MyAvatar.clearJointData("LeftUpLeg"); @@ -77,11 +78,14 @@ MyAvatar.clearJointData("LeftLeg"); } - function moveToSeat(position, rotation) { + function sitDown(position, rotation) { var eulers = Quat.safeEulerAngles(MyAvatar.orientation); eulers.y = Quat.safeEulerAngles(rotation).y; MyAvatar.position = Vec3.sum(position, Vec3.multiplyQbyV(props.rotation, sitTarget)); MyAvatar.orientation = Quat.fromPitchYawRollDegrees(eulers.x, eulers.y, eulers.z); + + enterSitPose(); + state = SITTING; } this.preload = function(entityID) { @@ -89,23 +93,18 @@ debugPrint("chair preload"); entity = entityID; _this = this; - checkTimer = Script.setInterval(this.maybeSitOrStand, UPDATE_INTERVAL_MSECS); settingsTimer = Script.setInterval(this.checkSettings, SETTINGS_INTERVAL_MSECS); }; - this.maybeSitOrStand = function() { + this.maybeStand = function() { props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); // First, check if the entity is far enough away to not need to do anything with it var howFar = howFarAway(props.position); - if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, sitDistance)) { - moveToSeat(props.position, props.rotation); - enterSitPose(); - state = SITTING; - debugPrint("Sitting from being close"); - } else if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) { + if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) { leaveSitPose(); + Script.clearInterval(checkTimer); + checkTimer = null; state = STANDING; - MyAvatar.characterControllerEnabled = true; debugPrint("Standing"); } } @@ -114,9 +113,8 @@ // If entity is clicked, sit props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) { - moveToSeat(props.position, props.rotation); - enterSitPose(); - state = SITTING; + sitDown(props.position, props.rotation); + checkTimer = Script.setInterval(this.maybeStand, CHECK_INTERVAL_MSECS); debugPrint("Sitting from mouse click"); } } @@ -125,9 +123,8 @@ // If entity is far clicked, sit props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) { - moveToSeat(props.position, props.rotation); - enterSitPose(); - state = SITTING; + sitDown(props.position, props.rotation); + checkTimer = Script.setInterval(this.maybeStand, CHECK_INTERVAL_MSECS); debugPrint("Sitting from far trigger"); } } @@ -159,8 +156,6 @@ } } - - this.unload = function(entityID) { debugPrint("chair unload"); if (checkTimer) {