blob: 62fd5f9103d915b6df72ef207633bb7732b29f62 (
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct InputCommand
{
public KeyCode key;
public float time;
}
// 处理输入逻辑,不涉及读取输入,只处理逻辑
public class InputManager : SingletonMB<InputManager>
{
PCController _pc;
List<InputCommand> m_CommandQueue = new List<InputCommand>();
const float threshold = 3;
PCController pc
{
get
{
if (_pc == null)
_pc = PCController.instance;
return _pc;
}
}
public void OnMoveRight()
{
UnitState.MoveParam move = new UnitState.MoveParam();
move.isRight = true;
move.key = "d";
pc.unitState.ChangeState(UnitState.EUnitState.Move, move);
}
public void OnMoveLeft()
{
UnitState.MoveParam move = new UnitState.MoveParam();
move.isRight = false;
move.key = "a";
pc.unitState.ChangeState(UnitState.EUnitState.Move, move);
}
public void OnTurnBack()
{
}
public void OnJump()
{
pc.unitState.ChangeState(UnitState.EUnitState.Jump, new UnitState.JumpParam());
}
public void OnAttack()
{
pc.unitState.ChangeState(UnitState.EUnitState.Attack, new UnitState.SkillParam());
}
public void OnUpdate()
{
foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(vKey))
{
InputCommand cmd = new InputCommand();
cmd.key = vKey;
cmd.time = Time.time;
m_CommandQueue.Add(cmd);
Debug.Log(cmd.time);
string cmdStr = "";
m_CommandQueue.ForEach(s => cmdStr += s.key.ToString() + ",");
Debug.Log(cmdStr);
}
}
float curTime = Time.time;
int removeCount = 0;
for(int i = 0; i < m_CommandQueue.Count; ++i)
{
if(curTime - m_CommandQueue[i].time > threshold)
{
removeCount++;
}
}
m_CommandQueue.RemoveRange(0, removeCount);
}
public bool TryCommand(float interval = 0.5f, params KeyCode[] keys)
{
if (keys.Length > m_CommandQueue.Count)
return false;
int count = m_CommandQueue.Count;
float preTime = m_CommandQueue[count - keys.Length].time;
for (int i = 0; i < keys.Length; ++i)
{
if(keys[i] == m_CommandQueue[i + count - keys.Length].key)
{
if(m_CommandQueue[i + count - keys.Length].time - preTime > interval)
{
return false;
}
preTime = m_CommandQueue[i + count - keys.Length].time;
}
else
{
return false;
}
}
m_CommandQueue.RemoveRange(count - keys.Length, keys.Length);
return true;
}
}
|