SmartBowController.cs 10 KB

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