SpiderShotTrainHandle.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 SpiderShotTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private TimeTask m_ObserveTimer;
  16. private SpiderShotTrainDifficultyData m_DifficultyData;
  17. private SpiderShotTrainScoreData m_ScoreData;
  18. private int roundIndex;
  19. private Dictionary<DifficultyType, List<Vector2>> m_SpiderPosDictionary = new Dictionary<DifficultyType, List<Vector2>>()
  20. {
  21. {
  22. DifficultyType.Standard,new List<Vector2>()
  23. {
  24. new Vector2(0, 0f),
  25. new Vector2(-1.75f, 1.25f), new Vector2(-2.5f, 0.5f), new Vector2(-2f, -0.5f),new Vector2(-1.25f, -1f),
  26. new Vector2(1.75f, 1.25f), new Vector2(2.5f, 0.5f), new Vector2(2f, -0.5f),new Vector2(1.25f, -1f)
  27. }
  28. },
  29. {
  30. DifficultyType.Advance,new List<Vector2>()
  31. {
  32. new Vector2(0, 0f),
  33. new Vector2(-3.25f, 2f), new Vector2(-4.25f, 1f), new Vector2(-4f, -0.75f),new Vector2(-2.25f, -1.5f),
  34. new Vector2(3.25f, 2f), new Vector2(4.25f, 1f), new Vector2(4f, -0.75f),new Vector2(2.25f, -1.5f)
  35. }
  36. },
  37. {
  38. DifficultyType.Professional,new List<Vector2>()
  39. {
  40. new Vector2(0, 0f),
  41. new Vector2(-5f, 3.25f), new Vector2(-6.5f, 1.5f), new Vector2(-6f, -1f),new Vector2(-3.25f, -1.5f),
  42. new Vector2(5f, 3.25f), new Vector2(6.5f, 1.5f), new Vector2(6f, -1f),new Vector2(3.25f, -1.5f)
  43. }
  44. },
  45. };
  46. private Dictionary<DifficultyType, List<Vector3>> m_SpiderObjTransform = new Dictionary<DifficultyType, List<Vector3>>()
  47. {
  48. { DifficultyType.Standard, new List<Vector3>(){ new Vector3(0,0.5f,6f), new Vector3(2,1,1) } },
  49. { DifficultyType.Advance, new List<Vector3>(){ new Vector3(0,1.75f,11f), new Vector3(3.5f,1.75f,1) } },
  50. { DifficultyType.Professional, new List<Vector3>(){ new Vector3(0,3,16.5f), new Vector3(7,2.25f,1) } }
  51. };
  52. private GameObject spiderObj;
  53. private double m_TotalHitNum;
  54. private double TotalHitNum
  55. {
  56. get { return m_TotalHitNum; }
  57. set
  58. {
  59. m_TotalHitNum = value;
  60. UpdatePrecision();
  61. }
  62. }
  63. private double m_MissHitNum;
  64. private double MissHitNum
  65. {
  66. get { return m_MissHitNum; }
  67. set
  68. {
  69. m_MissHitNum = value;
  70. UpdatePrecision();
  71. }
  72. }
  73. private double totalTargetNum;
  74. public SpiderShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  75. {
  76. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  77. m_DifficultyData = info.GetDifficultyData<SpiderShotTrainDifficultyData>(m_DifficultyType);
  78. m_ScoreData = info.GetScoreData<SpiderShotTrainScoreData>(m_DifficultyType);
  79. }
  80. #region Train
  81. protected override void ResetTrain()
  82. {
  83. ResetTarget();
  84. roundIndex = -1;
  85. m_TotalHitNum = 0;
  86. m_MissHitNum = 0;
  87. totalTargetNum = 0;
  88. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  89. m_ObserveTimer = null;
  90. GameObject.Destroy(spiderObj);
  91. }
  92. public override void PrepareTrain()
  93. {
  94. base.PrepareTrain();
  95. spiderObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SpiderObj"));
  96. spiderObj.transform.GetChild(0).transform.localPosition = m_SpiderObjTransform[DifficultyType.Standard][0];
  97. spiderObj.transform.GetChild(0).transform.localScale = m_SpiderObjTransform[DifficultyType.Standard][1];
  98. }
  99. public override void StartTrain()
  100. {
  101. base.StartTrain();
  102. GenerateShotTarget();
  103. }
  104. public override void PauseTrain()
  105. {
  106. base.PauseTrain();
  107. if (m_ObserveTimer != null)
  108. {
  109. m_ObserveTimer.pause = true;
  110. }
  111. }
  112. public override void ContinueTrain()
  113. {
  114. base.ContinueTrain();
  115. if (m_ObserveTimer != null)
  116. {
  117. m_ObserveTimer.pause = false;
  118. }
  119. }
  120. public override void CalculateTrainResult()
  121. {
  122. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  123. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  124. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / TotalHitNum, 2));
  125. SetResultMetricsDataValue(MetricsType.ShotsPerKill, Math.Round((TotalHitNum + MissHitNum) / TotalHitNum, 2));
  126. }
  127. #endregion
  128. #region TrainResult
  129. private void UpdatePrecision()
  130. {
  131. double totalShot = TotalHitNum + MissHitNum;
  132. double precision = 0;
  133. if (totalShot != 0)
  134. {
  135. precision = Math.Round(TotalHitNum / totalShot, 2);
  136. }
  137. SetResultMetricsDataValue(MetricsType.Precision, precision);
  138. }
  139. #endregion
  140. #region Target
  141. private void OnShottedTargetEvent(BaseShotTarget target)
  142. {
  143. if (target != null)
  144. {
  145. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  146. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  147. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  148. IncreaseScore(baseS + comboS + bestS);
  149. comboNums++;
  150. TotalHitNum++;
  151. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  152. m_ObserveTimer = null;
  153. DestroyShootTarget(target as SphereShotTarget);
  154. GenerateShotTarget();
  155. }
  156. else
  157. {
  158. MissHitNum++;
  159. comboNums = 0;
  160. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  161. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  162. IncreaseScore(missPunitiveS * -1);
  163. }
  164. }
  165. #endregion
  166. private void GenerateShotTarget()
  167. {
  168. roundIndex++;
  169. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  170. curTarget = obj.GetComponent<SphereShotTarget>();
  171. curTarget.onClickCanChangeColor = false;
  172. curTarget.onHoveringCanChangeColor = false;
  173. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  174. Vector3 pos = RandomPosition();
  175. pos.z = m_DifficultyData.distance;
  176. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, pos);
  177. curTarget.IsRunning = true;
  178. if(roundIndex % 3 != 1)
  179. {
  180. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, m_DifficultyData.duration);
  181. }
  182. totalTargetNum++;
  183. StartBestHitTimer();
  184. }
  185. private void DestroyShootTarget(SphereShotTarget target)
  186. {
  187. if (target != null)
  188. {
  189. target.IsRunning = false;
  190. GameObject.Destroy(target.gameObject);
  191. }
  192. }
  193. private void ObserveTimerTimeOut()
  194. {
  195. comboNums = 0;
  196. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  197. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.timeOutPunitiveMul) + Random.Range(-3, 3);
  198. IncreaseScore(missPunitiveS * -1);
  199. m_ObserveTimer = null;
  200. DestroyShootTarget(curTarget as SphereShotTarget);
  201. GenerateShotTarget();
  202. }
  203. private Vector2 RandomPosition()
  204. {
  205. int round = roundIndex % 4;
  206. switch (round)
  207. {
  208. case 0:
  209. case 2:
  210. return m_SpiderPosDictionary[m_DifficultyType][0];
  211. case 1:
  212. return m_SpiderPosDictionary[m_DifficultyType][Random.Range(1, 5)];
  213. case 3:
  214. return m_SpiderPosDictionary[m_DifficultyType][Random.Range(5, 9)];
  215. }
  216. return Vector3.zero;
  217. }
  218. private void ResetTarget()
  219. {
  220. if (curTarget != null)
  221. {
  222. curTarget.IsRunning = false;
  223. GameObject.Destroy(curTarget.gameObject);
  224. curTarget = null;
  225. }
  226. }
  227. }
  228. }