using System; using Sirenix.OdinInspector; using UniRx; using UnityEngine; using UnityEngine.Events; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine.Serialization; namespace Cielonos.MainGame.Characters { public partial class PlayerInputSubcontroller : SubcontrollerBase, IPlayerSubcontroller { 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; public bool IsHoldingPrimary { get; private set; } public bool IsHoldingSecondary { get; private set; } public bool IsHoldingTertiary { get; private set; } public bool IsHoldingQuaternary { get; private set; } public bool IsHoldingQuinary { 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; }); preinputSubmodule = new PlayerPreinputSubmodule(this); } private void Start() { RegisterActionsInputs(); RegisterMainWeaponInputs(); RegisterSupportEquipmentAndConsumableItemInputs(); 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) { operation.Dash(); } else { operation.Dodge(); } } }; inputActions.Player.LockOnTarget.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.LockOnTarget(); } }; 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 void RegisterMainWeaponInputs() { inputActions.Player.MainWeaponPrimary.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingPrimary = true; operation.MainWeaponPrimaryPress(); } }; 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(); } }; inputActions.Player.MainWeaponSecondary.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingSecondary = false; operation.MainWeaponSecondaryRelease(); } }; inputActions.Player.MainWeaponTertiary.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingTertiary = true; operation.MainWeaponTertiaryPress(); } }; inputActions.Player.MainWeaponTertiary.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingTertiary = false; operation.MainWeaponTertiaryRelease(); } }; inputActions.Player.MainWeaponQuaternary.started += ctx => { if (ctx.started && isCursorLocked.Value) { IsHoldingQuaternary = true; operation.MainWeaponQuaternaryPress(); } }; inputActions.Player.MainWeaponQuaternary.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingQuaternary = false; operation.MainWeaponQuaternaryRelease(); } }; inputActions.Player.MainWeaponQuinary.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.MainWeaponQuinaryHold(); } }; inputActions.Player.MainWeaponQuinary.canceled += ctx => { if (ctx.canceled && isCursorLocked.Value) { IsHoldingQuinary = false; operation.MainWeaponQuinaryRelease(); } }; inputActions.Player.SwitchMainWeapon.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.SwitchMainWeapon(ctx.ReadValue()); } }; inputActions.Player.FastSwitchMainWeapon.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.FastSwitchMainWeapon(); } }; inputActions.Player.RouletteSwitchMainWeapon.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.OpenMainWeaponRoulette(); } }; inputActions.Player.RouletteSwitchMainWeapon.canceled += ctx => { if (ctx.canceled) { operation.CloseMainWeaponRoulette(); } }; } private void RegisterSupportEquipmentAndConsumableItemInputs() { inputActions.Player.UseSupportEquipment0.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.UseSupportEquipment0(); } }; inputActions.Player.UseSupportEquipment1.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.UseSupportEquipment1(); } }; inputActions.Player.UseSupportEquipment2.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.UseSupportEquipment2(); } }; inputActions.Player.UseSupportEquipment3.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.UseSupportEquipment3(); } }; inputActions.Player.UseConsumableItem.performed += ctx => { if (ctx.performed && isCursorLocked.Value) { operation.UseConsumableItem(); } }; } } public partial class PlayerInputSubcontroller { private void OnSceneLoaded(Scene a, LoadSceneMode b) { isCursorLocked.Value = true; } } }