GridShotTrainHandle.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 GridShotTrainHandle : BaseTrainHandle
  13. {
  14. private int maxTargetNum = 3;
  15. private GridShotTrainDifficultyData m_DifficultyData;
  16. private GridShotTrainScoreData m_ScoreData;
  17. private List<TimeTask> generateTargetTimeTasks = new List<TimeTask>();
  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 totalTargetNum;
  39. private List<SphereShotTarget> curTargets = new List<SphereShotTarget>();
  40. public GridShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  41. {
  42. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  43. m_DifficultyData = info.GetDifficultyData<GridShotTrainDifficultyData>(m_DifficultyType);
  44. m_ScoreData = info.GetScoreData<GridShotTrainScoreData>(m_DifficultyType);
  45. }
  46. #region Train
  47. protected override void ResetTrain()
  48. {
  49. ResetTarget();
  50. for(int i = 0; i < generateTargetTimeTasks.Count; i++)
  51. {
  52. TimerSystem.GetInstance().RemoveTimeTask(generateTargetTimeTasks[i]);
  53. }
  54. generateTargetTimeTasks.Clear();
  55. m_TotalHitNum = 0;
  56. totalTargetNum = 0;
  57. m_MissHitNum = 0;
  58. }
  59. public override void StartTrain()
  60. {
  61. base.StartTrain();
  62. ResetTrain();
  63. InitShotTargets();
  64. StartBestHitTimer();
  65. }
  66. public override void PauseTrain()
  67. {
  68. base.PauseTrain();
  69. foreach(var timer in generateTargetTimeTasks)
  70. {
  71. timer.pause = true;
  72. }
  73. }
  74. public override void ContinueTrain()
  75. {
  76. base.ContinueTrain();
  77. foreach (var timer in generateTargetTimeTasks)
  78. {
  79. timer.pause = false;
  80. }
  81. }
  82. public override void CalculateTrainResult()
  83. {
  84. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / TotalHitNum, 2));
  85. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  86. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  87. SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum);
  88. }
  89. #endregion
  90. #region TrainResult
  91. private void UpdatePrecision()
  92. {
  93. double totalShot = TotalHitNum + MissHitNum;
  94. double precision = 0;
  95. if (totalShot != 0)
  96. {
  97. precision = Math.Round(TotalHitNum / totalShot, 2);
  98. }
  99. SetResultMetricsDataValue(MetricsType.Precision, precision);
  100. }
  101. #endregion
  102. #region ShootTarget
  103. private void OnShottedTargetEvent(BaseShotTarget target)
  104. {
  105. if (target != null)
  106. {
  107. int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * comboNums * m_ScoreData.comboMul);
  108. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.bestHitMul) : 0;
  109. IncreaseScore(m_ScoreData.baseScore + comboS + bestS);
  110. comboNums++;
  111. TotalHitNum++;
  112. DestroyShootTarget(target as SphereShotTarget);
  113. generateTargetTimeTasks.Add(TimerSystem.GetInstance().AddTimeTask(GenerateShotTarget, 0.5f));
  114. StartBestHitTimer();
  115. }
  116. else
  117. {
  118. MissHitNum++;
  119. comboNums = 0;
  120. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul);
  121. IncreaseScore(missPunitiveS * -1);
  122. }
  123. }
  124. #endregion
  125. private void InitShotTargets()
  126. {
  127. for (int i = 0; i < maxTargetNum; i++)
  128. {
  129. GenerateShotTarget();
  130. }
  131. }
  132. private void GenerateShotTarget()
  133. {
  134. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  135. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  136. target.IsRunning = true;
  137. target.onClickCanChangeColor = false;
  138. target.onHoveringCanChangeColor = false;
  139. target.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  140. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  141. curTargets.Add(target);
  142. totalTargetNum++;
  143. }
  144. private Vector3 RandomPosition()
  145. {
  146. List<Vector3> positions = curTargets.GetGameObjectWorldPosition<SphereShotTarget>();
  147. Vector2 widthLimt = Vector2.zero;
  148. Vector2 heightLimt = Vector2.zero;
  149. switch (m_DifficultyType)
  150. {
  151. case DifficultyType.Standard:
  152. widthLimt = new Vector2(-4, 4);
  153. heightLimt = new Vector2(-1.5f, 1.5f);
  154. break;
  155. case DifficultyType.Advance:
  156. widthLimt = new Vector2(-8, 8);
  157. heightLimt = new Vector2(-1.5f, 3.5f);
  158. break;
  159. case DifficultyType.Professional:
  160. widthLimt = new Vector2(-8, 8);
  161. heightLimt = new Vector2(-1.5f, 5f);
  162. break;
  163. }
  164. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  165. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  166. if (ExtraTool.TryGetNonOverlapRamdonPos(positions, min, max, 2, out Vector3 ramdonPos, true))
  167. {
  168. return ramdonPos;
  169. }
  170. else
  171. {
  172. return new Vector3(0, 0, 25f);
  173. }
  174. }
  175. private void DestroyShootTarget(SphereShotTarget target)
  176. {
  177. if (target != null)
  178. {
  179. curTargets.Remove(target);
  180. target.IsRunning = false;
  181. GameObject.Destroy(target.gameObject);
  182. }
  183. }
  184. private void ResetTarget()
  185. {
  186. for (int i = 0; i < curTargets.Count; i++)
  187. {
  188. curTargets[i].IsRunning = false;
  189. GameObject.Destroy(curTargets[i].gameObject);
  190. }
  191. curTargets.Clear();
  192. }
  193. }
  194. }