SixTargetsShotTrainHandle.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  61. SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum);
  62. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  63. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / TotalHitNum, 2));
  64. }
  65. #endregion
  66. #region ShootTarget
  67. private void OnShottedTargetEvent(BaseShotTarget target)
  68. {
  69. if (target != null)
  70. {
  71. int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * comboNums * m_ScoreData.comboMul);
  72. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.bestHitMul) : 0;
  73. IncreaseScore(m_ScoreData.baseScore + comboS + bestS);
  74. TotalHitNum++;
  75. DestroyShootTarget(target as SphereShotTarget);
  76. GenerateShootTargets();
  77. }
  78. else
  79. {
  80. MissHitNum++;
  81. comboNums = 0;
  82. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul);
  83. IncreaseScore(missPunitiveS * -1);
  84. }
  85. }
  86. #endregion
  87. private void GenerateShootTargets()
  88. {
  89. int num = maxTargetNum - curTargets.Count;
  90. for (int i = 0; i < num; i++)
  91. {
  92. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  93. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  94. target.IsRunning = true;
  95. target.onClickCanChangeColor = false;
  96. target.onHoveringCanChangeColor = false;
  97. target.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  98. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  99. curTargets.Add(target);
  100. totalTargetNum++;
  101. }
  102. StartBestHitTimer();
  103. }
  104. private void DestroyShootTarget(SphereShotTarget target)
  105. {
  106. if (target != null)
  107. {
  108. curTargets.Remove(target);
  109. target.IsRunning = false;
  110. GameObject.Destroy(target.gameObject);
  111. }
  112. }
  113. private Vector3 RandomPosition()
  114. {
  115. List<Vector3> positions = curTargets.GetGameObjectWorldPosition<SphereShotTarget>();
  116. Vector2 widthLimt = Vector2.zero;
  117. Vector2 heightLimt = Vector2.zero;
  118. switch (m_DifficultyType)
  119. {
  120. case DifficultyType.Standard:
  121. widthLimt = new Vector2(-12, 12);
  122. heightLimt = new Vector2(-1.5f, 4f);
  123. break;
  124. case DifficultyType.Advance:
  125. widthLimt = new Vector2(-12, 12);
  126. heightLimt = new Vector2(-1.5f, 6f);
  127. break;
  128. case DifficultyType.Professional:
  129. widthLimt = new Vector2(-12, 12);
  130. heightLimt = new Vector2(-1.5f, 7f);
  131. break;
  132. }
  133. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  134. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  135. if(ExtraTool.TryGetNonOverlapRamdonPos(positions, min, max, 1, out Vector3 ramdonPos, true))
  136. {
  137. return ramdonPos;
  138. }
  139. else
  140. {
  141. return new Vector3(0, 0, m_DifficultyData.distance);
  142. }
  143. }
  144. private void ResetTarget()
  145. {
  146. for (int i = 0; i < curTargets.Count; i++)
  147. {
  148. curTargets[i].IsRunning = false;
  149. GameObject.Destroy(curTargets[i].gameObject);
  150. }
  151. curTargets.Clear();
  152. }
  153. #region TrainResult
  154. private void UpdatePrecision()
  155. {
  156. double totalShot = TotalHitNum + MissHitNum;
  157. double precision = 0;
  158. if (totalShot != 0)
  159. {
  160. precision = Math.Round(TotalHitNum / totalShot, 2);
  161. }
  162. SetResultMetricsDataValue(MetricsType.Precision, precision);
  163. }
  164. #endregion
  165. }
  166. }