Files
soccar2d/Assets/Scripts/GameCanvas.cs
T
2026-04-17 01:35:53 +05:30

196 lines
6.0 KiB
C#

using System.Collections;
using Mirror;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using DG.Tweening;
public class GameCanvas : MonoBehaviour
{
public static GameCanvas instance;
const int OpponentWaitTimeoutSeconds = 15;
static readonly WaitForSeconds WaitOneSecond = new WaitForSeconds(1f);
int opponentWaitSecondsElapsed;
bool hasShownOpponentTimeout;
Coroutine opponentWaitRoutine;
void Awake()
{
instance = this;
ShowWaitingForOpponentPanel();
}
void OnDestroy()
{
StopOpponentWaitRoutine();
}
public EventTrigger gameInputPanel;
public Image turnTimerSliderTransform;
public Color redColor = Color.red;
public Color blueColor = Color.blue;
public TMP_Text blueTurnTimer;
[FormerlySerializedAs("txtTurnTimer")] public TMP_Text redTurnTimer;
public TMP_Text redName,blueName;
public TMP_Text txtWhosTurn;
public Image blueTurnTimerRound;
[FormerlySerializedAs("turnTimerRound")] public Image redTurnTimerRound;
public GameObject waitingForOpponentPanel;
public GameObject timeoutForOpponentPanel;
public Button btnLeaveGame;
void Start(){
if (btnLeaveGame != null)
btnLeaveGame.onClick.AddListener(OnLeaveGamePressed);
redName.text = GameManager.RedPlayer.Name;
blueName.text = GameManager.BluePlayer.Name;
}
void OnLeaveGamePressed()
{
if (CupidLobby.instance != null)
CupidLobby.instance.Cancel();
LoginManager.ClearMatchYourTeam();
if (NetworkManager.singleton != null && NetworkClient.active)
NetworkManager.singleton.StopClient();
LevelLoadManager.LoadLevel("MainMenu");
}
public void ShowWaitingForOpponentPanel(){
waitingForOpponentPanel.SetActive(true);
ResetOpponentTimeoutState();
StopOpponentWaitRoutine();
opponentWaitRoutine = StartCoroutine(OpponentWaitingTimeoutRoutine());
}
public void HideWaitingForOpponentPanel(){
StopOpponentWaitRoutine();
waitingForOpponentPanel.SetActive(false);
timeoutForOpponentPanel.SetActive(false);
ResetOpponentTimeoutState();
}
void StopOpponentWaitRoutine()
{
if (opponentWaitRoutine != null)
{
StopCoroutine(opponentWaitRoutine);
opponentWaitRoutine = null;
}
}
void Update(){
UpdateTurnTimer();
}
Team prevTeam = Team.Red;
bool init=false;
void UpdateTurnTimer(){
if(NetPlayer.localPlayer == null){return;}
if(!init){
init=true;
SwitchTeams();
}
float turnTimerFillAmount = 1 - (GameManager.instance.turnTimerCounter / GameManager.instance.turnTimer);
if(prevTeam != GameManager.instance.SelectedTeam){
SwitchTeams();
prevTeam = GameManager.instance.SelectedTeam;
}
turnTimerSliderTransform.transform.localScale = new Vector3(turnTimerFillAmount, 1, 1);
string timerString = (GameManager.instance.turnTimer - GameManager.instance.turnTimerCounter).ToString("F0");
if (GameManager.instance.SelectedTeam == Team.Red)
{
if (redTurnTimer != null) redTurnTimer.text = ":"+timerString;
if (blueTurnTimer != null) blueTurnTimer.text = string.Empty;
}
else
{
if (blueTurnTimer != null) blueTurnTimer.text = ":"+timerString;
if (redTurnTimer != null) redTurnTimer.text = string.Empty;
}
if (GameManager.instance.SelectedTeam == Team.Red)
{
if (redTurnTimerRound != null) redTurnTimerRound.fillAmount = turnTimerFillAmount;
if (blueTurnTimerRound != null) blueTurnTimerRound.fillAmount = 0f;
}
else
{
if (blueTurnTimerRound != null) blueTurnTimerRound.fillAmount = turnTimerFillAmount;
if (redTurnTimerRound != null) redTurnTimerRound.fillAmount = 0f;
}
}
void SwitchTeams(){
bool isMyTurn = GameManager.instance.SelectedTeam == NetPlayer.localPlayer.myTeam;
Color newColor = GameManager.instance.SelectedTeam == Team.Red ? redColor : blueColor;
turnTimerSliderTransform.DOColor(newColor, 0.5f);
if (txtWhosTurn != null)
{
txtWhosTurn.DOFade(0, 0.25f).OnComplete(() => {
txtWhosTurn.text = isMyTurn ? "Your Turn" : "Opponent's Turn";
txtWhosTurn.DOFade(1, 0.25f);
});
}
}
IEnumerator OpponentWaitingTimeoutRoutine()
{
while (true)
{
yield return WaitOneSecond;
if (waitingForOpponentPanel == null || timeoutForOpponentPanel == null)
yield break;
if (!waitingForOpponentPanel.activeSelf)
{
timeoutForOpponentPanel.SetActive(false);
ResetOpponentTimeoutState();
yield break;
}
bool isSoloPlayer = FindObjectsByType<NetPlayer>(FindObjectsSortMode.None).Length <= 1;
if (!isSoloPlayer)
{
timeoutForOpponentPanel.SetActive(false);
ResetOpponentTimeoutState();
continue;
}
if (hasShownOpponentTimeout)
continue;
opponentWaitSecondsElapsed++;
if (opponentWaitSecondsElapsed >= OpponentWaitTimeoutSeconds)
{
timeoutForOpponentPanel.transform.localScale = new Vector3(1, 0, 1);
timeoutForOpponentPanel.transform.DOScaleY(1,0.1f).SetEase(Ease.InOutBounce);
timeoutForOpponentPanel.SetActive(true);
hasShownOpponentTimeout = true;
}
}
}
void ResetOpponentTimeoutState()
{
opponentWaitSecondsElapsed = 0;
hasShownOpponentTimeout = false;
timeoutForOpponentPanel.SetActive(false);
}
}