AimHandler.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. public enum AimDeviceType {
  7. NONE = -1,
  8. HOUYI = 0,
  9. HOUYI2 = 1,
  10. ARTEMIS = 2,
  11. HOUYIPRO = 3,
  12. //枪械类
  13. Gun = 4,
  14. ARTEMISPRO = 5,
  15. }
  16. [Serializable]//需要在转换为json格式的类的上方添加序列化
  17. public class AimDeviceInfo
  18. {
  19. public int id;
  20. public int type = -1;//类型 AimDeviceType
  21. public int pos; //位置
  22. public bool bInitMac = false; //是否初始化Mac
  23. public string mac; //记录当前
  24. public AimDeviceInfo(int _id) {
  25. this.id = _id;
  26. }
  27. public void setInitMac(string macTemp) {
  28. bInitMac = true;
  29. mac = macTemp;
  30. }
  31. public void resetInitMac() {
  32. bInitMac = false;
  33. mac = "";
  34. }
  35. }
  36. [Serializable]
  37. public class AimDeviceInfos
  38. {
  39. public List<AimDeviceInfo> arry;
  40. }
  41. /* 瞄准处理器 */
  42. public class AimHandler : MonoBehaviour
  43. {
  44. CameraToLook m_controlObj {
  45. get {
  46. return CameraToLook.ins;
  47. }
  48. }
  49. //记录一个设备
  50. public AimDeviceInfo aimDeviceInfo = null;
  51. public AimDeviceInfo tempAimDeviceInfo = null;//临时处理值。最后赋值给aimDeviceInfo
  52. public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos();
  53. int deviceSelectIndex = 0;//默认选中第一个设备(1p)
  54. public Action aimDeviceInfoChangeEvent;
  55. public int DeviceType
  56. {
  57. get
  58. {
  59. if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9;
  60. if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663;
  61. return 0;
  62. }
  63. }
  64. private AxisBaseHandler m_axisHandler;
  65. //陀螺仪校准进度记录
  66. [NonSerialized] public int gyrCalibrateCompleteCount = 0;
  67. [NonSerialized] public int gyrCalibrateTotalCount = 2000;
  68. public static AimHandler ins;
  69. public bool bInitOne = false;
  70. void Start()
  71. {
  72. ins = this;
  73. BluetoothDispatcher.aim = OnDataReceived;
  74. // m_axisHandler = new Axis9NopackHandler(this); //九轴旧
  75. m_axisHandler = new Axis9Handler(this); //九轴新
  76. // m_axisHandler = new Axis663Handler(this); //双陀螺仪
  77. m_axisHandler.Init();
  78. // StartCoroutine(TestRecord());
  79. }
  80. public void onClearAimDeviceInfosNew()
  81. {
  82. PlayerPrefs.DeleteKey("aim-device-info-" + LoginMgr.myUserInfo.id);
  83. aimDeviceInfos.arry.Clear();
  84. }
  85. public void OnGetAimDeviceInfos() {
  86. string deviceInfo = PlayerPrefs.GetString("aim-device-info-" + LoginMgr.myUserInfo.id, "");
  87. Debug.Log("get deviceSelectIndex:" + deviceInfo);
  88. if (deviceInfo != "")
  89. {
  90. aimDeviceInfos = JsonUtility.FromJson<AimDeviceInfos>(deviceInfo);//这里的类是依据最外层{}决定的
  91. }
  92. aimDeviceInfoChangeEvent?.Invoke();
  93. }
  94. public void OnSaveAimDeviceInfos()
  95. {
  96. aimDeviceInfoChangeEvent?.Invoke();
  97. PlayerPrefs.SetString("aim-device-info-" + LoginMgr.myUserInfo.id, JsonUtility.ToJson(aimDeviceInfos));
  98. }
  99. //创建一个选择值
  100. public void onCreateTempAimDeviceInfo() {
  101. tempAimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
  102. Debug.Log("onCreateTempAimDeviceInfo deviceSelectIndex:" + deviceSelectIndex);
  103. }
  104. //是否存在AimDeviceInfo,存在更新,不存在创建;
  105. public void onCreateAimDeviceInfoById()
  106. {
  107. OnGetAimDeviceInfos();
  108. //deviceIndex 区分 1p 还是 2p
  109. bool bCanAdd = true;
  110. foreach (AimDeviceInfo p in aimDeviceInfos.arry)
  111. {
  112. if (deviceSelectIndex == p.id)
  113. {
  114. aimDeviceInfo = p;
  115. bCanAdd = false;
  116. }
  117. //Debug.Log("33deviceSelectIndex:" + deviceSelectIndex + ",bCanAdd:" + bCanAdd+ " , id:" + p.id +" , type:"+ p.type);
  118. }
  119. if (bCanAdd)
  120. {
  121. Debug.Log("add deviceSelectIndex:" + deviceSelectIndex);
  122. aimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
  123. aimDeviceInfos.arry.Add(aimDeviceInfo);
  124. }
  125. OnSaveAimDeviceInfos();
  126. }
  127. public void SetAimDeviceSelectIndex(int selectIndex) {
  128. if (selectIndex < 0) {
  129. Debug.LogWarning("selectIndex不能小于0:"+ selectIndex);
  130. return;
  131. }
  132. deviceSelectIndex = selectIndex;
  133. Debug.Log("SetAimDeviceSelectIndex :" + selectIndex);
  134. }
  135. public void SetTempAimDeviceType(AimDeviceType _aimDeviceType)
  136. {
  137. if (tempAimDeviceInfo == null) return;
  138. tempAimDeviceInfo.type = (int)_aimDeviceType;
  139. }
  140. public void SetAimDeviceType(AimDeviceType _aimDeviceType)
  141. {
  142. if (aimDeviceInfo == null) return;
  143. aimDeviceInfo.type = (int)_aimDeviceType;
  144. _AimDeviceInfosUpdate();
  145. OnSaveAimDeviceInfos();
  146. }
  147. public void SetAimDeviceType(int _aimDeviceType)
  148. {
  149. if (aimDeviceInfo == null) return;
  150. aimDeviceInfo.type = _aimDeviceType;
  151. _AimDeviceInfosUpdate();
  152. OnSaveAimDeviceInfos();
  153. }
  154. //APP连接需进行MAC地址的比对。
  155. //只有再引导初始化页面才保存连接使用的mac地址。
  156. public void SetAimDeviceMac(string _mac) {
  157. if (aimDeviceInfo == null) return;
  158. aimDeviceInfo.setInitMac(_mac);
  159. _AimDeviceInfosUpdate();
  160. OnSaveAimDeviceInfos();
  161. }
  162. //重置mac存储信息
  163. public void ResetAimDeviceMac() {
  164. if (aimDeviceInfo == null) return;
  165. aimDeviceInfo.resetInitMac();
  166. _AimDeviceInfosUpdate();
  167. OnSaveAimDeviceInfos();
  168. }
  169. void _AimDeviceInfosUpdate() {
  170. for (int i = 0; i < aimDeviceInfos.arry.Count; i++)
  171. {
  172. if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id)
  173. {
  174. aimDeviceInfos.arry[i] = aimDeviceInfo;
  175. }
  176. }
  177. }
  178. #region 根据设备1p或者2p 获取HOUYIPRO的状态
  179. public bool isHOUYIPRO(BluetoothPlayer bluetoothPlayer)
  180. {
  181. bool _isHOUYIPRO = false;
  182. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  183. {
  184. if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.HOUYIPRO)
  185. {
  186. _isHOUYIPRO = true;
  187. }
  188. }
  189. return _isHOUYIPRO;
  190. }
  191. #endregion
  192. #region 根据设备1p或者2p 获取ARTEMISPro的状态
  193. public bool isARTEMISPro(BluetoothPlayer bluetoothPlayer)
  194. {
  195. bool _isARTEMISPro = false;
  196. foreach (AimDeviceInfo p in ins.aimDeviceInfos.arry)
  197. {
  198. if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.ARTEMISPRO)
  199. {
  200. _isARTEMISPro = true;
  201. }
  202. }
  203. return _isARTEMISPro;
  204. }
  205. #endregion
  206. public void ReinitAxisHandler()
  207. {
  208. Debug.Log("ReinitAxisHandler");
  209. m_axisHandler.Init();
  210. }
  211. // System.Collections.IEnumerator TestRecord() {
  212. // while (LoginMgr.myUserInfo.id == 0) yield return null;
  213. // UserComp.Instance.saveMac();
  214. // }
  215. [NonSerialized] private Quaternion newRotation = Quaternion.identity;
  216. public void SetNewRotation(Quaternion quat) {
  217. this.newRotation = quat;
  218. }
  219. public void SetNewRotation(o0.Geometry.Quaternion o0Quat) {
  220. Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat);
  221. if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) {
  222. Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  223. return;
  224. }
  225. if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) {
  226. Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  227. return;
  228. }
  229. this.newRotation = quat;
  230. }
  231. [NonSerialized] public bool lerpForRotation = true;
  232. [NonSerialized] public float lerpTimeRate = 7;
  233. public void Update()
  234. {
  235. //&& !InfraredDemo.running
  236. if (m_controlObj && !m_ban9AxisCalculate && bRuning9Axis())
  237. {
  238. if (lerpForRotation)
  239. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate);
  240. else
  241. m_controlObj.localRotation = newRotation;
  242. }
  243. if (IsMagCompleted())
  244. {
  245. if (!m_magCompleted)
  246. {
  247. StartCoroutine(SaveMag());
  248. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success"));
  249. }
  250. m_magCompleted = true;
  251. } else {
  252. m_magCompleted = false;
  253. }
  254. if (Input.GetKeyDown(KeyCode.Space))
  255. {
  256. AutoResetView.DoIdentity();
  257. }
  258. }
  259. public void OnDataReceived(byte[] bytes)
  260. {
  261. if (bytes.Length != 27 && bytes.Length != 39)
  262. {
  263. if (bytes.Length == 2)
  264. {
  265. if (bytes[0] == 0x66 && bytes[1] == 0x31)
  266. {
  267. if (bInitOne)
  268. {
  269. bInitOne = false;
  270. if (aimDeviceInfo.type == (int)AimDeviceType.NONE ||
  271. aimDeviceInfo.type == (int)AimDeviceType.HOUYI ||
  272. aimDeviceInfo.type == (int)AimDeviceType.HOUYI2 ||
  273. aimDeviceInfo.type == (int)AimDeviceType.ARTEMIS)
  274. {
  275. AutoResetView.DoIdentity();
  276. }
  277. else
  278. {
  279. //准心开关
  280. CrossBtnEvent();
  281. }
  282. }
  283. else
  284. {
  285. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  286. {
  287. //视角回正
  288. DoIdentity();
  289. //鼠标居中自然会居中
  290. }
  291. else
  292. {
  293. if (aimDeviceInfo.type == (int)AimDeviceType.NONE ||
  294. aimDeviceInfo.type == (int)AimDeviceType.HOUYI ||
  295. aimDeviceInfo.type == (int)AimDeviceType.HOUYI2 ||
  296. aimDeviceInfo.type == (int)AimDeviceType.ARTEMIS)
  297. {
  298. AutoResetView.DoIdentity();
  299. }
  300. else
  301. {
  302. //准心开关
  303. CrossBtnEvent();
  304. }
  305. }
  306. }
  307. }
  308. else if (bytes[0] == 0x66 && bytes[1] == 0x32)
  309. {
  310. if (SB_EventSystem.ins)
  311. {
  312. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  313. //唤起/隐藏虚拟鼠标
  314. SB_EventSystem.ins.AwakenSimulateMouse();
  315. }
  316. }
  317. else if (bytes[1] == 10)
  318. {
  319. //显示电量
  320. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  321. //DeviceView.ins.RenderBattery(1, bytes[0]);
  322. }
  323. }
  324. else if (bytes[0] == 0x5b)
  325. {
  326. //红外射击检测
  327. ShootCheck.ins.ShootByInfrared(bytes);
  328. }
  329. else if (bytes[0] == 0x5C)
  330. {
  331. //00 弹夹分离,01 上弹夹
  332. ShootCheck.ins.UpdateTheMagazine(bytes);
  333. }
  334. else if (bytes[0] == 0x5E)
  335. {
  336. Debug.Log("接收到系统数据:" + BitConverter.ToString(bytes));
  337. //设备类型
  338. switch (bytes[1])
  339. {
  340. case 0x01:
  341. Debug.Log("设备类型:HOUYI Pro");
  342. break;
  343. case 0x02:
  344. Debug.Log("设备类型:ARTEMIS Pro");
  345. break;
  346. case 0x03:
  347. Debug.Log("设备类型:Pistol 1");
  348. break;
  349. }
  350. // 系统类型
  351. switch (bytes[2])
  352. {
  353. case 0x01:
  354. Debug.Log("系统类型:移动手机");
  355. break;
  356. case 0x02:
  357. Debug.Log("系统类型:PC电脑");
  358. break;
  359. case 0x03:
  360. Debug.Log("系统类型:VR设备");
  361. break;
  362. }
  363. }
  364. return;
  365. }
  366. //if (InfraredDemo.running)
  367. //{
  368. // BluetoothAim.ins.WriteData("4"); //关闭九轴
  369. // return;
  370. //}
  371. if (!bRuning9Axis())
  372. {
  373. return;
  374. }
  375. m_axisHandler.Update(bytes);
  376. if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
  377. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation);
  378. }
  379. public byte[] InsertByteAtBeginning(byte[] originalArray, byte newByte)
  380. {
  381. byte[] newArray = new byte[originalArray.Length + 1];
  382. newArray[0] = newByte;
  383. Array.Copy(originalArray, 0, newArray, 1, originalArray.Length);
  384. return newArray;
  385. }
  386. private bool m_magCompleted;
  387. public void CorrectMagCompleted(bool value) { m_magCompleted = value; }
  388. private bool m_ban9AxisCalculate = false;
  389. public void Ban9AxisCalculate(bool ban)
  390. {
  391. // m_ban9AxisCalculate = ban;
  392. // if (!ban)
  393. // {
  394. // SetMsOldDefault();
  395. // if (m_controlObj) m_controlObj.localRotation = newRotation;
  396. // }
  397. }
  398. public void SetMsOldDefault()
  399. {
  400. if (m_axisHandler.GetType() == typeof(Axis9NopackHandler))
  401. (m_axisHandler as Axis9NopackHandler).msOld = default;
  402. }
  403. public void DoIdentity()
  404. {
  405. //if (InfraredDemo.running) return;
  406. m_axisHandler.DoIdentity();
  407. if (!bRuning9Axis()) return;
  408. if (m_controlObj) m_controlObj.localRotation = newRotation;
  409. }
  410. public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); }
  411. public void CalibrateGyr(bool calibration) { m_axisHandler.CalibrateGyr(calibration); }
  412. public void InitGyr(string record) { m_axisHandler.InitGyr(record); }
  413. public void InitMag(string record) { m_axisHandler.InitMag(record); }
  414. public void ResetGyr() { m_axisHandler.ResetGyr(); }
  415. public void ResetMag() { m_axisHandler.ResetMag(); }
  416. public void ApplyImpreciseMag() { m_axisHandler.ApplyImpreciseMag(); }
  417. public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); }
  418. public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); }
  419. public IEnumerator SaveGyr() { return m_axisHandler.SaveGyr(); }
  420. public IEnumerator SaveMag() { return m_axisHandler.SaveMag(); }
  421. public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); }
  422. #region 获取一个连接的设备下,对应的 Archery 是运行9轴的代码处理位置信息判断
  423. public bool bRuning9Axis() {
  424. //连接的设备下,对应的 Archery 是运行9轴的代码处理位置信息判断
  425. //弓箭类型,但是不是HOUYI Pro 情况下。运行9轴代码
  426. bool bRuning = GlobalData.MyDeviceMode == DeviceMode.Archery && !BluetoothAim.ins.isMainConnectToInfraredDevice();
  427. return bRuning;
  428. }
  429. #endregion
  430. /// <summary>
  431. /// 操作准心开关事件
  432. /// </summary>
  433. private void CrossBtnEvent()
  434. {
  435. Debug.Log("CrossBtnEvent");
  436. //准心开关
  437. if (GameAssistUI.ins)
  438. {
  439. //显示控制准心按钮
  440. Button crossHairBtn = GameAssistUI.ins.transform.Find("Button5").GetComponent<Button>();
  441. crossHairBtn.onClick.Invoke();
  442. }
  443. else if (DuckHunter.GameUI.Instance)
  444. {
  445. //显示控制准心按钮
  446. Button crossHairBtn = DuckHunter.GameUI.Instance.transform.Find("BtnCrosshair").GetComponent<Button>();
  447. crossHairBtn.onClick.Invoke();
  448. }
  449. else
  450. {
  451. //水果
  452. Button crossHairBtn = GameObject.Find("PermanentCanvas/CrossHair_Btn").GetComponent<Button>();
  453. crossHairBtn.onClick.Invoke();
  454. }
  455. }
  456. }