using System; using Cielonos.MainGame.Inventory; using Cielonos.MainGame.UI; using Sirenix.OdinInspector; using UniRx; using Unity.Cinemachine; using UnityEngine; using UnityEngine.Events; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine.Serialization; namespace Cielonos.MainGame.Characters { public partial class PlayerInputSubcontroller : SubcontrollerBase { public Player player => owner; public PlayerInput playerInput; public PlayerInputActions inputActions; public PlayerOperationSubcontroller operation; public Vector2 Move { get; private set; } public Vector2 Look { get; private set; } public bool JumpPressed { get; private set; } public bool JumpHeld { get; private set; } public bool IsMoving => Move != Vector2.zero; public bool IsWalking { get; private set; } public string CurrentScheme => playerInput.currentControlScheme; [ShowInInspector] public bool IsHoldingPrimary { get; private set; } [ShowInInspector] public bool IsHoldingSecondary { get; private set; } [ShowInInspector] public bool IsHoldingSpecialA { get; private set; } [ShowInInspector] public bool IsHoldingSpecialB { get; private set; } [ShowInInspector] public bool IsHoldingSpecialC { get; private set; } public BoolReactiveProperty isCursorLocked; public PlayerPreinputSubmodule preinputSubmodule; public override void Initialize() { base.Initialize(); playerInput = owner.GetComponent(); inputActions = new PlayerInputActions(); inputActions.Player.Enable(); operation = owner.operationSc; isCursorLocked = new BoolReactiveProperty(true); isCursorLocked.Subscribe(isLocked => { Cursor.lockState = isLocked ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !isLocked; Look = Vector2.zero; Move = Vector2.zero; // 同步禁用 Cinemachine 的自有输入控制器,防止 UI 打开时视角继续转动 var cinemachineInput = player.viewSc.freeLookCamera.GetComponent(); cinemachineInput.enabled = isLocked; }); preinputSubmodule = new PlayerPreinputSubmodule(this); } private void Start() { RegisterActionsInputs(); RegisterMainWeaponInputs(); RegisterSupportEquipmentInputs(); RegisterFunctionInputs(); SceneManager.sceneLoaded += OnSceneLoaded; } private void Update() { if (isCursorLocked.Value) { Look = inputActions.Player.Look.ReadValue(); Move = inputActions.Player.Move.ReadValue().normalized; JumpPressed = inputActions.Player.Jump.WasPressedThisFrame(); JumpHeld = inputActions.Player.Jump.IsPressed(); } } private void OnDestroy() { inputActions.Disable(); inputActions.Dispose(); SceneManager.sceneLoaded -= OnSceneLoaded; } } public partial class PlayerInputSubcontroller { private void RegisterActionsInputs() { inputActions.Player.Interact.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.Interact(); } }; inputActions.Player.Dash.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { if (IsMoving) { Vector3 inputDirection = new Vector3(Move.x, 0, Move.y); operation.Dash(inputDirection); preinputSubmodule.RegisterPreinputAction(() => operation.Dash(inputDirection), 10); } else { operation.Dodge(); preinputSubmodule.RegisterPreinputAction(() => operation.Dodge(), 10); } } }; inputActions.Player.LockonTarget.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.LockonTarget(); } }; inputActions.Player.SelectLockonTarget.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { Debug.Log("Value: " + ctx.ReadValue()); operation.SelectLockonTarget(ctx.ReadValue()); } }; inputActions.Player.Walk.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { IsWalking = true; operation.WalkPress(); } }; inputActions.Player.Walk.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsWalking = false; operation.WalkRelease(); } }; } private MainWeaponBase currentMainWeapon => player.inventorySc.equipmentSm.currentMainWeapon; private void RegisterMainWeaponInputs() { inputActions.Player.MainWeaponPrimary.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingPrimary = true; operation.MainWeaponPrimaryPress(); if (!currentMainWeapon.disablePrimaryPreinput) { preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponPrimaryPress(), 0); } } }; inputActions.Player.MainWeaponPrimary.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingPrimary = false; operation.MainWeaponPrimaryRelease(); } }; inputActions.Player.MainWeaponSecondary.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingSecondary = true; operation.MainWeaponSecondaryPress(); if (!currentMainWeapon.disableSecondaryPreinput) { preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSecondaryPress(), 0); } } }; inputActions.Player.MainWeaponSecondary.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingSecondary = false; operation.MainWeaponSecondaryRelease(); } }; inputActions.Player.MainWeaponSpecialA.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingSpecialA = true; operation.MainWeaponSpecialAPress(); if (!currentMainWeapon.disableSpecialAPreinput) { preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSpecialAPress(), 0); } } }; inputActions.Player.MainWeaponSpecialA.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingSpecialA = false; operation.MainWeaponSpecialARelease(); } }; inputActions.Player.MainWeaponSpecialB.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingSpecialB = true; operation.MainWeaponSpecialBPress(); if (!currentMainWeapon.disableSpecialBPreinput) { preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSpecialBPress(), 0); } } }; inputActions.Player.MainWeaponSpecialB.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingSpecialB = false; operation.MainWeaponSpecialBRelease(); } }; inputActions.Player.MainWeaponSpecialC.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingSpecialC = true; operation.MainWeaponSpecialCPress(); if (!currentMainWeapon.disableSpecialCPreinput) { preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSpecialCPress(), 0); } } }; inputActions.Player.MainWeaponSpecialC.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingSpecialC = false; operation.MainWeaponSpecialCRelease(); } }; inputActions.Player.SwitchPreviousMainWeapon.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SwitchMainWeapon(-1); } }; inputActions.Player.SwitchNextMainWeapon.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SwitchMainWeapon(1); } }; inputActions.Player.RouletteSwitchMainWeapon.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.OpenMainWeaponRoulette(); } }; inputActions.Player.RouletteSwitchMainWeapon.canceled += ctx => { if (ctx.canceled) { operation.CloseMainWeaponRoulette(); } }; } private void RegisterSupportEquipmentInputs() { inputActions.Player.UseSupportEquipment0.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SupportEquipment0Press(); } }; inputActions.Player.UseSupportEquipment0.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { operation.SupportEquipment0Release(); } }; inputActions.Player.UseSupportEquipment1.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SupportEquipment1Press(); } }; inputActions.Player.UseSupportEquipment1.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { operation.SupportEquipment1Release(); } }; inputActions.Player.UseSupportEquipment2.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SupportEquipment2Press(); } }; inputActions.Player.UseSupportEquipment2.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { operation.SupportEquipment2Release(); } }; inputActions.Player.UseSupportEquipment3.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SupportEquipment3Press(); } }; inputActions.Player.UseSupportEquipment3.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { operation.SupportEquipment3Release(); } }; } private void RegisterFunctionInputs() { inputActions.Player.Map.performed += ctx => { var mapPage = PlayerCanvas.MainGamePages.mapPage; if(mapPage.IsOpen) mapPage.Close(); else mapPage.Open(); }; } } public partial class PlayerInputSubcontroller { private void OnSceneLoaded(Scene a, LoadSceneMode b) { isCursorLocked.Value = true; } } }