Main.cs 6.4 KB

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