ReactionShotTrainHandle.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. double reactionTime = 999;
  91. if (TotalHitNum + MissHitNum != 0)
  92. {
  93. reactionTime = Math.Round(totalReactionTime / (TotalHitNum + MissHitNum), 2);
  94. }
  95. SetResultMetricsDataValue(MetricsType.ReactionTime, reactionTime);
  96. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  97. SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum);
  98. }
  99. #endregion
  100. #region TrainResult
  101. private void UpdatePrecision()
  102. {
  103. double totalShot = TotalHitNum + MissHitNum;
  104. double precision = 0;
  105. if (totalShot != 0)
  106. {
  107. precision = Math.Round(TotalHitNum / totalShot, 2);
  108. }
  109. SetResultMetricsDataValue(MetricsType.Precision, precision);
  110. }
  111. #endregion
  112. #region Target
  113. private void OnShottedTargetEvent(BaseShotTarget target)
  114. {
  115. if (target != null)
  116. {
  117. TotalHitNum++;
  118. totalReactionTime += m_ObserveTimer.delay - m_ObserveTimer.remainingTime;
  119. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  120. m_ObserveTimer = null;
  121. DestroyShootTarget(target as SphereShotTarget);
  122. int baseS = current_IsFar ? m_ScoreData.farBaseScore : m_ScoreData.nearBaseScore;
  123. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  124. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  125. IncreaseScore(baseS + comboS + bestS);
  126. comboNums++;
  127. }
  128. else
  129. {
  130. MissHitNum++;
  131. comboNums = 0;
  132. int baseS = current_IsFar ? m_ScoreData.farBaseScore : m_ScoreData.nearBaseScore;
  133. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  134. IncreaseScore(missPunitiveS * -1);
  135. }
  136. }
  137. #endregion
  138. private void GenerateShotTarget()
  139. {
  140. StartBestHitTimer();
  141. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  142. curTarget = obj.GetComponent<SphereShotTarget>();
  143. curTarget.onClickCanChangeColor = false;
  144. curTarget.onHoveringCanChangeColor = false;
  145. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  146. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  147. curTarget.IsRunning = true;
  148. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 2.5f);
  149. }
  150. private void DestroyShootTarget(SphereShotTarget target)
  151. {
  152. if (target != null)
  153. {
  154. target.IsRunning = false;
  155. GameObject.Destroy(target.gameObject);
  156. }
  157. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, 2f);
  158. }
  159. private void IntervalTimerTimeOut()
  160. {
  161. GenerateShotTarget();
  162. }
  163. private void ObserveTimerTimeOut()
  164. {
  165. comboNums = 0;
  166. int baseS = current_IsFar ? m_ScoreData.farBaseScore : m_ScoreData.nearBaseScore;
  167. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  168. IncreaseScore(missPunitiveS * -1);
  169. m_ObserveTimer = null;
  170. totalReactionTime += 2.5f;
  171. DestroyShootTarget(curTarget as SphereShotTarget);
  172. }
  173. private Vector3 RandomPosition()
  174. {
  175. current_IsFar = ExtraTool.IsProbabolityInclusive(m_DifficultyData.farProbability);
  176. Vector2 widthLimt = Vector2.zero;
  177. Vector2 heightLimt = Vector2.zero;
  178. if (current_IsFar)
  179. {
  180. switch (m_DifficultyType)
  181. {
  182. case DifficultyType.Standard:
  183. widthLimt = new Vector2(-8, 8);
  184. heightLimt = new Vector2(-1.5f, 3.5f);
  185. break;
  186. case DifficultyType.Advance:
  187. widthLimt = new Vector2(-8, 8);
  188. heightLimt = new Vector2(-1.5f, 5f);
  189. break;
  190. case DifficultyType.Professional:
  191. widthLimt = new Vector2(-8, 8);
  192. heightLimt = new Vector2(-1.5f, 5f);
  193. break;
  194. }
  195. }
  196. else
  197. {
  198. widthLimt = new Vector2(-4, 4);
  199. heightLimt = new Vector2(-1.5f, 1.5f);
  200. }
  201. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, current_IsFar ? m_DifficultyData.farDistance : m_DifficultyData.nearDistance, widthLimt, heightLimt);
  202. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, current_IsFar ? m_DifficultyData.farDistance : m_DifficultyData.nearDistance, widthLimt, heightLimt);
  203. return ExtraTool.RandomPosition(min, max);
  204. }
  205. private void ResetTarget()
  206. {
  207. if (curTarget != null)
  208. {
  209. curTarget.IsRunning = false;
  210. GameObject.Destroy(curTarget.gameObject);
  211. curTarget = null;
  212. }
  213. }
  214. }
  215. }