History

remove sit when close

Philip Rosedale committed 9 years ago

Commit 07ecf601 parent 65b9aae1 file · +15 −20Browse files

Showing the changes to scripts/tutorials/entity_scripts (1 of 1 file). Show all

Unified · Split

scripts/tutorials/entity_scripts/chair.js+15−20
@@ -13,9 +13,9 @@ · 12 unmodified lines
1313
1414 (function(){
1515
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
1717 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?
1919 var HYSTERESIS = 1.1;
2020
2121 var sitTarget = { x: 0, y: 0, z: 0 }; // Offset where your butt should go relative
@@ -70,6 +70,7 @@ · 48 unmodified lines
7070 rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("LeftLeg"));
7171 MyAvatar.setJointData("LeftLeg", Quat.fromPitchYawRollDegrees(LOWER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("LeftLeg"));
7272 }
73+
7374 function leaveSitPose() {
7475 MyAvatar.clearJointData("RightUpLeg");
7576 MyAvatar.clearJointData("LeftUpLeg");
@@ -77,11 +78,14 @@ · 1 unmodified line
7778 MyAvatar.clearJointData("LeftLeg");
7879 }
7980
80− function moveToSeat(position, rotation) {
81+ function sitDown(position, rotation) {
8182 var eulers = Quat.safeEulerAngles(MyAvatar.orientation);
8283 eulers.y = Quat.safeEulerAngles(rotation).y;
8384 MyAvatar.position = Vec3.sum(position, Vec3.multiplyQbyV(props.rotation, sitTarget));
8485 MyAvatar.orientation = Quat.fromPitchYawRollDegrees(eulers.x, eulers.y, eulers.z);
86+
87+ enterSitPose();
88+ state = SITTING;
8589 }
8690
8791 this.preload = function(entityID) {
@@ -89,23 +93,18 @@ · 1 unmodified line
8993 debugPrint("chair preload");
9094 entity = entityID;
9195 _this = this;
92− checkTimer = Script.setInterval(this.maybeSitOrStand, UPDATE_INTERVAL_MSECS);
9396 settingsTimer = Script.setInterval(this.checkSettings, SETTINGS_INTERVAL_MSECS);
9497 };
9598
96− this.maybeSitOrStand = function() {
99+ this.maybeStand = function() {
97100 props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
98101 // First, check if the entity is far enough away to not need to do anything with it
99102 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)) {
106104 leaveSitPose();
105+ Script.clearInterval(checkTimer);
106+ checkTimer = null;
107107 state = STANDING;
108− MyAvatar.characterControllerEnabled = true;
109108 debugPrint("Standing");
110109 }
111110 }
@@ -114,9 +113,8 @@ · 2 unmodified lines
114113 // If entity is clicked, sit
115114 props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
116115 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);
120118 debugPrint("Sitting from mouse click");
121119 }
122120 }
@@ -125,9 +123,8 @@ · 2 unmodified lines
125123 // If entity is far clicked, sit
126124 props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
127125 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);
131128 debugPrint("Sitting from far trigger");
132129 }
133130 }
@@ -159,8 +156,6 @@ · 25 unmodified lines
159156 }
160157 }
161158
162−
163−
164159 this.unload = function(entityID) {
165160 debugPrint("chair unload");
166161 if (checkTimer) {