summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Input/InputManager.cs
blob: f24a3efad63900bd254a13214cd86e73a692933d (plain)
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
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;
		}
	}

	public void Init()
	{
		m_CommandRecord = new List<Command>();
		m_CurrentCommand = new Command(GamepadButton.Blank, 0);
	}

	public void Update()
	{
		GamepadButton cmd = GamepadButton.Blank;
		// 移动
		if (Input.GetKeyDown("w"))
			cmd = GamepadButton.Up;
		if (Input.GetKeyDown("s"))
			cmd = GamepadButton.Down;
		if (Input.GetKeyDown("a"))
			cmd = GamepadButton.Left;
		if (Input.GetKeyDown("d"))
			cmd = GamepadButton.Right;
		// 动作
		if (Input.GetKeyDown("j"))
			cmd = GamepadButton.Circle;
		if (Input.GetKeyDown("k"))
			cmd = GamepadButton.Triangle;
		if (Input.GetKeyDown("l"))
			cmd = GamepadButton.Square;
		if(Input.GetKeyDown("u"))
			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 "";
	}

	public bool IsButtonHold(GamepadButton button)
	{
		return Input.GetKey(GetGamepadButtonKey(button));
	}

}