ShootCheck.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class ShootCheck : MonoBehaviour
  5. {
  6. float[] accList = new float[30];
  7. int dataCount = 0;
  8. float gravity = 0;
  9. float maxAcc = 0;
  10. bool hasReachShootThreshold = false;
  11. bool locked = false;
  12. int hitCount = 0;
  13. [SerializeField] Text text;
  14. public float shootSpeed;
  15. public static ShootCheck ins;
  16. void Start()
  17. {
  18. ins = this;
  19. }
  20. void OnDestroy()
  21. {
  22. ins = null;
  23. }
  24. float[] ays = new float[4];
  25. public void OnDataReceived(byte[] bytes) {
  26. ays[0] = ToAcceleratedSpeed(bytes[7], bytes[8]);
  27. ays[1] = ToAcceleratedSpeed(bytes[17], bytes[18]);
  28. ays[2] = ToAcceleratedSpeed(bytes[27], bytes[28]);
  29. ays[3] = ToAcceleratedSpeed(bytes[37], bytes[38]);
  30. foreach (float ay in ays)
  31. {
  32. try
  33. {
  34. if (ins.check(ay))
  35. {
  36. if (ArmBow.ins != null)
  37. {
  38. ArmBow.ins.ADS_fire();
  39. }
  40. }
  41. }
  42. catch (Exception e)
  43. {
  44. Debug.Log(e.Message);
  45. }
  46. }
  47. }
  48. float ToAcceleratedSpeed(byte b1, byte b2)
  49. {
  50. int value = TwoByteToInt(b1, b2);
  51. return (float)value / 32768 * 16;
  52. }
  53. int TwoByteToInt(byte b1, byte b2)
  54. {
  55. ushort twoByte = (ushort)(b1 * 256 + b2);
  56. short shortNum = (short)twoByte;
  57. return (int)shortNum;
  58. }
  59. int afterReachShootFrameCount = 0;
  60. int steadyFrameCount = 6;
  61. bool check(float acc)
  62. {
  63. DebugLine.show(acc);
  64. DebugLine.showSteady(gravity);
  65. for (int i = accList.Length - 1; i > 0; i--)
  66. {
  67. accList[i] = accList[i - 1];
  68. }
  69. accList[0] = acc;
  70. dataCount++;
  71. if (locked)
  72. {
  73. return false;
  74. }
  75. if (hasReachShootThreshold) {
  76. if (acc <= gravity) {
  77. hitCount++;
  78. shootSpeed = maxAcc - gravity;
  79. Log("第" + hitCount + "次识别射箭\n过滤正轴重力" + gravity.ToString("#0.000") + "后\n所得最大加速度峰值" + (maxAcc - gravity).ToString("#0.000") + "\n有效帧数" + afterReachShootFrameCount);
  80. maxAcc = 0;
  81. afterReachShootFrameCount = 0;
  82. hasReachShootThreshold = false;
  83. Dolock();
  84. Invoke("Unlock", 0.8f);
  85. return true;
  86. } else if (acc > maxAcc) {
  87. maxAcc = acc;
  88. }
  89. afterReachShootFrameCount++;
  90. if (afterReachShootFrameCount >= 20)
  91. {
  92. maxAcc = 0;
  93. afterReachShootFrameCount = 0;
  94. hasReachShootThreshold = false;
  95. Log("不符合短时间爆发加速");
  96. }
  97. return false;
  98. }
  99. if (dataCount > steadyFrameCount)
  100. {
  101. float totalAcc = 0;
  102. for (int i = 0; i < steadyFrameCount; i++)
  103. {
  104. totalAcc += accList[i];
  105. }
  106. float rangeAcc = totalAcc / steadyFrameCount;
  107. float squareAcc = 0;
  108. for (int i = 0; i < steadyFrameCount; i++)
  109. {
  110. squareAcc += (float) Mathf.Pow(accList[i] - rangeAcc, 2);
  111. }
  112. squareAcc /= steadyFrameCount;
  113. if (squareAcc < 0.00012)
  114. {
  115. gravity = Mathf.Clamp(rangeAcc, -0.981f, 0.981f);
  116. }
  117. if (acc - gravity > 3)
  118. {
  119. // for (int i = 1; i < dataCount && i < 15; i++)
  120. // {
  121. // if (accList[i] - gravity < - 1)
  122. // {
  123. // Dolock();
  124. // Invoke("Unlock", 0.2f);
  125. // return false;
  126. // }
  127. // }
  128. hasReachShootThreshold = true;
  129. maxAcc = acc;
  130. }
  131. }
  132. return false;
  133. }
  134. void Dolock()
  135. {
  136. this.locked = true;
  137. }
  138. void Unlock()
  139. {
  140. this.locked = false;
  141. }
  142. void Log(string text)
  143. {
  144. if (this.text != null)
  145. {
  146. this.text.text = text;
  147. } else {
  148. Debug.Log(text);
  149. }
  150. }
  151. }