HorizontalMotionTargetTrackTrainHandle.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 HorizontalMotionTargetTrackTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private HorizontalMotionTargetTrackTrainDifficultyData m_DifficultyData;
  16. private HorizontalMotionTargetTrackTrainScoreData m_ScoreData;
  17. private bool firstScore;
  18. private double punitiveTime;
  19. private double m_TotalHoverTime;
  20. private double TotalHoverTime
  21. {
  22. get { return m_TotalHoverTime; }
  23. set
  24. {
  25. m_TotalHoverTime = value;
  26. UpdatePrecision();
  27. }
  28. }
  29. private double m_TotalMissTime;
  30. private double TotalMissTime
  31. {
  32. get { return m_TotalMissTime; }
  33. set
  34. {
  35. m_TotalMissTime = value;
  36. UpdatePrecision();
  37. }
  38. }
  39. public HorizontalMotionTargetTrackTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  40. {
  41. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnHovering, OnHoveringTargetEvent);
  42. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnNotEntering, OnNotHoveringEvent);
  43. m_DifficultyData = info.GetDifficultyData<HorizontalMotionTargetTrackTrainDifficultyData>(m_DifficultyType);
  44. m_ScoreData = info.GetScoreData<HorizontalMotionTargetTrackTrainScoreData>(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> pos = new List<Vector3>();
  52. float distance = Random.Range(5f, 20f);
  53. Vector2 widthLimt = new Vector2(-8, 8);
  54. Vector2 heightLimt = new Vector2(-1.5f, 5f);
  55. pos.Add(ExtraTool.GetScreenOffsetPosition(new Vector2(0.5f - (m_DifficultyData.horizontalMotionDistance / 2), 0.5f), distance, widthLimt, heightLimt));
  56. pos.Add(ExtraTool.GetScreenOffsetPosition(new Vector2(0.5f + (m_DifficultyData.horizontalMotionDistance / 2), 0.5f), distance, widthLimt, heightLimt));
  57. if (ExtraTool.IsProbabolityInclusive(0.5f))
  58. {
  59. Vector3 temp = pos[0];
  60. pos[0] = pos[1];
  61. pos[1] = temp;
  62. }
  63. curTarget.onClickCanChangeColor = false;
  64. curTarget.onHoveringCanChangeColor = true;
  65. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  66. curTarget.onHoveringColor = Color.green;
  67. curTarget.Init(2f, ReduceHealthTriggerType.OnHovering, true, ReduceHealthType.Time, MotionType.Foreach, ExtraTool.GetScreenOffsetPosition(new Vector2(0.5f, 0.5f), distance), pos, 2f);
  68. firstScore = false;
  69. }
  70. private void DestroyShootTarget(SphereShotTarget target)
  71. {
  72. if (target != null)
  73. {
  74. target.IsRunning = false;
  75. GameObject.Destroy(target.gameObject);
  76. }
  77. }
  78. private void ResetTarget()
  79. {
  80. if (curTarget != null)
  81. {
  82. GameObject.Destroy(curTarget.gameObject);
  83. curTarget = null;
  84. }
  85. }
  86. #region Train
  87. protected override void ResetTrain()
  88. {
  89. ResetTarget();
  90. punitiveTime = 0;
  91. firstScore = false;
  92. m_TotalHoverTime = 0;
  93. m_TotalMissTime = 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.Precision, Math.Round(TotalHoverTime / (TotalHoverTime + TotalMissTime), 2));
  113. SetResultMetricsDataValue(MetricsType.AverageTimeOnTarget, Math.Round(TotalHoverTime, 2));
  114. }
  115. #endregion
  116. private void UpdatePrecision()
  117. {
  118. SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHoverTime / (TotalHoverTime + TotalMissTime), 2));
  119. }
  120. #region ShotTarget
  121. private void OnHoveringTargetEvent(BaseShotTarget target)
  122. {
  123. if (target != null)
  124. {
  125. SphereShotTarget t = target as SphereShotTarget;
  126. if (t != null)
  127. {
  128. if (t.CurHealth <= 0)
  129. {
  130. int comboS = Mathf.CeilToInt(m_ScoreData.secondBaseScore * comboNums * m_ScoreData.comboMul);
  131. IncreaseScore(m_ScoreData.secondBaseScore + comboS);
  132. DestroyShootTarget(t);
  133. GenerateShotTargets();
  134. comboNums++;
  135. }
  136. else if (t.CurHealth <= 1)
  137. {
  138. if (!firstScore)
  139. {
  140. firstScore = true;
  141. int comboS = Mathf.CeilToInt(m_ScoreData.firstBaseScore * comboNums * m_ScoreData.comboMul);
  142. IncreaseScore(m_ScoreData.firstBaseScore + comboS);
  143. }
  144. }
  145. }
  146. TotalHoverTime += Time.deltaTime;
  147. }
  148. }
  149. private void OnNotHoveringEvent(BaseShotTarget target)
  150. {
  151. punitiveTime += Time.deltaTime;
  152. if (punitiveTime >= 1)
  153. {
  154. comboNums = 0;
  155. punitiveTime = 0;
  156. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.firstBaseScore * m_ScoreData.missPunitiveMul);
  157. IncreaseScore(missPunitiveS * -1);
  158. }
  159. TotalMissTime += Time.deltaTime;
  160. }
  161. #endregion
  162. }
  163. }