GridShotTrainHandle.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. double timeToKill = 999;
  85. if (TotalHitNum != 0)
  86. {
  87. timeToKill = Math.Round(TrainInfo.duration / TotalHitNum, 2);
  88. }
  89. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  90. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  91. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  92. SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum);
  93. }
  94. #endregion
  95. #region TrainResult
  96. private void UpdatePrecision()
  97. {
  98. double totalShot = TotalHitNum + MissHitNum;
  99. double precision = 0;
  100. if (totalShot != 0)
  101. {
  102. precision = Math.Round(TotalHitNum / totalShot, 2);
  103. }
  104. SetResultMetricsDataValue(MetricsType.Precision, precision);
  105. }
  106. #endregion
  107. #region ShootTarget
  108. private void OnShottedTargetEvent(BaseShotTarget target)
  109. {
  110. if (target != null)
  111. {
  112. int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * comboNums * m_ScoreData.comboMul);
  113. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.bestHitMul) : 0;
  114. IncreaseScore(m_ScoreData.baseScore + comboS + bestS);
  115. comboNums++;
  116. TotalHitNum++;
  117. DestroyShootTarget(target as SphereShotTarget);
  118. generateTargetTimeTasks.Add(TimerSystem.GetInstance().AddTimeTask(GenerateShotTarget, 0.5f));
  119. StartBestHitTimer();
  120. }
  121. else
  122. {
  123. MissHitNum++;
  124. comboNums = 0;
  125. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul);
  126. IncreaseScore(missPunitiveS * -1);
  127. }
  128. }
  129. #endregion
  130. private void InitShotTargets()
  131. {
  132. for (int i = 0; i < maxTargetNum; i++)
  133. {
  134. GenerateShotTarget();
  135. }
  136. }
  137. private void GenerateShotTarget()
  138. {
  139. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  140. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  141. target.IsRunning = true;
  142. target.onClickCanChangeColor = false;
  143. target.onHoveringCanChangeColor = false;
  144. target.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  145. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  146. curTargets.Add(target);
  147. totalTargetNum++;
  148. }
  149. private Vector3 RandomPosition()
  150. {
  151. List<Vector3> positions = curTargets.GetGameObjectWorldPosition<SphereShotTarget>();
  152. Vector2 widthLimt = Vector2.zero;
  153. Vector2 heightLimt = Vector2.zero;
  154. switch (m_DifficultyType)
  155. {
  156. case DifficultyType.Standard:
  157. widthLimt = new Vector2(-4, 4);
  158. heightLimt = new Vector2(-1.5f, 1.5f);
  159. break;
  160. case DifficultyType.Advance:
  161. widthLimt = new Vector2(-8, 8);
  162. heightLimt = new Vector2(-1.5f, 3.5f);
  163. break;
  164. case DifficultyType.Professional:
  165. widthLimt = new Vector2(-8, 8);
  166. heightLimt = new Vector2(-1.5f, 5f);
  167. break;
  168. }
  169. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  170. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  171. if (ExtraTool.TryGetNonOverlapRamdonPos(positions, min, max, 2, out Vector3 ramdonPos, true))
  172. {
  173. return ramdonPos;
  174. }
  175. else
  176. {
  177. return new Vector3(0, 0, 25f);
  178. }
  179. }
  180. private void DestroyShootTarget(SphereShotTarget target)
  181. {
  182. if (target != null)
  183. {
  184. curTargets.Remove(target);
  185. target.IsRunning = false;
  186. GameObject.Destroy(target.gameObject);
  187. }
  188. }
  189. private void ResetTarget()
  190. {
  191. for (int i = 0; i < curTargets.Count; i++)
  192. {
  193. curTargets[i].IsRunning = false;
  194. GameObject.Destroy(curTargets[i].gameObject);
  195. }
  196. curTargets.Clear();
  197. }
  198. }
  199. }