AimHandler.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. HOUYIPRO = 3,
  9. //枪械类
  10. Gun = 4,
  11. ARTEMISPRO = 5,
  12. }
  13. [Serializable]//需要在转换为json格式的类的上方添加序列化
  14. public class AimDeviceInfo
  15. {
  16. public int id;
  17. public int type = -1;//类型 AimDeviceType
  18. public int pos; //位置
  19. public bool bInitMac = false; //是否初始化Mac
  20. public string mac; //记录当前
  21. public AimDeviceInfo(int _id) {
  22. this.id = _id;
  23. }
  24. public void setInitMac(string macTemp) {
  25. bInitMac = true;
  26. mac = macTemp;
  27. }
  28. public void resetInitMac() {
  29. bInitMac = false;
  30. mac = "";
  31. }
  32. }
  33. [Serializable]
  34. public class AimDeviceInfos
  35. {
  36. public List<AimDeviceInfo> arry;
  37. }
  38. /* 瞄准处理器 */
  39. public class AimHandler : MonoBehaviour
  40. {
  41. //记录一个设备
  42. public AimDeviceInfo aimDeviceInfo = null;
  43. public AimDeviceInfo tempAimDeviceInfo = null;//临时处理值。最后赋值给aimDeviceInfo
  44. public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos();
  45. int deviceSelectIndex = 0;//默认选中第一个设备(1p)
  46. public Action aimDeviceInfoChangeEvent;
  47. public int DeviceType
  48. {
  49. get
  50. {
  51. //if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9;
  52. //if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663;
  53. return 0;
  54. }
  55. }
  56. //陀螺仪校准进度记录
  57. [NonSerialized] public int gyrCalibrateCompleteCount = 0;
  58. [NonSerialized] public int gyrCalibrateTotalCount = 2000;
  59. public static AimHandler ins;
  60. public bool bInitOne = false;
  61. public OnCrossBtnEventEvent OnCrossBtnEvent;
  62. public delegate void OnCrossBtnEventEvent();
  63. /// <summary>
  64. /// 准心事件
  65. /// </summary>
  66. public void InvokeOnCrossBtnEvent()
  67. {
  68. try
  69. {
  70. OnCrossBtnEvent?.Invoke();
  71. }
  72. catch (Exception e)
  73. {
  74. Debug.LogError(e);
  75. }
  76. }
  77. void Start()
  78. {
  79. ins = this;
  80. }
  81. public void onClearAimDeviceInfosNew()
  82. {
  83. PlayerPrefs.DeleteKey("aim-device-info-" + LoginMgr.myUserInfo.id);
  84. aimDeviceInfos.arry.Clear();
  85. }
  86. /// <summary>
  87. /// 移除2p。即除了1p之后的设备
  88. /// </summary>
  89. public void onClear2PAimDeviceInfosNew()
  90. {
  91. OnGetAimDeviceInfos();
  92. if (aimDeviceInfos.arry.Count > 1)
  93. {
  94. aimDeviceInfos.arry.RemoveRange(1, aimDeviceInfos.arry.Count - 1);
  95. }
  96. OnSaveAimDeviceInfos();
  97. }
  98. public void OnGetAimDeviceInfos() {
  99. string deviceInfo = PlayerPrefs.GetString("aim-device-info-" + LoginMgr.myUserInfo.id, "");
  100. Debug.Log("get deviceSelectIndex:" + deviceInfo);
  101. if (deviceInfo != "")
  102. {
  103. aimDeviceInfos = JsonUtility.FromJson<AimDeviceInfos>(deviceInfo);//这里的类是依据最外层{}决定的
  104. }
  105. aimDeviceInfoChangeEvent?.Invoke();
  106. }
  107. public void OnSaveAimDeviceInfos()
  108. {
  109. aimDeviceInfoChangeEvent?.Invoke();
  110. PlayerPrefs.SetString("aim-device-info-" + LoginMgr.myUserInfo.id, JsonUtility.ToJson(aimDeviceInfos));
  111. }
  112. //创建一个选择值
  113. public void onCreateTempAimDeviceInfo() {
  114. tempAimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
  115. Debug.Log("onCreateTempAimDeviceInfo deviceSelectIndex:" + deviceSelectIndex);
  116. }
  117. //是否存在AimDeviceInfo,存在更新,不存在创建;
  118. public void onCreateAimDeviceInfoById()
  119. {
  120. OnGetAimDeviceInfos();
  121. //deviceIndex 区分 1p 还是 2p
  122. bool bCanAdd = true;
  123. foreach (AimDeviceInfo p in aimDeviceInfos.arry)
  124. {
  125. if (deviceSelectIndex == p.id)
  126. {
  127. aimDeviceInfo = p;
  128. bCanAdd = false;
  129. }
  130. //Debug.Log("33deviceSelectIndex:" + deviceSelectIndex + ",bCanAdd:" + bCanAdd+ " , id:" + p.id +" , type:"+ p.type);
  131. }
  132. if (bCanAdd)
  133. {
  134. Debug.Log("add deviceSelectIndex:" + deviceSelectIndex);
  135. aimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
  136. aimDeviceInfos.arry.Add(aimDeviceInfo);
  137. }
  138. OnSaveAimDeviceInfos();
  139. }
  140. public void SetAimDeviceSelectIndex(int selectIndex) {
  141. if (selectIndex < 0) {
  142. Debug.LogWarning("selectIndex不能小于0:"+ selectIndex);
  143. return;
  144. }
  145. deviceSelectIndex = selectIndex;
  146. Debug.Log("SetAimDeviceSelectIndex :" + selectIndex);
  147. }
  148. public void SetTempAimDeviceType(AimDeviceType _aimDeviceType)
  149. {
  150. if (tempAimDeviceInfo == null) return;
  151. tempAimDeviceInfo.type = (int)_aimDeviceType;
  152. }
  153. public void SetAimDeviceType(AimDeviceType _aimDeviceType)
  154. {
  155. if (aimDeviceInfo == null) return;
  156. aimDeviceInfo.type = (int)_aimDeviceType;
  157. _AimDeviceInfosUpdate();
  158. OnSaveAimDeviceInfos();
  159. }
  160. public void SetAimDeviceType(int _aimDeviceType)
  161. {
  162. if (aimDeviceInfo == null) return;
  163. aimDeviceInfo.type = _aimDeviceType;
  164. _AimDeviceInfosUpdate();
  165. OnSaveAimDeviceInfos();
  166. }
  167. //APP连接需进行MAC地址的比对。
  168. //只有再引导初始化页面才保存连接使用的mac地址。
  169. public void SetAimDeviceMac(string _mac) {
  170. if (aimDeviceInfo == null) return;
  171. aimDeviceInfo.setInitMac(_mac);
  172. _AimDeviceInfosUpdate();
  173. OnSaveAimDeviceInfos();
  174. }
  175. //重置mac存储信息
  176. public void ResetAimDeviceMac() {
  177. if (aimDeviceInfo == null) return;
  178. aimDeviceInfo.resetInitMac();
  179. _AimDeviceInfosUpdate();
  180. OnSaveAimDeviceInfos();
  181. }
  182. void _AimDeviceInfosUpdate() {
  183. for (int i = 0; i < aimDeviceInfos.arry.Count; i++)
  184. {
  185. if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id)
  186. {
  187. aimDeviceInfos.arry[i] = aimDeviceInfo;
  188. }
  189. }
  190. }
  191. #region 根据设备1p或者2p 获取HOUYIPRO的状态
  192. public bool isHOUYIPRO(BluetoothPlayer bluetoothPlayer)
  193. {
  194. bool _isHOUYIPRO = false;
  195. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  196. {
  197. if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.HOUYIPRO)
  198. {
  199. _isHOUYIPRO = true;
  200. }
  201. }
  202. return _isHOUYIPRO;
  203. }
  204. #endregion
  205. #region 根据设备1p或者2p 获取ARTEMISPro的状态
  206. public bool isARTEMISPro(BluetoothPlayer bluetoothPlayer)
  207. {
  208. bool _isARTEMISPro = false;
  209. foreach (AimDeviceInfo p in ins.aimDeviceInfos.arry)
  210. {
  211. if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.ARTEMISPRO)
  212. {
  213. _isARTEMISPro = true;
  214. }
  215. }
  216. return _isARTEMISPro;
  217. }
  218. #endregion
  219. [NonSerialized] public bool lerpForRotation = true;
  220. [NonSerialized] public float lerpTimeRate = 7;
  221. //加一个枪射击的触发时间
  222. float _lastShootTime = 0;
  223. public void Update()
  224. {
  225. }
  226. public void OnDataReceived(byte[] bytes)
  227. {
  228. if (bytes.Length != 27 && bytes.Length != 39)
  229. {
  230. if (bytes.Length == 2)
  231. {
  232. if (bytes[0] == 0x66 && bytes[1] == 0x31)
  233. {
  234. if (bInitOne)
  235. {
  236. bInitOne = false;
  237. if (aimDeviceInfo.type == (int)AimDeviceType.NONE)
  238. {
  239. AutoResetView.DoIdentity();
  240. }
  241. else
  242. {
  243. //准心开关
  244. CrossBtnEvent();
  245. }
  246. }
  247. else
  248. {
  249. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  250. {
  251. //流程触发红外描点
  252. InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
  253. if (infraredScreenPositioningView) {
  254. InvokeOnCrossBtnEvent();
  255. }
  256. }
  257. else
  258. {
  259. if (aimDeviceInfo.type == (int)AimDeviceType.NONE)
  260. {
  261. AutoResetView.DoIdentity();
  262. }
  263. else
  264. {
  265. //准心开关
  266. CrossBtnEvent();
  267. }
  268. }
  269. }
  270. }
  271. else if (bytes[0] == 0x66 && bytes[1] == 0x32)
  272. {
  273. //红外部分
  274. if (GlobalData.MyDeviceMode == DeviceMode.Gun || BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice())
  275. {
  276. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  277. //‘校准'功能的调用,使用原来调出光标的功能键;即在HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键
  278. if (infraredGuider == null)
  279. {
  280. AutoResetView.DoIdentity();
  281. }
  282. else {
  283. //先判断是否处于校准步骤
  284. infraredGuider.onLongClickDevice();
  285. }
  286. }
  287. else if (SB_EventSystem.ins)
  288. {
  289. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  290. //唤起/隐藏虚拟鼠标
  291. SB_EventSystem.ins.AwakenSimulateMouse();
  292. }
  293. }
  294. else if (bytes[1] == 10)
  295. {
  296. //显示电量
  297. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  298. //DeviceView.ins.RenderBattery(1, bytes[0]);
  299. }
  300. }
  301. else if (bytes[0] == 0x5b)
  302. {
  303. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  304. {
  305. if (Time.realtimeSinceStartup - _lastShootTime >= 1)
  306. {
  307. _lastShootTime = Time.realtimeSinceStartup;
  308. InfraredScreenPositioningView view = FindAnyObjectByType<InfraredScreenPositioningView>();
  309. if (view)
  310. {
  311. InvokeOnCrossBtnEvent();
  312. }
  313. }
  314. }
  315. // 无论如何都执行红外射击检测
  316. ShootCheck.ins.ShootByInfrared(bytes);
  317. }
  318. else if (bytes[0] == 0x5C)
  319. {
  320. //00 弹夹分离,01 上弹夹
  321. ShootCheck.ins.UpdateTheMagazine(bytes);
  322. }
  323. else if (bytes[0] == 0x5E)
  324. {
  325. Debug.Log("接收到系统数据:" + BitConverter.ToString(bytes));
  326. var boxUserSettings = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
  327. //设备类型
  328. switch (bytes[1])
  329. {
  330. case 0x01:
  331. Debug.Log("设备类型:HOUYI Pro");
  332. UserSettings.ins.selectDevicesName = "HOUYI Pro";
  333. UserSettings.ins.Save();
  334. boxUserSettings?.OnUpdateClickInfoByName();
  335. break;
  336. case 0x02:
  337. Debug.Log("设备类型:ARTEMIS Pro");
  338. UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
  339. UserSettings.ins.Save();
  340. boxUserSettings?.OnUpdateClickInfoByName();
  341. break;
  342. case 0x03:
  343. Debug.Log("设备类型:Pistol 1");
  344. UserSettings.ins.selectDevicesName = "Pistol M9";
  345. UserSettings.ins.Save();
  346. boxUserSettings?.OnUpdateClickInfoByName();
  347. break;
  348. }
  349. // 系统类型
  350. switch (bytes[2])
  351. {
  352. case 0x01:
  353. Debug.Log("系统类型:移动手机");
  354. break;
  355. case 0x02:
  356. Debug.Log("系统类型:PC电脑");
  357. break;
  358. case 0x03:
  359. Debug.Log("系统类型:VR设备");
  360. break;
  361. }
  362. }
  363. return;
  364. }
  365. }
  366. /// <summary>
  367. /// 操作准心开关事件
  368. /// </summary>
  369. private void CrossBtnEvent()
  370. {
  371. Debug.Log("CrossBtnEvent");
  372. //准心开关
  373. InvokeOnCrossBtnEvent();
  374. //b端不处理准心
  375. if (CommonConfig.StandaloneModeOrPlatformB) {
  376. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  377. if (infraredGuider)
  378. {
  379. infraredGuider.onClickDevice();
  380. }
  381. return;
  382. }
  383. if (GameAssistUI.ins)
  384. {
  385. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  386. if (infraredGuider == null)
  387. {
  388. //显示控制准心按钮
  389. Transform _button5 = GameAssistUI.ins.transform.Find("Button5");
  390. if (_button5.gameObject.activeSelf)
  391. {
  392. Button crossHairBtn = _button5.GetComponent<Button>();
  393. crossHairBtn.onClick.Invoke();
  394. }
  395. else
  396. {
  397. bool onlyShow = !CrossHair.ins.GetOnlyShow();
  398. CrossHair.ins.SetOnlyShow(onlyShow);
  399. //保存准心状态
  400. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  401. }
  402. }
  403. else {
  404. infraredGuider.onClickDevice();
  405. }
  406. //if (GameMgr.bShowDistance)
  407. //{
  408. // //显示控制准心按钮
  409. // Button crossHairBtn = GameAssistUI.ins.transform.Find("Button5").GetComponent<Button>();
  410. // crossHairBtn.onClick.Invoke();
  411. //}
  412. //else {
  413. // //校准
  414. // InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  415. //}
  416. }
  417. else if (DuckHunter.GameUI.Instance)
  418. {
  419. //显示控制准心按钮
  420. Transform _btnCrosshair = DuckHunter.GameUI.Instance.transform.Find("BtnCrosshair");
  421. if (_btnCrosshair.gameObject.activeSelf)
  422. {
  423. Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
  424. crossHairBtn.onClick.Invoke();
  425. }
  426. else {
  427. bool onlyShow = !DuckHunter.CrossHair.Instance.GetOnlyShow();
  428. DuckHunter.CrossHair.Instance.SetOnlyShow(onlyShow);
  429. //记录准心值
  430. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  431. }
  432. }
  433. else if (HyperspaceGame.UIManager._ins) {
  434. //打枪
  435. Transform _btnCrosshair = HyperspaceGame.UIManager._ins.transform.Find("BtnCrosshair");
  436. if (_btnCrosshair.gameObject.activeSelf)
  437. {
  438. Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
  439. crossHairBtn.onClick.Invoke();
  440. }
  441. else {
  442. bool onlyShow = !HyperspaceGame.UIManager._ins.GetOnlyShow();
  443. HyperspaceGame.UIManager._ins.SetOnlyShow(onlyShow);
  444. //记录准心值
  445. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  446. }
  447. }
  448. else
  449. {
  450. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("FruitMaster")){
  451. //水果
  452. GameObject _crosshairBtn = GameObject.Find("PermanentCanvas/CrossHair_Btn");
  453. if (_crosshairBtn.gameObject.activeSelf)
  454. {
  455. Button crossHairBtn = _crosshairBtn.GetComponent<Button>();
  456. crossHairBtn.onClick.Invoke();
  457. }
  458. else {
  459. bool onlyShow = !JCFruitMaster.ins.GetOnlyShow();
  460. JCFruitMaster.ins.SetOnlyShow(onlyShow);
  461. //记录准心值
  462. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  463. }
  464. }
  465. }
  466. }
  467. }