ShootChecker_SDK.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using UnityEngine;
  2. namespace SmartBowSDK
  3. {
  4. /// <summary>
  5. /// 射箭检测器
  6. /// </summary>
  7. public class ShootChecker_SDK : MonoBehaviour
  8. {
  9. public SmartBowHelper smartBowHelper;
  10. /// <summary>
  11. /// 游戏里箭的速度
  12. /// </summary>
  13. public float gameArrowSpeed = 0;
  14. /// <summary>
  15. /// 游戏里箭的重量
  16. /// </summary>
  17. public float gameArrowWeight = 20;
  18. /// <summary>
  19. /// 现实里箭的重量
  20. /// </summary>
  21. public float solidArrowWeight = 75;
  22. /// <summary>
  23. /// 上一次的射击数据ID
  24. /// </summary>
  25. private int _lastShootID = -1;
  26. //记录当前的蓝牙设备信息
  27. private BluetoothDeviceType bluetoothDeviceType = BluetoothDeviceType.NONE;
  28. //记录连接的设备系统信息
  29. private ConnectPlatform connectPlatform = ConnectPlatform.NONE;
  30. /// <summary>
  31. /// 当收到红外数据时
  32. /// </summary>
  33. /// <param name="bytes"></param>
  34. public void OnInfraredDataReceived(byte[] bytes)
  35. {
  36. //序号
  37. int id = bytes[1];
  38. if (id == _lastShootID) return; //因为硬件为了避免丢包,会连续发几条相同射击通知过来
  39. _lastShootID = id;
  40. //时区1耗时
  41. float time1 = bytes[2] * 0.1f;
  42. //时区2耗时
  43. float time2 = bytes[3] * 0.1f;
  44. //时区耗时总和
  45. float totalTime = time1 + time2;
  46. //校验和
  47. int sumCheck = (bytes[0] + bytes[1] + bytes[2] + bytes[3]) & 0xff;
  48. //校验和比较结果
  49. bool sumCheckRes = sumCheck == bytes[4];
  50. //实体箭速度
  51. float solidArrowSpeed = 0.05f / (totalTime / 1000f);
  52. //通过动能定理求箭的速度(实体箭质量*实体箭速度^2=游戏中箭的质量*游戏中箭的速度^2)
  53. gameArrowSpeed = Mathf.Sqrt(solidArrowSpeed * solidArrowSpeed * solidArrowWeight / gameArrowWeight);
  54. //打印
  55. string logInfo = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},实体箭速度:{solidArrowSpeed}m/s,游戏箭速度:{gameArrowSpeed}m/s";
  56. SmartBowLogger.Log(this, logInfo);
  57. //收到正确的射箭数据,就回复硬件,否则n毫秒后硬件会认为丢包而进行重传
  58. if (sumCheckRes) smartBowHelper.bluetoothAim.ReplyInfraredShoot();
  59. //通知九轴算法,因为射箭抖动会使算法计算的姿态角产生误差
  60. smartBowHelper.aimHandler.NotifyAxisOnShot();
  61. smartBowHelper.InvokeOnShooting(gameArrowSpeed);
  62. }
  63. /// <summary>
  64. /// 回去枪指令
  65. /// </summary>
  66. /// <param name="bytes"></param>
  67. public void ReplyGun(byte[] bytes)
  68. {
  69. byte[] response = BluetoothDecryptor.GetResponse(bytes);
  70. smartBowHelper.bluetoothAim.ReplyByte(response);
  71. }
  72. /// <summary>
  73. /// 枪的弹夹状态
  74. /// 0x00 弹夹分离,0x01 上弹夹
  75. /// </summary>
  76. /// <param name="bytes"></param>
  77. public void UpdateTheMagazine(byte[] bytes)
  78. {
  79. SmartBowLogger.Log(this, "切换弹夹后:" + System.BitConverter.ToString(bytes));
  80. BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
  81. bluetoothDeviceType : BluetoothDeviceType.Pistol1;
  82. if (bytes[1] == 0x00)
  83. {
  84. smartBowHelper.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineSeparation);
  85. }
  86. else if (bytes[1] == 0x01)
  87. {
  88. smartBowHelper.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineLoading);
  89. }
  90. }
  91. /// <summary>
  92. /// 0x00 未上膛,0x01 已上膛
  93. /// </summary>
  94. /// <param name="bytes"></param>
  95. public void UpdateChamberState(byte[] bytes)
  96. {
  97. SmartBowLogger.Log(this, "操作上膛状态:" + System.BitConverter.ToString(bytes));
  98. BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
  99. bluetoothDeviceType : BluetoothDeviceType.Pistol1;
  100. if (bytes[1] == 0x00)
  101. {
  102. smartBowHelper.InvokeOnBleDevice(temp, BluetoothDeviceStatus.ChamberEmpty);
  103. }
  104. else if (bytes[1] == 0x01)
  105. {
  106. smartBowHelper.InvokeOnBleDevice(temp, BluetoothDeviceStatus.Chambered);
  107. }
  108. }
  109. /// <summary>
  110. /// 记录当前获得的设备连接数据
  111. /// </summary>
  112. /// <param name="bytes"></param>
  113. public void UpdateDeviceAndSysInfo(byte[] bytes)
  114. {
  115. //Debug.Log("接收到系统数据:" + System.BitConverter.ToString(bytes));
  116. //设备类型
  117. switch (bytes[1])
  118. {
  119. case 0x01:
  120. bluetoothDeviceType = BluetoothDeviceType.HOUYIPro;
  121. break;
  122. case 0x02:
  123. bluetoothDeviceType = BluetoothDeviceType.ARTEMISPro;
  124. break;
  125. case 0x03:
  126. bluetoothDeviceType = BluetoothDeviceType.PistolM9;
  127. break;
  128. case 0x04:
  129. bluetoothDeviceType = BluetoothDeviceType.APOLLO;
  130. break;
  131. case 0x05:
  132. bluetoothDeviceType = BluetoothDeviceType.PistolM17;
  133. break;
  134. case 0x06:
  135. bluetoothDeviceType = BluetoothDeviceType.RifleM416;
  136. break;
  137. }
  138. // 系统类型
  139. switch (bytes[2])
  140. {
  141. case 0x01:
  142. connectPlatform = ConnectPlatform.PHONE;
  143. break;
  144. case 0x02:
  145. connectPlatform = ConnectPlatform.PC;
  146. break;
  147. case 0x03:
  148. connectPlatform = ConnectPlatform.VR;
  149. break;
  150. }
  151. SmartBowLogger.Log(this, "设备类型:" + bluetoothDeviceType.ToString());
  152. SmartBowLogger.Log(this, "系统类型:" + connectPlatform.ToString());
  153. //初始化时候回调数据
  154. smartBowHelper.InvokeOnDeviceAndSystemInfoEvent(connectPlatform, bluetoothDeviceType);
  155. }
  156. }
  157. }