114 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-02-15 15:15:17 -08:00
(function() {
Script.include("/~/system/libraries/utils.js");
2017-02-15 18:10:33 -08:00
var ROLE = "fly";
2017-02-16 11:12:11 -08:00
var ANIMATION_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/animations/sitting_idle.fbx";
2017-02-15 15:15:17 -08:00
var ANIMATION_FPS = 30;
var ANIMATION_FIRST_FRAME = 1;
var ANIMATION_LAST_FRAME = 10;
var RELEASE_KEYS = ['w', 'a', 's', 'd', 'UP', 'LEFT', 'DOWN', 'RIGHT'];
var RELEASE_TIME = 500; // ms
var RELEASE_DISTANCE = 0.2; // meters
2017-02-16 17:49:17 -08:00
var MAX_IK_ERROR = 15;
2017-02-15 15:15:17 -08:00
this.entityID = null;
this.timers = {};
this.animStateHandlerID = null;
2017-02-15 15:15:17 -08:00
this.preload = function(entityID) {
this.entityID = entityID;
}
2017-02-15 18:10:33 -08:00
this.update = function(dt) {
if (MyAvatar.getParentID() === this.entityID) {
var properties = Entities.getEntityProperties(this.entityID, ["position"]);
2017-02-16 17:49:17 -08:00
var avatarDistance = Vec3.distance(MyAvatar.position, properties.position);
var ikError = MyAvatar.getIKErrorOnLastSolve();
print("IK error: " + ikError);
if (avatarDistance > RELEASE_DISTANCE || ikError > MAX_IK_ERROR) {
2017-02-15 18:10:33 -08:00
this.sitUp(this.entityID);
}
}
}
2017-02-15 15:15:17 -08:00
this.keyPressed = function(event) {
if (isInEditMode()) {
return;
}
2017-02-15 15:15:17 -08:00
if (RELEASE_KEYS.indexOf(event.text) !== -1) {
var that = this;
this.timers[event.text] = Script.setTimeout(function() {
that.sitUp();
}, RELEASE_TIME);
2017-02-15 15:15:17 -08:00
}
}
this.keyReleased = function(event) {
if (RELEASE_KEYS.indexOf(event.text) !== -1) {
if (this.timers[event.text]) {
print("Clear timeout");
Script.clearTimeout(this.timers[event.text]);
delete this.timers[event.text];
2017-02-15 15:15:17 -08:00
}
}
}
2017-02-15 18:10:33 -08:00
this.sitDown = function() {
var avatarIdentifiers = AvatarList.getAvatarIdentifiers();
for (var i in avatarIdentifiers) {
var avatar = AvatarList.getAvatar(avatarIdentifiers[i]);
if (avatar && avatar.getParentID() === this.entityID) {
print("Someone is already sitting in that chair.");
return;
}
}
2017-02-15 18:10:33 -08:00
MyAvatar.overrideRoleAnimation(ROLE, ANIMATION_URL, ANIMATION_FPS, true, ANIMATION_FIRST_FRAME, ANIMATION_LAST_FRAME);
MyAvatar.setParentID(this.entityID);
2017-02-15 15:15:17 -08:00
MyAvatar.characterControllerEnabled = false;
2017-02-15 18:10:33 -08:00
MyAvatar.hmdLeanRecenterEnabled = false;
2017-02-15 15:15:17 -08:00
2017-02-15 18:10:33 -08:00
var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
2017-02-15 15:15:17 -08:00
var index = MyAvatar.getJointIndex("Hips");
MyAvatar.pinJoint(index, properties.position, properties.rotation);
this.animStateHandlerID = MyAvatar.addAnimationStateHandler(function(props) {
return { headType: 0 };
}, ["headType"]);
2017-02-15 18:10:33 -08:00
Script.update.connect(this, this.update);
Controller.keyPressEvent.connect(this, this.keyPressed);
Controller.keyReleaseEvent.connect(this, this.keyReleased);
for (var i in RELEASE_KEYS) {
Controller.captureKeyEvents({ text: RELEASE_KEYS[i] });
}
2017-02-15 15:15:17 -08:00
}
this.sitUp = function() {
2017-02-15 18:10:33 -08:00
MyAvatar.restoreRoleAnimation(ROLE);
2017-02-15 15:15:17 -08:00
MyAvatar.setParentID("");
MyAvatar.characterControllerEnabled = true;
2017-02-15 18:10:33 -08:00
MyAvatar.hmdLeanRecenterEnabled = true;
2017-02-15 15:15:17 -08:00
var index = MyAvatar.getJointIndex("Hips");
MyAvatar.clearPinOnJoint(index);
MyAvatar.removeAnimationStateHandler(this.animStateHandlerID);
2017-02-15 18:10:33 -08:00
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] });
}
2017-02-15 15:15:17 -08:00
}
2017-02-15 18:10:33 -08:00
this.clickDownOnEntity = function () {
if (MyAvatar.getParentID() !== this.entityID) {
this.sitDown();
2017-02-15 15:15:17 -08:00
}
}
2017-02-15 18:10:33 -08:00
this.sit = function () {
this.sitDown();
}
2017-02-15 15:15:17 -08:00
});