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