Files
Cielonos/Assets/Scripts/MainGame/Characters/Player/Input/PlayerInputSubcontroller.cs

395 lines
14 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
using System;
2026-05-23 08:27:50 -04:00
using Cielonos.MainGame.Inventory;
2026-05-10 11:47:55 -04:00
using Cielonos.MainGame.UI;
2025-11-25 08:19:33 -05:00
using Sirenix.OdinInspector;
using UniRx;
2026-05-10 11:47:55 -04:00
using Unity.Cinemachine;
2025-11-25 08:19:33 -05:00
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
namespace Cielonos.MainGame.Characters
{
2026-02-13 09:22:11 -05:00
public partial class PlayerInputSubcontroller : SubcontrollerBase<Player>
2025-11-25 08:19:33 -05:00
{
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;
2026-02-13 09:22:11 -05:00
[ShowInInspector]
2025-11-25 08:19:33 -05:00
public bool IsHoldingPrimary { get; private set; }
2026-02-13 09:22:11 -05:00
[ShowInInspector]
2025-11-25 08:19:33 -05:00
public bool IsHoldingSecondary { get; private set; }
2026-02-13 09:22:11 -05:00
[ShowInInspector]
2025-12-08 05:27:53 -05:00
public bool IsHoldingSpecialA { get; private set; }
2026-02-13 09:22:11 -05:00
[ShowInInspector]
2025-12-08 05:27:53 -05:00
public bool IsHoldingSpecialB { get; private set; }
2026-02-13 09:22:11 -05:00
[ShowInInspector]
public bool IsHoldingSpecialC { get; private set; }
2025-11-25 08:19:33 -05:00
public BoolReactiveProperty isCursorLocked;
public PlayerPreinputSubmodule preinputSubmodule;
public override void Initialize()
{
base.Initialize();
playerInput = owner.GetComponent<PlayerInput>();
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;
2026-05-10 11:47:55 -04:00
// 同步禁用 Cinemachine 的自有输入控制器,防止 UI 打开时视角继续转动
var cinemachineInput = player.viewSc.freeLookCamera.GetComponent<CinemachineInputAxisController>();
cinemachineInput.enabled = isLocked;
2025-11-25 08:19:33 -05:00
});
preinputSubmodule = new PlayerPreinputSubmodule(this);
}
private void Start()
{
RegisterActionsInputs();
RegisterMainWeaponInputs();
2026-05-10 11:47:55 -04:00
RegisterSupportEquipmentInputs();
RegisterFunctionInputs();
2025-11-25 08:19:33 -05:00
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void Update()
{
if (isCursorLocked.Value)
{
Look = inputActions.Player.Look.ReadValue<Vector2>();
Move = inputActions.Player.Move.ReadValue<Vector2>().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)
{
2025-12-22 23:27:18 -05:00
Vector3 inputDirection = new Vector3(Move.x, 0, Move.y);
operation.Dash(inputDirection);
preinputSubmodule.RegisterPreinputAction(() => operation.Dash(inputDirection), 10);
2025-11-25 08:19:33 -05:00
}
else
{
operation.Dodge();
2025-12-08 05:27:53 -05:00
preinputSubmodule.RegisterPreinputAction(() => operation.Dodge(), 10);
2025-11-25 08:19:33 -05:00
}
}
};
2026-04-28 15:46:32 -04:00
inputActions.Player.LockonTarget.performed += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.performed && isCursorLocked.Value)
{
2026-04-28 15:46:32 -04:00
operation.LockonTarget();
}
};
inputActions.Player.SelectLockonTarget.performed += ctx =>
{
if (ctx.performed && isCursorLocked.Value)
{
Debug.Log("Value: " + ctx.ReadValue<float>());
operation.SelectLockonTarget(ctx.ReadValue<float>());
2025-11-25 08:19:33 -05:00
}
};
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();
}
};
}
2025-12-08 05:27:53 -05:00
private MainWeaponBase currentMainWeapon => player.inventorySc.equipmentSm.currentMainWeapon;
2025-11-25 08:19:33 -05:00
private void RegisterMainWeaponInputs()
{
inputActions.Player.MainWeaponPrimary.started += ctx =>
{
if (ctx.started && isCursorLocked.Value)
{
IsHoldingPrimary = true;
operation.MainWeaponPrimaryPress();
2025-12-08 05:27:53 -05:00
if (!currentMainWeapon.disablePrimaryPreinput)
{
preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponPrimaryPress(), 0);
}
2025-11-25 08:19:33 -05:00
}
};
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();
2025-12-08 05:27:53 -05:00
if (!currentMainWeapon.disableSecondaryPreinput)
{
preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSecondaryPress(), 0);
}
2025-11-25 08:19:33 -05:00
}
};
inputActions.Player.MainWeaponSecondary.canceled += ctx =>
{
if (ctx.canceled && isCursorLocked.Value)
{
IsHoldingSecondary = false;
operation.MainWeaponSecondaryRelease();
}
};
2025-12-08 05:27:53 -05:00
inputActions.Player.MainWeaponSpecialA.started += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.started && isCursorLocked.Value)
{
2025-12-08 05:27:53 -05:00
IsHoldingSpecialA = true;
operation.MainWeaponSpecialAPress();
if (!currentMainWeapon.disableSpecialAPreinput)
{
preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSpecialAPress(), 0);
}
2025-11-25 08:19:33 -05:00
}
};
2025-12-08 05:27:53 -05:00
inputActions.Player.MainWeaponSpecialA.canceled += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.canceled && isCursorLocked.Value)
{
2025-12-08 05:27:53 -05:00
IsHoldingSpecialA = false;
operation.MainWeaponSpecialARelease();
2025-11-25 08:19:33 -05:00
}
};
2025-12-08 05:27:53 -05:00
inputActions.Player.MainWeaponSpecialB.started += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.started && isCursorLocked.Value)
{
2025-12-08 05:27:53 -05:00
IsHoldingSpecialB = true;
operation.MainWeaponSpecialBPress();
if (!currentMainWeapon.disableSpecialBPreinput)
{
preinputSubmodule.RegisterPreinputAction(() => operation.MainWeaponSpecialBPress(), 0);
}
2025-11-25 08:19:33 -05:00
}
};
2025-12-08 05:27:53 -05:00
inputActions.Player.MainWeaponSpecialB.canceled += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.canceled && isCursorLocked.Value)
{
2025-12-08 05:27:53 -05:00
IsHoldingSpecialB = false;
operation.MainWeaponSpecialBRelease();
2025-11-25 08:19:33 -05:00
}
};
2025-12-08 05:27:53 -05:00
2026-02-13 09:22:11 -05:00
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();
}
};
2025-12-23 19:47:06 -05:00
inputActions.Player.SwitchPreviousMainWeapon.performed += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.performed && isCursorLocked.Value)
{
2025-12-23 19:47:06 -05:00
operation.SwitchMainWeapon(-1);
2025-11-25 08:19:33 -05:00
}
};
2025-12-23 19:47:06 -05:00
inputActions.Player.SwitchNextMainWeapon.performed += ctx =>
2025-11-25 08:19:33 -05:00
{
if (ctx.performed && isCursorLocked.Value)
{
2025-12-23 19:47:06 -05:00
operation.SwitchMainWeapon(1);
2025-11-25 08:19:33 -05:00
}
};
inputActions.Player.RouletteSwitchMainWeapon.performed += ctx =>
{
if (ctx.performed && isCursorLocked.Value)
{
operation.OpenMainWeaponRoulette();
}
};
inputActions.Player.RouletteSwitchMainWeapon.canceled += ctx =>
{
if (ctx.canceled)
{
operation.CloseMainWeaponRoulette();
}
};
}
2026-05-10 11:47:55 -04:00
private void RegisterSupportEquipmentInputs()
2025-11-25 08:19:33 -05:00
{
inputActions.Player.UseSupportEquipment0.performed += ctx =>
{
if (ctx.performed && isCursorLocked.Value)
{
2026-01-03 18:19:39 -05:00
operation.SupportEquipment0Press();
}
};
inputActions.Player.UseSupportEquipment0.canceled += ctx =>
{
if (ctx.canceled && isCursorLocked.Value)
{
operation.SupportEquipment0Release();
2025-11-25 08:19:33 -05:00
}
};
inputActions.Player.UseSupportEquipment1.performed += ctx =>
{
if (ctx.performed && isCursorLocked.Value)
{
2026-01-03 18:19:39 -05:00
operation.SupportEquipment1Press();
}
};
inputActions.Player.UseSupportEquipment1.canceled += ctx =>
{
if (ctx.canceled && isCursorLocked.Value)
{
operation.SupportEquipment1Release();
2025-11-25 08:19:33 -05:00
}
};
inputActions.Player.UseSupportEquipment2.performed += ctx =>
{
if (ctx.performed && isCursorLocked.Value)
{
2026-01-03 18:19:39 -05:00
operation.SupportEquipment2Press();
}
};
inputActions.Player.UseSupportEquipment2.canceled += ctx =>
{
if (ctx.canceled && isCursorLocked.Value)
{
operation.SupportEquipment2Release();
2025-11-25 08:19:33 -05:00
}
};
inputActions.Player.UseSupportEquipment3.performed += ctx =>
{
if (ctx.performed && isCursorLocked.Value)
{
2026-01-03 18:19:39 -05:00
operation.SupportEquipment3Press();
}
};
inputActions.Player.UseSupportEquipment3.canceled += ctx =>
{
if (ctx.canceled && isCursorLocked.Value)
{
operation.SupportEquipment3Release();
2025-11-25 08:19:33 -05:00
}
};
2026-05-10 11:47:55 -04:00
}
private void RegisterFunctionInputs()
{
inputActions.Player.Map.performed += ctx =>
2025-11-25 08:19:33 -05:00
{
2026-05-10 11:47:55 -04:00
var mapPage = PlayerCanvas.MainGamePages.mapPage;
if(mapPage.IsOpen) mapPage.Close();
else mapPage.Open();
2025-11-25 08:19:33 -05:00
};
}
}
public partial class PlayerInputSubcontroller
{
private void OnSceneLoaded(Scene a, LoadSceneMode b)
{
isCursorLocked.Value = true;
}
}
}