ReloadShotTrainHandle.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using ShotSimulator.Screen;
  2. using ShotSimulator.Target;
  3. using ShotSimulator.Tool;
  4. using ShotSimulator.Train.Info;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using Random = UnityEngine.Random;
  10. namespace ShotSimulator.Train
  11. {
  12. public class ReloadShotTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private ReloadShotTrainInfoDifficultyData m_DifficultyData;
  16. private ReloadShotTrainScoreData m_ScoreData;
  17. private TimeTask m_ObserveTimer;
  18. private double m_TotalHitNum;
  19. private double TotalHitNum
  20. {
  21. get { return m_TotalHitNum; }
  22. set
  23. {
  24. m_TotalHitNum = value;
  25. UpdatePrecision();
  26. }
  27. }
  28. private double m_MissHitNum;
  29. private double MissHitNum
  30. {
  31. get { return m_MissHitNum; }
  32. set
  33. {
  34. m_MissHitNum = value;
  35. UpdatePrecision();
  36. }
  37. }
  38. private double totalKillNum;
  39. private double totalTargetNum;
  40. private double totalReloadTime;
  41. private double reloadNums;
  42. public ReloadShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  43. {
  44. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  45. m_DifficultyData = info.GetDifficultyData<ReloadShotTrainInfoDifficultyData>(m_DifficultyType);
  46. m_ScoreData = info.GetScoreData<ReloadShotTrainScoreData>(m_DifficultyType);
  47. }
  48. #region Train
  49. protected override void ResetTrain()
  50. {
  51. ResetTarget();
  52. m_TotalHitNum = 0;
  53. m_MissHitNum = 0;
  54. totalKillNum = 0;
  55. totalTargetNum = 0;
  56. totalReloadTime = 0;
  57. reloadNums = 0;
  58. separationMagazineTime = 0;
  59. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  60. m_ObserveTimer = null;
  61. }
  62. public override void StartTrain()
  63. {
  64. base.StartTrain();
  65. GenerateShotTarget();
  66. }
  67. public override void PauseTrain()
  68. {
  69. base.PauseTrain();
  70. if (m_ObserveTimer != null)
  71. {
  72. m_ObserveTimer.pause = true;
  73. }
  74. }
  75. public override void ContinueTrain()
  76. {
  77. base.ContinueTrain();
  78. if (m_ObserveTimer != null)
  79. {
  80. m_ObserveTimer.pause = false;
  81. }
  82. }
  83. public override void CalculateTrainResult()
  84. {
  85. double timeToKill = 999;
  86. if (totalKillNum != 0)
  87. {
  88. timeToKill = Math.Round((m_TaskTimer.delay - m_TaskTimer.remainingTime) / totalKillNum, 2);
  89. }
  90. double reloadTime = 999;
  91. if (reloadNums != 0)
  92. {
  93. reloadTime = Math.Round(totalReloadTime / reloadNums, 2);
  94. }
  95. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  96. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  97. SetResultMetricsDataValue(MetricsType.ReloadTime, reloadTime);
  98. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNum);
  99. }
  100. #endregion
  101. #region TrainResult
  102. private void UpdatePrecision()
  103. {
  104. double totalShot = TotalHitNum + MissHitNum;
  105. double precision = 0;
  106. if (totalShot != 0)
  107. {
  108. precision = Math.Round(TotalHitNum / totalShot, 2);
  109. }
  110. SetResultMetricsDataValue(MetricsType.Precision, precision);
  111. }
  112. #endregion
  113. #region Target
  114. public override void SeparationMagazine()
  115. {
  116. base.SeparationMagazine();
  117. separationMagazineTime = m_TaskTimer.remainingTime;
  118. }
  119. public override void ReloadMagazine()
  120. {
  121. base.ReloadMagazine();
  122. totalReloadTime += (separationMagazineTime - m_TaskTimer.remainingTime);
  123. reloadNums++;
  124. }
  125. private void OnShottedTargetEvent(BaseShotTarget target)
  126. {
  127. if (target != null)
  128. {
  129. int baseS = m_ScoreData.baseScore;
  130. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  131. IncreaseScore(baseS + comboS);
  132. comboNums++;
  133. totalKillNum++;
  134. TotalHitNum++;
  135. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  136. m_ObserveTimer = null;
  137. SphereShotTarget t = target as SphereShotTarget;
  138. if (t.CurHealth <= 0)
  139. {
  140. DestroyShootTarget(t);
  141. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  142. {
  143. CurrentNumber--;
  144. if (CurrentNumber <= 0)
  145. {
  146. return;
  147. }
  148. }
  149. GenerateShotTarget();
  150. }
  151. }
  152. else
  153. {
  154. MissHitNum++;
  155. comboNums = 0;
  156. int baseS = m_ScoreData.baseScore;
  157. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul) + Random.Range(-3, 3);
  158. IncreaseScore(missPunitiveS * -1);
  159. }
  160. }
  161. #endregion
  162. private void GenerateShotTarget()
  163. {
  164. if (!IsRunning) return;
  165. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  166. curTarget = obj.GetComponent<SphereShotTarget>();
  167. curTarget.onClickCanChangeColor = false;
  168. curTarget.onHoveringCanChangeColor = false;
  169. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  170. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  171. curTarget.IsRunning = true;
  172. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, m_DifficultyData.duration);
  173. totalTargetNum++;
  174. StartBestHitTimer();
  175. }
  176. private void ObserveTimerTimeOut()
  177. {
  178. comboNums = 0;
  179. int baseS = m_ScoreData.baseScore;
  180. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.timeOutPunitiveMul) + Random.Range(-3, 3);
  181. IncreaseScore(missPunitiveS * -1);
  182. m_ObserveTimer = null;
  183. ResetTarget();
  184. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  185. {
  186. CurrentNumber--;
  187. if (CurrentNumber <= 0)
  188. {
  189. return;
  190. }
  191. }
  192. GenerateShotTarget();
  193. }
  194. private Vector3 RandomPosition()
  195. {
  196. Vector2 widthLimt = Vector2.zero;
  197. Vector2 heightLimt = Vector2.zero;
  198. switch (m_DifficultyType)
  199. {
  200. case DifficultyType.Standard:
  201. widthLimt = new Vector2(-4, 4);
  202. heightLimt = new Vector2(-1.5f, 1.5f);
  203. break;
  204. case DifficultyType.Advance:
  205. widthLimt = new Vector2(-8, 8);
  206. heightLimt = new Vector2(-1.5f, 3.5f);
  207. break;
  208. case DifficultyType.Professional:
  209. widthLimt = new Vector2(-8, 8);
  210. heightLimt = new Vector2(-1.5f, 5f);
  211. break;
  212. }
  213. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  214. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  215. return ExtraTool.RandomPosition(min, max);
  216. }
  217. private void DestroyShootTarget(SphereShotTarget target)
  218. {
  219. if (target != null)
  220. {
  221. GameObject.Destroy(target.gameObject);
  222. }
  223. }
  224. private void ResetTarget()
  225. {
  226. if (curTarget != null)
  227. {
  228. GameObject.Destroy(curTarget.gameObject);
  229. curTarget = null;
  230. }
  231. }
  232. }
  233. }