ReloadShotTrainHandle.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  86. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round((m_TaskTimer.delay - m_TaskTimer.remainingTime) / totalKillNum, 2));
  87. SetResultMetricsDataValue(MetricsType.ReloadTime, Math.Round(totalReloadTime / reloadNums, 2));
  88. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNum);
  89. }
  90. #endregion
  91. #region TrainResult
  92. private void UpdatePrecision()
  93. {
  94. double totalShot = TotalHitNum + MissHitNum;
  95. double precision = 0;
  96. if (totalShot != 0)
  97. {
  98. precision = Math.Round(TotalHitNum / totalShot, 2);
  99. }
  100. SetResultMetricsDataValue(MetricsType.Precision, precision);
  101. }
  102. #endregion
  103. #region Target
  104. public override void SeparationMagazine()
  105. {
  106. base.SeparationMagazine();
  107. separationMagazineTime = m_TaskTimer.remainingTime;
  108. }
  109. public override void ReloadMagazine()
  110. {
  111. base.ReloadMagazine();
  112. totalReloadTime += (separationMagazineTime - m_TaskTimer.remainingTime);
  113. reloadNums++;
  114. }
  115. private void OnShottedTargetEvent(BaseShotTarget target)
  116. {
  117. if (target != null)
  118. {
  119. int baseS = m_ScoreData.baseScore;
  120. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  121. IncreaseScore(baseS + comboS);
  122. comboNums++;
  123. totalKillNum++;
  124. TotalHitNum++;
  125. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  126. m_ObserveTimer = null;
  127. SphereShotTarget t = target as SphereShotTarget;
  128. if (t.CurHealth <= 0)
  129. {
  130. DestroyShootTarget(t);
  131. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  132. {
  133. CurrentNumber--;
  134. if (CurrentNumber <= 0)
  135. {
  136. return;
  137. }
  138. }
  139. GenerateShotTarget();
  140. }
  141. }
  142. else
  143. {
  144. MissHitNum++;
  145. comboNums = 0;
  146. int baseS = m_ScoreData.baseScore;
  147. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul) + Random.Range(-3, 3);
  148. IncreaseScore(missPunitiveS * -1);
  149. }
  150. }
  151. #endregion
  152. private void GenerateShotTarget()
  153. {
  154. if (!IsRunning) return;
  155. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  156. curTarget = obj.GetComponent<SphereShotTarget>();
  157. curTarget.onClickCanChangeColor = false;
  158. curTarget.onHoveringCanChangeColor = false;
  159. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  160. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  161. curTarget.IsRunning = true;
  162. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, m_DifficultyData.duration);
  163. totalTargetNum++;
  164. StartBestHitTimer();
  165. }
  166. private void ObserveTimerTimeOut()
  167. {
  168. comboNums = 0;
  169. int baseS = m_ScoreData.baseScore;
  170. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.timeOutPunitiveMul) + Random.Range(-3, 3);
  171. IncreaseScore(missPunitiveS * -1);
  172. m_ObserveTimer = null;
  173. ResetTarget();
  174. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  175. {
  176. CurrentNumber--;
  177. if (CurrentNumber <= 0)
  178. {
  179. return;
  180. }
  181. }
  182. GenerateShotTarget();
  183. }
  184. private Vector3 RandomPosition()
  185. {
  186. Vector2 widthLimt = Vector2.zero;
  187. Vector2 heightLimt = Vector2.zero;
  188. switch (m_DifficultyType)
  189. {
  190. case DifficultyType.Standard:
  191. widthLimt = new Vector2(-4, 4);
  192. heightLimt = new Vector2(-1.5f, 1.5f);
  193. break;
  194. case DifficultyType.Advance:
  195. widthLimt = new Vector2(-8, 8);
  196. heightLimt = new Vector2(-1.5f, 3.5f);
  197. break;
  198. case DifficultyType.Professional:
  199. widthLimt = new Vector2(-8, 8);
  200. heightLimt = new Vector2(-1.5f, 5f);
  201. break;
  202. }
  203. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  204. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  205. return ExtraTool.RandomPosition(min, max);
  206. }
  207. private void DestroyShootTarget(SphereShotTarget target)
  208. {
  209. if (target != null)
  210. {
  211. GameObject.Destroy(target.gameObject);
  212. }
  213. }
  214. private void ResetTarget()
  215. {
  216. if (curTarget != null)
  217. {
  218. GameObject.Destroy(curTarget.gameObject);
  219. curTarget = null;
  220. }
  221. }
  222. }
  223. }