EnemyTideShotTrainHandle.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ShotSimulator.Target;
  2. using ShotSimulator.Train.Info;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using Random = UnityEngine.Random;
  8. namespace ShotSimulator.Train
  9. {
  10. public class EnemyTideShotTrainHandle : BaseTrainHandle
  11. {
  12. private List<Vector3> originPos = new List<Vector3>()
  13. {
  14. new Vector3(-4,-2f,15),new Vector3(4,-2f,16),
  15. new Vector3(-4,-2f,11f),new Vector3(4,-2f,11f),
  16. new Vector3(-4,-2f,6f),new Vector3(4,-2f,6f),
  17. };
  18. private EnemyTideShotTrainDifficultyData m_DifficultyData;
  19. private EnemyTideShotTrainScoreData m_ScoreData;
  20. private List<AIHumanoidShotTarget> curTargets = new List<AIHumanoidShotTarget>();
  21. private double m_TotalHitNum;
  22. private double TotalHitNum
  23. {
  24. get { return m_TotalHitNum; }
  25. set
  26. {
  27. m_TotalHitNum = value;
  28. UpdatePrecision();
  29. }
  30. }
  31. private double m_MissHitNum;
  32. private double MissHitNum
  33. {
  34. get { return m_MissHitNum; }
  35. set
  36. {
  37. m_MissHitNum = value;
  38. UpdatePrecision();
  39. }
  40. }
  41. private double totalKillNum;
  42. private double Headshots;
  43. public EnemyTideShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  44. {
  45. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  46. m_DifficultyData = info.GetDifficultyData<EnemyTideShotTrainDifficultyData>(m_DifficultyType);
  47. m_ScoreData = info.GetScoreData<EnemyTideShotTrainScoreData>(m_DifficultyType);
  48. }
  49. #region Train
  50. protected override void ResetTrain()
  51. {
  52. ResetTarget();
  53. m_TotalHitNum = 0;
  54. m_MissHitNum = 0;
  55. totalKillNum = 0;
  56. Headshots = 0;
  57. }
  58. public override void StartTrain()
  59. {
  60. base.StartTrain();
  61. GenerateShotTarget();
  62. StartBestHitTimer();
  63. }
  64. public override void PauseTrain()
  65. {
  66. base.PauseTrain();
  67. for(int i = 0; i< curTargets.Count; i++)
  68. {
  69. curTargets[i].SetPause(true);
  70. }
  71. }
  72. public override void ContinueTrain()
  73. {
  74. base.ContinueTrain();
  75. for (int i = 0; i < curTargets.Count; i++)
  76. {
  77. curTargets[i].SetPause(false);
  78. }
  79. }
  80. public override void CalculateTrainResult()
  81. {
  82. double timeToKill = 999;
  83. if (totalKillNum != 0)
  84. {
  85. timeToKill = Math.Round(TrainInfo.duration / totalKillNum, 2);
  86. }
  87. SetResultMetricsDataValue(MetricsType.Headshots, Headshots);
  88. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  89. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNum);
  90. }
  91. #endregion
  92. #region Target
  93. private void OnShottedTargetEvent(BaseShotTarget target)
  94. {
  95. if (target != null)
  96. {
  97. HumanoidPartShotTarget part = target as HumanoidPartShotTarget;
  98. AIHumanoidShotTarget dependTarget = part.DependTarget as AIHumanoidShotTarget;
  99. int baseS = 0;
  100. switch (part.PartType)
  101. {
  102. case HumanoidPartType.Head:
  103. baseS = m_ScoreData.headHitBaseScore;
  104. break;
  105. case HumanoidPartType.Body:
  106. baseS = m_ScoreData.bodyHitBaseScore;
  107. break;
  108. }
  109. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  110. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  111. IncreaseScore(baseS + comboS + bestS);
  112. if (dependTarget.Health <= 0)
  113. {
  114. if (part.PartType == HumanoidPartType.Head)
  115. {
  116. Headshots++;
  117. }
  118. totalKillNum++;
  119. DestroyShootingTarget(dependTarget);
  120. if (curTargets.Count == 0)
  121. {
  122. GenerateShotTarget();
  123. }
  124. comboNums++;
  125. }
  126. TotalHitNum++;
  127. StartBestHitTimer();
  128. }
  129. else
  130. {
  131. MissHitNum++;
  132. comboNums = 0;
  133. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.bodyHitBaseScore * m_ScoreData.missPunitiveMul);
  134. IncreaseScore(missPunitiveS * -1);
  135. }
  136. }
  137. #endregion
  138. #region TrainResult
  139. private void UpdatePrecision()
  140. {
  141. double totalShot = TotalHitNum + MissHitNum;
  142. double precision = 0;
  143. if (totalShot != 0)
  144. {
  145. precision = Math.Round(TotalHitNum / totalShot, 2);
  146. }
  147. SetResultMetricsDataValue(MetricsType.Precision, precision);
  148. }
  149. #endregion
  150. private void GenerateShotTarget()
  151. {
  152. for (int i = 0; i < m_DifficultyData.targetNums; i++)
  153. {
  154. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/AIHumanoidShotTarget"));
  155. AIHumanoidShotTarget target = obj.GetComponent<AIHumanoidShotTarget>();
  156. target.InitHumanoidShotTarget(3, RandomPosition());
  157. Vector3 minBoundaryPos = new Vector3(-2f, -2f, 4.5f);
  158. Vector3 maxBoundaryPos = new Vector3(2f, -2f, 4.5f + m_DifficultyData.boundaryDistance);
  159. target.InitBoundaryPosition(minBoundaryPos, maxBoundaryPos);
  160. target.InitHide(originPos, m_DifficultyData.hideProbability);
  161. target.SetCrouchTimeProportion(m_DifficultyData.crouchTimeProportion);
  162. target.SetSpeed(m_DifficultyData.speed);
  163. curTargets.Add(target);
  164. }
  165. }
  166. private void DestroyShootingTarget(AIHumanoidShotTarget target)
  167. {
  168. if (target != null)
  169. {
  170. curTargets.Remove(target);
  171. GameObject.Destroy(target.gameObject);
  172. }
  173. }
  174. private Vector3 RandomPosition()
  175. {
  176. return originPos[Random.Range(0, originPos.Count)];
  177. }
  178. private void ResetTarget()
  179. {
  180. for (int i = 0; i < curTargets.Count; i++)
  181. {
  182. GameObject.Destroy(curTargets[i].gameObject);
  183. }
  184. curTargets.Clear();
  185. }
  186. }
  187. }