using UnityEngine; using UnityEngine.InputSystem; namespace GrassCutter { /// Input facade (keyboard + mouse, with gamepad basics). Gameplay input is muted while UI or the Steam overlay is open. public static class Ctl { public static bool UIOpen; /// The phone is up at your face: the cursor is the player's and you stand still, while the world goes on. public static bool PhoneFocus; /// A text box on the phone has the keyboard, so no key means anything to the game. public static bool Typing; /// Dev bots hold / tap the use buttons through these (they bypass focus checks). public static bool BotUseHeld, BotUsePressed, BotAltHeld, BotAltPressed; public static Vector2 BotMove; /// The test bots run. public static bool BotSprint; public static bool BotJumpPressed; /// The opening film is running: nobody moves. public static bool Cinematic; public static bool Blocked => UIOpen || PhoneFocus || Cinematic || SteamManager.OverlayActive || !Application.isFocused; static bool PhoneFree => !UIOpen && !Cinematic && !Typing && !SteamManager.OverlayActive && Application.isFocused; public static float Sensitivity = 1f, PadSensitivity = 1f; public static bool InvertY, SprintToggle; static Keyboard K => Keyboard.current; static Mouse M => Mouse.current; static Gamepad G => Gamepad.current; public static Vector2 Move { get { if (BotMove != Vector2.zero) return BotMove; if (Blocked) return Vector2.zero; Vector2 v = Vector2.zero; if (Binds.Held(Act.Forward)) v.y += 1; if (Binds.Held(Act.Back)) v.y -= 1; if (Binds.Held(Act.Right)) v.x += 1; if (Binds.Held(Act.Left)) v.x -= 1; if (G != null) v += G.leftStick.ReadValue(); return Vector2.ClampMagnitude(v, 1f); } } public static Vector2 Look { get { if (Blocked) return Vector2.zero; Vector2 v = Vector2.zero; if (M != null) v += M.delta.ReadValue() * 0.08f; if (G != null) v += G.rightStick.ReadValue() * 180f * Time.unscaledDeltaTime * PadSensitivity; v *= Sensitivity; if (InvertY) v.y = -v.y; return v; } } /// Run (on foot) or boost (driving): held, or with a press turns it on until you stop. public static bool Sprint { get { if (Blocked) { s_sprintOn = false; return false; } if (BotSprint) return true; if (G != null && G.leftStickButton.isPressed) return true; if (!SprintToggle) return Binds.Held(Act.Sprint); if (s_sprintFrame != Time.frameCount) { s_sprintFrame = Time.frameCount; if (Binds.Pressed(Act.Sprint)) s_sprintOn = !s_sprintOn; else if (Move.sqrMagnitude < 0.01f) s_sprintOn = false; } return s_sprintOn; } } static bool s_sprintOn; static int s_sprintFrame = -1; public static bool JumpHeld => !Blocked && (Binds.Held(Act.Jump) || (G != null && G.buttonSouth.isPressed)); /// Down while flying (the cheat fly mode). Ctrl only: C is Cruise. public static bool DownHeld => !Blocked && ((K != null && K.leftCtrlKey.isPressed) || (G != null && G.buttonEast.isPressed)); /// ~ / Ё (the key under Esc) or F1: the cheat console. Works even while UI is open. public static bool ConsolePressed => Application.isFocused && !Typing && K != null && (K.backquoteKey.wasPressedThisFrame || K.f1Key.wasPressedThisFrame); public static bool JumpPressed => BotJumpPressed || !Blocked && (Binds.Pressed(Act.Jump) || (G != null && G.buttonSouth.wasPressedThisFrame)); public static bool UseHeld => BotUseHeld || !Blocked && (Binds.Held(Act.Use) || (G != null && G.rightTrigger.isPressed)); public static bool UsePressed => BotUsePressed || !Blocked && (Binds.Pressed(Act.Use) || (G != null && G.rightTrigger.wasPressedThisFrame)); public static bool AltPressed => BotAltPressed || !Blocked && (Binds.Pressed(Act.Alt) || (G != null && G.leftTrigger.wasPressedThisFrame)); public static bool AltHeld => BotAltHeld || !Blocked && (Binds.Held(Act.Alt) || (G != null && G.leftTrigger.isPressed)); public static bool RotatePressed => !Blocked && (Binds.Pressed(Act.Rotate) || (G != null && G.buttonNorth.wasPressedThisFrame)); public static bool BuildPressed => !Blocked && Binds.Pressed(Act.Build); public static bool BotMovePressed, BotCopyPressed; public static bool CtrlHeld => !Blocked && K != null && K.leftCtrlKey.isPressed; public static bool MovePressed => BotMovePressed || !Blocked && Binds.Pressed(Act.MovePiece); public static bool CopyPressed => BotCopyPressed || !Blocked && Binds.Pressed(Act.Copy); public static bool FreePressed => !Blocked && Binds.Pressed(Act.Free); public static bool NotebookPressed => Application.isFocused && !Typing && !SteamManager.OverlayActive && Binds.Pressed(Act.Notebook); public static bool LampPressed => !Blocked && Binds.Pressed(Act.Lamp); /// R while driving: from the seat or from behind the machine. public static bool ViewPressed => !Blocked && (Binds.Pressed(Act.View) || (G != null && G.rightStickButton.wasPressedThisFrame)); public static bool CruisePressed => !Blocked && Binds.Pressed(Act.Cruise); public static bool BotBootsPressed; public static bool BootsPressed => BotBootsPressed || !Blocked && Binds.Pressed(Act.Boots); public static bool BotInteractPressed; public static bool InteractPressed => BotInteractPressed || !Blocked && (Binds.Pressed(Act.Interact) || (G != null && G.buttonWest.wasPressedThisFrame)); public static bool BotInteractHeld; public static bool InteractHeld => BotInteractHeld || !Blocked && (Binds.Held(Act.Interact) || (G != null && G.buttonWest.isPressed)); public static bool CategoryPressed => !Blocked && (Binds.Pressed(Act.Category) || (G != null && G.dpad.right.wasPressedThisFrame)); public static bool BotPackHeld; public static bool PackHeld => BotPackHeld || !Blocked && (Binds.Held(Act.Pack) || (G != null && G.dpad.left.isPressed)); public static bool PackPressed => !Blocked && (Binds.Pressed(Act.Pack) || (G != null && G.dpad.left.wasPressedThisFrame)); public static bool PausePressed => (K != null && K.escapeKey.wasPressedThisFrame) || (G != null && G.startButton.wasPressedThisFrame); public static bool MapPressed => Application.isFocused && !Typing && !SteamManager.OverlayActive && (Binds.Pressed(Act.Map) || (G != null && G.dpad.down.wasPressedThisFrame)); public static bool BotTabHeld; public static bool TabHeld => BotTabHeld || !Typing && ((Binds.Held(Act.Stats) && !(K != null && K.leftShiftKey.isPressed && Binds.Get(Act.Stats, 0) == (int)Key.Tab)) || (G != null && G.selectButton.isPressed)); public static bool PhonePressed => PhoneFree && Binds.Pressed(Act.Phone); public static bool PhoneUpPressed => PhoneFree && Binds.Pressed(Act.PhoneTap); public static bool PhoneTurnPressed => PhoneFree && Binds.Pressed(Act.PhoneTurn); public static float PhoneWheel => PhoneFree && Phone.TakesWheel && M != null ? M.scroll.ReadValue().y : 0f; public static Vector2 Pointer => M != null ? M.position.ReadValue() : Vector2.zero; public static bool PointerDown => PhoneFree && M != null && M.leftButton.wasPressedThisFrame; public static bool PointerHeld => M != null && M.leftButton.isPressed; public static bool PointerUp => M != null && M.leftButton.wasReleasedThisFrame; public static float Scroll { get { if (Blocked || Phone.TakesWheel) return 0f; float s = M != null ? M.scroll.ReadValue().y : 0f; if (G != null) { if (G.rightShoulder.wasPressedThisFrame) s -= 1f; if (G.leftShoulder.wasPressedThisFrame) s += 1f; } return s; } } /// Returns 0-7 when a number key 1-8 was pressed this frame, otherwise -1. public static int SlotPressed { get { if (Blocked || K == null) return -1; if (K.digit1Key.wasPressedThisFrame) return 0; if (K.digit2Key.wasPressedThisFrame) return 1; if (K.digit3Key.wasPressedThisFrame) return 2; if (K.digit4Key.wasPressedThisFrame) return 3; if (K.digit5Key.wasPressedThisFrame) return 4; if (K.digit6Key.wasPressedThisFrame) return 5; if (K.digit7Key.wasPressedThisFrame) return 6; if (K.digit8Key.wasPressedThisFrame) return 7; return -1; } } public static bool KeyDown(Key key) => K != null && K[key].wasPressedThisFrame; // Scripted demo/test runs never grab the mouse, so they can run while the user keeps working. static readonly bool Bot = CommandLine.Has("-autodemo") || CommandLine.Has("-mpdemo") || CommandLine.Has("-screenshot"); public static void SetCursor(bool locked) { if (Bot) locked = false; // free in the menus, but kept inside the game window Cursor.lockState = locked ? CursorLockMode.Locked : Bot ? CursorLockMode.None : CursorLockMode.Confined; Cursor.visible = !locked; } } }