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 SmallTargetShotTrainHandle : BaseTrainHandle { private SmallTargetShotTrainInfoDifficultyData m_DifficultyData; private SmallTargetShotTrainScoreData m_ScoreData; private TimeTask m_ObserveTimer; private int roundIndex; private double m_TotalHitNum; private double TotalHitNum { get { return m_TotalHitNum; } set { m_TotalHitNum = value; UpdatePrecision(); } } private double m_MissHitNum; private double MissHitNum { get { return m_MissHitNum; } set { m_MissHitNum = value; UpdatePrecision(); } } private double totalTargetNum; private float totalReactionTime; private SphereShotTarget curTarget; public SmallTargetShotTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType) { SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnShottedTargetEvent); m_DifficultyData = info.GetDifficultyData(m_DifficultyType); m_ScoreData = info.GetScoreData(m_DifficultyType); } #region Train protected override void ResetTrain() { ResetTarget(); TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer); m_ObserveTimer = null; roundIndex = 0; m_TotalHitNum = 0; m_MissHitNum = 0; totalTargetNum = 0; totalReactionTime = 0; } public override void StartTrain() { base.StartTrain(); ResetTrain(); GenerateShotTarget(); } public override void PauseTrain() { base.PauseTrain(); if (m_ObserveTimer != null) { m_ObserveTimer.pause = true; } } public override void ContinueTrain() { base.ContinueTrain(); if (m_ObserveTimer != null) { m_ObserveTimer.pause = false; } } public override void CalculateTrainResult() { SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(TotalHitNum / TrainInfo.duration, 2)); SetResultMetricsDataValue(MetricsType.KillTotal, TotalHitNum); SetResultMetricsDataValue(MetricsType.ReactionTime, Math.Round(totalReactionTime / (TotalHitNum + MissHitNum), 2)); SetResultMetricsDataValue(MetricsType.Target, totalTargetNum); } #endregion #region TrainResult private void UpdatePrecision() { double totalShot = TotalHitNum + MissHitNum; double precision = 0; if (totalShot != 0) { precision = Math.Round(TotalHitNum / totalShot, 2); } SetResultMetricsDataValue(MetricsType.Precision, precision); } #endregion #region ShootTarget private void OnShottedTargetEvent(BaseShotTarget target) { if (target != null) { int baseS = roundIndex % 2 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore; int comboS = Mathf.CeilToInt(baseS * comboNums * m_ScoreData.comboMul); int bestS = m_IsBestHit ? Mathf.CeilToInt(baseS * m_ScoreData.bestHitMul) : 0; IncreaseScore(baseS + comboS + bestS); comboNums++; TotalHitNum++; if (m_ObserveTimer != null) { totalReactionTime += m_ObserveTimer.delay - m_ObserveTimer.remainingTime; } TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer); m_ObserveTimer = null; DestroyShootTarget(target as SphereShotTarget); GenerateShotTarget(); } else { MissHitNum++; comboNums = 0; int baseS = roundIndex % 3 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore; int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.missPunitiveMul); IncreaseScore(missPunitiveS * -1); } } #endregion private void GenerateShotTarget() { roundIndex++; GameObject obj = GameObject.Instantiate(Resources.Load("Prefabs/SmallSphereTarget")); curTarget = obj.GetComponent(); curTarget.IsRunning = true; curTarget.onClickCanChangeColor = false; curTarget.onHoveringCanChangeColor = false; curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor(); Vector3 originPos = roundIndex % 2 == 1 ? ExtraTool.GetWorldPositionWithResolutionPercent(0.5f, 0.5f, 10) : RandomPosition(); curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, originPos); if (roundIndex % 2 != 1) { m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, m_DifficultyData.duration); } totalTargetNum++; StartBestHitTimer(); } private void ObserveTimerTimeOut() { comboNums = 0; int baseS = roundIndex % 2 == 1 ? m_ScoreData.firstBaseScore : m_ScoreData.secondBaseScore; int missPunitiveS = Mathf.CeilToInt(baseS * m_ScoreData.timeOutPunitiveMul) + Random.Range(-3, 3); IncreaseScore(missPunitiveS * -1); totalReactionTime += m_ObserveTimer.delay; m_ObserveTimer = null; DestroyShootTarget(curTarget as SphereShotTarget); GenerateShotTarget(); } private Vector3 RandomPosition() { Vector2 widthLimt = new Vector2(-8, 8); Vector2 heightLimt = new Vector2(-1.5f, 3f); Vector3 min = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenLeftBottomOffset, 10f, widthLimt, heightLimt); Vector3 max = ExtraTool.GetScreenOffsetPosition(ExtraTool.Sphere_Diameter1_ScreenRightTopOffset, 10f, widthLimt, heightLimt); return ExtraTool.RandomPosition(min, max); } private void DestroyShootTarget(SphereShotTarget target) { if (target != null) { target.IsRunning = false; GameObject.Destroy(target.gameObject); } } private void ResetTarget() { if (curTarget != null) { curTarget.IsRunning = false; GameObject.Destroy(curTarget.gameObject); curTarget = null; } } } }