Showing the changes to scripts/tutorials/entity_scripts (1 of 1 file). Show all
Unified · Split
| @@ -13,9 +13,9 @@ · 12 unmodified lines | ||
| 13 | 13 | |
| 14 | 14 | (function(){ |
| 15 | 15 | |
| 16 | − var UPDATE_INTERVAL_MSECS = 1000; // Update the spring/object at 45Hz | |
| 16 | + var CHECK_INTERVAL_MSECS = 250; // When sitting, check for need to stand up | |
| 17 | 17 | var SETTINGS_INTERVAL_MSECS = 1000; // Periodically check user data for updates |
| 18 | − var DEFAULT_SIT_DISTANCE = 1.0; // How strong/stiff is the spring? | |
| 18 | + var DEFAULT_SIT_DISTANCE = 1.0; // How far away from the chair can you sit? | |
| 19 | 19 | var HYSTERESIS = 1.1; |
| 20 | 20 | |
| 21 | 21 | var sitTarget = { x: 0, y: 0, z: 0 }; // Offset where your butt should go relative |
| @@ -70,6 +70,7 @@ · 48 unmodified lines | ||
| 70 | 70 | rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("LeftLeg")); |
| 71 | 71 | MyAvatar.setJointData("LeftLeg", Quat.fromPitchYawRollDegrees(LOWER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("LeftLeg")); |
| 72 | 72 | } |
| 73 | + | |
| 73 | 74 | function leaveSitPose() { |
| 74 | 75 | MyAvatar.clearJointData("RightUpLeg"); |
| 75 | 76 | MyAvatar.clearJointData("LeftUpLeg"); |
| @@ -77,11 +78,14 @@ · 1 unmodified line | ||
| 77 | 78 | MyAvatar.clearJointData("LeftLeg"); |
| 78 | 79 | } |
| 79 | 80 | |
| 80 | − function moveToSeat(position, rotation) { | |
| 81 | + function sitDown(position, rotation) { | |
| 81 | 82 | var eulers = Quat.safeEulerAngles(MyAvatar.orientation); |
| 82 | 83 | eulers.y = Quat.safeEulerAngles(rotation).y; |
| 83 | 84 | MyAvatar.position = Vec3.sum(position, Vec3.multiplyQbyV(props.rotation, sitTarget)); |
| 84 | 85 | MyAvatar.orientation = Quat.fromPitchYawRollDegrees(eulers.x, eulers.y, eulers.z); |
| 86 | + | |
| 87 | + enterSitPose(); | |
| 88 | + state = SITTING; | |
| 85 | 89 | } |
| 86 | 90 | |
| 87 | 91 | this.preload = function(entityID) { |
| @@ -89,23 +93,18 @@ · 1 unmodified line | ||
| 89 | 93 | debugPrint("chair preload"); |
| 90 | 94 | entity = entityID; |
| 91 | 95 | _this = this; |
| 92 | − checkTimer = Script.setInterval(this.maybeSitOrStand, UPDATE_INTERVAL_MSECS); | |
| 93 | 96 | settingsTimer = Script.setInterval(this.checkSettings, SETTINGS_INTERVAL_MSECS); |
| 94 | 97 | }; |
| 95 | 98 | |
| 96 | − this.maybeSitOrStand = function() { | |
| 99 | + this.maybeStand = function() { | |
| 97 | 100 | props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); |
| 98 | 101 | // First, check if the entity is far enough away to not need to do anything with it |
| 99 | 102 | var howFar = howFarAway(props.position); |
| 100 | − if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, sitDistance)) { | |
| 101 | − moveToSeat(props.position, props.rotation); | |
| 102 | − enterSitPose(); | |
| 103 | − state = SITTING; | |
| 104 | − debugPrint("Sitting from being close"); | |
| 105 | − } else if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) { | |
| 103 | + if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) { | |
| 106 | 104 | leaveSitPose(); |
| 105 | + Script.clearInterval(checkTimer); | |
| 106 | + checkTimer = null; | |
| 107 | 107 | state = STANDING; |
| 108 | − MyAvatar.characterControllerEnabled = true; | |
| 109 | 108 | debugPrint("Standing"); |
| 110 | 109 | } |
| 111 | 110 | } |
| @@ -114,9 +113,8 @@ · 2 unmodified lines | ||
| 114 | 113 | // If entity is clicked, sit |
| 115 | 114 | props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); |
| 116 | 115 | if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) { |
| 117 | − moveToSeat(props.position, props.rotation); | |
| 118 | − enterSitPose(); | |
| 119 | − state = SITTING; | |
| 116 | + sitDown(props.position, props.rotation); | |
| 117 | + checkTimer = Script.setInterval(this.maybeStand, CHECK_INTERVAL_MSECS); | |
| 120 | 118 | debugPrint("Sitting from mouse click"); |
| 121 | 119 | } |
| 122 | 120 | } |
| @@ -125,9 +123,8 @@ · 2 unmodified lines | ||
| 125 | 123 | // If entity is far clicked, sit |
| 126 | 124 | props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); |
| 127 | 125 | if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) { |
| 128 | − moveToSeat(props.position, props.rotation); | |
| 129 | − enterSitPose(); | |
| 130 | − state = SITTING; | |
| 126 | + sitDown(props.position, props.rotation); | |
| 127 | + checkTimer = Script.setInterval(this.maybeStand, CHECK_INTERVAL_MSECS); | |
| 131 | 128 | debugPrint("Sitting from far trigger"); |
| 132 | 129 | } |
| 133 | 130 | } |
| @@ -159,8 +156,6 @@ · 25 unmodified lines | ||
| 159 | 156 | } |
| 160 | 157 | } |
| 161 | 158 | |
| 162 | − | |
| 163 | − | |
| 164 | 159 | this.unload = function(entityID) { |
| 165 | 160 | debugPrint("chair unload"); |
| 166 | 161 | if (checkTimer) { |