MemoryShotTrainHandle.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. double reactionTime = 999;
  116. if (invalidDecisionNums + validityDecisionNums != 0)
  117. {
  118. reactionTime = Math.Round(totalReactionTime / (invalidDecisionNums + validityDecisionNums), 2);
  119. }
  120. double deactionAccurancy = 0;
  121. if (validityDecisionNums + invalidDecisionNums != 0)
  122. {
  123. deactionAccurancy = Math.Round(validityDecisionNums / (invalidDecisionNums + validityDecisionNums), 2);
  124. }
  125. SetResultMetricsDataValue(MetricsType.ReactionTime, reactionTime);
  126. SetResultMetricsDataValue(MetricsType.DeactionAccurancy, deactionAccurancy);
  127. SetResultMetricsDataValue(MetricsType.VisualMemory, validityDecisionNums);
  128. }
  129. #endregion
  130. private void UpdatePrecision()
  131. {
  132. double totalShot = TotalHitNum + MissHitNum;
  133. double precision = 0;
  134. if (totalShot != 0)
  135. {
  136. precision = Math.Round(TotalHitNum / totalShot, 2);
  137. }
  138. SetResultMetricsDataValue(MetricsType.Precision, precision);
  139. }
  140. #region ShotTarget
  141. private void OnShottedTargetEvent(BaseShotTarget target)
  142. {
  143. if (curStep == JudgeShotStep.Judge)
  144. {
  145. if (target != null)
  146. {
  147. CalculateShootResult((target as SphereShotTarget).defaultColor);
  148. SwitchTrainStep(JudgeShotStep.Observe);
  149. TotalHitNum++;
  150. }
  151. else
  152. {
  153. comboNums = 0;
  154. MissHitNum++;
  155. }
  156. }
  157. }
  158. #endregion
  159. private void SwitchTrainStep(JudgeShotStep step)
  160. {
  161. ResetTarget();
  162. curStep = step;
  163. switch (curStep)
  164. {
  165. case JudgeShotStep.Observe:
  166. ExecuteObserveStepTarget();
  167. break;
  168. case JudgeShotStep.Judge:
  169. ExecuteJudgeStepTarget();
  170. break;
  171. }
  172. }
  173. private void ExecuteObserveStepTarget()
  174. {
  175. generateTargetPos.Clear();
  176. falseColors.Clear();
  177. falseColors.AddRange(RandomColors);
  178. falseColors.FisherYatesShuffle<Color>();
  179. curTargetColor = falseColors[0];
  180. falseColors.RemoveAt(0);
  181. int removeNum = falseColors.Count - generateTargetNum[curGenerateTargetNumIndex % generateTargetNum.Count];
  182. while (removeNum > 0)
  183. {
  184. falseColors.RemoveAt(0);
  185. removeNum--;
  186. }
  187. curTargetDistance = Random.Range(5f, 7f);
  188. for (int i = 0; i < falseColors.Count; i++)
  189. {
  190. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  191. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  192. curTargets.Add(target);
  193. target.onClickCanChangeColor = false;
  194. target.onHoveringCanChangeColor = false;
  195. target.defaultColor = falseColors[i];
  196. Vector3 pos = RandomPosition();
  197. generateTargetPos.Add(pos);
  198. target.Init(1, ReduceHealthTriggerType.None, false, ReduceHealthType.Per, MotionType.None, pos);
  199. target.IsRunning = true;
  200. }
  201. curGenerateTargetNumIndex++;
  202. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimeOut, m_DifficultyData.observeTime);
  203. }
  204. private void ExecuteJudgeStepTarget()
  205. {
  206. StartBestHitTimer();
  207. //falseColors.FisherYatesShuffle<Color>();
  208. int index = Random.Range(0, falseColors.Count);
  209. for (int i = 0; i < falseColors.Count; i++)
  210. {
  211. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  212. SphereShotTarget target = obj.GetComponent<SphereShotTarget>();
  213. curTargets.Add(target);
  214. target.IsRunning = true;
  215. target.onClickCanChangeColor = false;
  216. target.onHoveringCanChangeColor = false;
  217. target.defaultColor = i == index ? curTargetColor : falseColors[i];
  218. target.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, generateTargetPos[i]);
  219. }
  220. }
  221. private void ObserveTimeOut()
  222. {
  223. ResetTarget();
  224. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, 0.5f);
  225. }
  226. private void IntervalTimerTimeOut()
  227. {
  228. curObaserveFinishTime = m_TaskTimer.remainingTime;
  229. SwitchTrainStep(JudgeShotStep.Judge);
  230. }
  231. private void CalculateShootResult(Color color)
  232. {
  233. bool validity = curTargetColor == color;
  234. if (validity)
  235. {
  236. validityDecisionNums++;
  237. int comboS = Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * comboNums * m_ScoreData.comboMul);
  238. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * m_ScoreData.bestHitMul) : 0;
  239. IncreaseScore(m_ScoreData.doubleWheelBaseScore + comboS + bestS);
  240. comboNums++;
  241. }
  242. else
  243. {
  244. invalidDecisionNums++;
  245. comboNums = 0;
  246. int falsePunitiveS = Mathf.CeilToInt(m_ScoreData.doubleWheelBaseScore * m_ScoreData.doubleWheelFalsePunitiveMul);
  247. IncreaseScore(falsePunitiveS * -1);
  248. }
  249. totalReactionTime += (curObaserveFinishTime - m_TaskTimer.remainingTime);
  250. }
  251. private Vector3 RandomPosition()
  252. {
  253. List<Vector3> positions = curTargets.GetGameObjectWorldPosition<SphereShotTarget>();
  254. Vector2 widthLimt = new Vector2(-4, 4);
  255. Vector2 heightLimt = new Vector2(-1.5f, 1.5f);
  256. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, curTargetDistance, widthLimt, heightLimt);
  257. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, curTargetDistance, widthLimt, heightLimt);
  258. if (ExtraTool.TryGetNonOverlapRamdonPos(positions, min, max, 1, out Vector3 ramdonPos, false))
  259. {
  260. return ramdonPos;
  261. }
  262. else
  263. {
  264. return new Vector3(0, 0, curTargetDistance);
  265. }
  266. }
  267. private void ResetTarget()
  268. {
  269. for (int i = 0; i < curTargets.Count; i++)
  270. {
  271. curTargets[i].IsRunning = false;
  272. GameObject.Destroy(curTargets[i].gameObject);
  273. }
  274. curTargets.Clear();
  275. }
  276. }
  277. }