PistolSniperTrainHandle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 PistolSniperTrainHandle : BaseTrainHandle
  13. {
  14. private Camera shiperCamera;
  15. private SphereShotTarget curTarget;
  16. private PistolSniperTrainInfoDifficultyData m_DifficultyData;
  17. private PistolSniperTrainScoreData m_ScoreData;
  18. private TimeTask m_ObserveTimer;
  19. private int roundIndex;
  20. private double m_TotalHitNum;
  21. private double TotalHitNum
  22. {
  23. get { return m_TotalHitNum; }
  24. set
  25. {
  26. m_TotalHitNum = value;
  27. UpdatePrecision();
  28. }
  29. }
  30. private double m_MissHitNum;
  31. private double MissHitNum
  32. {
  33. get { return m_MissHitNum; }
  34. set
  35. {
  36. m_MissHitNum = value;
  37. UpdatePrecision();
  38. }
  39. }
  40. private double totalKillNums;
  41. private double totalTargetNums;
  42. public PistolSniperTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  43. {
  44. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnClickTargetEvent);
  45. m_DifficultyData = info.GetDifficultyData<PistolSniperTrainInfoDifficultyData>(m_DifficultyType);
  46. m_ScoreData = info.GetScoreData<PistolSniperTrainScoreData>(m_DifficultyType);
  47. shiperCamera = Camera.main.transform.Find("SnipeCamera").GetComponent<Camera>();
  48. }
  49. private void GenerateShotTarget()
  50. {
  51. roundIndex++;
  52. int round = roundIndex % 2;
  53. MotionType motionType = MotionType.None;
  54. Vector3 origin = round == 1 ? new Vector3(0, 0, m_DifficultyData.distance) : RandomPosition();
  55. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  56. curTarget = obj.GetComponent<SphereShotTarget>();
  57. curTarget.onClickCanChangeColor = false;
  58. curTarget.onHoveringCanChangeColor = false;
  59. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  60. curTarget.onHoveringColor = Color.red;
  61. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, origin, null, 1f);
  62. curTarget.IsRunning = true;
  63. totalTargetNums++;
  64. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 5f);
  65. StartBestHitTimer();
  66. }
  67. private void ObserveTimerTimeOut()
  68. {
  69. comboNums = 0;
  70. int outTimePunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.outTimePunitiveMul) + Random.Range(-3, 3);
  71. IncreaseScore(outTimePunitiveS * -1);
  72. m_ObserveTimer = null;
  73. DestroyShootTarget(curTarget as SphereShotTarget);
  74. GenerateShotTarget();
  75. }
  76. private Vector3 RandomPosition()
  77. {
  78. Vector2 widthLimt = Vector2.zero;
  79. Vector2 heightLimt = Vector2.zero;
  80. switch (m_DifficultyType)
  81. {
  82. case DifficultyType.Standard:
  83. widthLimt = new Vector2(-12, 12);
  84. heightLimt = new Vector2(-1.5f, 10f);
  85. break;
  86. case DifficultyType.Advance:
  87. widthLimt = new Vector2(-12, 12);
  88. heightLimt = new Vector2(-1.5f, 12f);
  89. break;
  90. case DifficultyType.Professional:
  91. widthLimt = new Vector2(-12, 12);
  92. heightLimt = new Vector2(-1.5f, 15f);
  93. break;
  94. }
  95. Vector2 minOffset = new Vector2(0.4f, 0.4f);
  96. Vector2 maxOffset = new Vector2(0.6f, 0.6f);
  97. Vector3 min = ExtraTool.GetScreenOffsetPosition(minOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  98. Vector3 max = ExtraTool.GetScreenOffsetPosition(maxOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  99. return ExtraTool.RandomPosition(min, max);
  100. }
  101. private void ResetTarget()
  102. {
  103. if (curTarget != null)
  104. {
  105. curTarget.IsRunning = false;
  106. GameObject.Destroy(curTarget.gameObject);
  107. curTarget = null;
  108. }
  109. }
  110. private void DestroyShootTarget(SphereShotTarget target)
  111. {
  112. if (target != null)
  113. {
  114. target.IsRunning = false;
  115. GameObject.Destroy(target.gameObject);
  116. }
  117. }
  118. #region TrainResult
  119. private void UpdatePrecision()
  120. {
  121. SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHitNum / (TotalHitNum + MissHitNum), 2));
  122. }
  123. #endregion
  124. #region Train
  125. protected override void ResetTrain()
  126. {
  127. ResetTarget();
  128. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  129. m_ObserveTimer = null;
  130. roundIndex = 0;
  131. m_TotalHitNum = 0;
  132. m_MissHitNum = 0;
  133. totalKillNums = 0;
  134. totalTargetNums = 0;
  135. shiperCamera.gameObject.SetActive(false);
  136. }
  137. public override void PrepareTrain()
  138. {
  139. base.PrepareTrain();
  140. shiperCamera.gameObject.SetActive(true);
  141. }
  142. public override void StartTrain()
  143. {
  144. base.StartTrain();
  145. GenerateShotTarget();
  146. }
  147. public override void PauseTrain()
  148. {
  149. base.PauseTrain();
  150. curTarget.IsRunning = false;
  151. if (m_ObserveTimer != null)
  152. {
  153. m_ObserveTimer.pause = true;
  154. }
  155. }
  156. public override void ContinueTrain()
  157. {
  158. base.ContinueTrain();
  159. curTarget.IsRunning = true;
  160. if (m_ObserveTimer != null)
  161. {
  162. m_ObserveTimer.pause = false;
  163. }
  164. }
  165. public override void CalculateTrainResult()
  166. {
  167. double timeToKill = 999;
  168. if (totalKillNums != 0)
  169. {
  170. timeToKill = Math.Round(TrainInfo.duration / totalKillNums, 2);
  171. }
  172. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(totalKillNums / TrainInfo.duration, 2));
  173. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNums);
  174. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  175. SetResultMetricsDataValue(MetricsType.Target, totalTargetNums);
  176. }
  177. #endregion
  178. #region ShotTarget
  179. private void OnClickTargetEvent(BaseShotTarget target)
  180. {
  181. if (target != null)
  182. {
  183. SphereShotTarget t = target as SphereShotTarget;
  184. if (t != null)
  185. {
  186. int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * comboNums * m_ScoreData.comboMul);
  187. int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.bestHitMul) : 0;
  188. IncreaseScore(m_ScoreData.baseScore + comboS + bestS);
  189. comboNums++;
  190. if (t.CurHealth <= 0)
  191. {
  192. totalKillNums++;
  193. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  194. m_ObserveTimer = null;
  195. DestroyShootTarget(t);
  196. GenerateShotTarget();
  197. }
  198. }
  199. TotalHitNum++;
  200. }
  201. else
  202. {
  203. MissHitNum++;
  204. comboNums = 0;
  205. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul);
  206. IncreaseScore(missPunitiveS * -1);
  207. }
  208. }
  209. #endregion
  210. }
  211. }