MozambiqueTrainHandle.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. double timeToKill = 999;
  91. double shotsPerKill = 999;
  92. if (totalKillNums != 0)
  93. {
  94. timeToKill = Math.Round(TrainInfo.duration / totalKillNums, 2);
  95. shotsPerKill = Math.Round((TotalHitNum + MissHitNum) / totalKillNums, 2);
  96. }
  97. SetResultMetricsDataValue(MetricsType.VisualMemory, mozambiqueKillNums);
  98. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(totalKillNums / TrainInfo.duration, 2));
  99. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNums);
  100. SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill);
  101. SetResultMetricsDataValue(MetricsType.ShotsPerKill, shotsPerKill);
  102. }
  103. #endregion
  104. #region Target
  105. private void OnShottedTargetEvent(BaseShotTarget target)
  106. {
  107. int baseS = GetCurrentBaseScore(target != null);
  108. if (target != null)
  109. {
  110. HumanoidPartShotTarget part = target as HumanoidPartShotTarget;
  111. MozambiqueHumanoidShotTarget dependTarget = part.DependTarget as MozambiqueHumanoidShotTarget;
  112. if (dependTarget.Health <= 0)
  113. {
  114. totalKillNums++;
  115. if(IsMozambiqueShotOrder(dependTarget.m_CauseHumanoidPartReduceHPOrder))
  116. {
  117. mozambiqueKillNums++;
  118. }
  119. TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer);
  120. m_ObserveTimer = null;
  121. DestroyShootTarget(dependTarget);
  122. }
  123. TotalHitNum++;
  124. if (!dependTarget.isCurFalseHit)
  125. {
  126. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  127. IncreaseScore(baseS + bestS);
  128. }
  129. else
  130. {
  131. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.falsePunitiveMul) + Random.Range(-2, 2);
  132. IncreaseScore(missPunitiveS * -1);
  133. }
  134. StartBestHitTimer();
  135. }
  136. else
  137. {
  138. MissHitNum++;
  139. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul) + Random.Range(-3, 3);
  140. IncreaseScore(missPunitiveS * -1);
  141. }
  142. }
  143. #endregion
  144. public bool IsMozambiqueShotOrder(List<HumanoidPartType> order)
  145. {
  146. if (order == null) return false;
  147. if (order.Count != 3) return false;
  148. return order[0] == HumanoidPartType.Body && order[1] == HumanoidPartType.Body && order[2] == HumanoidPartType.Head;
  149. }
  150. private int GetCurrentBaseScore(bool isHit)
  151. {
  152. int baseS = 0;
  153. switch (isHit? curTarget.Health: curTarget.Health - 1)
  154. {
  155. case 2:
  156. baseS = m_ScoreData.firstBaseScore;
  157. break;
  158. case 1:
  159. baseS = m_ScoreData.secondBaseScore;
  160. break;
  161. case 0:
  162. baseS = m_ScoreData.thirdBaseScore;
  163. break;
  164. default:
  165. baseS = m_ScoreData.firstBaseScore;
  166. break;
  167. }
  168. return baseS;
  169. }
  170. private void GenerateShotTarget()
  171. {
  172. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/MozambiqueHumanoidShotTarget"));
  173. obj.transform.rotation = Quaternion.Euler(0, 180, 0);
  174. curTarget = obj.GetComponent<MozambiqueHumanoidShotTarget>();
  175. curTarget.InitHumanoidShotTarget(3, RandomPosition());
  176. curTarget.InitMozambiqueHumanoidShotTarget();
  177. m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 5);
  178. StartBestHitTimer();
  179. }
  180. private void IntervalTimerTimeOut()
  181. {
  182. GenerateShotTarget();
  183. }
  184. private void ObserveTimerTimeOut()
  185. {
  186. m_ObserveTimer = null;
  187. int baseS = GetCurrentBaseScore(false);
  188. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.outTimePunitiveMul) + Random.Range(-2, 2);
  189. IncreaseScore(missPunitiveS * -1);
  190. DestroyShootTarget(curTarget);
  191. }
  192. private Vector3 RandomPosition()
  193. {
  194. Vector2 minOffset = Vector2.zero;
  195. Vector2 maxOffset = Vector2.zero;
  196. switch (m_DifficultyData.difficultyType)
  197. {
  198. case DifficultyType.Standard:
  199. minOffset = new Vector2(0.2f, 0f);
  200. maxOffset = new Vector2(0.8f, 0f);
  201. break;
  202. case DifficultyType.Advance:
  203. minOffset = new Vector2(0.2f, 0.15f);
  204. maxOffset = new Vector2(0.8f, 0.4f);
  205. break;
  206. case DifficultyType.Professional:
  207. minOffset = new Vector2(0.2f, 0.15f);
  208. maxOffset = new Vector2(0.8f, 0.6f);
  209. break;
  210. }
  211. Vector2 widthLimt = Vector2.zero;
  212. Vector2 heightLimt = Vector2.zero;
  213. switch (m_DifficultyType)
  214. {
  215. case DifficultyType.Standard:
  216. widthLimt = new Vector2(-3.5f, 3.5f);
  217. heightLimt = new Vector2(-1.8f, -1.8f);
  218. break;
  219. case DifficultyType.Advance:
  220. widthLimt = new Vector2(-5, 5);
  221. heightLimt = new Vector2(-1.8f, -1f);
  222. break;
  223. case DifficultyType.Professional:
  224. widthLimt = new Vector2(-8, 8);
  225. heightLimt = new Vector2(-1.8f, 0f);
  226. break;
  227. }
  228. Vector3 min = ExtraTool.GetScreenOffsetPosition(minOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  229. Vector3 max = ExtraTool.GetScreenOffsetPosition(maxOffset, m_DifficultyData.distance, widthLimt, heightLimt);
  230. return ExtraTool.RandomPosition(min, max);
  231. }
  232. private void DestroyShootTarget(HumanoidShotTarget target)
  233. {
  234. if (target != null)
  235. {
  236. GameObject.Destroy(target.gameObject);
  237. }
  238. m_IntervalTimer = TimerSystem.GetInstance().AddTimeTask(IntervalTimerTimeOut, 3f);
  239. }
  240. private void ResetTarget()
  241. {
  242. if (curTarget != null)
  243. {
  244. GameObject.Destroy(curTarget.gameObject);
  245. curTarget = null;
  246. }
  247. }
  248. private void UpdatePrecision()
  249. {
  250. double totalShot = TotalHitNum + MissHitNum;
  251. double precision = 0;
  252. if (totalShot != 0)
  253. {
  254. precision = Math.Round(TotalHitNum / totalShot, 2);
  255. }
  256. SetResultMetricsDataValue(MetricsType.Precision, precision);
  257. }
  258. }
  259. }