SmartBowController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using SmartBowSDK;
  4. namespace DuckHunter
  5. {
  6. public class SmartBowController : MonoBehaviour
  7. {
  8. [SerializeField] Button _btnConnect;
  9. [SerializeField] Button _btnCalibrateGyr;
  10. [SerializeField] Button _btnCalibrateMag;
  11. [SerializeField] Button _btnResetAim;
  12. public static SmartBowController Instance;
  13. void Awake()
  14. {
  15. if (Instance)
  16. {
  17. Destroy(gameObject);
  18. return;
  19. }
  20. Instance = this;
  21. DontDestroyOnLoad(gameObject);
  22. }
  23. void Start()
  24. {
  25. _btnCalibrateGyr.onClick.AddListener(OnClick_CalibrateGyr);
  26. _btnCalibrateMag.onClick.AddListener(OnClick_CalibrateMag);
  27. SmartBowHelper.GetInstance().calibrateDataStorageMode = CalibrateDataStorageMode.Local;
  28. //监听蓝牙状态变化
  29. SmartBowHelper.GetInstance().OnBluetoothStatusChanged += OnBluetoothStatusChanged;
  30. //监听蓝牙抛出的错误
  31. SmartBowHelper.GetInstance().OnBluetoothError += OnBluetoothError;
  32. //监听蓝牙模块初始化完成
  33. SmartBowHelper.GetInstance().OnBluetoothModuleInited += OnBluetoothModuleInited;
  34. //监听姿态角变化
  35. SmartBowHelper.GetInstance().OnRotationUpdate += OnRotationUpdate;
  36. //监听射击
  37. SmartBowHelper.GetInstance().OnShooting += OnShooting;
  38. //监听模块功能键短按
  39. SmartBowHelper.GetInstance().OnFunctionKeyPress += OnFunctionKeyPress;
  40. //监听模块功能键长按
  41. SmartBowHelper.GetInstance().OnFunctionKeyLongPress += OnFunctionKeyLongPress;
  42. UpdateBtnConnectText();
  43. // ActiveCanvasUI();
  44. // ActiveBoxBLEModule(false);
  45. }
  46. float lastAutoConnectTime = 0;
  47. void Update()
  48. {
  49. // if (_boxBLEActived)
  50. // {
  51. // UpdateCalibrateGyrText();
  52. // UpdateCalibrateMagText();
  53. // }
  54. UpdateCalibrateGyrText();
  55. UpdateCalibrateMagText();
  56. if (
  57. SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None &&
  58. Time.realtimeSinceStartup - lastAutoConnectTime > 1
  59. ) {
  60. lastAutoConnectTime = Time.realtimeSinceStartup;
  61. SmartBowHelper.GetInstance().Connect();
  62. }
  63. }
  64. /// <summary>
  65. /// 按钮逻辑-连接/断开蓝牙模块
  66. /// </summary>
  67. public void OnClick_Connect()
  68. {
  69. AudioMgr.ins.PlayBtn();
  70. if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
  71. if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
  72. {
  73. SmartBowHelper.GetInstance().Connect();
  74. return;
  75. }
  76. if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
  77. {
  78. SmartBowHelper.GetInstance().Disconnect();
  79. return;
  80. }
  81. }
  82. /// <summary>
  83. /// 按钮逻辑-视角归位
  84. /// </summary>
  85. public void OnClick_ResetAim()
  86. {
  87. AudioMgr.ins.PlayBtn();
  88. SmartBowHelper.GetInstance().ResetAim();
  89. }
  90. /// <summary>
  91. /// 按钮逻辑-开始/停止陀螺仪校准
  92. /// </summary>
  93. public void OnClick_CalibrateGyr()
  94. {
  95. AudioMgr.ins.PlayBtn();
  96. if (SmartBowHelper.GetInstance().IsGyrCalibrating())
  97. SmartBowHelper.GetInstance().StopGyrCalibration();
  98. else
  99. SmartBowHelper.GetInstance().StartGyrCalibration();
  100. }
  101. void UpdateCalibrateGyrText()
  102. {
  103. Text text = _btnCalibrateGyr.GetComponentInChildren<Text>();
  104. //当陀螺仪校准完成时
  105. if (SmartBowHelper.GetInstance().IsGyrCompleted())
  106. {
  107. //UI显示校准完成
  108. text.text = "<color=#00FF00>陀螺仪初始化完成</color>";
  109. }
  110. else
  111. {
  112. //获取陀螺仪校准进度
  113. int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
  114. //UI显示校准进度
  115. if (SmartBowHelper.GetInstance().IsGyrCalibrating()) text.text = string.Format("<color=#FFC500>停止</color>初始化陀螺仪{0}%", progress);
  116. else text.text = string.Format("开始初始化陀螺仪{0}%", progress);
  117. }
  118. }
  119. float _clickCalibrateMagTime = -100;
  120. /// <summary>
  121. /// 按钮逻辑-重置地磁计校准
  122. /// </summary>
  123. public void OnClick_CalibrateMag()
  124. {
  125. AudioMgr.ins.PlayBtn();
  126. _clickCalibrateMagTime = Time.realtimeSinceStartup;
  127. SmartBowHelper.GetInstance().StartMagCalibration();
  128. }
  129. void UpdateCalibrateMagText()
  130. {
  131. Text text = _btnCalibrateMag.GetComponentInChildren<Text>();
  132. //当地磁计校准完成时
  133. if (SmartBowHelper.GetInstance().IsMagCompleted())
  134. {
  135. text.text = "<color=#00FF00>地磁计初始化完成</color>";
  136. }
  137. else
  138. {
  139. text.text = Time.realtimeSinceStartup - _clickCalibrateMagTime < 1 ? "<color=#FFC500>地磁计已重置</color>" : "地磁计自动初始化中";
  140. }
  141. }
  142. void UpdateBtnConnectText()
  143. {
  144. // //获取蓝牙状态
  145. // BluetoothStatusEnum bluetoothStatus = SmartBowHelper.GetInstance().GetBluetoothStatus();
  146. // Text text = _btnConnect.GetComponentInChildren<Text>();
  147. // //显示蓝牙状态-未连接
  148. // if (bluetoothStatus == BluetoothStatusEnum.None) text.text = "连接";
  149. // //显示蓝牙状态-连接中
  150. // if (bluetoothStatus == BluetoothStatusEnum.Connecting) text.text = "<color=#FFC500>连接中</color>";
  151. // //显示蓝牙状态-已连接
  152. // if (bluetoothStatus == BluetoothStatusEnum.Connected)
  153. // {
  154. // //判断蓝牙模块是否初始化完成
  155. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) text.text = "<color=#00FF00>已连接</color>";
  156. // else text.text = "<color=#00FF00>已连接</color> <color=#00DFFF>(初始化中)</color>";
  157. // }
  158. }
  159. void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  160. {
  161. UpdateBtnConnectText();
  162. if (newStatus == BluetoothStatusEnum.Connected) {
  163. BowCamera.isTouchMode = false;
  164. } else {
  165. BowCamera.isTouchMode = true;
  166. }
  167. }
  168. void OnBluetoothError(BluetoothError error, string message)
  169. {
  170. Debug.Log("OnBluetoothError:" + error + " " + message);
  171. string errorText = null;
  172. if (error == BluetoothError.BluetoothNotEnabled)
  173. errorText = "蓝牙开关未打开";
  174. else if (error == BluetoothError.LocationPermissionNotGranted)
  175. errorText = "尚未授予定位权限";
  176. else if (error == BluetoothError.ScanPermissionNotGranted)
  177. errorText = "尚未授予扫描附近设备权限";
  178. else if (error == BluetoothError.ScanNotFoundTargetDevice)
  179. errorText = "连接失败,未发现目标设备!";
  180. if (errorText != null)
  181. TextSmartBowTip.Show(errorText);
  182. }
  183. /// <summary>
  184. /// 当蓝牙模块初始化完成时
  185. /// </summary>
  186. void OnBluetoothModuleInited()
  187. {
  188. UpdateBtnConnectText();
  189. //获取Mac地址
  190. string macAddress = SmartBowHelper.GetInstance().GetMacAddress();
  191. Debug.Log("Mac地址为" + macAddress);
  192. //获取电量
  193. int battery = SmartBowHelper.GetInstance().GetBattery();
  194. Debug.Log("当前电量为" + battery);
  195. //开启九轴传感
  196. SmartBowHelper.GetInstance().StartRotationSensor();
  197. //开启射箭传感
  198. SmartBowHelper.GetInstance().StartShootingSensor();
  199. }
  200. void OnRotationUpdate(Quaternion rotation)
  201. {
  202. CameraToLook.ins.transform.localRotation = rotation;
  203. }
  204. float _lastShootTime = 0;
  205. void OnShooting(float speed)
  206. {
  207. if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
  208. _lastShootTime = Time.realtimeSinceStartup;
  209. if (ArmBow.ins) {
  210. ArmBow.ins.shootSpeed = speed;
  211. ArmBow.ins.ADS_fire();
  212. }
  213. }
  214. void OnFunctionKeyPress()
  215. {
  216. Debug.Log("功能键短按");
  217. AutoResetView.DoIdentity();
  218. }
  219. [ContextMenu("TestIdentity")]
  220. void TestIdentity() {
  221. AutoResetView.DoIdentity();
  222. }
  223. void OnFunctionKeyLongPress()
  224. {
  225. Debug.Log("功能键长按");
  226. // SB_EventSystem.ins.AwakenSimulateMouse();
  227. }
  228. private void ActiveCanvasUI()
  229. {
  230. transform.Find("CanvasUI").gameObject.SetActive(true);
  231. }
  232. // bool _boxBLEActived = false;
  233. // public void ActiveBoxBLEModule(bool active)
  234. // {
  235. // var o = transform.Find("CanvasUI/BoxBLEModule").gameObject;
  236. // o.SetActive(active);
  237. // _boxBLEActived = active;
  238. // }
  239. /// <summary>
  240. /// 反转激活BoxBLEModule
  241. /// </summary>
  242. public void ActiveBoxBLEModule()
  243. {
  244. // var o = transform.Find("CanvasUI/BoxBLEModule").gameObject;
  245. // o.SetActive(!o.activeSelf);
  246. // _boxBLEActived = o.activeSelf;
  247. }
  248. /// <summary>
  249. /// 按钮逻辑-停止传感器
  250. /// </summary>
  251. public void OnClick_StopSensor()
  252. {
  253. //停止九轴传感
  254. SmartBowHelper.GetInstance().StopRotationSensor();
  255. //停止射箭传感
  256. SmartBowHelper.GetInstance().StopShootingSensor();
  257. }
  258. }
  259. }