MemoryShotTrainHandle.cs 11 KB

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