ObserveShotTrainHandle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 ObserveShotTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private TimeTask m_IntervalTimer;
  16. private TimeTask m_ObserveTimer;
  17. private bool isTargetAppears;
  18. private ObserveShotTrainDifficultyData m_DifficultyData;
  19. private ObserveShotTrainScoreData m_ScoreData;
  20. private float scoreFormulaK;
  21. private float scoreFormulaB;
  22. private double m_TotalHitNum;
  23. private double TotalHitNum
  24. {
  25. get { return m_TotalHitNum; }
  26. set
  27. {
  28. m_TotalHitNum = value;
  29. UpdatePrecision();
  30. }
  31. }
  32. private double m_MissHitNum;
  33. private double MissHitNum
  34. {
  35. get { return m_MissHitNum; }
  36. set
  37. {
  38. m_MissHitNum = value;
  39. UpdatePrecision();
  40. }
  41. }
  42. private float totaleObserveTime;
  43. private float gunSoundTime;
  44. public ObserveShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  45. {
  46. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  47. m_DifficultyData = info.GetDifficultyData<ObserveShotTrainDifficultyData>(m_DifficultyType);
  48. m_ScoreData = info.GetScoreData<ObserveShotTrainScoreData>(m_DifficultyType);
  49. scoreFormulaK = (m_ScoreData.bestBaseScore - m_ScoreData.commonBaseScore) / (0 - 1);
  50. scoreFormulaB = m_ScoreData.bestBaseScore;
  51. }
  52. #region Train
  53. protected override void ResetTrain()
  54. {
  55. ResetTarget();
  56. isTargetAppears = false;
  57. gunSoundTime = 0;
  58. m_TotalHitNum = 0;
  59. m_MissHitNum = 0;
  60. totaleObserveTime = 0;
  61. TimerSystem.GetInstance().RemoveTimeTask(m_IntervalTimer);
  62. m_IntervalTimer = null;
  63. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  64. m_ObserveTimer = null;
  65. }
  66. public override void StartTrain()
  67. {
  68. base.StartTrain();
  69. StartNewRound();
  70. }
  71. public override void PauseTrain()
  72. {
  73. base.PauseTrain();
  74. if (m_IntervalTimer != null)
  75. {
  76. m_IntervalTimer.pause = true;
  77. }
  78. if (m_ObserveTimer != null)
  79. {
  80. m_ObserveTimer.pause = true;
  81. }
  82. }
  83. public override void ContinueTrain()
  84. {
  85. base.ContinueTrain();
  86. if (m_IntervalTimer != null)
  87. {
  88. m_IntervalTimer.pause = false;
  89. }
  90. if (m_ObserveTimer != null)
  91. {
  92. m_ObserveTimer.pause = false;
  93. }
  94. }
  95. public override void CalculateTrainResult()
  96. {
  97. SetResultMetricsDataValue(MetricsType.ReactionTime, Math.Round(totaleObserveTime / TotalHitNum, 2));
  98. SetResultMetricsDataValue(MetricsType.FalseAlarms, MissHitNum);
  99. }
  100. #endregion
  101. #region Target
  102. private void OnShottedTargetEvent(BaseShotTarget target)
  103. {
  104. if (isTargetAppears)
  105. {
  106. TotalHitNum++;
  107. float observeTime = (gunSoundTime - m_TaskTimer.remainingTime);
  108. totaleObserveTime += observeTime;
  109. if (observeTime <= 1)
  110. {
  111. int baseS = Mathf.CeilToInt(observeTime * scoreFormulaK + scoreFormulaB);
  112. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  113. IncreaseScore(baseS + comboS);
  114. comboNums++;
  115. }
  116. else
  117. {
  118. comboNums = 0;
  119. IncreaseScore(m_ScoreData.commonBaseScore);
  120. }
  121. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  122. {
  123. CurrentNumber--;
  124. if (CurrentNumber <= 0)
  125. {
  126. return;
  127. }
  128. }
  129. StartNewRound();
  130. }
  131. else
  132. {
  133. MissHitNum++;
  134. IncreaseScore(m_ScoreData.bestBaseScore * -1);
  135. }
  136. }
  137. #endregion
  138. private void UpdatePrecision()
  139. {
  140. double totalShot = TotalHitNum + MissHitNum;
  141. double precision = 0;
  142. if (totalShot != 0)
  143. {
  144. precision = Math.Round(TotalHitNum / totalShot, 2);
  145. }
  146. //SetResultMetricsDataValue(MetricsType.Precision, precision);
  147. }
  148. private void GenerateShotTarget()
  149. {
  150. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  151. curTarget = obj.GetComponent<SphereShotTarget>();
  152. curTarget.onClickCanChangeColor = false;
  153. curTarget.onHoveringCanChangeColor = false;
  154. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  155. curTarget.Init(1, ReduceHealthTriggerType.None, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  156. curTarget.IsRunning = true;
  157. }
  158. private Vector3 RandomPosition()
  159. {
  160. Vector2 widthLimt = Vector2.zero;
  161. Vector2 heightLimt = Vector2.zero;
  162. switch (m_DifficultyType)
  163. {
  164. case DifficultyType.Standard:
  165. widthLimt = new Vector2(-4, 4);
  166. heightLimt = new Vector2(-1.5f, 1.5f);
  167. break;
  168. case DifficultyType.Advance:
  169. widthLimt = new Vector2(-8, 8);
  170. heightLimt = new Vector2(-1.5f, 3.5f);
  171. break;
  172. case DifficultyType.Professional:
  173. widthLimt = new Vector2(-8, 8);
  174. heightLimt = new Vector2(-1.5f, 5f);
  175. break;
  176. }
  177. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  178. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  179. return ExtraTool.RandomPosition(min, max);
  180. }
  181. private void StartNewRound()
  182. {
  183. ResetTarget();
  184. isTargetAppears = false;
  185. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  186. m_ObserveTimer = null;
  187. TimerSystem.GetInstance().RemoveTimeTask(m_IntervalTimer);
  188. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, Random.Range(1f, 5f));
  189. }
  190. private void IntervalTimerTimeOut()
  191. {
  192. m_IntervalTimer = null;
  193. GenerateShotTarget();
  194. gunSoundTime = m_TaskTimer.remainingTime;
  195. isTargetAppears = true;
  196. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 3f);
  197. }
  198. private void ResetTarget()
  199. {
  200. if (curTarget != null)
  201. {
  202. curTarget.IsRunning = false;
  203. GameObject.Destroy(curTarget.gameObject);
  204. curTarget = null;
  205. }
  206. }
  207. private void ObserveTimerTimeOut()
  208. {
  209. comboNums = 0;
  210. totaleObserveTime += 3f;
  211. m_ObserveTimer = null;
  212. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  213. {
  214. CurrentNumber--;
  215. if (CurrentNumber <= 0)
  216. {
  217. return;
  218. }
  219. }
  220. StartNewRound();
  221. }
  222. }
  223. }