aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/InputHandler.cs
blob: e1c70d05ce646ef9d0480c770a585acb437a7b41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DS
{
    /// <summary>
    /// 输入处理器 - 负责处理玩家输入(移动、视角控制)并将输入数据分发给其他系统(如相机和角色控制器)。
    /// </summary>
    public class InputHandler : MonoBehaviour
    {
        // ===== 公共输入数据(供其他系统读取) =====
        [Header("移动输入")]
        [Tooltip("水平移动输入值 (-1~1),负值表示左,正值表示右")]
        public float horizontal;

        [Tooltip("垂直移动输入值 (-1~1),负值表示下,正值表示上")]
        public float vertical;

        [Tooltip("综合移动量 (0~1),水平与垂直绝对值的和,并被限制在 0~1 之间")]
        public float moveAmount;

        [Header("视角输入")]
        [Tooltip("鼠标或右摇杆的水平移动增量")]
        public float mouseX;

        [Tooltip("鼠标或右摇杆的垂直移动增量")]
        public float mouseY;

        public bool b_Input;
        public bool rollFlag;
        public bool isInteracting;

        [Header("其他操作输入")]
        [Tooltip("翻滚是否触发")]
        public bool rollTriggered;

        // ===== 私有引用与输入缓存 =====
        private PlayerControls _inputActions;   // 新输入系统生成的 PlayerControls 实例
        private CameraHandler _cameraHandler;   // 相机处理器单例引用

        private Vector2 _movementInput;         // 缓存移动输入值 (x => 水平, y => 垂直)
        private Vector2 _cameraInput;           // 缓存视角输入值 (x => 水平视角, y => 垂直视角)

        // ===== Unity 生命周期方法 =====

        private void Start()
        {
            // 获取相机处理器的单例引用,用于在 FixedUpdate 中驱动相机跟随与旋转
            _cameraHandler = CameraHandler.singleton;
        }

        /// <summary>
        /// 固定频率更新(用于物理相关的相机跟随和旋转)。
        /// </summary>
        private void FixedUpdate()
        {
            float delta = Time.fixedDeltaTime; // 固定时间步长,确保物理一致性

            if (_cameraHandler != null)
            {
                // 让相机跟随目标(通常是玩家角色)
                _cameraHandler.FollowTarget(delta);
                // 根据鼠标/摇杆输入旋转相机
                _cameraHandler.HandleCameraRotation(delta, mouseX, mouseY);
            }
        }

        /// <summary>
        /// 在脚本启用时注册输入事件并启用输入系统。
        /// </summary>
        public void OnEnable()
        {
            if (_inputActions == null)
            {
                // 实例化输入操作,并绑定事件回调
                _inputActions = new PlayerControls();

                // 当移动按键被触发(按下/推摇杆)时,将输入值存入 _movementInput
                _inputActions.PlayerMovement.Movement.performed +=
                    ctx => _movementInput = ctx.ReadValue<Vector2>();

                // 当视角控制被触发(鼠标移动/右摇杆)时,将输入值存入 _cameraInput
                _inputActions.PlayerMovement.Camera.performed +=
                    ctx => _cameraInput = ctx.ReadValue<Vector2>();
                
            }

            // 启用输入动作监听
            _inputActions.Enable();
        }

        /// <summary>
        /// 在脚本禁用时禁用输入系统,避免误触。
        /// </summary>
        private void OnDisable()
        {
            _inputActions.Disable();
        }

        // ===== 公共更新入口 =====

        /// <summary>
        /// 每帧调用的输入处理入口(通常由 GameManager 或其他管理器调用)。
        /// </summary>
        /// <param name="delta">当前帧的时间增量</param>
        public void TickInput(float delta)
        {
            MoveInput(delta);
            HandleRollInput(delta);
        }

        // ===== 私有输入处理逻辑 =====

        /// <summary>
        /// 从缓存的输入向量中提取各个方向的分量,并计算综合移动量。
        /// </summary>
        /// <param name="delta">当前帧的时间增量(目前未使用,保留以准备将来可能的平滑处理)</param>
        private void MoveInput(float delta)
        {
            // 从缓存中提取水平和垂直移动分量
            horizontal = _movementInput.x;
            vertical = _movementInput.y;

            // 综合移动量 = 水平与垂直绝对值的和,限制在 0~1,用于控制移动动画混合等
            moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));

            // 从缓存中提取视角控制分量
            mouseX = _cameraInput.x;
            mouseY = _cameraInput.y;
        }

        private void HandleRollInput(float delta)
        {
            b_Input = _inputActions.PlayerActions.Roll.phase == UnityEngine.InputSystem.InputActionPhase.Performed;
            Debug.Log(b_Input);
            if (b_Input)
            {
                rollFlag = true;
            }
        }
    }
}