AimHandler.cs 17 KB

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