sea-battle/Assets/Scripts/UI/UIManager.cs
2026-01-09 08:45:22 +03:00

308 lines
8.7 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static GameManager;
public class UIManager : MonoBehaviour
{
[SerializeField] private int playerId;
public TurnTimerUI turnTimerUI;
public GameObject mainMenuPanel;
// public GameObject botSelectPanel;
public GameObject placementPanel;
public GameObject gameplayPanel;
public GameObject gameOverPanel;
public GameObject setingPanel;
public GameObject QRCode_Panel;
public GameObject Input_Panel;
public GameObject CharacterSettingPanel;
public Button playButton; // â áîé
[Header("Game Over UI")]
public Button replayButton; // Ïåðåèãðàòü
public Button continueButton; // Ïðîäîëæèòü (äîñòóïåí òîëüêî åñëè ïîáåäèë)
public TMPro.TextMeshProUGUI resultText; // Òåêñò "Ïîáåäà" èëè "Ïîðàæåíèå"
[Header("Coin UI")]
public Coin coin; // ññûëêà íà îáúåêò Coin
private int lastCoins = 0;
[Header("Main Menu Buttons")]
public Button playVsBotButton;
public Button playVsPlayerButton;
private GameOver gameOverStatusImag;
public GameObject uiParticle;
[SerializeField] private GameManager gameManager;
[SerializeField] private GameObject invitePanel;
[SerializeField] private TMP_Text inviteText;
public void ShowInvite(int fromPlayerId)
{
inviteText.text = $"Cûãðàåì?";
ShowPanel(GameState.Invite); // ïåðåêëþ÷àåìñÿ íà ñîñòîÿíèå Invite
}
public void HideInvite()
{
ShowPanel(GameState.MainMenu); // âîçâðàùàåìñÿ â ãëàâíîå ìåíþ èëè ïðåäûäóùåå ñîñòîÿíèå
}
private void OnEnable()
{
ComWrapper.OnInitialize += Subscribe;
GameManager.OnInviteSent += HandleInvite;
}
private void OnDisable()
{
ComWrapper.onCoinAdded.RemoveListener(HandleCoinsChanged);
ComWrapper.onCoinRemoved.RemoveListener(HandleCoinsChanged);
GameManager.OnInviteSent -= HandleInvite;
ComWrapper.OnInitialize -= Subscribe;
}
private void Subscribe()
{
ComWrapper.onCoinAdded.AddListener(HandleCoinsChanged);
ComWrapper.onCoinRemoved.AddListener(HandleCoinsChanged);
}
private void HandleInvite(int fromPlayerId, int toPlayerId)
{
if (toPlayerId != playerId) return;
// Ïðèãëàøåíèÿ ïîêàçûâàåì òîëüêî åñëè èãðîê ñåé÷àñ â ãëàâíîì ìåíþ
if (!mainMenuPanel.activeSelf) return;
ShowInvite(fromPlayerId);
}
private void Start()
{
int current = ComWrapper.GetPlayerCredits();
lastCoins = current;
if (coin != null)
{
coin.ChangeCoinText($"{current}");
if (current == 0)
coin.FreezeCoin();
else
coin.UnfreezeCoin();
}
gameOverStatusImag = gameOverPanel.GetComponentInChildren<GameOver>();
}
public void ShowPanel(GameState state)
{
// ãàñèì âñå ïàíåëè
mainMenuPanel.SetActive(false);
placementPanel.SetActive(false);
gameplayPanel.SetActive(false);
gameOverPanel.SetActive(false);
setingPanel.SetActive(false);
invitePanel.SetActive(false);
QRCode_Panel.SetActive(false);
Input_Panel.SetActive(false);
CharacterSettingPanel.SetActive(false);
// âêëþ÷àåì íóæíóþ ïàíåëü
GameObject activePanel = state switch
{
GameState.MainMenu => mainMenuPanel,
GameState.ShipPlacement => placementPanel,
GameState.Playing => gameplayPanel,
GameState.GameOver => gameOverPanel,
GameState.Setting => setingPanel,
GameState.Invite => invitePanel,
GameState.Prize => QRCode_Panel,
GameState.Input => Input_Panel,
GameState.CharacterSetting => CharacterSettingPanel,
_ => null
};
if (activePanel != null)
{
activePanel.SetActive(true);
ClearHighlights(activePanel);
}
if (state == GameState.MainMenu)
UpdateMainMenuButtons();
}
private void ClearHighlights(GameObject panel)
{
var highlights = panel.GetComponentsInChildren<UIButtonHighlight>(true);
foreach (var h in highlights)
h.SetHighlighted(false);
}
public void ShowTimer(bool show)
{
if (show)
turnTimerUI.StartTimer();
else
turnTimerUI.StopTimer();
}
public void ShowGameOverMenu(bool isWin, bool isBotGame)
{
ShowPanel(GameState.GameOver);
if(uiParticle != null)
{
uiParticle.SetActive(isWin);
}
if (resultText != null)
{
resultText.text = isWin ? "Õîðîø!" : "Óâû...";
resultText.color = isWin ? new Color(1f, 0.84f, 0f) : Color.white;
gameOverStatusImag.GameOverSwitch(isWin);
// Çîëîòîé èëè êðàñíûé
}
int credits = ComWrapper.GetPlayerCredits(playerId);
// Ñíà÷àëà âñ¸ âûêëþ÷èì
if (continueButton != null) continueButton.gameObject.SetActive(false);
if (replayButton != null) replayButton.gameObject.SetActive(false);
if (isBotGame)
{
if (isWin)
{
// Ïîáåäà ïðîòèâ áîòà êíîïêà "Ïðîäîëæèòü"
if (continueButton != null)
{
continueButton.gameObject.SetActive(true);
continueButton.interactable = true;
}
}
else
{
// Ïîðàæåíèå ïðîòèâ áîòà êíîïêà "Ïåðåèãðàòü"
if (replayButton != null)
{
replayButton.gameObject.SetActive(true);
replayButton.interactable = credits >= 1;
replayButton.GetComponentInChildren<TMP_Text>().text = "Ïåðåèãðàòü";
}
}
}
else
{
// Èãðà ïðîòèâ èãðîêà òîëüêî êíîïêà "Åù¸ ðàç!"
if (replayButton != null)
{
int oponentId = playerId == 1 ? 2 : 1;
int oponentCredits = ComWrapper.GetPlayerCredits(oponentId);
replayButton.gameObject.SetActive(true);
replayButton.interactable = credits >= 2 || (credits >= 1 && oponentCredits >= 1);
replayButton.GetComponentInChildren<TMP_Text>().text = "Åù¸ ðàç!";
}
}
}
public void SetupPlayButtons(bool toggle) // ìåòîä êîòîðûé âêëþ÷àåò êíîïêó â áîé
{
if (playButton != null)
{
playButton.interactable = toggle;
}
}
private void HandleCoinsChanged(int playerID, int newAmount )
{
if (coin != null && playerID == playerId)
{
// îáíîâëÿåì òåêñò
coin.ChangeCoinText($"{newAmount}");
// ïîäñâåòêà
if (newAmount == 0)
coin.FreezeCoin();
else
coin.UnfreezeCoin();
// åñëè êîëè÷åñòâî óâåëè÷èëîñü, çàïóñêàåì àíèìàöèþ ïîïîëíåíèÿ
if (newAmount > lastCoins)
coin.PlayAddCoinsAnimation();
lastCoins = newAmount;
}
UpdateMainMenuButtons();
UpdateGameOverButtons();
}
private void UpdateMainMenuButtons()
{
int myCoins = ComWrapper.GetPlayerCredits(playerId);
int enemyId = playerId == 1 ? 2 : 1;
int enemyCoins = ComWrapper.GetPlayerCredits(enemyId);
// Êíîïêà "Èãðàòü ñ ÈÈ" äîñòóïíà ïðè íàëè÷èè õîòÿ áû 1 ìîíåòû
if (playVsBotButton != null)
playVsBotButton.interactable = myCoins >= 1 || SettingsManager.instance.GetSettingValue("Áåñïëàòíàÿ èãðà") == "Âêë";
// Äëÿ PvP íóæíî, ÷òîáû ó îáîèõ èãðîêîâ áûëî ïî êðàéíåé ìåðå 1 ìîíåòå
bool bothHaveEnough = myCoins >= 2 || (myCoins >= 1 && enemyCoins >= 1 ) || SettingsManager.instance.GetSettingValue("Áåñïëàòíàÿ èãðà") == "Âêë";
if (playVsPlayerButton != null)
playVsPlayerButton.interactable = bothHaveEnough;
}
private void UpdateGameOverButtons() //TODO: Ïåðåäåëàòü ïîä îáà ðåæèìà èãðû
{
int credits = ComWrapper.GetPlayerCredits(playerId);
int oponentId = playerId == 1 ? 2 : 1;
int oponentCredits = ComWrapper.GetPlayerCredits(oponentId);
if (continueButton != null && continueButton.gameObject.activeSelf)
continueButton.interactable = credits >= 1;
if (replayButton != null && replayButton.gameObject.activeSelf)
{
if (gameManager.gameMode == GameMode.PlayerVsBot) replayButton.interactable = credits >= 1;
else replayButton.interactable = credits >= 2 || (credits >= 1 && oponentCredits >= 1);
}
}
}