1 using UnityEngine;
2 using System.Collections.Generic;
3 using System;
4
5 public class KeyCombination : MonoBehaviour
6 {
7
8 //方向键上的贴图
9 public Texture imageUp;
10 //方向键下的贴图
11 public Texture imageDown;
12 //方向键左的贴图
13 public Texture imageLeft;
14 //方向键右的贴图
15 public Texture imageRight;
16 //按键成功的贴图
17 public Texture imageSuccess;
18
19 //自定义方向键的储存值
20 public const int KEY_UP = 0;
21 public const int KEY_DOWN = 1;
22 public const int KEY_LEFT = 2;
23 public const int KEY_RIGHT = 3;
24 public const int KEY_FIRT = 4;
25 //连续按键的事件限制
26 public const int FRAME_COUNT = 100;
27 //仓库中储存技能的数量
28 public const int SAMPLE_SIZE = 3;
29 //每组技能的按键数量
30 public const int SAMPLE_COUNT = 5;
31 //技能仓库
32 int[,] Sample =
33 {
34 //下 + 前 + 下 + 前 + 拳
35 {KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_RIGHT,KEY_FIRT},
36 //下 + 前 + 下 + 后 + 拳
37 {KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_LEFT,KEY_FIRT},
38 //下 + 后 + 下 + 后 + 拳
39 {KEY_DOWN,KEY_LEFT,KEY_DOWN,KEY_LEFT,KEY_FIRT},
40 };
41 //记录当前按下按键的键值
42 int currentkeyCode = 0;
43 //标志是否开启监听按键
44 bool startFrame = false;
45 //记录当前开启监听到现在的时间
46 int currentFrame = 0;
47 //保存一段时间内玩家输入的按键组合
48 List playerSample;
49 //标志完成操作
50 bool isSuccess = false;
51 void Start()
52 {
53 //初始话按键组合链表
54 playerSample = new List();
55 }
56 void OnGUI()
57 {
58 //获得按键组合链表中储存按键的数量
59 int size = playerSample.Count;
60 //遍历该按键组合链表
61 for (int i = 0; i < size; i++)
62 {
63 //将按下按键对应的图片显示在屏幕中
64 int key = playerSample[i];
65 Texture temp = null;
66 switch (key)
67 {
68 case KEY_UP:
69 temp = imageUp;
70 break;
71 case KEY_DOWN:
72 temp = imageDown;
73 break;
74 case KEY_LEFT:
75 temp = imageLeft;
76 break;
77 case KEY_RIGHT:
78 temp = imageRight;
79 break;
80 }
81 if (temp != null)
82 {
83 GUILayout.Label(temp);
84 }
85 }
86 if (isSuccess)
87 {
88 //显示成功贴图
89 GUILayout.Label(imageSuccess);
90 }
91 //默认提示信息
92 GUILayout.Label("连续组合按键1:下、前、下、前、拳");
93 GUILayout.Label("连续组合按键2:下、前、下、后、拳");
94 GUILayout.Label("连续组合按键2:下、后、下、后、拳");
95 }
96
97 void Update()
98 {
99 //更新按键
100 UpdateKey();
101 if (Input.anyKeyDown)
102 {
103 if (isSuccess)
104 {
105 //按键成功后重置
106 isSuccess = false;
107 Reset();
108 }
109 if (!startFrame)
110 {
111 //启动时间计数器
112 startFrame = true;
113 }
114 //将按键值添加如链表中
115 playerSample.Add(currentkeyCode);
116 //遍历链表
117 int size = playerSample.Count;
118 if (size == SAMPLE_COUNT)
119 {
120 for (int i = 0; i < SAMPLE_SIZE; i++)
121 {
122 int SuccessCount = 0;
123 for (int j = 0; j < SAMPLE_COUNT; j++)
124 {
125 int temp = playerSample[j];
126 if (temp == Sample[i, j])
127 {
128 SuccessCount++;
129 }
130 }
131 //玩家按下的组合按键与仓库中的按键组合相同表示释放技能成功
132 if (SuccessCount == SAMPLE_COUNT)
133 {
134 isSuccess = true;
135 break;
136 }
137 }
138 }
139 }
140 if (startFrame)
141 {
142 //计数器++
143 currentFrame++;
144 }
145 if (currentFrame >= FRAME_COUNT)
146 {
147 //计数器超时
148 if (!isSuccess)
149 {
150 Reset();
151 }
152 }
153 }
154 void Reset()
155 {
156 //重置按键相关信息
157 currentFrame = 0;
158 startFrame = false;
159 playerSample.Clear();
160 }
161
162 void UpdateKey()
163 {
164 //获取当前键盘的按键信息
165 if (Input.GetKeyDown(KeyCode.W))
166 {
167 currentkeyCode = KEY_UP;
168 }
169 if (Input.GetKeyDown(KeyCode.S))
170 {
171 currentkeyCode = KEY_DOWN;
172 }
173 if (Input.GetKeyDown(KeyCode.A))
174 {
175 currentkeyCode = KEY_LEFT;
176 }
177 if (Input.GetKeyDown(KeyCode.D))
178 {
179 currentkeyCode = KEY_RIGHT;
180 }
181 if (Input.GetKeyDown(KeyCode.Space))
182 {
183 currentkeyCode = KEY_FIRT;
184 }
185 }
186 }