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 PistolSniperTrainHandle : BaseTrainHandle { private Camera shiperCamera; private SphereShotTarget curTarget; private PistolSniperTrainInfoDifficultyData m_DifficultyData; private PistolSniperTrainScoreData 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 totalKillNums; private double totalTargetNums; public PistolSniperTrainHandle(BaseTrainCallBack callBack, BaseTrainInfo info, DifficultyType difficultyType) : base(callBack, info, difficultyType) { SetShotTargetInteractiveEvent(ShotTargetInteractiveType.OnClick, OnClickTargetEvent); m_DifficultyData = info.GetDifficultyData(m_DifficultyType); m_ScoreData = info.GetScoreData(m_DifficultyType); shiperCamera = Camera.main.transform.Find("SnipeCamera").GetComponent(); } private void GenerateShotTarget() { roundIndex++; int round = roundIndex % 2; MotionType motionType = MotionType.None; Vector3 origin = round == 1 ? new Vector3(0, 0, m_DifficultyData.distance) : RandomPosition(); GameObject obj = GameObject.Instantiate(Resources.Load("Prefabs/SphereTarget")); curTarget = obj.GetComponent(); curTarget.onClickCanChangeColor = false; curTarget.onHoveringCanChangeColor = false; curTarget.defaultColor = ScreenEffectManager.GetInstance().GetTargetColor(); curTarget.onHoveringColor = Color.red; curTarget.Init(1, ReduceHealthTriggerType.OnClick, false, ReduceHealthType.Per, MotionType.None, origin, null, 1f); curTarget.IsRunning = true; totalTargetNums++; m_ObserveTimer = TimerSystem.GetInstance().AddTimeTask(ObserveTimerTimeOut, 5f); StartBestHitTimer(); } private void ObserveTimerTimeOut() { comboNums = 0; int outTimePunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.outTimePunitiveMul) + Random.Range(-3, 3); IncreaseScore(outTimePunitiveS * -1); m_ObserveTimer = null; DestroyShootTarget(curTarget as SphereShotTarget); GenerateShotTarget(); } private Vector3 RandomPosition() { Vector2 widthLimt = Vector2.zero; Vector2 heightLimt = Vector2.zero; switch (m_DifficultyType) { case DifficultyType.Standard: widthLimt = new Vector2(-12, 12); heightLimt = new Vector2(-1.5f, 10f); break; case DifficultyType.Advance: widthLimt = new Vector2(-12, 12); heightLimt = new Vector2(-1.5f, 12f); break; case DifficultyType.Professional: widthLimt = new Vector2(-12, 12); heightLimt = new Vector2(-1.5f, 15f); break; } Vector2 minOffset = new Vector2(0.4f, 0.4f); Vector2 maxOffset = new Vector2(0.6f, 0.6f); Vector3 min = ExtraTool.GetScreenOffsetPosition(minOffset, m_DifficultyData.distance, widthLimt, heightLimt); Vector3 max = ExtraTool.GetScreenOffsetPosition(maxOffset, m_DifficultyData.distance, widthLimt, heightLimt); return ExtraTool.RandomPosition(min, max); } private void ResetTarget() { if (curTarget != null) { curTarget.IsRunning = false; GameObject.Destroy(curTarget.gameObject); curTarget = null; } } private void DestroyShootTarget(SphereShotTarget target) { if (target != null) { target.IsRunning = false; GameObject.Destroy(target.gameObject); } } #region TrainResult private void UpdatePrecision() { SetResultMetricsDataValue(MetricsType.Precision, Math.Round(TotalHitNum / (TotalHitNum + MissHitNum), 2)); } #endregion #region Train protected override void ResetTrain() { ResetTarget(); TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer); m_ObserveTimer = null; roundIndex = 0; m_TotalHitNum = 0; m_MissHitNum = 0; totalKillNums = 0; totalTargetNums = 0; shiperCamera.gameObject.SetActive(false); } public override void PrepareTrain() { base.PrepareTrain(); shiperCamera.gameObject.SetActive(true); } public override void StartTrain() { base.StartTrain(); GenerateShotTarget(); } public override void PauseTrain() { base.PauseTrain(); curTarget.IsRunning = false; if (m_ObserveTimer != null) { m_ObserveTimer.pause = true; } } public override void ContinueTrain() { base.ContinueTrain(); curTarget.IsRunning = true; if (m_ObserveTimer != null) { m_ObserveTimer.pause = false; } } public override void CalculateTrainResult() { double timeToKill = 999; if (totalKillNums != 0) { timeToKill = Math.Round(TrainInfo.duration / totalKillNums, 2); } SetResultMetricsDataValue(MetricsType.Kill_Sec, Math.Round(totalKillNums / TrainInfo.duration, 2)); SetResultMetricsDataValue(MetricsType.KillTotal, totalKillNums); SetResultMetricsDataValue(MetricsType.TimeToKill, timeToKill); SetResultMetricsDataValue(MetricsType.Target, totalTargetNums); } #endregion #region ShotTarget private void OnClickTargetEvent(BaseShotTarget target) { if (target != null) { SphereShotTarget t = target as SphereShotTarget; if (t != null) { int comboS = Mathf.CeilToInt(m_ScoreData.baseScore * comboNums * m_ScoreData.comboMul); int bestS = m_IsBestHit ? Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.bestHitMul) : 0; IncreaseScore(m_ScoreData.baseScore + comboS + bestS); comboNums++; if (t.CurHealth <= 0) { totalKillNums++; TimerSystem.GetInstance().RemoveTimeTask(m_ObserveTimer); m_ObserveTimer = null; DestroyShootTarget(t); GenerateShotTarget(); } } TotalHitNum++; } else { MissHitNum++; comboNums = 0; int missPunitiveS = Mathf.CeilToInt(m_ScoreData.baseScore * m_ScoreData.missPunitiveMul); IncreaseScore(missPunitiveS * -1); } } #endregion } }