using ShotSimulator.Screen; using ShotSimulator.Target; using ShotSimulator.Tool; using ShotSimulator.Train.Info; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Random = UnityEngine.Random; namespace ShotSimulator.Train { public class SwitchTargetTrackTrainHandle : BaseTrainHandle { private int maxTargetNum = 3; private List curTargets = new List(); private List randomYD1 = new List() { -1.5f, -0.25f, 1f }; private List randomYD2 = new List() { -1.5f, 0.5f, 2.5f }; private List randomYD3 = new List() { -1.5f, 1.25f, 4f }; private List selectedY = new List(); private List unSelectedY = new List(); private SwitchTargetTrackDifficultyData m_DifficultyData; private SwitchTargetTrackTrainScoreData m_ScoreData; private List firstTargets = new List(); private double punitiveTime; private double m_TotalHoverTime; private double TotalHoverTime { get { return m_TotalHoverTime; } set { m_TotalHoverTime = value; UpdatePrecision(); } } private double m_TotalMissTime; private double TotalMissTime { get { return m_TotalMissTime; } set { m_TotalMissTime = value; UpdatePrecision(); } } public SwitchTargetTrackTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType) { SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnHovering, OnHoveringTargetEvent); SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnNotEntering, OnNotHoveringEvent); m_DifficultyData = info.GetDifficultyData(m_DifficultyType); m_ScoreData = info.GetScoreData(m_DifficultyType); } private void InitGenerateShotTargets() { for (int i = 0; i < maxTargetNum; i++) { GenerateShotTarget(); } } private void GenerateShotTarget() { GameObject obj = GameObject.Instantiate(Resources.Load("Prefabs/SphereTarget")); SphereShotTarget target = obj.GetComponent(); float randomY = RandomY(); List pos = new List(); Vector2 widthLimt = new Vector2(-8, 8); Vector2 heightLimt = new Vector2(-1.5f, 5f); Vector3 leftPos = ExtraTool.GetScreenOffsetPosition(new Vector2(0.25f, 0.5f), m_DifficultyData.targetDistance, widthLimt, heightLimt); leftPos.y = randomY; Vector3 rightPos = ExtraTool.GetScreenOffsetPosition(new Vector2(0.75f, 0.5f), m_DifficultyData.targetDistance, widthLimt, heightLimt); rightPos.y = randomY; Vector3 originPos = ExtraTool.GetScreenOffsetPosition(new Vector2(Random.Range(0.25f, 0.75f), 0.5f), m_DifficultyData.targetDistance, widthLimt, heightLimt); originPos.y = randomY; pos.Add(leftPos); pos.Add(rightPos); target.onClickCanChangeColor = false; target.onHoveringCanChangeColor = true; target.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor(); target.onHoveringColor = Color.green; target.Init(2f, ReduceHealthTriggerType.OnHovering, true, ReduceHealthType.Time, MotionType.Foreach, originPos, pos, m_DifficultyData.motionSpeed); target.IsRunning = true; curTargets.Add(target); } private float RandomY() { int yIndex = Random.Range(0, unSelectedY.Count); float y = unSelectedY[yIndex]; unSelectedY.RemoveAt(yIndex); selectedY.Add(y); return y; } private void DestroyShootingTarget(SphereShotTarget target) { if (target != null) { selectedY.Remove(target.transform.position.y); unSelectedY.Add(target.transform.position.y); curTargets.Remove(target); target.IsRunning = false; GameObject.Destroy(target.gameObject); } } private void ResetTarget() { for (int i = 0; i < curTargets.Count; i++) { curTargets[i].IsRunning = false; GameObject.Destroy(curTargets[i].gameObject); } curTargets.Clear(); } private void SetCurTargetsRunning(bool isRunning) { for (int i = 0; i < curTargets.Count; i++) { curTargets[i].IsRunning = isRunning; } } private void UpdatePrecision() { SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHoverTime / (TotalHoverTime + TotalMissTime), 2)); } #region Train protected override void ResetTrain() { firstTargets.Clear(); ResetTarget(); punitiveTime = 0; selectedY.Clear(); unSelectedY.Clear(); m_TotalHoverTime = 0; m_TotalMissTime = 0; } public override void StartTrain() { base.StartTrain(); switch (m_DifficultyData.difficultyType) { case DifficultyType.Standard: unSelectedY.AddRange(randomYD1); break; case DifficultyType.Advance: unSelectedY.AddRange(randomYD2); break; case DifficultyType.Professional: unSelectedY.AddRange(randomYD3); break; } InitGenerateShotTargets(); } public override void PauseTrain() { base.PauseTrain(); SetCurTargetsRunning(false); } public override void ContinueTrain() { base.ContinueTrain(); SetCurTargetsRunning(true); } public override void CalculateTrainResult() { SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHoverTime / (TotalHoverTime + TotalMissTime), 2)); SetResultMetricsDataValue(MetricsType.AverageTimeOnTarget, Math.Round(TotalHoverTime, 2)); } #endregion #region ShotTarget private void OnHoveringTargetEvent(BaseShotTarget target) { if (target != null) { SphereShotTarget t = target as SphereShotTarget; if (t.CurHealth <= 0) { int comboS = Mathf.CeilToInt(m_ScoreData.secondBaseScore * comboNums * m_ScoreData.comboMul); IncreaseScore(m_ScoreData.secondBaseScore + comboS); firstTargets.Remove(t); DestroyShootingTarget(t); GenerateShotTarget(); comboNums++; } else if (t.CurHealth <= 1) { if (!firstTargets.Contains(t)) { firstTargets.Add(t); int comboS = Mathf.CeilToInt(m_ScoreData.firstBaseScore * comboNums * m_ScoreData.comboMul); IncreaseScore(m_ScoreData.firstBaseScore + comboS); } } TotalHoverTime += Time.deltaTime; } } private void OnNotHoveringEvent(BaseShotTarget target) { punitiveTime += Time.deltaTime; if (punitiveTime >= 1) { comboNums = 0; punitiveTime = 0; int missPunitiveS = Mathf.CeilToInt(m_ScoreData.firstBaseScore * m_ScoreData.missPunitiveMul); IncreaseScore(missPunitiveS * -1); } TotalMissTime += Time.deltaTime; } #endregion } }