ObserveShotTrainHandle.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. double detctionTime = 999;
  98. if (TotalHitNum != 0)
  99. {
  100. detctionTime = Math.Round(totaleObserveTime / TotalHitNum, 2);
  101. }
  102. SetResultMetricsDataValue(MetricsType.ReactionTime, detctionTime);
  103. SetResultMetricsDataValue(MetricsType.FalseAlarms, MissHitNum);
  104. }
  105. #endregion
  106. #region Target
  107. private void OnShottedTargetEvent(BaseShotTarget target)
  108. {
  109. if (isTargetAppears)
  110. {
  111. TotalHitNum++;
  112. float observeTime = (gunSoundTime - m_TaskTimer.remainingTime);
  113. totaleObserveTime += observeTime;
  114. if (observeTime <= 1)
  115. {
  116. int baseS = Mathf.CeilToInt(observeTime * scoreFormulaK + scoreFormulaB);
  117. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  118. IncreaseScore(baseS + comboS);
  119. comboNums++;
  120. }
  121. else
  122. {
  123. comboNums = 0;
  124. IncreaseScore(m_ScoreData.commonBaseScore);
  125. }
  126. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  127. {
  128. CurrentNumber--;
  129. if (CurrentNumber <= 0)
  130. {
  131. return;
  132. }
  133. }
  134. StartNewRound();
  135. }
  136. else
  137. {
  138. MissHitNum++;
  139. IncreaseScore(m_ScoreData.bestBaseScore * -1);
  140. }
  141. }
  142. #endregion
  143. private void UpdatePrecision()
  144. {
  145. double totalShot = TotalHitNum + MissHitNum;
  146. double precision = 0;
  147. if (totalShot != 0)
  148. {
  149. precision = Math.Round(TotalHitNum / totalShot, 2);
  150. }
  151. //SetResultMetricsDataValue(MetricsType.Precision, precision);
  152. }
  153. private void GenerateShotTarget()
  154. {
  155. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  156. curTarget = obj.GetComponent<SphereShotTarget>();
  157. curTarget.onClickCanChangeColor = false;
  158. curTarget.onHoveringCanChangeColor = false;
  159. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  160. curTarget.Init(1, ReduceHealthTriggerType.None, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  161. curTarget.IsRunning = true;
  162. }
  163. private Vector3 RandomPosition()
  164. {
  165. Vector2 widthLimt = Vector2.zero;
  166. Vector2 heightLimt = Vector2.zero;
  167. switch (m_DifficultyType)
  168. {
  169. case DifficultyType.Standard:
  170. widthLimt = new Vector2(-4, 4);
  171. heightLimt = new Vector2(-1.5f, 1.5f);
  172. break;
  173. case DifficultyType.Advance:
  174. widthLimt = new Vector2(-8, 8);
  175. heightLimt = new Vector2(-1.5f, 3.5f);
  176. break;
  177. case DifficultyType.Professional:
  178. widthLimt = new Vector2(-8, 8);
  179. heightLimt = new Vector2(-1.5f, 5f);
  180. break;
  181. }
  182. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  183. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  184. return ExtraTool.RandomPosition(min, max);
  185. }
  186. private void StartNewRound()
  187. {
  188. ResetTarget();
  189. isTargetAppears = false;
  190. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  191. m_ObserveTimer = null;
  192. TimerSystem.GetInstance().RemoveTimeTask(m_IntervalTimer);
  193. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, Random.Range(1f, 5f));
  194. }
  195. private void IntervalTimerTimeOut()
  196. {
  197. m_IntervalTimer = null;
  198. GenerateShotTarget();
  199. gunSoundTime = m_TaskTimer.remainingTime;
  200. isTargetAppears = true;
  201. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 3f);
  202. }
  203. private void ResetTarget()
  204. {
  205. if (curTarget != null)
  206. {
  207. curTarget.IsRunning = false;
  208. GameObject.Destroy(curTarget.gameObject);
  209. curTarget = null;
  210. }
  211. }
  212. private void ObserveTimerTimeOut()
  213. {
  214. comboNums = 0;
  215. totaleObserveTime += 3f;
  216. m_ObserveTimer = null;
  217. if (m_TrainTaskFinishType == TrainTaskFinishType.Number)
  218. {
  219. CurrentNumber--;
  220. if (CurrentNumber <= 0)
  221. {
  222. return;
  223. }
  224. }
  225. StartNewRound();
  226. }
  227. }
  228. }