summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Input/_InputManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/Input/_InputManager.cs')
-rw-r--r--Assets/Scripts/Input/_InputManager.cs164
1 files changed, 164 insertions, 0 deletions
diff --git a/Assets/Scripts/Input/_InputManager.cs b/Assets/Scripts/Input/_InputManager.cs
new file mode 100644
index 00000000..a9d120e4
--- /dev/null
+++ b/Assets/Scripts/Input/_InputManager.cs
@@ -0,0 +1,164 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class _InputManager : Singleton<_InputManager>
+{
+ private List<Command> m_CommandRecord;
+ public List<Command> CommandRecord
+ {
+ get
+ {
+ return m_CommandRecord;
+ }
+ }
+ private readonly int kCommandRecords = 10;
+
+ // 本帧内的指令,会在下一帧被清空
+ private Command m_CurrentCommand;
+ public Command CurrentCommand
+ {
+ get
+ {
+ return m_CurrentCommand;
+ }
+ }
+
+ private enum Axis { Up, Down, Left, Right};
+
+ private bool[] m_Axis = new bool[4] { false, false, false, false};
+
+ public void Init()
+ {
+ m_CommandRecord = new List<Command>();
+ m_CurrentCommand = new Command(GamepadButton.Blank, 0);
+ }
+
+ private bool GetAxis(Axis axis)
+ {
+ return false;
+ bool axisRaw = false;
+ switch (axis)
+ {
+ case Axis.Left: axisRaw = Input.GetAxisRaw("Horizontal") == -1; break;
+ case Axis.Right: axisRaw = Input.GetAxisRaw("Horizontal") == 1; break;
+ case Axis.Down: axisRaw = Input.GetAxisRaw("Vertical") == -1; break;
+ case Axis.Up: axisRaw = Input.GetAxisRaw("Vertical") == 1; break;
+ }
+ if (axisRaw)
+ {
+ if(!m_Axis[(int)axis])
+ {
+ m_Axis[(int)axis] = true;
+ return true;
+ }
+ return false;
+ }
+ else
+ {
+ m_Axis[(int)axis] = false;
+ return false;
+ }
+ }
+
+ public void Update()
+ {
+ GamepadButton cmd = GamepadButton.Blank;
+ // 移动
+ if (Input.GetKeyDown("w") || GetAxis(Axis.Up))
+ cmd = GamepadButton.Up;
+ if (Input.GetKeyDown("s") || GetAxis(Axis.Down))
+ cmd = GamepadButton.Down;
+ if (Input.GetKeyDown("a") || GetAxis(Axis.Left))
+ cmd = GamepadButton.Left;
+ if (Input.GetKeyDown("d") || GetAxis(Axis.Right))
+ cmd = GamepadButton.Right;
+ // 动作
+ if (Input.GetKeyDown("j") || Input.GetKeyDown(KeyCode.Joystick1Button2))
+ cmd = GamepadButton.Circle;
+ if (Input.GetKeyDown("k") || Input.GetKeyDown(KeyCode.Joystick1Button0))
+ cmd = GamepadButton.Triangle;
+ if (Input.GetKeyDown("l") || Input.GetKeyDown(KeyCode.Joystick1Button1))
+ cmd = GamepadButton.Square;
+ if (Input.GetKeyDown("u") || Input.GetKeyDown(KeyCode.Joystick1Button3))
+ cmd = GamepadButton.Cross;
+
+ if(cmd != GamepadButton.Blank)
+ {
+ float time = Time.time;
+ Command command = new Command(cmd, time);
+ //Debug.Log(CommandToString(command));
+
+ m_CurrentCommand = command;
+ m_CommandRecord.Add(command);
+
+ if(m_CommandRecord.Count > 10)
+ m_CommandRecord.RemoveRange(0, m_CommandRecord.Count - 10);
+ }
+ else if(m_CurrentCommand.code != GamepadButton.Blank)
+ {
+ m_CurrentCommand = Command.Blank;
+ }
+ }
+
+ string CommandCodeToString(GamepadButton cmd)
+ {
+ switch(cmd)
+ {
+ case GamepadButton.Left: return "←";
+ case GamepadButton.Right: return "→";
+ case GamepadButton.Up: return "↑";
+ case GamepadButton.Down: return "↓";
+ case GamepadButton.Circle: return "○";
+ case GamepadButton.Triangle: return "△";
+ case GamepadButton.Square: return "□";
+ case GamepadButton.Cross: return "×";
+ default: return "Unknown";
+ }
+ }
+
+ string CommandToString(Command cmd)
+ {
+ string sign = CommandCodeToString(cmd.code);
+ return sign + " " + cmd.time + "s" + " " + cmd.id;
+ }
+
+ string GetGamepadButtonKey(GamepadButton button)
+ {
+ switch (button)
+ {
+ case GamepadButton.Blank: return "";
+ case GamepadButton.Up: return "w";
+ case GamepadButton.Down: return "s";
+ case GamepadButton.Left: return "a";
+ case GamepadButton.Right: return "d";
+ case GamepadButton.Circle: return "j";
+ case GamepadButton.Triangle: return "k";
+ case GamepadButton.Square: return "l";
+ case GamepadButton.Cross: return "u";
+ }
+ return "";
+ }
+
+ bool GetGamepadButtonState(GamepadButton button)
+ {
+ switch (button)
+ {
+ case GamepadButton.Up: return m_Axis[(int)Axis.Up];
+ case GamepadButton.Down: return m_Axis[(int)Axis.Down];
+ case GamepadButton.Left: return m_Axis[(int)Axis.Left];
+ case GamepadButton.Right: return m_Axis[(int)Axis.Right];
+ case GamepadButton.Circle: return Input.GetKey(KeyCode.Joystick1Button2);
+ case GamepadButton.Triangle: return Input.GetKey(KeyCode.Joystick1Button0);
+ case GamepadButton.Square: return Input.GetKey(KeyCode.Joystick1Button1);
+ case GamepadButton.Cross: return Input.GetKey(KeyCode.Joystick1Button3);
+ }
+ return false;
+ }
+
+ public bool IsButtonHold(GamepadButton button)
+ {
+ return Input.GetKey(GetGamepadButtonKey(button)) || GetGamepadButtonState(button);
+ }
+
+}