UPF/Assets/Game/Scripts/GameManager.cs
2022-08-29 02:19:41 +05:30

114 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Threading.Tasks;
public class GameManager : MonoBehaviour
{
private static GameManager _instance;
public static GameManager instance => _instance;
public TMP_Text[] usernameTxts;
public TMP_Text[] coinsTxt;
public TMP_Text[] gemsTxt;
public TMP_Text metalTxt;
public TMP_Text oxygenTxt;
public TMP_Text[] levelTxts;
public Slider[] levelSliders;
public TMP_Text[] levelProgressTxts;
public GameObject loadingScreen;
[Header("Profile")]
public GameObject profilePopup;
public GameObject networkErrorPopup;
public float NetworkCheckInterval = 3;
void Start()
{
_instance = this;
//Go back to login if not logged
if (!DBmanager.LoggedIn)
{
SceneManager.LoadScene(0);
}
else
{
foreach (TMP_Text usernameTxt in usernameTxts)
{
usernameTxt.text = DBmanager.username;
}
RefreshData();
}
loadingScreen.SetActive(false);
}
float networkCheckTimer = 0;
void Update()
{
networkCheckTimer += Time.deltaTime;
if (networkCheckTimer > NetworkCheckInterval)
{
Debug.Log(Application.internetReachability);
if (Application.internetReachability == NetworkReachability.NotReachable)
{
networkErrorPopup.SetActive(true);
}else{
networkErrorPopup.SetActive(false);
}
networkCheckTimer=0;
}
}
public static void Refresh(){
if(_instance != null){
_instance.RefreshData();
}
}
private void RefreshData()
{
foreach (TMP_Text txt in coinsTxt)
{
txt.text = DBmanager.Coins.ToString();
}
foreach (TMP_Text txt in gemsTxt)
{
txt.text = DBmanager.Gems.ToString();
}
metalTxt.text = DBmanager.Metal.ToString();
oxygenTxt.text = DBmanager.Oxygen.ToString();
foreach (TMP_Text levelTxt in levelTxts)
{
levelTxt.text = Mathf.CeilToInt(DBmanager.Level).ToString();
}
float levelExcess = DBmanager.Level - Mathf.FloorToInt(DBmanager.Level);
Debug.Log("Level : " + DBmanager.Level);
foreach (Slider levelSlider in levelSliders)
{
levelSlider.value = Mathf.Clamp(levelExcess, 0.1f, 1);
}
foreach (TMP_Text levelProgressTxt in levelProgressTxts)
{
levelProgressTxt.text = $"{DBmanager.Xp}/{DBmanager.XpForNextLevel()}";
}
}
public async void LoadMinigame()
{
loadingScreen.SetActive(true);
await Task.Delay(1000);
SceneManager.LoadScene("MinigameMenu");
}
}