AimHandler.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
  305. _lastShootTime = Time.realtimeSinceStartup;
  306. //流程触发红外描点
  307. InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
  308. if (infraredScreenPositioningView)
  309. {
  310. InvokeOnCrossBtnEvent();
  311. }
  312. }
  313. //红外射击检测
  314. ShootCheck.ins.ShootByInfrared(bytes);
  315. }
  316. else if (bytes[0] == 0x5C)
  317. {
  318. //00 弹夹分离,01 上弹夹
  319. ShootCheck.ins.UpdateTheMagazine(bytes);
  320. }
  321. else if (bytes[0] == 0x5E)
  322. {
  323. Debug.Log("接收到系统数据:" + BitConverter.ToString(bytes));
  324. var boxUserSettings = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
  325. //设备类型
  326. switch (bytes[1])
  327. {
  328. case 0x01:
  329. Debug.Log("设备类型:HOUYI Pro");
  330. UserSettings.ins.selectDevicesName = "HOUYI Pro";
  331. UserSettings.ins.Save();
  332. boxUserSettings?.OnUpdateClickInfoByName();
  333. break;
  334. case 0x02:
  335. Debug.Log("设备类型:ARTEMIS Pro");
  336. UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
  337. UserSettings.ins.Save();
  338. boxUserSettings?.OnUpdateClickInfoByName();
  339. break;
  340. case 0x03:
  341. Debug.Log("设备类型:Pistol 1");
  342. UserSettings.ins.selectDevicesName = "Pistol M9";
  343. UserSettings.ins.Save();
  344. boxUserSettings?.OnUpdateClickInfoByName();
  345. break;
  346. }
  347. // 系统类型
  348. switch (bytes[2])
  349. {
  350. case 0x01:
  351. Debug.Log("系统类型:移动手机");
  352. break;
  353. case 0x02:
  354. Debug.Log("系统类型:PC电脑");
  355. break;
  356. case 0x03:
  357. Debug.Log("系统类型:VR设备");
  358. break;
  359. }
  360. }
  361. return;
  362. }
  363. }
  364. /// <summary>
  365. /// 操作准心开关事件
  366. /// </summary>
  367. private void CrossBtnEvent()
  368. {
  369. Debug.Log("CrossBtnEvent");
  370. //准心开关
  371. InvokeOnCrossBtnEvent();
  372. //b端不处理准心
  373. if (CommonConfig.StandaloneModeOrPlatformB) {
  374. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  375. if (infraredGuider)
  376. {
  377. infraredGuider.onClickDevice();
  378. }
  379. return;
  380. }
  381. if (GameAssistUI.ins)
  382. {
  383. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  384. if (infraredGuider == null)
  385. {
  386. //显示控制准心按钮
  387. Transform _button5 = GameAssistUI.ins.transform.Find("Button5");
  388. if (_button5.gameObject.activeSelf)
  389. {
  390. Button crossHairBtn = _button5.GetComponent<Button>();
  391. crossHairBtn.onClick.Invoke();
  392. }
  393. else
  394. {
  395. bool onlyShow = !CrossHair.ins.GetOnlyShow();
  396. CrossHair.ins.SetOnlyShow(onlyShow);
  397. //保存准心状态
  398. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  399. }
  400. }
  401. else {
  402. infraredGuider.onClickDevice();
  403. }
  404. //if (GameMgr.bShowDistance)
  405. //{
  406. // //显示控制准心按钮
  407. // Button crossHairBtn = GameAssistUI.ins.transform.Find("Button5").GetComponent<Button>();
  408. // crossHairBtn.onClick.Invoke();
  409. //}
  410. //else {
  411. // //校准
  412. // InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  413. //}
  414. }
  415. else if (DuckHunter.GameUI.Instance)
  416. {
  417. //显示控制准心按钮
  418. Transform _btnCrosshair = DuckHunter.GameUI.Instance.transform.Find("BtnCrosshair");
  419. if (_btnCrosshair.gameObject.activeSelf)
  420. {
  421. Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
  422. crossHairBtn.onClick.Invoke();
  423. }
  424. else {
  425. bool onlyShow = !DuckHunter.CrossHair.Instance.GetOnlyShow();
  426. DuckHunter.CrossHair.Instance.SetOnlyShow(onlyShow);
  427. //记录准心值
  428. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  429. }
  430. }
  431. else if (HyperspaceGame.UIManager._ins) {
  432. //打枪
  433. Transform _btnCrosshair = HyperspaceGame.UIManager._ins.transform.Find("BtnCrosshair");
  434. if (_btnCrosshair.gameObject.activeSelf)
  435. {
  436. Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
  437. crossHairBtn.onClick.Invoke();
  438. }
  439. else {
  440. bool onlyShow = !HyperspaceGame.UIManager._ins.GetOnlyShow();
  441. HyperspaceGame.UIManager._ins.SetOnlyShow(onlyShow);
  442. //记录准心值
  443. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  444. }
  445. }
  446. else
  447. {
  448. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("FruitMaster")){
  449. //水果
  450. GameObject _crosshairBtn = GameObject.Find("PermanentCanvas/CrossHair_Btn");
  451. if (_crosshairBtn.gameObject.activeSelf)
  452. {
  453. Button crossHairBtn = _crosshairBtn.GetComponent<Button>();
  454. crossHairBtn.onClick.Invoke();
  455. }
  456. else {
  457. bool onlyShow = !JCFruitMaster.ins.GetOnlyShow();
  458. JCFruitMaster.ins.SetOnlyShow(onlyShow);
  459. //记录准心值
  460. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  461. }
  462. }
  463. }
  464. }
  465. }