EnemyTideShotTrainHandle.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. SetResultMetricsDataValue(MetricsType.Headshots, Headshots);
  83. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / totalKillNum, 2));
  84. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNum);
  85. }
  86. #endregion
  87. #region Target
  88. private void OnShottedTargetEvent(BaseShotTarget target)
  89. {
  90. if (target != null)
  91. {
  92. HumanoidPartShotTarget part = target as HumanoidPartShotTarget;
  93. AIHumanoidShotTarget dependTarget = part.DependTarget as AIHumanoidShotTarget;
  94. int baseS = 0;
  95. switch (part.PartType)
  96. {
  97. case HumanoidPartType.Head:
  98. baseS = m_ScoreData.headHitBaseScore;
  99. break;
  100. case HumanoidPartType.Body:
  101. baseS = m_ScoreData.bodyHitBaseScore;
  102. break;
  103. }
  104. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  105. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  106. IncreaseScore(baseS + comboS + bestS);
  107. if (dependTarget.Health <= 0)
  108. {
  109. if (part.PartType == HumanoidPartType.Head)
  110. {
  111. Headshots++;
  112. }
  113. totalKillNum++;
  114. DestroyShootingTarget(dependTarget);
  115. if (curTargets.Count == 0)
  116. {
  117. GenerateShotTarget();
  118. }
  119. comboNums++;
  120. }
  121. TotalHitNum++;
  122. StartBestHitTimer();
  123. }
  124. else
  125. {
  126. MissHitNum++;
  127. comboNums = 0;
  128. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.bodyHitBaseScore * m_ScoreData.missPunitiveMul);
  129. IncreaseScore(missPunitiveS * -1);
  130. }
  131. }
  132. #endregion
  133. #region TrainResult
  134. private void UpdatePrecision()
  135. {
  136. double totalShot = TotalHitNum + MissHitNum;
  137. double precision = 0;
  138. if (totalShot != 0)
  139. {
  140. precision = Math.Round(TotalHitNum / totalShot, 2);
  141. }
  142. SetResultMetricsDataValue(MetricsType.Precision, precision);
  143. }
  144. #endregion
  145. private void GenerateShotTarget()
  146. {
  147. for (int i = 0; i < m_DifficultyData.targetNums; i++)
  148. {
  149. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/AIHumanoidShotTarget"));
  150. AIHumanoidShotTarget target = obj.GetComponent<AIHumanoidShotTarget>();
  151. target.InitHumanoidShotTarget(3, RandomPosition());
  152. Vector3 minBoundaryPos = new Vector3(-2f, -2f, 4.5f);
  153. Vector3 maxBoundaryPos = new Vector3(2f, -2f, 4.5f + m_DifficultyData.boundaryDistance);
  154. target.InitBoundaryPosition(minBoundaryPos, maxBoundaryPos);
  155. target.InitHide(originPos, m_DifficultyData.hideProbability);
  156. target.SetCrouchTimeProportion(m_DifficultyData.crouchTimeProportion);
  157. target.SetSpeed(m_DifficultyData.speed);
  158. curTargets.Add(target);
  159. }
  160. }
  161. private void DestroyShootingTarget(AIHumanoidShotTarget target)
  162. {
  163. if (target != null)
  164. {
  165. curTargets.Remove(target);
  166. GameObject.Destroy(target.gameObject);
  167. }
  168. }
  169. private Vector3 RandomPosition()
  170. {
  171. return originPos[Random.Range(0, originPos.Count)];
  172. }
  173. private void ResetTarget()
  174. {
  175. for (int i = 0; i < curTargets.Count; i++)
  176. {
  177. GameObject.Destroy(curTargets[i].gameObject);
  178. }
  179. curTargets.Clear();
  180. }
  181. }
  182. }