AimHandler.cs 19 KB

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