ShootCheck.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public float shootSpeed;
  14. [SerializeField] Text text;
  15. public static ShootCheck ins;
  16. void Start()
  17. {
  18. ins = this;
  19. BluetoothClient.onDataReceived = (byte[] bytes) => {
  20. float ay = ToAcceleratedSpeed(bytes[6], bytes[7]);
  21. try
  22. {
  23. if (ShootCheck.ins.check(ay)) {
  24. if (ArmBow.ins != null) {
  25. ArmBow.ins.ADS_fire();
  26. }
  27. }
  28. }
  29. catch (Exception e)
  30. {
  31. Debug.Log(e.Message);
  32. }
  33. };
  34. }
  35. float ToAcceleratedSpeed(byte b1, byte b2)
  36. {
  37. int value = TwoByteToInt(b1, b2);
  38. return (float)value / 32768 * 16;
  39. }
  40. int TwoByteToInt(byte b1, byte b2)
  41. {
  42. ushort twoByte = (ushort)(b1 * 256 + b2);
  43. short shortNum = (short)twoByte;
  44. return (int)shortNum;
  45. }
  46. public bool check(float acc)
  47. {
  48. DebugLine.show(acc);
  49. for (int i = accList.Length - 1; i > 0; i--)
  50. {
  51. accList[i] = accList[i - 1];
  52. }
  53. accList[0] = acc;
  54. dataCount++;
  55. if (locked)
  56. {
  57. return false;
  58. }
  59. if (hasReachShootThreshold) {
  60. if (acc <= gravity) {
  61. hitCount++;
  62. Log("第" + hitCount + "次识别射箭,过滤正轴重力" + gravity.ToString("#0.000") + "后,所得最大加速度峰值" + (maxAcc - gravity).ToString("#0.000"));
  63. hasReachShootThreshold = false;
  64. maxAcc = 0;
  65. Dolock();
  66. Invoke("Unlock", 0.8f);
  67. return true;
  68. } else if (accList[1] > accList[0] && accList[1] > accList[2]) {
  69. if (accList[1] > maxAcc)
  70. {
  71. maxAcc = accList[1];
  72. }
  73. return false;
  74. }
  75. return false;
  76. }
  77. if (dataCount > 3)
  78. {
  79. float totalAcc = 0;
  80. for (int i = 0; i < 3; i++)
  81. {
  82. totalAcc += accList[i];
  83. }
  84. float rangeAcc = totalAcc / 3;
  85. float squareAcc = 0;
  86. for (int i = 0; i < 3; i++)
  87. {
  88. squareAcc += (float) Mathf.Pow(accList[i] - rangeAcc, 2);
  89. }
  90. squareAcc /= 3;
  91. if (squareAcc < 0.0003)
  92. {
  93. gravity = Mathf.Clamp(rangeAcc, -0.981f, 0.981f);
  94. }
  95. if (accList[1] - gravity > 3.3)
  96. {
  97. if (accList[1] > accList[0] && accList[1] > accList[2])
  98. {
  99. hasReachShootThreshold = true;
  100. maxAcc = accList[1];
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. void Dolock()
  107. {
  108. this.locked = true;
  109. }
  110. void Unlock()
  111. {
  112. this.locked = false;
  113. }
  114. void Log(string text)
  115. {
  116. if (this.text != null)
  117. {
  118. this.text.text = text;
  119. } else {
  120. Debug.Log(text);
  121. }
  122. }
  123. }