RandomGFGoStop/Assets/Scripts/UI/TitleScene/TitleCanvas.cs

423 lines
11 KiB
C#

using DG.Tweening;
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
public class TitleCanvas : MonoBehaviour
{
private enum TitleCanvasState
{
CheckInternet,
CheckVersion,
DownloadData,
LoadUnlockHuntingImage,
LoadScene,
}
[SerializeField] private MaskableProgressBar MaskableBar;
[SerializeField] private RectTransform blinkRT;
[SerializeField] private TextMeshProUGUI progressbarText;
[SerializeField] private AgeIndication ageIndication = null;
private TitleCanvasState state = TitleCanvasState.CheckInternet;
private Coroutine DownloadDataCoroutine = null;
private Coroutine LoadUnlockHuntingImageCoroutine = null;
private Coroutine EndAgeIndicationCoroutine = null;
private void Awake()
{
if (GameManager.Instance != null)
Debug.Log("base: Call Manager");
MaskableBar.FillAmount = 1.0f;
blinkRT.DOAnchorPosX(250, 1.5f).SetEase(Ease.OutExpo).SetLoops(-1, LoopType.Restart).From(Vector2.left * 250);
if (ageIndication != null)
{
ageIndication.gameObject.SetActive(true);
ageIndication.OnCompleteHide += OnCompleteAgeIndicationHide;
if (EndAgeIndicationCoroutine == null)
{
EndAgeIndicationCoroutine = StartCoroutine(EndAgeIndication());
}
}
else
{
OnCompleteAgeIndicationHide();
}
}
private void OnDestroy()
{
if (DOTween.IsTweening(blinkRT))
blinkRT.DOKill();
//GameManager.BGDatabase.ReleaseHuntingUnlockImage();
GameManager.OnCompleteCreatemanager -= OnCompleteCreatemanager;
GameManager.Network.OnNetworkOnline -= OnNetworkOnline;
GameManager.BGDatabase.OnCompleteLoadHuntingImage -= OnSucceedLoadUnlockHuntingImage;
ageIndication.OnCompleteHide -= OnCompleteAgeIndicationHide;
GameManager.Addressable.NotifiyCatalogUpdated -= OnCatalogUpdated;
GameManager.Addressable.NotifiySizeDownloaded -= OnSizeDownloaded;
GameManager.Addressable.NotifiyDownloadFinished -= OnDownloadFinished;
GameManager.OnCompleteCreatemanager -= OnCompleteCreatemanager;
if (EndAgeIndicationCoroutine != null)
{
StopCoroutine(EndAgeIndicationCoroutine);
}
}
private void OnCompleteCreatemanager()
{
DateTime loadTime = GameManager.Timer.LoadTime();
if (loadTime.Day != DateTime.Now.Day)
{
GameManager.DB.ResetADSRewardActionCount();
GameManager.DB.ResetFreeHeartCount();
}
state = TitleCanvasState.CheckInternet;
StartCurrentState();
}
private void StartCurrentState()
{
switch (state)
{
case TitleCanvasState.CheckInternet:
StartCoroutine(CheckInternet());
break;
case TitleCanvasState.CheckVersion:
/*Addressables.ClearDependencyCacheAsync("TestPopup");
Addressables.DownloadDependenciesAsync("TestPopup").Completed += (handle) =>
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
StartCoroutine(LoadTestPopup());
/*Addressables.LoadResourceLocationsAsync("TestPopup").Completed += (handle) =>
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
//Addressables.LoadAssetsAsync(handle.Result).
}
};
}
};*/
GameManager.ADS.InitADSManager();
CheckVersion();
break;
case TitleCanvasState.DownloadData:
//DownloadData(); // 리소스 다운로드 없음
NextState();
StartCurrentState();
break;
case TitleCanvasState.LoadUnlockHuntingImage:
LoadUnlockHuntingImage();
break;
case TitleCanvasState.LoadScene:
StartCoroutine(coroGoToNextScene());
break;
}
}
private void NextState()
{
switch (state)
{
case TitleCanvasState.CheckInternet:
state = TitleCanvasState.CheckVersion;
break;
case TitleCanvasState.CheckVersion:
state = TitleCanvasState.DownloadData;
break;
case TitleCanvasState.DownloadData:
state = TitleCanvasState.LoadUnlockHuntingImage;
break;
case TitleCanvasState.LoadUnlockHuntingImage:
state = TitleCanvasState.LoadScene;
break;
}
}
private IEnumerator CheckInternet()
{
yield return null;
progressbarText.text = "네트워크 확인 하는 중";
yield return new WaitForSecondsRealtime(0.5f);
if (GameManager.Network.IsOnline)
{
NextState();
StartCurrentState();
}
else
{
if (GameManager.DB.ExistSaveData())
{
state = TitleCanvasState.LoadUnlockHuntingImage;
StartCurrentState();
}
else
{
WarrnigNeedInternet();
GameManager.Network.OnNetworkOnline += OnNetworkOnline;
}
}
}
private void CheckVersion()
{
progressbarText.text = "버전 확인 하는 중...";
StartCoroutine(LoadVersiontData("https://filgoodbanditshomepage.web.app/RandomGFGoStop.txt"));
}
private void DownloadData()
{
progressbarText.text = "다운로드 하는 중...";
GameManager.Addressable.DownloadData();
if (DownloadDataCoroutine == null)
{
DownloadDataCoroutine = StartCoroutine(UpdateDownloadData());
}
}
private void LoadUnlockHuntingImage()
{
// 튜토리얼 기능을 위해 main씬에서 세이브 초기화로 결정
// 임시적으로 세이브기능을 이곳에 나중에 다운로드 완료 후에 세이브하도록 수정
//GameManager.DB.SaveDatas();
progressbarText.text = "이미지 로드 하는 중...";
GameManager.BGDatabase.InitDataBase();
GameManager.BGDatabase.OnCompleteLoadHuntingImage += OnSucceedLoadUnlockHuntingImage;
if (LoadUnlockHuntingImageCoroutine == null)
{
LoadUnlockHuntingImageCoroutine = StartCoroutine(UpdateLoadUnlockHuntingImage());
}
}
private IEnumerator coroGoToNextScene()
{
progressbarText.text = "로비 로드 하는 중...";
yield return new WaitUntil(() => GameManager.Instance != null);
float cnt = 0;
while (cnt < 2)
{
cnt += Time.deltaTime;
MaskableBar.FillAmount = cnt / 2;
yield return null;
}
GameManager.Scene.LoadScene(ESceneType.Main);
}
private IEnumerator LoadVersiontData(string url)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.SendWebRequest();
string updateVersion = www.downloadHandler.text;
string currentVersion = Application.version;
updateVersion = updateVersion.Replace(".", "");
currentVersion = currentVersion.Replace(".", "");
if (int.Parse(updateVersion) > int.Parse(currentVersion))
{
WarrnigDifferentVersion();
}
else
{
NextState();
StartCurrentState();
}
yield break;
}
}
private void OnSucceedLoadUnlockHuntingImage(bool isSucceed)
{
if (!isSucceed)
{
//Logging.LogError("LoadOutGameImage is Fail");
}
else
{
if (LoadUnlockHuntingImageCoroutine != null)
{
StopCoroutine(LoadUnlockHuntingImageCoroutine);
}
NextState();
StartCurrentState();
}
}
private void OnNetworkOnline(bool isOnline)
{
if (isOnline == true)
{
if (GameManager.UI.IsOpendPopup(EPopupType.NotOnlinePopup))
{
GameManager.UI.HideTopPopup();
}
NextState();
StartCurrentState();
}
}
private void WarrnigDifferentVersion()
{
GameManager.UI.ShowNStackPopup(EPopupType.NotMinimumVersionPopup);
}
private void WarrnigNeedInternet()
{
GameManager.UI.ShowNStackPopup(EPopupType.NotOnlinePopup);
}
private void WarrnigFailDownload()
{
Debug.LogWarning("base: WarrnigFailDownload");
//ReturnFirstState();
}
private void ReturnFirstState()
{
state = TitleCanvasState.CheckInternet;
StartCurrentState();
}
private void OnCatalogUpdated(bool isUpdateCataglog)
{
if (isUpdateCataglog == true)
{
}
else
{
}
}
private void OnSizeDownloaded(long size)
{
}
private void OnDownloadFinished(bool completed)
{
if (DownloadDataCoroutine != null)
{
StopCoroutine(DownloadDataCoroutine);
}
if (completed == true)
{
NextState();
StartCurrentState();
}
else
{
WarrnigFailDownload();
}
}
private IEnumerator UpdateDownloadData()
{
MaskableBar.FillAmount = 0.0f;
while (true)
{
yield return null;
float percent = GameManager.Addressable.GetDownloadDataPercent();
if (percent <= 0.8f)
{
percent = (percent / 0.8f) * 0.2f;
}
else
{
percent = 0.2f + ((percent - 0.8f) / 0.2f) * 0.8f;
}
MaskableBar.FillAmount = percent;
}
}
private IEnumerator UpdateLoadUnlockHuntingImage()
{
MaskableBar.FillAmount = 0.0f;
yield return new WaitForSeconds(1.0f);
GameManager.BGDatabase.LoadHuntingUnlockImage();
while (true)
{
yield return null;
MaskableBar.FillAmount = GameManager.BGDatabase.LoadPercent;
}
}
private IEnumerator EndAgeIndication()
{
if (ageIndication != null)
{
yield return new WaitForSecondsRealtime(ageIndication.GetFadeoutAnimationLength());
}
OnCompleteAgeIndicationHide();
}
private void OnCompleteAgeIndicationHide()
{
if (EndAgeIndicationCoroutine != null)
{
StopCoroutine(EndAgeIndicationCoroutine);
}
Destroy(ageIndication.gameObject);
GameManager.Addressable.NotifiyCatalogUpdated += OnCatalogUpdated;
GameManager.Addressable.NotifiySizeDownloaded += OnSizeDownloaded;
GameManager.Addressable.NotifiyDownloadFinished += OnDownloadFinished;
if (GameManager.DB.CompleteDataLoad == false)
{
GameManager.OnCompleteCreatemanager += OnCompleteCreatemanager;
}
else
{
OnCompleteCreatemanager();
}
}
}