diff --git a/scripts/system/away.js b/scripts/system/away.js index 95af49d070..f4a1a22bf6 100644 --- a/scripts/system/away.js +++ b/scripts/system/away.js @@ -16,6 +16,7 @@ (function() { // BEGIN LOCAL_SCOPE +var BASIC_TIMER_INTERVAL = 50; // 50ms = 20hz var OVERLAY_WIDTH = 1920; var OVERLAY_HEIGHT = 1080; var OVERLAY_DATA = { @@ -49,6 +50,21 @@ var AWAY_INTRO = { endFrame: 83.0 }; +// MAIN CONTROL +var isEnabled = true; +var wasMuted; // unknonwn? +var isAway = false; // we start in the un-away state +var wasOverlaysVisible = Menu.isOptionChecked("Overlays"); +var eventMappingName = "io.highfidelity.away"; // goActive on hand controller button events, too. +var eventMapping = Controller.newMapping(eventMappingName); +var avatarPosition = MyAvatar.position; +var wasHmdMounted = HMD.mounted; + + +// some intervals we may create/delete +var avatarMovedInterval; + + // prefetch the kneel animation and hold a ref so it's always resident in memory when we need it. var _animation = AnimationCache.prefetch(AWAY_INTRO.url); @@ -125,33 +141,28 @@ function maybeMoveOverlay() { var halfWayBetweenOldAndLookAt = Vec3.multiply(lookAtChange, EASE_BY_RATIO); var newOverlayPosition = Vec3.sum(lastOverlayPosition, halfWayBetweenOldAndLookAt); lastOverlayPosition = newOverlayPosition; - var actualOverlayPositon = moveCloserToCamera(lastOverlayPosition); Overlays.editOverlay(overlayHMD, { visible: true, position: actualOverlayPositon }); // make sure desktop version is hidden Overlays.editOverlay(overlay, { visible: false }); + + // also remember avatar position + avatarPosition = MyAvatar.position; + } } } function ifAvatarMovedGoActive() { - if (Vec3.distance(MyAvatar.position, avatarPosition) > AVATAR_MOVE_FOR_ACTIVE_DISTANCE) { + var newAvatarPosition = MyAvatar.position; + if (Vec3.distance(newAvatarPosition, avatarPosition) > AVATAR_MOVE_FOR_ACTIVE_DISTANCE) { goActive(); } + avatarPosition = newAvatarPosition; } -// MAIN CONTROL -var isEnabled = true; -var wasMuted, isAway; -var wasOverlaysVisible = Menu.isOptionChecked("Overlays"); -var eventMappingName = "io.highfidelity.away"; // goActive on hand controller button events, too. -var eventMapping = Controller.newMapping(eventMappingName); -var avatarPosition = MyAvatar.position; - -var wasHmdMounted = HMD.mounted; - -function goAway() { +function goAway(fromStartup) { if (!isEnabled || isAway) { return; } @@ -159,7 +170,6 @@ function goAway() { UserActivityLogger.toggledAway(true); isAway = true; - print('going "away"'); wasMuted = AudioDevice.getMuted(); if (!wasMuted) { AudioDevice.toggleMute(); @@ -184,7 +194,18 @@ function goAway() { wasHmdMounted = HMD.mounted; // always remember the correct state avatarPosition = MyAvatar.position; - Script.update.connect(ifAvatarMovedGoActive); + + // If we're entering away mode from some other state than startup, then we create our move timer immediately. + // However if we're just stating up, we need to delay this process so that we don't think the initial teleport + // is actually a move. + if (fromStartup === undefined || fromStartup === false) { + avatarMovedInterval = Script.setInterval(ifAvatarMovedGoActive, BASIC_TIMER_INTERVAL); + } else { + var WAIT_FOR_MOVE_ON_STARTUP = 3000; // 3 seconds + Script.setTimeout(function() { + avatarMovedInterval = Script.setInterval(ifAvatarMovedGoActive, BASIC_TIMER_INTERVAL); + }, WAIT_FOR_MOVE_ON_STARTUP); + } } function goActive() { @@ -195,7 +216,6 @@ function goActive() { UserActivityLogger.toggledAway(false); isAway = false; - print('going "active"'); if (!wasMuted) { AudioDevice.toggleMute(); } @@ -224,7 +244,7 @@ function goActive() { } wasHmdMounted = HMD.mounted; // always remember the correct state - Script.update.disconnect(ifAvatarMovedGoActive); + Script.clearInterval(avatarMovedInterval); } function maybeGoActive(event) { @@ -242,12 +262,12 @@ var wasHmdActive = HMD.active; var wasMouseCaptured = Reticle.mouseCaptured; function maybeGoAway() { - // If our active state change (went to or from HMD mode), and we are now in the HMD, go into away if (HMD.active !== wasHmdActive) { wasHmdActive = !wasHmdActive; if (wasHmdActive) { goAway(); + return; } } @@ -258,11 +278,13 @@ function maybeGoAway() { wasMouseCaptured = !wasMouseCaptured; if (!wasMouseCaptured) { goAway(); + return; } } // If you've removed your HMD from your head, and we can detect it, we will also go away... if (HMD.mounted != wasHmdMounted) { + wasHmdMounted = HMD.mounted; print("HMD mounted changed..."); // We're putting the HMD on... switch to those devices @@ -273,14 +295,13 @@ function maybeGoAway() { if (HMD.active) { goAway(); + return; } } } - wasHmdMounted = HMD.mounted; } function setEnabled(value) { - print("setting away enabled: ", value); if (!value) { goActive(); } @@ -297,9 +318,12 @@ var handleMessage = function(channel, message, sender) { Messages.subscribe(CHANNEL_AWAY_ENABLE); Messages.messageReceived.connect(handleMessage); -Script.update.connect(maybeMoveOverlay); +var maybeIntervalTimer = Script.setInterval(function(){ + maybeMoveOverlay(); + maybeGoAway(); +}, BASIC_TIMER_INTERVAL); + -Script.update.connect(maybeGoAway); Controller.mousePressEvent.connect(goActive); Controller.keyPressEvent.connect(maybeGoActive); // Note peek() so as to not interfere with other mappings. @@ -320,7 +344,7 @@ eventMapping.from(Controller.Standard.Start).peek().to(goActive); Controller.enableMapping(eventMappingName); Script.scriptEnding.connect(function () { - Script.update.disconnect(maybeGoAway); + Script.clearInterval(maybeIntervalTimer); goActive(); Controller.disableMapping(eventMappingName); Controller.mousePressEvent.disconnect(goActive); @@ -329,7 +353,7 @@ Script.scriptEnding.connect(function () { if (HMD.active && !HMD.mounted) { print("Starting script, while HMD is active and not mounted..."); - goAway(); + goAway(true); }