using System.Collections; using System.Collections.Generic; using UnityEngine; public class InputManager : Singleton { private List m_CommandRecord; private readonly int kCommandRecords = 10; // 本帧内的指令,会在下一帧被清空 private Command m_CurrentCommand; public Command CurrentCommand { get { return m_CurrentCommand; } } public void Init() { m_CommandRecord = new List(); m_CurrentCommand = new Command(CommandCode.Blank, 0); } public void Update() { CommandCode cmd = CommandCode.Blank; // 移动 if (Input.GetKeyDown("w")) cmd = CommandCode.Up; if (Input.GetKeyDown("s")) cmd = CommandCode.Down; if (Input.GetKeyDown("a")) cmd = CommandCode.Left; if (Input.GetKeyDown("d")) cmd = CommandCode.Right; // 动作 if (Input.GetKeyDown("j")) cmd = CommandCode.Circle; if (Input.GetKeyDown("k")) cmd = CommandCode.Triangle; if (Input.GetKeyDown("l")) cmd = CommandCode.Square; if(Input.GetKeyDown("u")) cmd = CommandCode.Cross; if(cmd != CommandCode.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 != CommandCode.Blank) { m_CurrentCommand = Command.Blank; } } string CommandCodeToString(CommandCode cmd) { switch(cmd) { case CommandCode.Left: return "←"; case CommandCode.Right: return "→"; case CommandCode.Up: return "↑"; case CommandCode.Down: return "↓"; case CommandCode.Circle: return "○"; case CommandCode.Triangle: return "△"; case CommandCode.Square: return "□"; case CommandCode.Cross: return "×"; default: return "Unknown"; } } string CommandToString(Command cmd) { string sign = CommandCodeToString(cmd.code); return sign + " " + cmd.time + "s" + " " + cmd.id; } }