Files
2025TapTapGameJam/Assets/Script/Gameplay/Input/InputManager.cs
2025-10-15 21:31:13 +08:00

103 lines
3.0 KiB
C#

using System;
using UnityEngine;
using Core;
namespace Input
{
public class InputManager : MonoSingleton<InputManager>, IInputManager
{
private PlayerInputActions _input; // 自动生成的输入类
// 当前输入值
public Vector2 Move { get; private set; }
public Vector2 Look { get; private set; }
public bool JumpPressed { get; private set; }
public bool PausePressed { get; private set; }
public bool InteractPressed { get; private set; }
private void Awake()
{
_input = new PlayerInputActions();
}
private void OnEnable()
{
_input.Enable();
// 注册事件
_input.Player.Move.performed += ctx => Move = ctx.ReadValue<Vector2>();
_input.Player.Move.canceled += ctx => Move = Vector2.zero;
_input.Player.Look.performed += ctx => Look = ctx.ReadValue<Vector2>();
_input.Player.Look.canceled += ctx => Look = Vector2.zero;
_input.Player.Jump.performed += ctx => JumpPressed = true;
_input.Player.Jump.canceled += ctx => JumpPressed = false;
// _input.Player.Pause.performed += ctx => PausePressed = true;
// _input.Player.Pause.canceled += ctx => PausePressed = false;
_input.Player.Interact.performed += ctx => InteractPressed = true;
_input.Player.Interact.canceled += ctx => InteractPressed = false;
}
private void OnDisable()
{
_input.Disable();
}
private void Start()
{
UIManager.Instance.RegisterInputManager(this);
}
private void Update()
{
// 在此更新一次性触发的输入,例如“按下瞬间触发”
// if (PausePressed)
// {
// Debug.Log("Pause Pressed!");
// PausePressed = false; // 手动清除
// }
}
// 🔧 示例方法:允许外部模块手动启用/禁用输入(比如暂停菜单)
public void SetInputEnabled(bool enabled)
{
if (enabled) _input.Enable();
else _input.Disable();
}
public void SetCursorState(bool visible, CursorLockMode lockMode)
{
Cursor.visible = visible;
Cursor.lockState = lockMode;
}
public void SetInputForLook(bool enabled)
{
if (enabled)
{
_input.Player.Look.Enable();
}
else
{
_input.Player.Look.Disable();
Look = Vector2.zero; // 禁用时清除Look值
}
}
public void SetInputForMove(bool enabled)
{
if (enabled)
{
_input.Player.Move.Enable();
}
else
{
_input.Player.Move.Disable();
Move = Vector2.zero; // 禁用时清除Move值
}
}
}
}