Make sit script more stable.

This commit is contained in:
Atlante45 2017-03-13 17:22:06 -08:00
parent f0d971fc6f
commit 9e6312ba0e

View file

@ -10,8 +10,8 @@
var RELEASE_KEYS = ['w', 'a', 's', 'd', 'UP', 'LEFT', 'DOWN', 'RIGHT']; var RELEASE_KEYS = ['w', 'a', 's', 'd', 'UP', 'LEFT', 'DOWN', 'RIGHT'];
var RELEASE_TIME = 500; // ms var RELEASE_TIME = 500; // ms
var RELEASE_DISTANCE = 0.2; // meters var RELEASE_DISTANCE = 0.2; // meters
var MAX_IK_ERROR = 20; var MAX_IK_ERROR = 30;
var DESKTOP_UI_CHECK_INTERVAL = 250; var DESKTOP_UI_CHECK_INTERVAL = 100;
var DESKTOP_MAX_DISTANCE = 5; var DESKTOP_MAX_DISTANCE = 5;
var SIT_DELAY = 25 var SIT_DELAY = 25
var MAX_RESET_DISTANCE = 0.5 var MAX_RESET_DISTANCE = 0.5
@ -19,15 +19,16 @@
this.entityID = null; this.entityID = null;
this.timers = {}; this.timers = {};
this.animStateHandlerID = null; this.animStateHandlerID = null;
this.interval = null;
this.preload = function(entityID) { this.preload = function(entityID) {
this.entityID = entityID; this.entityID = entityID;
} }
this.unload = function() { this.unload = function() {
if (MyAvatar.sessionUUID === this.getSeatUser()) { if (Settings.getValue(SETTING_KEY) === this.entityID) {
this.sitUp(); this.sitUp();
} }
if (this.interval) { if (this.interval !== null) {
Script.clearInterval(this.interval); Script.clearInterval(this.interval);
this.interval = null; this.interval = null;
} }
@ -35,42 +36,60 @@
} }
this.setSeatUser = function(user) { this.setSeatUser = function(user) {
var userData = Entities.getEntityProperties(this.entityID, ["userData"]).userData; try {
userData = JSON.parse(userData); var userData = Entities.getEntityProperties(this.entityID, ["userData"]).userData;
userData = JSON.parse(userData);
if (user) { if (user !== null) {
userData.seat.user = user; userData.seat.user = user;
} else { } else {
delete userData.seat.user; delete userData.seat.user;
}
Entities.editEntity(this.entityID, {
userData: JSON.stringify(userData)
});
} catch (e) {
// Do Nothing
} }
Entities.editEntity(this.entityID, {
userData: JSON.stringify(userData)
});
} }
this.getSeatUser = function() { this.getSeatUser = function() {
var properties = Entities.getEntityProperties(this.entityID, ["userData", "position"]); try {
var userData = JSON.parse(properties.userData); var properties = Entities.getEntityProperties(this.entityID, ["userData", "position"]);
var userData = JSON.parse(properties.userData);
if (userData.seat.user && userData.seat.user !== MyAvatar.sessionUUID) { // If MyAvatar return my uuid
var avatar = AvatarList.getAvatar(userData.seat.user); if (userData.seat.user === MyAvatar.sessionUUID) {
if (avatar && Vec3.distance(avatar.position, properties.position) > RELEASE_DISTANCE) { return userData.seat.user;
return null;
} }
// If Avatar appears to be sitting
if (userData.seat.user) {
var avatar = AvatarList.getAvatar(userData.seat.user);
if (avatar && (Vec3.distance(avatar.position, properties.position) < RELEASE_DISTANCE)) {
return userData.seat.user;
}
}
} catch (e) {
// Do nothing
} }
return userData.seat.user;
// Nobody on the seat
return null;
} }
// Is the seat used
this.checkSeatForAvatar = function() { this.checkSeatForAvatar = function() {
var seatUser = this.getSeatUser(); var seatUser = this.getSeatUser();
var avatarIdentifiers = AvatarList.getAvatarIdentifiers();
for (var i in avatarIdentifiers) { // If MyAvatar appears to be sitting
var avatar = AvatarList.getAvatar(avatarIdentifiers[i]); if (seatUser === MyAvatar.sessionUUID) {
if (avatar && avatar.sessionUUID === seatUser) { var properties = Entities.getEntityProperties(this.entityID, ["position"]);
return true; return Vec3.distance(MyAvatar.position, properties.position) < RELEASE_DISTANCE;
}
} }
return false;
return seatUser !== null;
} }
this.sitDown = function() { this.sitDown = function() {
@ -79,10 +98,9 @@
return; return;
} }
this.setSeatUser(MyAvatar.sessionUUID);
var previousValue = Settings.getValue(SETTING_KEY); var previousValue = Settings.getValue(SETTING_KEY);
Settings.setValue(SETTING_KEY, this.entityID); Settings.setValue(SETTING_KEY, this.entityID);
this.setSeatUser(MyAvatar.sessionUUID);
if (previousValue === "") { if (previousValue === "") {
MyAvatar.characterControllerEnabled = false; MyAvatar.characterControllerEnabled = false;
MyAvatar.hmdLeanRecenterEnabled = false; MyAvatar.hmdLeanRecenterEnabled = false;
@ -92,32 +110,34 @@
MyAvatar.resetSensorsAndBody(); MyAvatar.resetSensorsAndBody();
} }
var that = this; var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
Script.setTimeout(function() { var index = MyAvatar.getJointIndex("Hips");
var properties = Entities.getEntityProperties(that.entityID, ["position", "rotation"]); MyAvatar.pinJoint(index, properties.position, properties.rotation);
var index = MyAvatar.getJointIndex("Hips");
MyAvatar.pinJoint(index, properties.position, properties.rotation);
that.animStateHandlerID = MyAvatar.addAnimationStateHandler(function(properties) { this.animStateHandlerID = MyAvatar.addAnimationStateHandler(function(properties) {
return { headType: 0 }; return { headType: 0 };
}, ["headType"]); }, ["headType"]);
Script.update.connect(that, that.update); Script.update.connect(this, this.update);
Controller.keyPressEvent.connect(that, that.keyPressed); Controller.keyPressEvent.connect(this, this.keyPressed);
Controller.keyReleaseEvent.connect(that, that.keyReleased); Controller.keyReleaseEvent.connect(this, this.keyReleased);
for (var i in RELEASE_KEYS) { for (var i in RELEASE_KEYS) {
Controller.captureKeyEvents({ text: RELEASE_KEYS[i] }); Controller.captureKeyEvents({ text: RELEASE_KEYS[i] });
} }
}, SIT_DELAY);
} }
this.sitUp = function() { this.sitUp = function() {
if (MyAvatar.sessionUUID !== this.getSeatUser()) { MyAvatar.removeAnimationStateHandler(this.animStateHandlerID);
// Duplicate call, bail Script.update.disconnect(this, this.update);
return; Controller.keyPressEvent.disconnect(this, this.keyPressed);
Controller.keyReleaseEvent.disconnect(this, this.keyReleased);
for (var i in RELEASE_KEYS) {
Controller.releaseKeyEvents({ text: RELEASE_KEYS[i] });
} }
this.setSeatUser(null);
this.setSeatUser(null);
if (Settings.getValue(SETTING_KEY) === this.entityID) { if (Settings.getValue(SETTING_KEY) === this.entityID) {
Settings.setValue(SETTING_KEY, "");
for (i in ROLES) { for (i in ROLES) {
MyAvatar.restoreRoleAnimation(ROLES[i]); MyAvatar.restoreRoleAnimation(ROLES[i]);
} }
@ -133,16 +153,6 @@
MyAvatar.bodyPitch = 0.0; MyAvatar.bodyPitch = 0.0;
MyAvatar.bodyRoll = 0.0; MyAvatar.bodyRoll = 0.0;
}, SIT_DELAY); }, SIT_DELAY);
Settings.setValue(SETTING_KEY, "");
}
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] });
} }
} }
@ -203,15 +213,11 @@
// Move avatar in front of the chair to avoid getting stuck in collision hulls // Move avatar in front of the chair to avoid getting stuck in collision hulls
if (avatarDistance < MAX_RESET_DISTANCE) { if (avatarDistance < MAX_RESET_DISTANCE) {
var offset = { x: 0, y: 1.0, z: -0.5 - properties.dimensions.z * properties.registrationPoint.z }; var offset = { x: 0, y: 1.0, z: -0.5 - properties.dimensions.z * properties.registrationPoint.z };
Vec3.print("offset:", Vec3.multiplyQbyV(properties.rotation, offset));
var position = Vec3.sum(properties.position, Vec3.multiplyQbyV(properties.rotation, offset)); var position = Vec3.sum(properties.position, Vec3.multiplyQbyV(properties.rotation, offset));
MyAvatar.position = position; MyAvatar.position = position;
} }
var that = this; this.sitUp();
Script.setTimeout(function() {
that.sitUp();
}, SIT_DELAY);
} }
} }
} }
@ -223,20 +229,19 @@
if (RELEASE_KEYS.indexOf(event.text) !== -1) { if (RELEASE_KEYS.indexOf(event.text) !== -1) {
var that = this; var that = this;
this.timers[event.text] = Script.setTimeout(function() { this.timers[event.text] = Script.setTimeout(function() {
delete that.timers[event.text];
var properties = Entities.getEntityProperties(that.entityID); var properties = Entities.getEntityProperties(that.entityID);
var avatarDistance = Vec3.distance(MyAvatar.position, properties.position); var avatarDistance = Vec3.distance(MyAvatar.position, properties.position);
// Move avatar in front of the chair to avoid getting stuck in collision hulls // Move avatar in front of the chair to avoid getting stuck in collision hulls
if (avatarDistance < MAX_RESET_DISTANCE) { if (avatarDistance < MAX_RESET_DISTANCE) {
var offset = { x: 0, y: 1.0, z: -0.5 - properties.dimensions.z * properties.registrationPoint.z }; var offset = { x: 0, y: 1.0, z: -0.5 - properties.dimensions.z * properties.registrationPoint.z };
Vec3.print("offset1:", Vec3.multiplyQbyV(properties.rotation, offset));
var position = Vec3.sum(properties.position, Vec3.multiplyQbyV(properties.rotation, offset)); var position = Vec3.sum(properties.position, Vec3.multiplyQbyV(properties.rotation, offset));
MyAvatar.position = position; MyAvatar.position = position;
} }
Script.setTimeout(function() { that.sitUp();
that.sitUp();
}, SIT_DELAY);
}, RELEASE_TIME); }, RELEASE_TIME);
} }
} }
@ -256,7 +261,7 @@
} }
this.hoverEnterEntity = function(event) { this.hoverEnterEntity = function(event) {
if (isInEditMode() || (MyAvatar.sessionUUID === this.getSeatUser())) { if (isInEditMode() || this.interval !== null) {
return; return;
} }
@ -272,7 +277,7 @@
}, DESKTOP_UI_CHECK_INTERVAL); }, DESKTOP_UI_CHECK_INTERVAL);
} }
this.hoverLeaveEntity = function(event) { this.hoverLeaveEntity = function(event) {
if (this.interval) { if (this.interval !== null) {
Script.clearInterval(this.interval); Script.clearInterval(this.interval);
this.interval = null; this.interval = null;
} }
@ -280,7 +285,7 @@
} }
this.clickDownOnEntity = function (id, event) { this.clickDownOnEntity = function (id, event) {
if (isInEditMode() || (MyAvatar.sessionUUID === this.getSeatUser())) { if (isInEditMode()) {
return; return;
} }
if (event.isPrimaryButton && this.canSitDesktop()) { if (event.isPrimaryButton && this.canSitDesktop()) {