MotionTargetShotTrainHandle.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 MotionTargetShotTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private MotionTargetShotTrainDifficultyData m_DifficultyData;
  16. private MotionTargetShotTrainScoreData m_ScoreData;
  17. private List<Vector2> m_CurBrokenLineOffset;
  18. private List<Vector2> m_StandardBrokenLineOffset = new List<Vector2>()
  19. {
  20. new Vector2(0.35f,0.25f),
  21. new Vector2(0.4f,0.75f),
  22. new Vector2(0.45f,0.25f),
  23. new Vector2(0.5f,0.75f),
  24. new Vector2(0.55f,0.25f),
  25. new Vector2(0.6f,0.75f),
  26. new Vector2(0.65f,0.25f)
  27. };
  28. private List<Vector2> m_AdvanceBrokenLineOffset = new List<Vector2>()
  29. {
  30. new Vector2(0.25f,0.25f),
  31. new Vector2(0.3f,0.75f),
  32. new Vector2(0.35f,0.25f),
  33. new Vector2(0.4f,0.75f),
  34. new Vector2(0.45f,0.25f),
  35. new Vector2(0.5f,0.75f),
  36. new Vector2(0.55f,0.25f),
  37. new Vector2(0.6f,0.75f),
  38. new Vector2(0.65f,0.25f),
  39. new Vector2(0.7f,0.75f),
  40. new Vector2(0.75f,0.25f)
  41. };
  42. private List<Vector2> m_ProfessionalBrokenLineOffset = new List<Vector2>()
  43. {
  44. new Vector2(0.3f,0.75f),
  45. new Vector2(0.35f,0.25f),
  46. new Vector2(0.4f,0.75f),
  47. new Vector2(0.45f,0.25f),
  48. new Vector2(0.5f,0.75f),
  49. new Vector2(0.55f,0.25f),
  50. new Vector2(0.6f,0.75f),
  51. new Vector2(0.65f,0.25f),
  52. new Vector2(0.7f,0.75f)
  53. };
  54. private int roundIndex;
  55. private double m_TotalHitNum;
  56. private double TotalHitNum
  57. {
  58. get { return m_TotalHitNum; }
  59. set
  60. {
  61. m_TotalHitNum = value;
  62. UpdatePrecision();
  63. }
  64. }
  65. private double m_MissHitNum;
  66. private double MissHitNum
  67. {
  68. get { return m_MissHitNum; }
  69. set
  70. {
  71. m_MissHitNum = value;
  72. UpdatePrecision();
  73. }
  74. }
  75. private double totalKillNums;
  76. private double totalTargetNums;
  77. public MotionTargetShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  78. {
  79. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnClickTargetEvent);
  80. m_DifficultyData = info.GetDifficultyData<MotionTargetShotTrainDifficultyData>(m_DifficultyType);
  81. m_ScoreData = info.GetScoreData<MotionTargetShotTrainScoreData>(m_DifficultyType);
  82. }
  83. private void GenerateShotTargets()
  84. {
  85. roundIndex++;
  86. int round = roundIndex % 2;
  87. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  88. curTarget = obj.GetComponent<SphereShotTarget>();
  89. curTarget.onClickCanChangeColor = false;
  90. curTarget.onHoveringCanChangeColor = false;
  91. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  92. curTarget.onHoveringColor = Color.red;
  93. MotionType motionType = MotionType.None;
  94. List<Vector3> motionPos = new List<Vector3>();
  95. Vector3 origin = new Vector3();
  96. if (round == 1)
  97. {
  98. motionType = MotionType.None;
  99. origin = new Vector3(0, 0, m_DifficultyData.targetDistance);
  100. }
  101. else
  102. {
  103. motionType = MotionType.PingPong;
  104. //switch (m_DifficultyType)
  105. //{
  106. // case DifficultyType.Standard:
  107. // m_CurBrokenLineOffset = m_StandardBrokenLineOffset;
  108. // break;
  109. // case DifficultyType.Advance:
  110. // m_CurBrokenLineOffset = m_AdvanceBrokenLineOffset;
  111. // break;
  112. // case DifficultyType.Professional:
  113. // m_CurBrokenLineOffset = m_ProfessionalBrokenLineOffset;
  114. // break;
  115. //}
  116. //for (int i = 0; i < m_CurBrokenLineOffset.Count; i++)
  117. //{
  118. // motionPos.Add(RandomPosition(i));
  119. //}
  120. for (int i = 0; i < 20; i++)
  121. {
  122. motionPos.Add(RandomPosition());
  123. }
  124. curTarget.curTargetPosIndex = Random.Range(0, motionPos.Count);
  125. origin = motionPos[curTarget.curTargetPosIndex];
  126. }
  127. curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, motionType, origin, motionPos, m_DifficultyData.speed);
  128. curTarget.IsRunning = true;
  129. totalTargetNums++;
  130. StartBestHitTimer();
  131. }
  132. private Vector3 RandomPosition(int index)
  133. {
  134. Vector2 widthLimt = Vector2.zero;
  135. Vector2 heightLimt = Vector2.zero;
  136. switch (m_DifficultyType)
  137. {
  138. case DifficultyType.Standard:
  139. widthLimt = new Vector2(-4, 4);
  140. heightLimt = new Vector2(-1.5f, 1.5f);
  141. break;
  142. case DifficultyType.Advance:
  143. widthLimt = new Vector2(-8, 8);
  144. heightLimt = new Vector2(-1.5f, 3.5f);
  145. break;
  146. case DifficultyType.Professional:
  147. widthLimt = new Vector2(-8, 8);
  148. heightLimt = new Vector2(-1.5f, 5f);
  149. break;
  150. }
  151. return ExtraTool.GetScreenOffsetPosition(m_CurBrokenLineOffset[index], m_DifficultyData.targetDistance, widthLimt, heightLimt);
  152. }
  153. private void ResetTarget()
  154. {
  155. if (curTarget != null)
  156. {
  157. curTarget.IsRunning = false;
  158. GameObject.Destroy(curTarget.gameObject);
  159. curTarget = null;
  160. }
  161. }
  162. private void DestroyShootTarget(SphereShotTarget target)
  163. {
  164. if (target != null)
  165. {
  166. target.IsRunning = false;
  167. GameObject.Destroy(target.gameObject);
  168. }
  169. }
  170. private Vector3 RandomPosition()
  171. {
  172. Vector2 widthLimt = Vector2.zero;
  173. Vector2 heightLimt = Vector2.zero;
  174. switch (m_DifficultyType)
  175. {
  176. case DifficultyType.Standard:
  177. widthLimt = new Vector2(-4, 4);
  178. heightLimt = new Vector2(-1.5f, 1.5f);
  179. break;
  180. case DifficultyType.Advance:
  181. widthLimt = new Vector2(-8, 8);
  182. heightLimt = new Vector2(-1.5f, 3.5f);
  183. break;
  184. case DifficultyType.Professional:
  185. widthLimt = new Vector2(-8, 8);
  186. heightLimt = new Vector2(-1.5f, 5f);
  187. break;
  188. }
  189. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  190. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  191. return ExtraTool.RandomPosition(min, max);
  192. }
  193. #region TrainResult
  194. private void UpdatePrecision()
  195. {
  196. double totalShot = TotalHitNum + MissHitNum;
  197. double precision = 0;
  198. if (totalShot != 0)
  199. {
  200. precision = Math.Round(TotalHitNum / totalShot, 2);
  201. }
  202. SetResultMetricsDataValue(MetricsType.Precision, precision);
  203. }
  204. #endregion
  205. #region Train
  206. protected override void ResetTrain()
  207. {
  208. ResetTarget();
  209. roundIndex = 0;
  210. m_TotalHitNum = 0;
  211. m_MissHitNum = 0;
  212. totalKillNums = 0;
  213. totalTargetNums = 0;
  214. }
  215. public override void StartTrain()
  216. {
  217. base.StartTrain();
  218. GenerateShotTargets();
  219. }
  220. public override void PauseTrain()
  221. {
  222. base.PauseTrain();
  223. curTarget.IsRunning = false;
  224. }
  225. public override void ContinueTrain()
  226. {
  227. base.ContinueTrain();
  228. curTarget.IsRunning = true;
  229. }
  230. public override void CalculateTrainResult()
  231. {
  232. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(totalKillNums / TrainInfo.duration, 2));
  233. SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNums);
  234. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / totalKillNums, 2));
  235. SetResultMetricsDataValue(MetricsType.Target, totalTargetNums);
  236. }
  237. #endregion
  238. #region ShotTarget
  239. private void OnClickTargetEvent(BaseShotTarget target)
  240. {
  241. if (target != null)
  242. {
  243. SphereShotTarget t = target as SphereShotTarget;
  244. if (t.CurHealth <= 0)
  245. {
  246. totalKillNums++;
  247. DestroyShootTarget(t);
  248. GenerateShotTargets();
  249. }
  250. int baseS = 0;
  251. switch(roundIndex % 2)
  252. {
  253. case 0:
  254. baseS = m_ScoreData.staticBaseScore;
  255. break;
  256. case 1:
  257. baseS = m_ScoreData.motionBaseScore;
  258. break;
  259. }
  260. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  261. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  262. IncreaseScore(baseS + comboS + bestS);
  263. TotalHitNum++;
  264. comboNums++;
  265. }
  266. else
  267. {
  268. MissHitNum++;
  269. comboNums = 0;
  270. int baseS = 0;
  271. switch (roundIndex % 2)
  272. {
  273. case 0:
  274. baseS = m_ScoreData.staticBaseScore;
  275. break;
  276. case 1:
  277. baseS = m_ScoreData.motionBaseScore;
  278. break;
  279. }
  280. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul);
  281. IncreaseScore(missPunitiveS * -1);
  282. }
  283. }
  284. #endregion
  285. }
  286. }