SixTargetsShotTrainHandle.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 SixTargetsShotTrainHandle : BaseTrainHandle
  13. {
  14. private int maxTargetNum = 6;
  15. private SixTargetsShotDifficultyData m_DifficultyData;
  16. private SixTargetsShotScoreData m_ScoreData;
  17. private double m_TotalHitNum;
  18. private double TotalHitNum
  19. {
  20. get { return m_TotalHitNum; }
  21. set
  22. {
  23. m_TotalHitNum = value;
  24. UpdatePrecision();
  25. }
  26. }
  27. private double m_MissHitNum;
  28. private double MissHitNum
  29. {
  30. get { return m_MissHitNum; }
  31. set
  32. {
  33. m_MissHitNum = value;
  34. UpdatePrecision();
  35. }
  36. }
  37. private double totalTargetNum;
  38. private List<SphereShotTarget> curTargets = new List<SphereShotTarget>();
  39. public SixTargetsShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  40. {
  41. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  42. m_DifficultyData = info.GetDifficultyData<SixTargetsShotDifficultyData>(m_DifficultyType);
  43. m_ScoreData = info.GetScoreData<SixTargetsShotScoreData>(m_DifficultyType);
  44. }
  45. #region Train
  46. protected override void ResetTrain()
  47. {
  48. ResetTarget();
  49. m_TotalHitNum = 0;
  50. m_MissHitNum = 0;
  51. totalTargetNum = 0;
  52. }
  53. public override void StartTrain()
  54. {
  55. base.StartTrain();
  56. GenerateShootTargets();
  57. }
  58. public override void CalculateTrainResult()
  59. {
  60. double timeToKill = 999;
  61. if (TotalHitNum != 0)
  62. {
  63. timeToKill = Math.Round(TrainInfo.duration / TotalHitNum, 2);
  64. }
  65. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  66. SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum);
  67. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  68. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  69. }
  70. #endregion
  71. #region ShootTarget
  72. private void OnShottedTargetEvent(BaseShotTarget target)
  73. {
  74. if (target != null)
  75. {
  76. int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * comboNums * m_ScoreData.comboMul);
  77. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.bestHitMul) : 0;
  78. IncreaseScore(m_ScoreData.baseScore + comboS + bestS);
  79. TotalHitNum++;
  80. DestroyShootTarget(target as SphereShotTarget);
  81. GenerateShootTargets();
  82. }
  83. else
  84. {
  85. MissHitNum++;
  86. comboNums = 0;
  87. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul);
  88. IncreaseScore(missPunitiveS * -1);
  89. }
  90. }
  91. #endregion
  92. private void GenerateShootTargets()
  93. {
  94. int num = maxTargetNum - curTargets.Count;
  95. for (int i = 0; i < num; i++)
  96. {
  97. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  98. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  99. target.IsRunning = true;
  100. target.onClickCanChangeColor = false;
  101. target.onHoveringCanChangeColor = false;
  102. target.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  103. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  104. curTargets.Add(target);
  105. totalTargetNum++;
  106. }
  107. StartBestHitTimer();
  108. }
  109. private void DestroyShootTarget(SphereShotTarget target)
  110. {
  111. if (target != null)
  112. {
  113. curTargets.Remove(target);
  114. target.IsRunning = false;
  115. GameObject.Destroy(target.gameObject);
  116. }
  117. }
  118. private Vector3 RandomPosition()
  119. {
  120. List<Vector3> positions = curTargets.GetGameObjectWorldPosition<SphereShotTarget>();
  121. Vector2 widthLimt = Vector2.zero;
  122. Vector2 heightLimt = Vector2.zero;
  123. switch (m_DifficultyType)
  124. {
  125. case DifficultyType.Standard:
  126. widthLimt = new Vector2(-12, 12);
  127. heightLimt = new Vector2(-1.5f, 4f);
  128. break;
  129. case DifficultyType.Advance:
  130. widthLimt = new Vector2(-12, 12);
  131. heightLimt = new Vector2(-1.5f, 6f);
  132. break;
  133. case DifficultyType.Professional:
  134. widthLimt = new Vector2(-12, 12);
  135. heightLimt = new Vector2(-1.5f, 7f);
  136. break;
  137. }
  138. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  139. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  140. if(ExtraTool.TryGetNonOverlapRamdonPos(positions, min, max, 1, out Vector3 ramdonPos, true))
  141. {
  142. return ramdonPos;
  143. }
  144. else
  145. {
  146. return new Vector3(0, 0, m_DifficultyData.distance);
  147. }
  148. }
  149. private void ResetTarget()
  150. {
  151. for (int i = 0; i < curTargets.Count; i++)
  152. {
  153. curTargets[i].IsRunning = false;
  154. GameObject.Destroy(curTargets[i].gameObject);
  155. }
  156. curTargets.Clear();
  157. }
  158. #region TrainResult
  159. private void UpdatePrecision()
  160. {
  161. double totalShot = TotalHitNum + MissHitNum;
  162. double precision = 0;
  163. if (totalShot != 0)
  164. {
  165. precision = Math.Round(TotalHitNum / totalShot, 2);
  166. }
  167. SetResultMetricsDataValue(MetricsType.Precision, precision);
  168. }
  169. #endregion
  170. }
  171. }