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