ReactionShotTrainHandle.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using ShotSimulator.Target;
  2. using ShotSimulator.Train.Info;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using Random = UnityEngine.Random;
  8. using ShotSimulator.Tool;
  9. using ShotSimulator.Screen;
  10. namespace ShotSimulator.Train
  11. {
  12. public class ReactionShotTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private TimeTask m_ObserveTimer;
  16. private TimeTask m_IntervalTimer;
  17. private bool current_IsFar;
  18. private ReactionShotTrainDifficultyData m_DifficultyData;
  19. private ReactionShotTrainScoreData m_ScoreData;
  20. private double m_TotalHitNum;
  21. private double TotalHitNum
  22. {
  23. get { return m_TotalHitNum; }
  24. set
  25. {
  26. m_TotalHitNum = value;
  27. UpdatePrecision();
  28. }
  29. }
  30. private double m_MissHitNum;
  31. private double MissHitNum
  32. {
  33. get { return m_MissHitNum; }
  34. set
  35. {
  36. m_MissHitNum = value;
  37. UpdatePrecision();
  38. }
  39. }
  40. protected float totalReactionTime;
  41. public ReactionShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  42. {
  43. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  44. m_DifficultyData = info.GetDifficultyData<ReactionShotTrainDifficultyData>(m_DifficultyType);
  45. m_ScoreData = info.GetScoreData<ReactionShotTrainScoreData>(m_DifficultyType);
  46. }
  47. #region Train
  48. protected override void ResetTrain()
  49. {
  50. ResetTarget();
  51. m_TotalHitNum = 0;
  52. m_MissHitNum = 0;
  53. totalReactionTime = 0;
  54. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  55. m_ObserveTimer = null;
  56. TimerSystem.GetInstance().RemoveTimeTask(m_IntervalTimer);
  57. m_IntervalTimer = null;
  58. }
  59. public override void StartTrain()
  60. {
  61. base.StartTrain();
  62. GenerateShotTarget();
  63. }
  64. public override void PauseTrain()
  65. {
  66. base.PauseTrain();
  67. if (m_ObserveTimer != null)
  68. {
  69. m_ObserveTimer.pause = true;
  70. }
  71. if (m_IntervalTimer != null)
  72. {
  73. m_IntervalTimer.pause = true;
  74. }
  75. }
  76. public override void ContinueTrain()
  77. {
  78. base.ContinueTrain();
  79. if (m_ObserveTimer != null)
  80. {
  81. m_ObserveTimer.pause = false;
  82. }
  83. if (m_IntervalTimer != null)
  84. {
  85. m_IntervalTimer.pause = false;
  86. }
  87. }
  88. public override void CalculateTrainResult()
  89. {
  90. SetResultMetricsDataValue(MetricsType.ReactionTime, Math.Round(totalReactionTime / (TotalHitNum + MissHitNum), 2));
  91. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  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 Target
  108. private void OnShottedTargetEvent(BaseShotTarget target)
  109. {
  110. if (target != null)
  111. {
  112. TotalHitNum++;
  113. totalReactionTime += m_ObserveTimer.delay - m_ObserveTimer.remainingTime;
  114. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  115. m_ObserveTimer = null;
  116. DestroyShootTarget(target as SphereShotTarget);
  117. int baseS = current_IsFar ? m_ScoreData.farBaseScore : m_ScoreData.nearBaseScore;
  118. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  119. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  120. IncreaseScore(baseS + comboS + bestS);
  121. comboNums++;
  122. }
  123. else
  124. {
  125. MissHitNum++;
  126. comboNums = 0;
  127. int baseS = current_IsFar ? m_ScoreData.farBaseScore : m_ScoreData.nearBaseScore;
  128. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  129. IncreaseScore(missPunitiveS * -1);
  130. }
  131. }
  132. #endregion
  133. private void GenerateShotTarget()
  134. {
  135. StartBestHitTimer();
  136. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  137. curTarget = obj.GetComponent<SphereShotTarget>();
  138. curTarget.onClickCanChangeColor = false;
  139. curTarget.onHoveringCanChangeColor = false;
  140. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  141. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  142. curTarget.IsRunning = true;
  143. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 2.5f);
  144. }
  145. private void DestroyShootTarget(SphereShotTarget target)
  146. {
  147. if (target != null)
  148. {
  149. target.IsRunning = false;
  150. GameObject.Destroy(target.gameObject);
  151. }
  152. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, 2f);
  153. }
  154. private void IntervalTimerTimeOut()
  155. {
  156. GenerateShotTarget();
  157. }
  158. private void ObserveTimerTimeOut()
  159. {
  160. comboNums = 0;
  161. int baseS = current_IsFar ? m_ScoreData.farBaseScore : m_ScoreData.nearBaseScore;
  162. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  163. IncreaseScore(missPunitiveS * -1);
  164. m_ObserveTimer = null;
  165. totalReactionTime += 2.5f;
  166. DestroyShootTarget(curTarget as SphereShotTarget);
  167. }
  168. private Vector3 RandomPosition()
  169. {
  170. current_IsFar = ExtraTool.IsProbabolityInclusive(m_DifficultyData.farProbability);
  171. Vector2 widthLimt = Vector2.zero;
  172. Vector2 heightLimt = Vector2.zero;
  173. if (current_IsFar)
  174. {
  175. switch (m_DifficultyType)
  176. {
  177. case DifficultyType.Standard:
  178. widthLimt = new Vector2(-8, 8);
  179. heightLimt = new Vector2(-1.5f, 3.5f);
  180. break;
  181. case DifficultyType.Advance:
  182. widthLimt = new Vector2(-8, 8);
  183. heightLimt = new Vector2(-1.5f, 5f);
  184. break;
  185. case DifficultyType.Professional:
  186. widthLimt = new Vector2(-8, 8);
  187. heightLimt = new Vector2(-1.5f, 5f);
  188. break;
  189. }
  190. }
  191. else
  192. {
  193. widthLimt = new Vector2(-4, 4);
  194. heightLimt = new Vector2(-1.5f, 1.5f);
  195. }
  196. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, current_IsFar ? m_DifficultyData.farDistance : m_DifficultyData.nearDistance, widthLimt, heightLimt);
  197. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, current_IsFar ? m_DifficultyData.farDistance : m_DifficultyData.nearDistance, widthLimt, heightLimt);
  198. return ExtraTool.RandomPosition(min, max);
  199. }
  200. private void ResetTarget()
  201. {
  202. if (curTarget != null)
  203. {
  204. curTarget.IsRunning = false;
  205. GameObject.Destroy(curTarget.gameObject);
  206. curTarget = null;
  207. }
  208. }
  209. }
  210. }