HorizontalMotionShotTrainHandle.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 HorizontalMotionShotTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private HorizontalMotionShotTrainDifficultyData m_DifficultyData;
  16. private HorizontalMotionShotTrainScoreData m_ScoreData;
  17. private int baseS = 0;
  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 totalKillNum;
  39. private double totalTargetNum;
  40. public HorizontalMotionShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  41. {
  42. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnOnClickTargetEvent);
  43. m_DifficultyData = info.GetDifficultyData<HorizontalMotionShotTrainDifficultyData>(m_DifficultyType);
  44. m_ScoreData = info.GetScoreData<HorizontalMotionShotTrainScoreData>(m_DifficultyType);
  45. }
  46. private void GenerateShotTargets()
  47. {
  48. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  49. curTarget = obj.GetComponent<SphereShotTarget>();
  50. curTarget.IsRunning = true;
  51. List<Vector3> randomPos = new List<Vector3>();
  52. float randomY = Random.Range(ExtraTool.Sphere_Diameter1_ScreenOffsetY, 1 - ExtraTool.Sphere_Diameter1_ScreenOffsetY);
  53. for (int i = 0; i < 20; i++)
  54. {
  55. float randomX = Random.Range(ExtraTool.Sphere_Diameter1_ScreenOffsetX, 1 - ExtraTool.Sphere_Diameter1_ScreenOffsetX);
  56. Vector2 widthLimt = new Vector2(-8, 8);
  57. Vector2 heightLimt = new Vector2(-1.5f, 3.5f);
  58. randomPos.Add(ExtraTool.GetWorldPositionWithResolutionPercent(randomX, randomY, 10, widthLimt, heightLimt));
  59. }
  60. curTarget.onClickCanChangeColor = false;
  61. curTarget.onHoveringCanChangeColor = false;
  62. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  63. curTarget.onHoveringColor = Color.green;
  64. curTarget.Init(3, ReduceHealthTriggerType.OnClick, true, ReduceHealthType.Per, MotionType.Foreach, randomPos[0], randomPos, m_DifficultyData.motionSpeed);
  65. totalTargetNum++;
  66. baseS = m_ScoreData.firstBaseScore;
  67. StartBestHitTimer();
  68. }
  69. private void DestroyShootTarget(SphereShotTarget target)
  70. {
  71. if (target != null)
  72. {
  73. target.IsRunning = false;
  74. GameObject.Destroy(target.gameObject);
  75. }
  76. }
  77. private void ResetTarget()
  78. {
  79. if (curTarget != null)
  80. {
  81. GameObject.Destroy(curTarget.gameObject);
  82. curTarget = null;
  83. }
  84. }
  85. #region Train
  86. protected override void ResetTrain()
  87. {
  88. ResetTarget();
  89. baseS = 0;
  90. m_TotalHitNum = 0;
  91. m_MissHitNum = 0;
  92. totalKillNum = 0;
  93. totalTargetNum = 0;
  94. }
  95. public override void StartTrain()
  96. {
  97. base.StartTrain();
  98. GenerateShotTargets();
  99. }
  100. public override void PauseTrain()
  101. {
  102. base.PauseTrain();
  103. curTarget.IsRunning = false;
  104. }
  105. public override void ContinueTrain()
  106. {
  107. base.ContinueTrain();
  108. curTarget.IsRunning = true;
  109. }
  110. public override void CalculateTrainResult()
  111. {
  112. SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(totalKillNum / TrainInfo.duration, 2));
  113. SetResultMetricsDataValue(MetricsType.Target, totalTargetNum);
  114. SetResultMetricsDataValue(MetricsType.TimeToKill, Math.Round(TrainInfo.duration / totalKillNum, 2));
  115. SetResultMetricsDataValue(MetricsType.ShotsPerKill, Math.Round((TotalHitNum + MissHitNum) / totalKillNum, 2));
  116. }
  117. #endregion
  118. #region TrainResult
  119. private void UpdatePrecision()
  120. {
  121. double totalShot = TotalHitNum + MissHitNum;
  122. double precision = 0;
  123. if (totalShot != 0)
  124. {
  125. precision = Math.Round(TotalHitNum / totalShot, 2);
  126. }
  127. SetResultMetricsDataValue(MetricsType.Precision, precision);
  128. }
  129. #endregion
  130. #region ShotTarget
  131. private void OnOnClickTargetEvent(BaseShotTarget target)
  132. {
  133. if (target != null)
  134. {
  135. SphereShotTarget t = target as SphereShotTarget;
  136. switch (t.CurHealth)
  137. {
  138. case 0:
  139. baseS = m_ScoreData.thirdBaseScore;
  140. break;
  141. case 1:
  142. baseS = m_ScoreData.secondBaseScore;
  143. break;
  144. case 2:
  145. baseS = m_ScoreData.firstBaseScore;
  146. break;
  147. }
  148. int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul);
  149. int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0;
  150. IncreaseScore(baseS + comboS + bestS);
  151. comboNums++;
  152. if (t.CurHealth <= 0)
  153. {
  154. DestroyShootTarget(t);
  155. GenerateShotTargets();
  156. totalKillNum++;
  157. }
  158. TotalHitNum++;
  159. StartBestHitTimer();
  160. }
  161. else
  162. {
  163. MissHitNum++;
  164. comboNums = 0;
  165. int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul) + Random.Range(-2, 2);
  166. IncreaseScore(missPunitiveS * -1);
  167. }
  168. }
  169. #endregion
  170. }
  171. }