TrainTaskLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using ProjectBase.UI;
  2. using ShotSimulator.Target;
  3. using ShotSimulator.Tool;
  4. using ShotSimulator.Train.Info;
  5. using ShotSimulator.UI;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.Android;
  11. using UnityEngine.Events;
  12. using UnityEngine.EventSystems;
  13. using UnityEngine.SceneManagement;
  14. using UnityEngine.UI;
  15. namespace ShotSimulator.Train
  16. {
  17. public class TrainTaskLoader : MonoSingleton<TrainTaskLoader>
  18. {
  19. public MetricsTypeConfig m_MetricsTypeConfig;
  20. private BaseTrainInfo[] m_TrainInfos;
  21. private BaseTrainHandle curTrainHandle = null;
  22. private BaseFirearmData[] m_FirearmDatas;
  23. [SerializeField]
  24. private BaseFirearmData curFirearmData = null;
  25. private BaseShotTarget curShootingTarget = null;
  26. public BaseTrainInfo[] TrainInfos
  27. {
  28. get { return m_TrainInfos; }
  29. private set { m_TrainInfos = value; }
  30. }
  31. public BaseFirearmData[] FirearmDatas
  32. {
  33. get { return m_FirearmDatas; }
  34. private set { m_FirearmDatas = value; }
  35. }
  36. public BaseTrainHandle CurTrainHandle
  37. {
  38. get { return curTrainHandle; }
  39. }
  40. public BaseFirearmData CurFirearmData
  41. {
  42. get { return curFirearmData; }
  43. }
  44. private string current_EnvScene = string.Empty;
  45. private void Awake()
  46. {
  47. if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
  48. {
  49. Permission.RequestUserPermission(Permission.ExternalStorageRead);
  50. }
  51. TrainInfos = Resources.LoadAll<BaseTrainInfo>("ScriptableObjects/TrainInfo");
  52. FirearmDatas = Resources.LoadAll<BaseFirearmData>("ScriptableObjects/Firearm");
  53. m_MetricsTypeConfig = Resources.Load<MetricsTypeConfig>("ScriptableObjects/Config/Ö¸ąęŔŕĐÍąí");
  54. curFirearmData = GetFirearmData(Main.GetInstance().CurrentFirearmDevice);
  55. Debug.Log("[TrainTaskLoader] CurrentFirearmDeviceŁş" + Main.GetInstance().CurrentFirearmDevice.ToString());
  56. Debug.Log("[TrainTaskLoader] curFirearmDataŁş" + curFirearmData.gunName);
  57. //VirtualMouse.GetInstance().SetCurCursorType(CursorType.DefaultCursor);
  58. }
  59. private BaseFirearmData GetFirearmData(FirearmDeviceType type)
  60. {
  61. for(int i = 0; i < FirearmDatas.Length; i++)
  62. {
  63. if (FirearmDatas[i].firearmDeviceType == type)
  64. {
  65. return FirearmDatas[i];
  66. }
  67. }
  68. return null;
  69. }
  70. public void Shoot(float speed)
  71. {
  72. if (VirtualMouse.GetInstance().IsSelectUIObject()) return;
  73. if (curTrainHandle == null) return;
  74. if (!curTrainHandle.IsRunning || curTrainHandle.IsPause) return;
  75. //#if UNITY_EDITOR || UNITY_STANDALONE
  76. // curTrainHandle.OnShottedTarget(curShootingTarget, Input.mousePosition);
  77. //#elif UNITY_ANDROID
  78. curTrainHandle.OnShottedTarget(curShootingTarget, VirtualMouse.GetInstance().screenPos);
  79. //#endif
  80. }
  81. public void ChangedMagazineStatus(SmartBowSDK.BluetoothDeviceType bleDeviceType, SmartBowSDK.BluetoothDeviceStatus gunStatusEnum)
  82. {
  83. if (curTrainHandle == null) return;
  84. if (!curTrainHandle.IsRunning || curTrainHandle.IsPause) return;
  85. switch (gunStatusEnum)
  86. {
  87. case SmartBowSDK.BluetoothDeviceStatus.MagazineSeparation:
  88. curTrainHandle.SeparationMagazine();
  89. break;
  90. case SmartBowSDK.BluetoothDeviceStatus.MagazineLoading:
  91. curTrainHandle.ReloadMagazine();
  92. break;
  93. case SmartBowSDK.BluetoothDeviceStatus.Chambered:
  94. curTrainHandle.IsChambered = true;
  95. break;
  96. }
  97. }
  98. private void Update()
  99. {
  100. if (curTrainHandle == null) return;
  101. if (!curTrainHandle.IsRunning || curTrainHandle.IsPause) return;
  102. //#if UNITY_EDITOR || UNITY_STANDALONE
  103. // Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  104. //#else
  105. Ray ray = Camera.main.ScreenPointToRay(VirtualMouse.GetInstance().screenPos);
  106. //#endif
  107. if (Physics.Raycast(ray, out var hit, 100f, 1 << LayerMask.NameToLayer("ShotTarget")))
  108. {
  109. BaseShotTarget newTarget = hit.transform.GetComponentInParent<BaseShotTarget>();
  110. if (curShootingTarget == null)
  111. {
  112. curShootingTarget = newTarget;
  113. curTrainHandle.OnEnteredTarget(curShootingTarget);
  114. }
  115. else
  116. {
  117. if (curShootingTarget == newTarget)
  118. {
  119. curTrainHandle.OnHoveringTarget(curShootingTarget);
  120. }
  121. else
  122. {
  123. curTrainHandle.OnExitedTarget(curShootingTarget);
  124. curShootingTarget = newTarget;
  125. curTrainHandle.OnEnteredTarget(curShootingTarget);
  126. }
  127. }
  128. }
  129. else
  130. {
  131. if (curShootingTarget != null)
  132. {
  133. curTrainHandle.OnExitedTarget(curShootingTarget);
  134. curShootingTarget = null;
  135. }
  136. curTrainHandle.OnNotEnteringTarget(null);
  137. }
  138. #if UNITY_EDITOR || UNITY_STANDALONE
  139. if (Input.GetMouseButtonDown(0))
  140. {
  141. Shoot(0);
  142. }
  143. if (Input.GetKeyDown(KeyCode.R))
  144. {
  145. ChangedMagazineStatus(SmartBowSDK.BluetoothDeviceType.Pistol1, SmartBowSDK.BluetoothDeviceStatus.MagazineLoading);
  146. }
  147. else if (Input.GetKeyDown(KeyCode.S))
  148. {
  149. if (curTrainHandle == null) return;
  150. if (!curTrainHandle.IsRunning || curTrainHandle.IsPause) return;
  151. curTrainHandle.SeparationMagazine();
  152. }
  153. if (Input.GetKeyDown(KeyCode.C))
  154. {
  155. ChangedMagazineStatus(SmartBowSDK.BluetoothDeviceType.Pistol1, SmartBowSDK.BluetoothDeviceStatus.Chambered);
  156. }
  157. #endif
  158. }
  159. public void PrepareTrain(BaseTrainInfo info, DifficultyType type)
  160. {
  161. BaseTrainCallBack callback = NewTrainCallBack();
  162. curTrainHandle = ConstructorTrainHandle(info.trainHandleClassName, info, callback, type);
  163. LoadEnvironmentScene(info.environmentScene, () => { curTrainHandle.PrepareTrain(); });
  164. }
  165. private void LoadEnvironmentScene(string sceneName, UnityAction action)
  166. {
  167. if (current_EnvScene != string.Empty && current_EnvScene != sceneName)
  168. {
  169. ScenesManager.GetInstance().UnloadSceneAsyn(current_EnvScene, null);
  170. }
  171. if (current_EnvScene != sceneName)
  172. {
  173. current_EnvScene = sceneName;
  174. ScenesManager.GetInstance().LoadSceneAsyn(sceneName, action, LoadSceneMode.Additive);
  175. }
  176. else
  177. {
  178. if (action != null)
  179. {
  180. action();
  181. }
  182. }
  183. }
  184. private BaseTrainHandle ConstructorTrainHandle(string typeName, BaseTrainInfo info, BaseTrainCallBack callback, DifficultyType type)
  185. {
  186. object instance = Activator.CreateInstance(Type.GetType(typeName), callback, info, type);
  187. BaseTrainHandle handle = instance as BaseTrainHandle;
  188. handle.InitGunData(curFirearmData);
  189. return instance as BaseTrainHandle;
  190. }
  191. public void StartTrainTask()
  192. {
  193. curTrainHandle.StartTrain();
  194. }
  195. private BaseTrainCallBack NewTrainCallBack()
  196. {
  197. BaseTrainCallBack callBack = new BaseTrainCallBack();
  198. callBack.AddTrainCallBack(TrainEventType.OnPrepare, OnPrepareTrainCallBack);
  199. callBack.AddTrainCallBack(TrainEventType.OnStartTrain, OnStartTrainCallBack);
  200. callBack.AddTrainCallBack(TrainEventType.OnPauseTrain, OnContinueOrPauseTrainCallBack);
  201. callBack.AddTrainCallBack(TrainEventType.OnContinueTrain, OnContinueOrPauseTrainCallBack);
  202. callBack.AddTrainCallBack(TrainEventType.OnExitTrain, OnExitTrainCallBack);
  203. callBack.AddTrainCallBack(TrainEventType.OnCompleteTrain, OnCompleteTrainCallBack);
  204. return callBack;
  205. }
  206. public void SetContinueOrPauseTrain(bool pause)
  207. {
  208. if (curTrainHandle != null)
  209. {
  210. if (pause)
  211. {
  212. curTrainHandle.PauseTrain();
  213. }
  214. else
  215. {
  216. curTrainHandle.ContinueTrain();
  217. }
  218. }
  219. }
  220. public void FinishTrain()
  221. {
  222. if (curTrainHandle != null)
  223. {
  224. curTrainHandle.ExitTrain();
  225. curTrainHandle = null;
  226. }
  227. }
  228. public void ReStartTrain()
  229. {
  230. if (curTrainHandle != null)
  231. {
  232. curTrainHandle.PrepareTrain();
  233. }
  234. }
  235. public void OnPrepareTrainCallBack(BaseTrainHandle handle)
  236. {
  237. UIManager.GetInstance().HideAllUIView();
  238. UIManager.GetInstance().ShowUIView("ExecuteTrainUIView", handle);
  239. UIManager.GetInstance().ShowUIView("CursorUIView", CursorType.UICursor);
  240. }
  241. public void OnStartTrainCallBack(BaseTrainHandle handle)
  242. {
  243. CursorUIView view = UIManager.GetInstance().GetOnShowUIView("CursorUIView") as CursorUIView;
  244. view.SetArguments(handle.TrainInfo.cursorType);
  245. }
  246. public void OnCompleteTrainCallBack(BaseTrainHandle handle)
  247. {
  248. UIManager.GetInstance().HideAllUIView();
  249. UIManager.GetInstance().ShowUIView("TrainSettlementUIView", handle);
  250. UIManager.GetInstance().ShowUIView("CursorUIView", CursorType.UICursor);
  251. }
  252. public void OnContinueOrPauseTrainCallBack(BaseTrainHandle handle)
  253. {
  254. CursorUIView view = UIManager.GetInstance().GetOnShowUIView("CursorUIView") as CursorUIView;
  255. if (handle.IsPause)
  256. {
  257. view.SetArguments(CursorType.UICursor);
  258. }
  259. else
  260. {
  261. view.SetArguments(handle.TrainInfo.cursorType);
  262. }
  263. }
  264. public void OnExitTrainCallBack(BaseTrainHandle handle)
  265. {
  266. UIManager.GetInstance().HideAllUIView();
  267. UIManager.GetInstance().ShowUIView("TrainTaskInfoUIView");
  268. UIManager.GetInstance().ShowUIView("CursorUIView", CursorType.UICursor);
  269. }
  270. }
  271. }