InputM.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. public class InputM : MonoBehaviour {
  6. public GameObject dicePrefab;
  7. public static string keyLeft = "a", keyRight = "d", keyJump = "w", keyCrouch = "s", keyFire = "l", keyReload = "r";
  8. public enum KeyType { Left, Right, Jump, Crouch, Fire, Reload }
  9. public static void SetKey(KeyType key, string newKey) {
  10. _ = key switch {
  11. KeyType.Left => keyLeft = newKey,
  12. KeyType.Right => keyRight = newKey,
  13. KeyType.Jump => keyJump = newKey,
  14. KeyType.Crouch => keyCrouch = newKey,
  15. KeyType.Fire => keyFire = newKey,
  16. _ => keyReload = newKey,
  17. };
  18. foreach (GameObject dice in instance.dicesUI) dice.GetComponent<DiceUI>().UpdateLetter();
  19. }
  20. public static bool GetKeyUI(KeyType key) => key switch {
  21. KeyType.Left => keyLeft == "l" ? Input.GetMouseButton(0) : (keyLeft == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyLeft)),
  22. KeyType.Right => keyRight == "l" ? Input.GetMouseButton(0) : (keyRight == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyRight)),
  23. KeyType.Jump => keyJump == "l" ? Input.GetMouseButton(0) : (keyJump == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyJump)),
  24. KeyType.Crouch => keyCrouch == "l" ? Input.GetMouseButton(0) : (keyCrouch == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyCrouch)),
  25. KeyType.Fire => keyFire == "l" ? Input.GetMouseButton(0) : (keyFire == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyFire)),
  26. _ => keyReload == "l" ? Input.GetMouseButton(0) : (keyReload == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyReload)),
  27. };
  28. public static bool GetKeyEvent(KeyType key) => key switch {
  29. KeyType.Left => keyLeft == "l" ? Input.GetMouseButton(0) : (keyLeft == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyLeft)),
  30. KeyType.Right => keyRight == "l" ? Input.GetMouseButton(0) : (keyRight == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyRight)),
  31. KeyType.Jump => keyJump == "l" ? Input.GetMouseButtonDown(0) : (keyJump == "r" ? Input.GetMouseButtonDown(1) : Input.GetKeyDown(keyJump)),
  32. KeyType.Crouch => keyCrouch == "l" ? Input.GetMouseButton(0) : (keyCrouch == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyCrouch)),
  33. KeyType.Fire => keyFire == "l" ? Input.GetMouseButton(0) : (keyFire == "r" ? Input.GetMouseButton(1) : Input.GetKey(keyFire)),
  34. _ => keyReload == "l" ? Input.GetMouseButtonDown(0) : (keyReload == "r" ? Input.GetMouseButtonDown(1) : Input.GetKeyDown(keyReload)),
  35. };
  36. public static string GetKeyRaw(KeyType key) => key switch {
  37. KeyType.Left => keyLeft,
  38. KeyType.Right => keyRight,
  39. KeyType.Jump => keyJump,
  40. KeyType.Crouch => keyCrouch,
  41. KeyType.Fire => keyFire,
  42. _ => keyReload,
  43. };
  44. public static string GetKeyRawDefault(KeyType key) => key switch {
  45. KeyType.Left => "a",
  46. KeyType.Right => "d",
  47. KeyType.Jump => "w",
  48. KeyType.Crouch => "s",
  49. KeyType.Fire => "l",
  50. _ => "r",
  51. };
  52. public static InputM instance;
  53. private void Awake() {
  54. instance = this;
  55. keyLeft = "a"; keyRight = "d"; keyJump = "w"; keyCrouch = "s"; keyFire = "l"; keyReload = "r";
  56. }
  57. public Sprite[] spritesText, spritesType;
  58. public GameObject[] dicesUI;
  59. public static Sprite GetKeyTextSprite(string key) => key switch {
  60. "w" => instance.spritesText[0],
  61. "a" => instance.spritesText[1],
  62. "s" => instance.spritesText[2],
  63. "d" => instance.spritesText[3],
  64. "l" => instance.spritesText[4],
  65. _ => instance.spritesText[5],
  66. };
  67. public static Sprite GetKeyTypeSprite(KeyType key) => key switch {
  68. KeyType.Jump => instance.spritesType[0],
  69. KeyType.Left => instance.spritesType[1],
  70. KeyType.Crouch => instance.spritesType[2],
  71. KeyType.Right => instance.spritesType[3],
  72. KeyType.Fire => instance.spritesType[4],
  73. _ => instance.spritesType[5],
  74. };
  75. public static GameObject GetDiceUI(KeyType key) => key switch {
  76. KeyType.Jump => instance.dicesUI[0],
  77. KeyType.Left => instance.dicesUI[1],
  78. KeyType.Crouch => instance.dicesUI[2],
  79. KeyType.Right => instance.dicesUI[3],
  80. KeyType.Fire => instance.dicesUI[4],
  81. _ => instance.dicesUI[5],
  82. };
  83. public readonly static KeyType[] keyTypes = new[] { KeyType.Left, KeyType.Right, KeyType.Jump, KeyType.Crouch, KeyType.Fire, KeyType.Reload };
  84. public static string GetPossibleResults(KeyType key, DiceEntity.DiceType type) {
  85. List<string> accepted = "wasdlr".ToList().Select(c => c.ToString()).ToList();
  86. List<string> refused = new();
  87. // 删除冲突键位
  88. // - 不能同时跳跃、蹲下
  89. if (key == KeyType.Jump) refused.Add(GetKeyRaw(KeyType.Crouch));
  90. if (key == KeyType.Crouch) refused.Add(GetKeyRaw(KeyType.Jump));
  91. // - 不能同时向左、向右
  92. if (key == KeyType.Left) refused.Add(GetKeyRaw(KeyType.Right));
  93. if (key == KeyType.Right) refused.Add(GetKeyRaw(KeyType.Left));
  94. // - 不能同时开火、装弹
  95. if (key == KeyType.Fire) refused.Add(GetKeyRaw(KeyType.Reload));
  96. if (key == KeyType.Reload) refused.Add(GetKeyRaw(KeyType.Fire));
  97. // 禁止选择已经有两个绑定项的字母、尽量选择没有绑定项的字母
  98. foreach (string letter in "wasdlr".ToCharArray().Select(c => c.ToString())) {
  99. if (keyTypes.Count(keyType => GetKeyRaw(keyType) == letter) >= 2) refused.Add(letter);
  100. if (keyTypes.Count(keyType => GetKeyRaw(keyType) == letter) == 0) { accepted.Add(letter); accepted.Add(letter); }
  101. }
  102. // 禁止与当前项相同
  103. refused.Add(GetKeyRaw(key));
  104. // 是否可以还原回原始键位
  105. switch (type) {
  106. case DiceEntity.DiceType.Restore:
  107. for (int i = 0; i < 10; i++) accepted.Add(GetKeyRawDefault(key)); break;
  108. case DiceEntity.DiceType.NoRestore:
  109. refused.Add(GetKeyRawDefault(key)); break;
  110. case DiceEntity.DiceType.Bad:
  111. break; // 直接修改最后结果
  112. }
  113. // 输出
  114. string resultStr = "";
  115. accepted.ForEach(resultChar => {
  116. foreach (string refusedChar in refused) if (resultChar == refusedChar) return;
  117. resultStr += resultChar;
  118. });
  119. if (type == DiceEntity.DiceType.Bad) resultStr = refused[0];
  120. return resultStr;
  121. }
  122. public static void DropDice(KeyType key, DiceEntity.DiceType type = DiceEntity.DiceType.NoRestore) {
  123. GetDiceUI(key).GetComponent<DiceUI>().DropDice(type);
  124. }
  125. }