ShootCheck.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using ArduinoBluetoothAPI;
  7. using DG.Tweening;
  8. using UnityEngine.SceneManagement;
  9. using System.Linq;
  10. using BestHTTP.WebSocket;
  11. using SmartBowSDK;
  12. /* 射箭检测 */
  13. public class ShootCheck : MonoBehaviour
  14. {
  15. [SerializeField] Text text;
  16. public float shootSpeed;
  17. public static ShootCheck ins;
  18. void Awake()
  19. {
  20. ins = this;
  21. }
  22. [SerializeField] InputField ArmBowInputField = default;
  23. public void SetShootBackTime()
  24. {
  25. ArmBow.ins.shootBackTime = int.Parse(ArmBowInputField.text);
  26. }
  27. [NonSerialized] public byte byteTime1;
  28. [NonSerialized] public byte byteTime2;
  29. /**通过红外线数据进行射击 */
  30. public void ShootByInfrared(byte[] bytes) {
  31. byteTime1 = bytes[2];
  32. byteTime1 = bytes[3];
  33. int id = bytes[1]; //序号
  34. float time1 = bytes[2] * 0.1f; //时区1耗时
  35. float time2 = bytes[3] * 0.1f; //时区2耗时
  36. float totalTime = time1 + time2;
  37. // if (totalTime <= 0) {
  38. // totalTime = 0.3f;
  39. // }
  40. //校验和
  41. int sumCheck = bytes[0] + bytes[1] + bytes[2] + bytes[3];
  42. sumCheck &= 0xff;
  43. //校验和比较结果
  44. bool sumCheckRes = sumCheck == bytes[4];
  45. //弓轨速度
  46. float speed = 0.05f / (totalTime / 1000f);
  47. //通过动能定理求箭的速度(实体箭质量*实体箭速度^2=游戏中箭的质量*游戏中箭的速度^2)
  48. shootSpeed = Mathf.Sqrt(speed * speed * CommonConfig.arrowWeight / UserSettings.ins.actualArrowWeight);
  49. //打印
  50. string logTxt = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s";
  51. if (DebugForDevice.ins) DebugForDevice.ins.LogInfrared(logTxt);
  52. //Debug.Log(logTxt);
  53. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
  54. SB_EventSystem.ins.ClickMouse();
  55. } else if (ArmBow.ins) {
  56. ArmBow.ins.ADS_fire(true);
  57. } else if (DuckHunter.SmartBowController.Instance) {
  58. //野鸭射击
  59. DuckHunter.SmartBowController.Instance.OnShooting(shootSpeed);
  60. } else if (WildAttack.SmartBowController.Instance)
  61. {
  62. //荒野射击
  63. WildAttack.SmartBowController.Instance.OnShooting(shootSpeed);
  64. } else if (GameController.ins && GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer) != null) {
  65. //本地双人模式下处理1p,2P 在 BluetoothAim 类处理
  66. GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer).ADS_fire(true, shootSpeed);
  67. } else if (GeneratingTarget.gm != null) {
  68. //移动目标
  69. GeneratingTarget.gm.Shooting(true);
  70. }
  71. else {
  72. OnGameShoot?.Invoke(shootSpeed);
  73. }
  74. }
  75. public Action<float> OnGameShoot;
  76. //分离弹夹事件
  77. public Action OnGameUpdateTheMagazine;
  78. //弹夹状态
  79. public BluetoothDeviceStatus bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineLoading;
  80. /// <summary>
  81. /// 0x00 - 弹夹分离 0x01 - 弹夹上膛
  82. /// </summary>
  83. /// <param name="bytes"></param>
  84. public void UpdateTheMagazine(byte[] bytes) {
  85. BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
  86. bluetoothDeviceType : BluetoothDeviceType.Pistol1;
  87. if (bytes[1] == 0x00) {
  88. //添加一个同步状态
  89. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineSeparation);
  90. Debug.Log("弹夹分离:" + BitConverter.ToString(bytes));
  91. bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineSeparation;
  92. if (GeneratingTarget.gm) {
  93. //Hyperspace
  94. GeneratingTarget.gm.OnSeparation();
  95. }
  96. } else if ( bytes[1] == 0x01){
  97. //添加一个同步状态
  98. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineLoading);
  99. //播放上弹夹时候的声音
  100. AudioMgr.ins.PlayBeLoaded();
  101. Debug.Log("弹夹上膛:" + BitConverter.ToString(bytes));
  102. bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineLoading;
  103. if (Billboard.ins)
  104. {
  105. Billboard.ins.bulletManager.ResetBullets(); //game
  106. }
  107. else if (GeneratingTarget.gm)
  108. { //Hyperspace
  109. GeneratingTarget.gm.OnLoading();
  110. }
  111. else
  112. {
  113. OnGameUpdateTheMagazine?.Invoke(); //水果在gamingmanager
  114. }
  115. }
  116. }
  117. //记录当前的蓝牙设备信息
  118. private BluetoothDeviceType bluetoothDeviceType = BluetoothDeviceType.NONE;
  119. //记录连接的设备系统信息
  120. private ConnectPlatform connectPlatform = ConnectPlatform.NONE;
  121. /// <summary>
  122. /// 0x00 未上膛,0x01 已上膛
  123. /// </summary>
  124. /// <param name="bytes"></param>
  125. public void UpdateChamberState(byte[] bytes)
  126. {
  127. Debug.Log("操作上膛状态:" + BitConverter.ToString(bytes));
  128. BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
  129. bluetoothDeviceType : BluetoothDeviceType.Pistol1;
  130. if (bytes[1] == 0x00)
  131. {
  132. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.ChamberEmpty);
  133. }
  134. else if (bytes[1] == 0x01)
  135. {
  136. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.Chambered);
  137. }
  138. }
  139. /// <summary>
  140. /// 记录当前获得的设备连接数据
  141. /// </summary>
  142. /// <param name="bytes"></param>
  143. public void UpdateDeviceAndSysInfo(byte[] bytes)
  144. {
  145. //Debug.Log("接收到系统数据:" + System.BitConverter.ToString(bytes));
  146. //设备类型
  147. switch (bytes[1])
  148. {
  149. case 0x01:
  150. bluetoothDeviceType = BluetoothDeviceType.HOUYIPro;
  151. break;
  152. case 0x02:
  153. bluetoothDeviceType = BluetoothDeviceType.ARTEMISPro;
  154. break;
  155. case 0x03:
  156. bluetoothDeviceType = BluetoothDeviceType.PistolM9;
  157. break;
  158. case 0x04:
  159. bluetoothDeviceType = BluetoothDeviceType.APOLLO;
  160. break;
  161. case 0x05:
  162. bluetoothDeviceType = BluetoothDeviceType.PistolM17;
  163. break;
  164. case 0x06:
  165. bluetoothDeviceType = BluetoothDeviceType.RifleM416;
  166. break;
  167. }
  168. // 系统类型
  169. switch (bytes[2])
  170. {
  171. case 0x01:
  172. connectPlatform = ConnectPlatform.PHONE;
  173. break;
  174. case 0x02:
  175. connectPlatform = ConnectPlatform.PC;
  176. break;
  177. case 0x03:
  178. connectPlatform = ConnectPlatform.VR;
  179. break;
  180. }
  181. Debug.Log("设备类型:" + bluetoothDeviceType.ToString());
  182. Debug.Log("系统类型:" + connectPlatform.ToString());
  183. //初始化时候回调数据
  184. BluetoothAim.ins.InvokeOnDeviceAndSystemInfoEvent(connectPlatform, bluetoothDeviceType);
  185. }
  186. void Log(string text)
  187. {
  188. if (this.text)
  189. {
  190. this.text.text = text;
  191. } else {
  192. Debug.Log(text);
  193. }
  194. }
  195. }