blob: 6bce55c635d3a097fdf822fd69243e8ee297155b (
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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);
}
}
|