JudgeShotTrainHandle.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using ShotSimulator.Target;
  2. using ShotSimulator.Tool;
  3. using ShotSimulator.Train.Info;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using Random = UnityEngine.Random;
  9. namespace ShotSimulator.Train
  10. {
  11. public enum JudgeShotStep
  12. {
  13. Observe,
  14. Judge,
  15. }
  16. public class JudgeShotTrainHandle : BaseTrainHandle
  17. {
  18. private Color curTargetColor;
  19. private List<Color> colors = new List<Color>() { Color.red, Color.green };
  20. private JudgeShotStep curStep;
  21. private List<SphereShotTarget> curTargets = new List<SphereShotTarget>();
  22. private TimeTask m_ObserveTimer;
  23. private JudgeShotTrainDifficultyData m_DifficultyData;
  24. private JudgeShotTrainScoreData m_ScoreData;
  25. protected float totalReactionTime;
  26. private float invalidDecisionNums;
  27. private float validityDecisionNums;
  28. private double m_TotalHitNum;
  29. private double TotalHitNum
  30. {
  31. get { return m_TotalHitNum; }
  32. set
  33. {
  34. m_TotalHitNum = value;
  35. UpdatePrecision();
  36. }
  37. }
  38. private double m_MissHitNum;
  39. private double MissHitNum
  40. {
  41. get { return m_MissHitNum; }
  42. set
  43. {
  44. m_MissHitNum = value;
  45. UpdatePrecision();
  46. }
  47. }
  48. public JudgeShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  49. {
  50. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  51. m_DifficultyData = info.GetDifficultyData<JudgeShotTrainDifficultyData>(m_DifficultyType);
  52. m_ScoreData = info.GetScoreData<JudgeShotTrainScoreData>(m_DifficultyType);
  53. }
  54. #region Train
  55. protected override void ResetTrain()
  56. {
  57. ResetTarget();
  58. curTargetColor = Color.white;
  59. curStep = JudgeShotStep.Observe;
  60. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  61. m_ObserveTimer = null;
  62. totalReactionTime = 0;
  63. invalidDecisionNums = 0;
  64. validityDecisionNums = 0;
  65. m_TotalHitNum = 0;
  66. m_MissHitNum = 0;
  67. }
  68. public override void StartTrain()
  69. {
  70. base.StartTrain();
  71. SwitchTrainStep(JudgeShotStep.Observe);
  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 PauseTrain()
  82. {
  83. base.PauseTrain();
  84. if (m_ObserveTimer != null)
  85. {
  86. m_ObserveTimer.pause = true;
  87. }
  88. }
  89. public override void CalculateTrainResult()
  90. {
  91. SetResultMetricsDataValue(MetricsType.ReactionTime, Math.Round(totalReactionTime / (invalidDecisionNums + validityDecisionNums), 2));
  92. SetResultMetricsDataValue(MetricsType.DeactionAccurancy, Math.Round(validityDecisionNums / (invalidDecisionNums + validityDecisionNums), 2));
  93. }
  94. #endregion
  95. #region ShotTarget
  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. private void OnShottedTargetEvent(BaseShotTarget target)
  107. {
  108. if (target != null)
  109. {
  110. TotalHitNum++;
  111. CalculateShootingResult((target as SphereShotTarget).defaultColor);
  112. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  113. JudgeShotStep nextStep = curStep == JudgeShotStep.Judge ? JudgeShotStep.Observe : JudgeShotStep.Judge;
  114. SwitchTrainStep(nextStep);
  115. }
  116. else
  117. {
  118. comboNums = 0;
  119. int missPunitiveS = 0;
  120. switch (curStep)
  121. {
  122. case JudgeShotStep.Observe:
  123. missPunitiveS = Mathf.CeilToInt(m_ScoreData.singleWheelBaseScore * m_ScoreData.singleWheelMissPunitiveMul) + Random.Range(-2, 2);
  124. break;
  125. case JudgeShotStep.Judge:
  126. missPunitiveS = Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * m_ScoreData.doubleWheelMissPunitiveMul) + Random.Range(-2, 2);
  127. break;
  128. }
  129. IncreaseScore(missPunitiveS * -1);
  130. MissHitNum++;
  131. }
  132. }
  133. #endregion
  134. private void SwitchTrainStep(JudgeShotStep step)
  135. {
  136. ResetTarget();
  137. curStep = step;
  138. StartBestHitTimer();
  139. switch (curStep)
  140. {
  141. case JudgeShotStep.Observe:
  142. ExecuteObserveStepTarget();
  143. break;
  144. case JudgeShotStep.Judge:
  145. ExecuteJudgeStepTarget();
  146. break;
  147. }
  148. }
  149. private void ExecuteObserveStepTarget()
  150. {
  151. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  152. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  153. target.onClickCanChangeColor = false;
  154. target.onHoveringCanChangeColor = false;
  155. target.defaultColor = RandomTargetColorType();
  156. Vector3 centerPos = new Vector3(0, 0, m_DifficultyData.targetDistance);
  157. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, centerPos);
  158. curTargets.Add(target);
  159. target.IsRunning = true;
  160. }
  161. private void ExecuteJudgeStepTarget()
  162. {
  163. for (int i = 0; i < 2; i++)
  164. {
  165. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  166. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  167. curTargets.Add(target);
  168. target.IsRunning = true;
  169. target.onClickCanChangeColor = false;
  170. target.onHoveringCanChangeColor = false;
  171. target.defaultColor = i == 0 ? curTargetColor : RandomTargetColorType();
  172. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, RandomPosition());
  173. }
  174. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimeOut, m_DifficultyData.observeTime);
  175. }
  176. private void ObserveTimeOut()
  177. {
  178. invalidDecisionNums++;
  179. totalReactionTime += m_DifficultyData.observeTime;
  180. comboNums = 0;
  181. int outTimePunitiveS = Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * m_ScoreData.doubleWheelOutTimePunitiveMul) + Random.Range(-2, 2);
  182. IncreaseScore(outTimePunitiveS * -1);
  183. SwitchTrainStep(JudgeShotStep.Observe);
  184. }
  185. private void CalculateShootingResult(Color color)
  186. {
  187. switch (curStep)
  188. {
  189. case JudgeShotStep.Observe:
  190. CalculateObserveStepResult(color);
  191. break;
  192. case JudgeShotStep.Judge:
  193. CalculateJudgeStepResult(color);
  194. break;
  195. }
  196. }
  197. private void CalculateObserveStepResult(Color color)
  198. {
  199. curTargetColor = color;
  200. int comboS = Mathf.CeilToInt(m_ScoreData.singleWheelBaseScore * comboNums * m_ScoreData.comboMul);
  201. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.singleWheelBaseScore * m_ScoreData.bestHitMul) + Random.Range(-1, 3) : 0;
  202. IncreaseScore(m_ScoreData.singleWheelBaseScore + comboS + bestS);
  203. comboNums++;
  204. }
  205. private void CalculateJudgeStepResult(Color color)
  206. {
  207. bool validity = curTargetColor == color;
  208. if (validity)
  209. {
  210. validityDecisionNums++;
  211. int comboS = Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * comboNums * m_ScoreData.comboMul);
  212. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * m_ScoreData.bestHitMul) + Random.Range(-1, 3) : 0;
  213. IncreaseScore(m_ScoreData.doubleWheelBaseScore + comboS + bestS);
  214. comboNums++;
  215. }
  216. else
  217. {
  218. invalidDecisionNums++;
  219. comboNums = 0;
  220. int falsePunitiveS = Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * m_ScoreData.doubleWheelFalsePunitiveMul) + Random.Range(-3, 3);
  221. IncreaseScore(falsePunitiveS * -1);
  222. }
  223. totalReactionTime += m_ObserveTimer.delay - m_ObserveTimer.remainingTime;
  224. }
  225. private Color RandomTargetColorType()
  226. {
  227. Color color = colors[Random.Range(0, colors.Count)];
  228. while (color == curTargetColor)
  229. {
  230. color = colors[Random.Range(0, colors.Count)];
  231. }
  232. return color;
  233. }
  234. private Vector3 RandomPosition()
  235. {
  236. List<Vector3> positions = curTargets.GetGameObjectWorldPosition<SphereShotTarget>();
  237. Vector2 widthLimt = Vector2.zero;
  238. Vector2 heightLimt = Vector2.zero;
  239. switch (m_DifficultyType)
  240. {
  241. case DifficultyType.Standard:
  242. widthLimt = new Vector2(-4, 4);
  243. heightLimt = new Vector2(-1.5f, 1.5f);
  244. break;
  245. case DifficultyType.Advance:
  246. widthLimt = new Vector2(-8, 8);
  247. heightLimt = new Vector2(-1.5f, 3.5f);
  248. break;
  249. case DifficultyType.Professional:
  250. widthLimt = new Vector2(-8, 8);
  251. heightLimt = new Vector2(-1.5f, 5f);
  252. break;
  253. }
  254. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  255. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  256. if (ExtraTool.TryGetNonOverlapRamdonPos(positions, min, max, 1, out Vector3 ramdonPos, false))
  257. {
  258. return ramdonPos;
  259. }
  260. else
  261. {
  262. return new Vector3(0, 0, m_DifficultyData.targetDistance);
  263. }
  264. }
  265. private void ResetTarget()
  266. {
  267. for (int i = 0; i < curTargets.Count; i++)
  268. {
  269. curTargets[i].IsRunning = false;
  270. GameObject.Destroy(curTargets[i].gameObject);
  271. }
  272. curTargets.Clear();
  273. }
  274. }
  275. }