AimHandler.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. 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. //记录一个设备
  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. InvokeOnCrossBtnEvent();
  258. }
  259. }
  260. else
  261. {
  262. if (aimDeviceInfo.type == (int)AimDeviceType.NONE)
  263. {
  264. AutoResetView.DoIdentity();
  265. }
  266. else
  267. {
  268. //准心开关
  269. CrossBtnEvent();
  270. }
  271. }
  272. }
  273. }
  274. else if (bytes[0] == 0x66 && bytes[1] == 0x32)
  275. {
  276. //红外部分
  277. if (GlobalData.MyDeviceMode == DeviceMode.Gun || BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice())
  278. {
  279. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  280. //‘校准'功能的调用,使用原来调出光标的功能键;即在HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键
  281. if (infraredGuider == null)
  282. {
  283. AutoResetView.DoIdentity();
  284. }
  285. else {
  286. //先判断是否处于校准步骤
  287. infraredGuider.onLongClickDevice();
  288. }
  289. }
  290. else if (SB_EventSystem.ins)
  291. {
  292. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  293. //唤起/隐藏虚拟鼠标
  294. SB_EventSystem.ins.AwakenSimulateMouse();
  295. }
  296. }
  297. else if (bytes[1] == 10)
  298. {
  299. //显示电量
  300. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  301. //DeviceView.ins.RenderBattery(1, bytes[0]);
  302. }
  303. }
  304. else if (bytes[0] == 0x5b)
  305. {
  306. if (GlobalData.MyDeviceMode == DeviceMode.Gun) {
  307. if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
  308. _lastShootTime = Time.realtimeSinceStartup;
  309. //流程触发红外描点
  310. InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
  311. if (infraredScreenPositioningView)
  312. {
  313. InvokeOnCrossBtnEvent();
  314. }
  315. }
  316. //红外射击检测
  317. ShootCheck.ins.ShootByInfrared(bytes);
  318. }
  319. else if (bytes[0] == 0x5C)
  320. {
  321. //00 弹夹分离,01 上弹夹
  322. ShootCheck.ins.UpdateTheMagazine(bytes);
  323. }
  324. else if (bytes[0] == 0x5E)
  325. {
  326. Debug.Log("接收到系统数据:" + BitConverter.ToString(bytes));
  327. var boxUserSettings = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
  328. //设备类型
  329. switch (bytes[1])
  330. {
  331. case 0x01:
  332. Debug.Log("设备类型:HOUYI Pro");
  333. UserSettings.ins.selectDevicesName = "HOUYI Pro";
  334. UserSettings.ins.Save();
  335. boxUserSettings?.FlushDeviceSelect();
  336. break;
  337. case 0x02:
  338. Debug.Log("设备类型:ARTEMIS Pro");
  339. UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
  340. UserSettings.ins.Save();
  341. boxUserSettings?.FlushDeviceSelect();
  342. break;
  343. case 0x03:
  344. Debug.Log("设备类型:Pistol M9");
  345. UserSettings.ins.selectDevicesName = "Pistol M9";
  346. UserSettings.ins.Save();
  347. boxUserSettings?.FlushDeviceSelect();
  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. }
  367. /// <summary>
  368. /// 操作准心开关事件
  369. /// </summary>
  370. private void CrossBtnEvent()
  371. {
  372. Debug.Log("CrossBtnEvent");
  373. //准心开关
  374. InvokeOnCrossBtnEvent();
  375. //b端不处理准心
  376. if (CommonConfig.StandaloneModeOrPlatformB) {
  377. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  378. if (infraredGuider)
  379. {
  380. infraredGuider.onClickDevice();
  381. }
  382. return;
  383. }
  384. if (GameAssistUI.ins)
  385. {
  386. InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
  387. if (infraredGuider == null)
  388. {
  389. //显示控制准心按钮
  390. Transform _button5 = GameAssistUI.ins.transform.Find("Button5");
  391. if (_button5.gameObject.activeSelf)
  392. {
  393. Button crossHairBtn = _button5.GetComponent<Button>();
  394. crossHairBtn.onClick.Invoke();
  395. }
  396. else
  397. {
  398. bool onlyShow = !CrossHair.ins.GetOnlyShow();
  399. CrossHair.ins.SetOnlyShow(onlyShow);
  400. //保存准心状态
  401. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  402. }
  403. }
  404. else {
  405. infraredGuider.onClickDevice();
  406. }
  407. //if (GameMgr.bShowDistance)
  408. //{
  409. // //显示控制准心按钮
  410. // Button crossHairBtn = GameAssistUI.ins.transform.Find("Button5").GetComponent<Button>();
  411. // crossHairBtn.onClick.Invoke();
  412. //}
  413. //else {
  414. // //校准
  415. // InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  416. //}
  417. }
  418. else if (DuckHunter.GameUI.Instance)
  419. {
  420. //显示控制准心按钮
  421. Transform _btnCrosshair = DuckHunter.GameUI.Instance.transform.Find("BtnCrosshair");
  422. if (_btnCrosshair.gameObject.activeSelf)
  423. {
  424. Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
  425. crossHairBtn.onClick.Invoke();
  426. }
  427. else {
  428. bool onlyShow = !DuckHunter.CrossHair.Instance.GetOnlyShow();
  429. DuckHunter.CrossHair.Instance.SetOnlyShow(onlyShow);
  430. //记录准心值
  431. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  432. }
  433. }
  434. else if (HyperspaceGame.UIManager._ins) {
  435. //打枪
  436. Transform _btnCrosshair = HyperspaceGame.UIManager._ins.transform.Find("BtnCrosshair");
  437. if (_btnCrosshair.gameObject.activeSelf)
  438. {
  439. Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
  440. crossHairBtn.onClick.Invoke();
  441. }
  442. else {
  443. bool onlyShow = !HyperspaceGame.UIManager._ins.GetOnlyShow();
  444. HyperspaceGame.UIManager._ins.SetOnlyShow(onlyShow);
  445. //记录准心值
  446. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  447. }
  448. }
  449. else
  450. {
  451. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("FruitMaster")){
  452. //水果
  453. GameObject _crosshairBtn = GameObject.Find("PermanentCanvas/CrossHair_Btn");
  454. if (_crosshairBtn.gameObject.activeSelf)
  455. {
  456. Button crossHairBtn = _crosshairBtn.GetComponent<Button>();
  457. crossHairBtn.onClick.Invoke();
  458. }
  459. else {
  460. bool onlyShow = !JCFruitMaster.ins.GetOnlyShow();
  461. JCFruitMaster.ins.SetOnlyShow(onlyShow);
  462. //记录准心值
  463. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  464. }
  465. }
  466. }
  467. }
  468. /// <summary>
  469. /// 退出进入游戏事件
  470. /// </summary>
  471. public void ExitIntoEvent()
  472. {
  473. string sceneName = SceneManager.GetActiveScene().name;
  474. if (sceneName == "Home")
  475. {
  476. //进入游戏
  477. //EnterTheGame();
  478. return;
  479. }
  480. switch (sceneName)
  481. {
  482. //退出游戏
  483. case "Game":
  484. GameAssistUI.ins.onBtnBack();
  485. break;
  486. case "GameChallenge":
  487. GameAssistUI.ins.onBtnBack();
  488. break;
  489. case "GameDouble":
  490. GameAssistUI.ins.onBtnBack();
  491. break;
  492. case "Hyperspace03":
  493. HyperspaceGame.UIManager._ins.OnShutDown();
  494. break;
  495. case "Hyperspace02":
  496. HyperspaceGame.UIManager._ins.OnShutDown();
  497. break;
  498. case "Hyperspace01":
  499. HyperspaceGame.UIManager._ins.OnShutDown();
  500. break;
  501. case "FruitMaster":
  502. GamingManager.gaming.onShowResultView();
  503. break;
  504. case "WildAttack":
  505. UIManager.GetInstance().onShowResultView();
  506. break;
  507. case "DuckHunter":
  508. GameUI.Instance.onShowResultView();
  509. break;
  510. }
  511. }
  512. public int gameNum = 0;
  513. void EnterTheGame()
  514. {
  515. if (gameNum == 0)
  516. {
  517. HomeView_ChallengeOption.homeView_.OnSelectionChanged(HomeView_ChallengeOption.homeView_.gameIndex, true);
  518. gameNum += 1;
  519. }
  520. if(gameNum == 1)
  521. {
  522. Debug.Log("进入单人游戏模式");
  523. ModeSelectView.ins.OnChangeButton(0);
  524. gameNum = 0;
  525. }
  526. }
  527. }