SmallTargetShotTrainHandle.cs 7.2 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 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. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  84. SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum);
  85. SetResultMetricsDataValue(MetricsType.ReactionTime, Math.Round(totalReactionTime / (TotalHitNum + MissHitNum), 2));
  86. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  87. }
  88. #endregion
  89. #region TrainResult
  90. private void UpdatePrecision()
  91. {
  92. double totalShot = TotalHitNum + MissHitNum;
  93. double precision = 0;
  94. if (totalShot != 0)
  95. {
  96. precision = Math.Round(TotalHitNum / totalShot, 2);
  97. }
  98. SetResultMetricsDataValue(MetricsType.Precision, precision);
  99. }
  100. #endregion
  101. #region ShootTarget
  102. private void OnShottedTargetEvent(BaseShotTarget target)
  103. {
  104. if (target != null)
  105. {
  106. int baseS = roundIndex % 2 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  107. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  108. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  109. IncreaseScore(baseS + comboS + bestS);
  110. comboNums++;
  111. TotalHitNum++;
  112. if (m_ObserveTimer != null)
  113. {
  114. totalReactionTime += m_ObserveTimer.delay - m_ObserveTimer.remainingTime;
  115. }
  116. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  117. m_ObserveTimer = null;
  118. DestroyShootTarget(target as SphereShotTarget);
  119. GenerateShotTarget();
  120. }
  121. else
  122. {
  123. MissHitNum++;
  124. comboNums = 0;
  125. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  126. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  127. IncreaseScore(missPunitiveS * -1);
  128. }
  129. }
  130. #endregion
  131. private void GenerateShotTarget()
  132. {
  133. roundIndex++;
  134. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SmallSphereTarget"));
  135. curTarget = obj.GetComponent<SphereShotTarget>();
  136. curTarget.IsRunning = true;
  137. curTarget.onClickCanChangeColor = false;
  138. curTarget.onHoveringCanChangeColor = false;
  139. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  140. Vector3 originPos = roundIndex % 2 == 1 ? ExtraTool.GetWorldPositionWithResolutionPercent(0.5f, 0.5f, 10) : RandomPosition();
  141. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, originPos);
  142. if (roundIndex % 2 != 1)
  143. {
  144. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, m_DifficultyData.duration);
  145. }
  146. totalTargetNum++;
  147. StartBestHitTimer();
  148. }
  149. private void ObserveTimerTimeOut()
  150. {
  151. comboNums = 0;
  152. int baseS = roundIndex % 2 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  153. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.timeOutPunitiveMul) + Random.Range(-3, 3);
  154. IncreaseScore(missPunitiveS * -1);
  155. totalReactionTime += m_ObserveTimer.delay;
  156. m_ObserveTimer = null;
  157. DestroyShootTarget(curTarget as SphereShotTarget);
  158. GenerateShotTarget();
  159. }
  160. private Vector3 RandomPosition()
  161. {
  162. Vector2 widthLimt = new Vector2(-8, 8);
  163. Vector2 heightLimt = new Vector2(-1.5f, 3f);
  164. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, 10f, widthLimt, heightLimt);
  165. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, 10f, widthLimt, heightLimt);
  166. return ExtraTool.RandomPosition(min, max);
  167. }
  168. private void DestroyShootTarget(SphereShotTarget target)
  169. {
  170. if (target != null)
  171. {
  172. target.IsRunning = false;
  173. GameObject.Destroy(target.gameObject);
  174. }
  175. }
  176. private void ResetTarget()
  177. {
  178. if (curTarget != null)
  179. {
  180. curTarget.IsRunning = false;
  181. GameObject.Destroy(curTarget.gameObject);
  182. curTarget = null;
  183. }
  184. }
  185. }
  186. }