MozambiqueTrainHandle.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using ShotSimulator.Target;
  2. using ShotSimulator.Tool;
  3. using ShotSimulator.Train.Info;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using Random = UnityEngine.Random;
  9. namespace ShotSimulator.Train
  10. {
  11. public class MozambiqueTrainHandle : BaseTrainHandle
  12. {
  13. private MozambiqueHumanoidShotTarget curTarget;
  14. private MozambiqueTrainDifficultyData m_DifficultyData;
  15. private MozambiqueTrainScoreData m_ScoreData;
  16. private TimeTask m_ObserveTimer;
  17. private TimeTask m_IntervalTimer;
  18. private double m_TotalHitNum;
  19. private double TotalHitNum
  20. {
  21. get { return m_TotalHitNum; }
  22. set
  23. {
  24. m_TotalHitNum = value;
  25. UpdatePrecision();
  26. }
  27. }
  28. private double m_MissHitNum;
  29. private double MissHitNum
  30. {
  31. get { return m_MissHitNum; }
  32. set
  33. {
  34. m_MissHitNum = value;
  35. UpdatePrecision();
  36. }
  37. }
  38. private double totalKillNums;
  39. private double mozambiqueKillNums;
  40. public MozambiqueTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  41. {
  42. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent);
  43. m_DifficultyData = info.GetDifficultyData<MozambiqueTrainDifficultyData>(m_DifficultyType);
  44. m_ScoreData = info.GetScoreData<MozambiqueTrainScoreData>(m_DifficultyType);
  45. }
  46. #region Train
  47. protected override void ResetTrain()
  48. {
  49. ResetTarget();
  50. m_TotalHitNum = 0;
  51. m_MissHitNum = 0;
  52. totalKillNums = 0;
  53. mozambiqueKillNums = 0;
  54. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  55. m_ObserveTimer = null;
  56. TimerSystem.GetInstance().RemoveTimeTask(m_IntervalTimer);
  57. m_IntervalTimer = null;
  58. }
  59. public override void StartTrain()
  60. {
  61. base.StartTrain();
  62. GenerateShotTarget();
  63. }
  64. public override void PauseTrain()
  65. {
  66. base.PauseTrain();
  67. if (m_ObserveTimer != null)
  68. {
  69. m_ObserveTimer.pause = true;
  70. }
  71. if (m_IntervalTimer != null)
  72. {
  73. m_IntervalTimer.pause = true;
  74. }
  75. }
  76. public override void ContinueTrain()
  77. {
  78. base.ContinueTrain();
  79. if (m_ObserveTimer != null)
  80. {
  81. m_ObserveTimer.pause = false;
  82. }
  83. if (m_IntervalTimer != null)
  84. {
  85. m_IntervalTimer.pause = false;
  86. }
  87. }
  88. public override void CalculateTrainResult()
  89. {
  90. SetResultMetricsDataValue(MetricsType.VisualMemory, mozambiqueKillNums);
  91. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(totalKillNums / TrainInfo.duration, 2));
  92. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNums);
  93. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / totalKillNums, 2));
  94. SetResultMetricsDataValue(MetricsType.ShotsPerKill, Math.Round((TotalHitNum + MissHitNum) / totalKillNums, 2));
  95. }
  96. #endregion
  97. #region Target
  98. private void OnShottedTargetEvent(BaseShotTarget target)
  99. {
  100. int baseS = GetCurrentBaseScore(target != null);
  101. if (target != null)
  102. {
  103. HumanoidPartShotTarget part = target as HumanoidPartShotTarget;
  104. MozambiqueHumanoidShotTarget dependTarget = part.DependTarget as MozambiqueHumanoidShotTarget;
  105. if (dependTarget.Health <= 0)
  106. {
  107. totalKillNums++;
  108. if(IsMozambiqueShotOrder(dependTarget.m_CauseHumanoidPartReduceHPOrder))
  109. {
  110. mozambiqueKillNums++;
  111. }
  112. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  113. m_ObserveTimer = null;
  114. DestroyShootTarget(dependTarget);
  115. }
  116. TotalHitNum++;
  117. if (!dependTarget.isCurFalseHit)
  118. {
  119. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  120. IncreaseScore(baseS + bestS);
  121. }
  122. else
  123. {
  124. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.falsePunitiveMul) + Random.Range(-2, 2);
  125. IncreaseScore(missPunitiveS * -1);
  126. }
  127. StartBestHitTimer();
  128. }
  129. else
  130. {
  131. MissHitNum++;
  132. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul) + Random.Range(-3, 3);
  133. IncreaseScore(missPunitiveS * -1);
  134. }
  135. }
  136. #endregion
  137. public bool IsMozambiqueShotOrder(List<HumanoidPartType> order)
  138. {
  139. if (order == null) return false;
  140. if (order.Count != 3) return false;
  141. return order[0] == HumanoidPartType.Body && order[1] == HumanoidPartType.Body && order[2] == HumanoidPartType.Head;
  142. }
  143. private int GetCurrentBaseScore(bool isHit)
  144. {
  145. int baseS = 0;
  146. switch (isHit? curTarget.Health: curTarget.Health - 1)
  147. {
  148. case 2:
  149. baseS = m_ScoreData.firstBaseScore;
  150. break;
  151. case 1:
  152. baseS = m_ScoreData.secondBaseScore;
  153. break;
  154. case 0:
  155. baseS = m_ScoreData.thirdBaseScore;
  156. break;
  157. default:
  158. baseS = m_ScoreData.firstBaseScore;
  159. break;
  160. }
  161. return baseS;
  162. }
  163. private void GenerateShotTarget()
  164. {
  165. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/MozambiqueHumanoidShotTarget"));
  166. obj.transform.rotation = Quaternion.Euler(0, 180, 0);
  167. curTarget = obj.GetComponent<MozambiqueHumanoidShotTarget>();
  168. curTarget.InitHumanoidShotTarget(3, RandomPosition());
  169. curTarget.InitMozambiqueHumanoidShotTarget();
  170. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 5);
  171. StartBestHitTimer();
  172. }
  173. private void IntervalTimerTimeOut()
  174. {
  175. GenerateShotTarget();
  176. }
  177. private void ObserveTimerTimeOut()
  178. {
  179. m_ObserveTimer = null;
  180. int baseS = GetCurrentBaseScore(false);
  181. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.outTimePunitiveMul) + Random.Range(-2, 2);
  182. IncreaseScore(missPunitiveS * -1);
  183. DestroyShootTarget(curTarget);
  184. }
  185. private Vector3 RandomPosition()
  186. {
  187. Vector2 minOffset = Vector2.zero;
  188. Vector2 maxOffset = Vector2.zero;
  189. switch (m_DifficultyData.difficultyType)
  190. {
  191. case DifficultyType.Standard:
  192. minOffset = new Vector2(0.2f, 0f);
  193. maxOffset = new Vector2(0.8f, 0f);
  194. break;
  195. case DifficultyType.Advance:
  196. minOffset = new Vector2(0.2f, 0.15f);
  197. maxOffset = new Vector2(0.8f, 0.4f);
  198. break;
  199. case DifficultyType.Professional:
  200. minOffset = new Vector2(0.2f, 0.15f);
  201. maxOffset = new Vector2(0.8f, 0.6f);
  202. break;
  203. }
  204. Vector2 widthLimt = Vector2.zero;
  205. Vector2 heightLimt = Vector2.zero;
  206. switch (m_DifficultyType)
  207. {
  208. case DifficultyType.Standard:
  209. widthLimt = new Vector2(-3.5f, 3.5f);
  210. heightLimt = new Vector2(-1.8f, -1.8f);
  211. break;
  212. case DifficultyType.Advance:
  213. widthLimt = new Vector2(-5, 5);
  214. heightLimt = new Vector2(-1.8f, -1f);
  215. break;
  216. case DifficultyType.Professional:
  217. widthLimt = new Vector2(-8, 8);
  218. heightLimt = new Vector2(-1.8f, 0f);
  219. break;
  220. }
  221. Vector3 min = ExtraTool.GetScreenOffsetPosition(minOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  222. Vector3 max = ExtraTool.GetScreenOffsetPosition(maxOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  223. return ExtraTool.RandomPosition(min, max);
  224. }
  225. private void DestroyShootTarget(HumanoidShotTarget target)
  226. {
  227. if (target != null)
  228. {
  229. GameObject.Destroy(target.gameObject);
  230. }
  231. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, 3f);
  232. }
  233. private void ResetTarget()
  234. {
  235. if (curTarget != null)
  236. {
  237. GameObject.Destroy(curTarget.gameObject);
  238. curTarget = null;
  239. }
  240. }
  241. private void UpdatePrecision()
  242. {
  243. double totalShot = TotalHitNum + MissHitNum;
  244. double precision = 0;
  245. if (totalShot != 0)
  246. {
  247. precision = Math.Round(TotalHitNum / totalShot, 2);
  248. }
  249. SetResultMetricsDataValue(MetricsType.Precision, precision);
  250. }
  251. }
  252. }