using System; using Cielonos.MainGame.Inventory; 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 IsHoldingSpecialA { get; private set; } public bool IsHoldingSpecialB { 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) { 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.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.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; } } }