InputM.cs 6.1 KB

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