Main.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using AppUI.Bluetooth;
  2. using Newtonsoft.Json;
  3. using ProjectBase.UI;
  4. using ShotSimulator.Screen;
  5. using ShotSimulator.Train;
  6. using ShotSimulator.UI;
  7. using ShotSimulator.User;
  8. using SmartBowSDK;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.SceneManagement;
  13. namespace ShotSimulator
  14. {
  15. public class Main : MonoSingleton<Main>
  16. {
  17. private void Awake()
  18. {
  19. //Application.targetFrameRate = 60;
  20. SoundManager.GetInstance().InitManager();
  21. ScreenEffectManager.GetInstance().InitManager();
  22. VirtualMouse.GetInstance().InitManager();
  23. UIManager.GetInstance().InitManager();
  24. UserManager.GetInstance().InitManager();
  25. InitExternalCallback();
  26. }
  27. private void Start()
  28. {
  29. SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
  30. UIManager.GetInstance().ShowUIView("CursorUIView", CursorType.UICursor);
  31. UIManager.GetInstance().ShowUIView("TrainTaskInfoUIView");
  32. //UIManager.GetInstance().ShowUIView("MainScreenUIView");
  33. }
  34. private void Update()
  35. {
  36. //if (Input.GetKeyDown(KeyCode.U))
  37. //{
  38. // RankingFilter filter = new RankingFilter()
  39. // {
  40. // trainTaskType = TrainTaskType.MemoryShot ,
  41. // difficultyType = DifficultyType.Advance ,
  42. // modeType = ModeType.Tactical,
  43. // firearmDeviceType = FirearmDeviceType.M17
  44. // };
  45. // UserManager.GetInstance().UploadCustomLeaderboard(filter, 800, (result) =>
  46. // {
  47. // List<RankingData> datas = JsonConvert.DeserializeObject<List<RankingData>>(JsonConvert.SerializeObject(result.data, Formatting.Indented));
  48. // foreach (var item in datas)
  49. // {
  50. // Debug.Log($"Rank: {item.rank}, Name: {item.userName}, Score: {item.score}, Avatar: {item.avatarUrl}, IsSelf: {item.isSelf}");
  51. // }
  52. // });
  53. //}1
  54. //#if UNITY_EDITOR
  55. // if (Input.GetKeyDown(KeyCode.U))
  56. // {
  57. // //模拟拉栓
  58. // List<byte> data = new List<byte>();
  59. // data.Add(0x60);
  60. // data.Add(0x00);
  61. // data.Add(0x02);
  62. // data.Add(0x5D);
  63. // ShootCheck.ins.UpdateChamberState(data.ToArray());
  64. // }
  65. // if (Input.GetKeyUp(KeyCode.U))
  66. // {
  67. // //模拟拉栓
  68. // List<byte> data = new List<byte>();
  69. // data.Add(0x60);
  70. // data.Add(0x01);
  71. // data.Add(0x02);
  72. // data.Add(0x5D);
  73. // ShootCheck.ins.UpdateChamberState(data.ToArray());
  74. // }
  75. //#endif
  76. }
  77. private void OnDestroy()
  78. {
  79. ResetExternalCallback();
  80. }
  81. private void InitExternalCallback()
  82. {
  83. SmartBowDeviceHub.EnsureReady();
  84. SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
  85. if (hub == null) return;
  86. hub.SubscribeShooting(BluetoothPlayer.FIRST_PLAYER, MainShoot);
  87. hub.SubscribeBleDeviceState(TrainTaskLoader.GetInstance().ChangedMagazineStatus);
  88. hub.SubscribeDeviceAndSystemInfoEvent(OnDeviceAndSystemInfoEvent);
  89. hub.NotifyDeviceAndSystemInfo();
  90. hub.OnStateChanged += OnHubStateChanged;
  91. }
  92. void OnHubStateChanged(SmartBowDeviceState state)
  93. {
  94. if (!state.Is1PConnected) return;
  95. SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
  96. if (hub == null) return;
  97. hub.UnsubscribeShooting(BluetoothPlayer.FIRST_PLAYER, MainShoot);
  98. hub.SubscribeShooting(BluetoothPlayer.FIRST_PLAYER, MainShoot);
  99. }
  100. float _lastShootTime = 0;
  101. /// <summary>
  102. /// 添加一个设计间隔,防止多次触发
  103. /// </summary>
  104. /// <param name="speed"></param>
  105. private void MainShoot(float speed) {
  106. if (CurrentFirearmDevice == FirearmDeviceType.M416)
  107. {
  108. //Rifle M416在训练游戏(新)中,要响应连发,APP正常响应蓝牙发过来的射击命令
  109. VirtualMouse.GetInstance().OnShooting(speed);
  110. TrainTaskLoader.GetInstance().Shoot(speed);
  111. }
  112. else {
  113. //Pisto1M17在新游戏中保持和M9一样的功能
  114. //枪情况下间隔时间0.2,弓箭按0.5
  115. float interval = GlobalData.MyDeviceMode == DeviceMode.Gun ? 0.2f : 0.5f;
  116. //加个间隔
  117. if (Time.realtimeSinceStartup - _lastShootTime < interval) return;
  118. _lastShootTime = Time.realtimeSinceStartup;
  119. VirtualMouse.GetInstance().OnShooting(speed);
  120. TrainTaskLoader.GetInstance().Shoot(speed);
  121. }
  122. }
  123. private void LoginCallBack(LoginResult result)
  124. {
  125. }
  126. private void ResetExternalCallback()
  127. {
  128. SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
  129. if (hub == null) return;
  130. hub.OnStateChanged -= OnHubStateChanged;
  131. hub.UnsubscribeShooting(BluetoothPlayer.FIRST_PLAYER, MainShoot);
  132. hub.UnsubscribeBleDeviceState(TrainTaskLoader.GetInstance().ChangedMagazineStatus);
  133. hub.UnsubscribeDeviceAndSystemInfoEvent(OnDeviceAndSystemInfoEvent);
  134. }
  135. public void ResetAim()
  136. {
  137. InfraredDemo._ins.SetAdjustPointsOffset(PlayerType.FirstPlayer);
  138. }
  139. #region 接入训练游戏时候添加
  140. public FirearmDeviceType CurrentFirearmDevice;
  141. public void OnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bleDeviceType)
  142. {
  143. Debug.Log("[Main]OnDeviceAndSystemInfoEvent:" + connectPlatform + ",bleDeviceType:" + bleDeviceType);
  144. //CurrentFirearmDevice = FirearmDeviceType.M416;
  145. switch (bleDeviceType)
  146. {
  147. case BluetoothDeviceType.NONE:
  148. break;
  149. case BluetoothDeviceType.HOUYIPro:
  150. break;
  151. case BluetoothDeviceType.ARTEMISPro:
  152. break;
  153. case BluetoothDeviceType.Pistol1:
  154. case BluetoothDeviceType.PistolM9:
  155. CurrentFirearmDevice = FirearmDeviceType.M9;
  156. break;
  157. case BluetoothDeviceType.APOLLO:
  158. break;
  159. case BluetoothDeviceType.PistolM17:
  160. CurrentFirearmDevice = FirearmDeviceType.M17;
  161. break;
  162. case BluetoothDeviceType.RifleM416:
  163. CurrentFirearmDevice = FirearmDeviceType.M416;
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. #endregion
  170. }
  171. }