SpiderShotTrainHandle.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. double timeToKill = 999;
  123. double shotsPerKill = 999;
  124. if (TotalHitNum != 0)
  125. {
  126. timeToKill = Math.Round(TrainInfo.duration / TotalHitNum, 2);
  127. shotsPerKill = Math.Round((TotalHitNum + MissHitNum) / TotalHitNum, 2);
  128. }
  129. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2));
  130. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  131. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  132. SetResultMetricsDataValue(MetricsType.ShotsPerKill, shotsPerKill);
  133. }
  134. #endregion
  135. #region TrainResult
  136. private void UpdatePrecision()
  137. {
  138. double totalShot = TotalHitNum + MissHitNum;
  139. double precision = 0;
  140. if (totalShot != 0)
  141. {
  142. precision = Math.Round(TotalHitNum / totalShot, 2);
  143. }
  144. SetResultMetricsDataValue(MetricsType.Precision, precision);
  145. }
  146. #endregion
  147. #region Target
  148. private void OnShottedTargetEvent(BaseShotTarget target)
  149. {
  150. if (target != null)
  151. {
  152. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  153. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  154. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  155. IncreaseScore(baseS + comboS + bestS);
  156. comboNums++;
  157. TotalHitNum++;
  158. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  159. m_ObserveTimer = null;
  160. DestroyShootTarget(target as SphereShotTarget);
  161. GenerateShotTarget();
  162. }
  163. else
  164. {
  165. MissHitNum++;
  166. comboNums = 0;
  167. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  168. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  169. IncreaseScore(missPunitiveS * -1);
  170. }
  171. }
  172. #endregion
  173. private void GenerateShotTarget()
  174. {
  175. roundIndex++;
  176. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  177. curTarget = obj.GetComponent<SphereShotTarget>();
  178. curTarget.onClickCanChangeColor = false;
  179. curTarget.onHoveringCanChangeColor = false;
  180. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  181. Vector3 pos = RandomPosition();
  182. pos.z = m_DifficultyData.distance;
  183. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, pos);
  184. curTarget.IsRunning = true;
  185. if(roundIndex % 3 != 1)
  186. {
  187. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, m_DifficultyData.duration);
  188. }
  189. totalTargetNum++;
  190. StartBestHitTimer();
  191. }
  192. private void DestroyShootTarget(SphereShotTarget target)
  193. {
  194. if (target != null)
  195. {
  196. target.IsRunning = false;
  197. GameObject.Destroy(target.gameObject);
  198. }
  199. }
  200. private void ObserveTimerTimeOut()
  201. {
  202. comboNums = 0;
  203. int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore;
  204. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.timeOutPunitiveMul) + Random.Range(-3, 3);
  205. IncreaseScore(missPunitiveS * -1);
  206. m_ObserveTimer = null;
  207. DestroyShootTarget(curTarget as SphereShotTarget);
  208. GenerateShotTarget();
  209. }
  210. private Vector2 RandomPosition()
  211. {
  212. int round = roundIndex % 4;
  213. switch (round)
  214. {
  215. case 0:
  216. case 2:
  217. return m_SpiderPosDictionary[m_DifficultyType][0];
  218. case 1:
  219. return m_SpiderPosDictionary[m_DifficultyType][Random.Range(1, 5)];
  220. case 3:
  221. return m_SpiderPosDictionary[m_DifficultyType][Random.Range(5, 9)];
  222. }
  223. return Vector3.zero;
  224. }
  225. private void ResetTarget()
  226. {
  227. if (curTarget != null)
  228. {
  229. curTarget.IsRunning = false;
  230. GameObject.Destroy(curTarget.gameObject);
  231. curTarget = null;
  232. }
  233. }
  234. }
  235. }