Files
soccar2d/Assets/Scripts/Utils/SceneLoad/LevelLoadManager.cs
T
2026-05-17 20:11:42 +05:30

205 lines
6.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.SceneManagement;
using System.Collections;
using TMPro;
using Mirror;
public class LevelLoadManager : MonoBehaviour
{
public static LevelLoadManager instance;
public Image contentImage;
public float transitionDuration = 0.5f;
CanvasGroup canvasGroup;
public bool isShowing => canvasGroup.alpha == 1;
[Header("Matchmade UI")]
public GameObject matchmadeUI;
public GameObject p1_red,p2_red,p1_blue,p2_blue;
public Image p1_icon, p2_icon;
public TMP_Text p1_name, p2_name;
public TMP_Text p1_l10, p2_l10;
public Sprite redIcon, blueIcon;
public TMP_Text txtMiddle;
public TMP_Text txtRcPrize;
[Header("Game Over UI")]
public GameObject p1_winner;
public GameObject p2_winner;
void Awake()
{
if(instance != null){
Destroy(gameObject);
return;
}
canvasGroup = GetComponent<CanvasGroup>();
instance = this;
DontDestroyOnLoad(gameObject);
Hide();
matchmadeUI.SetActive(false);
}
public static void LoadLevel(string levelName){
instance.loadLevel(levelName);
}
void loadLevel(string levelName){
StartCoroutine(CoroutineLoadLevel(levelName));
}
/// <summary>When true, Main Menu bootstrap (matchmaker settings) owns the overlay; suppress duplicate work in <see cref="MainMenuManager"/>.</summary>
public static bool BlocksMainMenuSettingsBootstrap { get; private set; }
IEnumerator CoroutineLoadLevel(string levelName){
instance.Show();
yield return new WaitForSeconds(transitionDuration);
if(matchmadeUI.activeSelf){
yield return new WaitForSeconds(3f);
}
bool mainMenuDestination = levelName == MainMenuManager.MainMenuSceneName;
if (mainMenuDestination)
BlocksMainMenuSettingsBootstrap = true;
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(levelName);
yield return asyncLoad;
if (mainMenuDestination)
{
try
{
MainMenuManager menu = UnityEngine.Object.FindObjectOfType<MainMenuManager>();
if (menu != null)
yield return menu.FinishLoadingScreenAndApplySettings();
}
finally
{
BlocksMainMenuSettingsBootstrap = false;
}
}
instance.Hide();
}
public void Show(){
if(isShowing){return;} //already is showing
canvasGroup.blocksRaycasts=true;
canvasGroup.interactable=true;
contentImage.fillOrigin = 0;
canvasGroup.DOFade(1, transitionDuration).SetEase(Ease.OutSine);
contentImage.DOFillAmount(1, transitionDuration).SetEase(Ease.OutSine);
}
public void Hide(){
canvasGroup.blocksRaycasts=false;
canvasGroup.interactable=false;
contentImage.fillOrigin = 1;
canvasGroup.DOFade(0, transitionDuration).SetEase(Ease.InSine);
contentImage.DOFillAmount(0, transitionDuration).SetEase(Ease.InSine).OnComplete(() => {
if(gameOver){
gameOver = false;
matchmadeUI.SetActive(false);
}
});
}
Team myTeam = Team.Red;
public void SetupRematch(int rc_prize, int port, int gameId){
txtRcPrize.text = CoinHelper.FormatString(rc_prize) + " RC";
}
public void SetupMatchMade(Team myTeam_, string opponentName, string myName, string myL10, string opponentL10, string rcPrize){
myTeam = myTeam_;
matchmadeUI.SetActive(true);
p1_winner.SetActive(false);
p2_winner.SetActive(false);
txtMiddle.text = "VS";
if(myTeam == Team.Red){
p1_red.SetActive(true);
p1_blue.SetActive(false);
p2_red.SetActive(false);
p2_blue.SetActive(true);
p1_icon.sprite = redIcon;
p2_icon.sprite = blueIcon;
}else{
p1_red.SetActive(false);
p1_blue.SetActive(true);
p2_red.SetActive(true);
p2_blue.SetActive(false);
p1_icon.sprite = blueIcon;
p2_icon.sprite = redIcon;
}
p1_name.text = opponentName;
p2_name.text = myName;
p1_l10.text = myL10;
p2_l10.text = opponentL10;
txtRcPrize.text = rcPrize;
}
public static MatchmadePlayer myPlayer;
public static MatchmadePlayer opponentPlayer;
public void SetupMatchMade(Team myTeam_, MatchmadePlayer myPlayer_, MatchmadePlayer opponentPlayer_, string rcPrize){
myTeam = myTeam_;
myPlayer = myPlayer_;
opponentPlayer = opponentPlayer_;
string opponentName = opponentPlayer_.Name;
string myName = myPlayer_.Name;
string myL10 = $"L10 {myPlayer.l10_wins} -{myPlayer.l10_losses}";
string opponentL10 = $"L10 {opponentPlayer.l10_wins} -{opponentPlayer.l10_losses}";
SetupMatchMade(myTeam, myName, opponentName, myL10, opponentL10, rcPrize);
}
bool gameOver = false;
public void SetupGameOver(Team winningTeam, int redScore, int blueScore){
gameOver = true;
txtMiddle.text = $"{(myTeam == Team.Red ? redScore : blueScore)} FINAL {(myTeam == Team.Red ? blueScore : redScore)}";
if(myTeam == winningTeam){
p1_winner.SetActive(true);
p2_winner.SetActive(false);
}else{
p1_winner.SetActive(false);
p2_winner.SetActive(true);
}
p1_l10.text = $"L10 {myPlayer.l10_wins}-{myPlayer.l10_losses}";
p2_l10.text = $"L10 {opponentPlayer.l10_wins}-{opponentPlayer.l10_losses}";
}
public void Leave(string levelName = "MainMenu"){
NetManager.MarkIntentionalClientDisconnect();
GameManager.instance.StopClient();
StartCoroutine(CoroutineWaitTillDisconnection(levelName));
}
IEnumerator CoroutineWaitTillDisconnection(string levelName){
Show(); //Load the UI until the level is loaded
while(NetworkClient.isConnected){
yield return null;
}
yield return new WaitForSeconds(1f); //Just to make sure
LoadLevel(levelName);
}
}