TrackBallTrainHandle.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 TrackBallTrainHandle : BaseTrainHandle
  13. {
  14. private SphereShotTarget curTarget;
  15. private TrackBallTrainDifficultyData m_DifficultyData;
  16. private TrackBallTrainScoreData m_ScoreData;
  17. private double scoreTime;
  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 TrackBallTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType)
  40. {
  41. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnHovering, OnHoveringTargetEvent);
  42. SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnNotEntering, OnNotEnteringEvent);
  43. m_DifficultyData = info.GetDifficultyData<TrackBallTrainDifficultyData>(m_DifficultyType);
  44. m_ScoreData = info.GetScoreData<TrackBallTrainScoreData>(m_DifficultyType);
  45. }
  46. private void GenerateShootingTargets()
  47. {
  48. GameObject obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/SphereTarget"));
  49. curTarget = obj.GetComponent<SphereShotTarget>();
  50. List<Vector3> pos = new List<Vector3>();
  51. for (int i = 0; i < 20; i++)
  52. {
  53. pos.Add(RandomPosition());
  54. }
  55. curTarget.onClickCanChangeColor = false;
  56. curTarget.onHoveringCanChangeColor = true;
  57. curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor();
  58. curTarget.onHoveringColor = Color.red;
  59. curTarget.Init(1, ReduceHealthTriggerType.None, false, ReduceHealthType.Per, MotionType.Foreach, new Vector3(0, 0, m_DifficultyData.targetDistance), pos, m_DifficultyData.speed);
  60. curTarget.IsRunning = true;
  61. }
  62. private Vector3 RandomPosition()
  63. {
  64. Vector2 widthLimt = Vector2.zero;
  65. Vector2 heightLimt = Vector2.zero;
  66. switch (m_DifficultyType)
  67. {
  68. case DifficultyType.Standard:
  69. widthLimt = new Vector2(-4, 4);
  70. heightLimt = new Vector2(-1.5f, 1.5f);
  71. break;
  72. case DifficultyType.Advance:
  73. widthLimt = new Vector2(-8, 8);
  74. heightLimt = new Vector2(-1.5f, 3.5f);
  75. break;
  76. case DifficultyType.Professional:
  77. widthLimt = new Vector2(-8, 8);
  78. heightLimt = new Vector2(-1.5f, 5f);
  79. break;
  80. }
  81. Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  82. Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, m_DifficultyData.targetDistance, widthLimt, heightLimt);
  83. return ExtraTool.RandomPosition(min, max);
  84. }
  85. private void ResetTarget()
  86. {
  87. if (curTarget != null)
  88. {
  89. curTarget.IsRunning = false;
  90. GameObject.Destroy(curTarget.gameObject);
  91. curTarget = null;
  92. }
  93. }
  94. #region Train
  95. protected override void ResetTrain()
  96. {
  97. ResetTarget();
  98. scoreTime = 0;
  99. punitiveTime = 0;
  100. m_TotalHoverTime = 0;
  101. m_TotalMissTime = 0;
  102. }
  103. public override void StartTrain()
  104. {
  105. base.StartTrain();
  106. GenerateShootingTargets();
  107. }
  108. public override void PauseTrain()
  109. {
  110. base.PauseTrain();
  111. curTarget.IsRunning = false;
  112. }
  113. public override void ContinueTrain()
  114. {
  115. base.ContinueTrain();
  116. curTarget.IsRunning = true;
  117. }
  118. public override void CalculateTrainResult()
  119. {
  120. SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHoverTime / (TotalHoverTime + TotalMissTime), 2));
  121. SetResultMetricsDataValue(MetricsType.AverageTimeOnTarget, Math.Round(TotalHoverTime, 2));
  122. }
  123. #endregion
  124. private void UpdatePrecision()
  125. {
  126. SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHoverTime / (TotalHoverTime + TotalMissTime), 2));
  127. }
  128. #region ShotTarget
  129. private void OnHoveringTargetEvent(BaseShotTarget target)
  130. {
  131. scoreTime += Time.deltaTime;
  132. if (scoreTime >= 1)
  133. {
  134. int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * (comboNums / 5) * m_ScoreData.comboMul);
  135. IncreaseScore(m_ScoreData.baseScore + comboS);
  136. comboNums++;
  137. scoreTime = 0;
  138. }
  139. TotalHoverTime += Time.deltaTime;
  140. }
  141. private void OnNotEnteringEvent(BaseShotTarget target)
  142. {
  143. punitiveTime += Time.deltaTime;
  144. if (punitiveTime >= 1)
  145. {
  146. comboNums = 0;
  147. punitiveTime = 0;
  148. int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul);
  149. IncreaseScore(missPunitiveS * -1);
  150. }
  151. TotalMissTime += Time.deltaTime;
  152. }
  153. #endregion
  154. }
  155. }