75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// Îòâå÷àåò çà îáíîâëåíèå interactable ñîñòîÿíèÿ êíîïîê ãëàâíîãî ìåíþ ïî áàëàíñó
|
|
/// </summary>
|
|
public class MainMenuButtonsUpdater : MonoBehaviour
|
|
{
|
|
[SerializeField] private int playerId;
|
|
[SerializeField] private Button playVsBotButton;
|
|
[SerializeField] private Button playVsPlayerButton;
|
|
|
|
private bool eventsSubscribed;
|
|
|
|
private void OnEnable()
|
|
{
|
|
ComWrapper.OnInitialize += Subscribe;
|
|
|
|
if (!eventsSubscribed && (ComWrapper.onCoinAdded != null || ComWrapper.onCoinRemoved != null))
|
|
Subscribe();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (eventsSubscribed)
|
|
{
|
|
if (ComWrapper.onCoinAdded != null) ComWrapper.onCoinAdded.RemoveListener(OnCoinsChanged);
|
|
if (ComWrapper.onCoinRemoved != null) ComWrapper.onCoinRemoved.RemoveListener(OnCoinsChanged);
|
|
eventsSubscribed = false;
|
|
}
|
|
|
|
ComWrapper.OnInitialize -= Subscribe;
|
|
}
|
|
|
|
private void Subscribe()
|
|
{
|
|
if (eventsSubscribed) return;
|
|
|
|
if (ComWrapper.onCoinAdded != null) ComWrapper.onCoinAdded.AddListener(OnCoinsChanged);
|
|
if (ComWrapper.onCoinRemoved != null) ComWrapper.onCoinRemoved.AddListener(OnCoinsChanged);
|
|
|
|
eventsSubscribed = true;
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void OnCoinsChanged(int playerID, int newAmount)
|
|
{
|
|
UpdateButtons();
|
|
}
|
|
|
|
public void UpdateButtons()
|
|
{
|
|
int myCoins = ComWrapper.GetPlayerCredits(playerId);
|
|
int enemyId = playerId == 1 ? 2 : 1;
|
|
int enemyCoins = ComWrapper.GetPlayerCredits(enemyId);
|
|
|
|
bool freePlay = SettingsManager.instance != null &&
|
|
SettingsManager.instance.GetSettingValue("Áåñïëàòíàÿ èãðà") == "Âêë";
|
|
|
|
if (playVsBotButton != null)
|
|
playVsBotButton.interactable = myCoins >= 1 || freePlay;
|
|
|
|
bool bothHaveEnough = myCoins >= 2 || (myCoins >= 1 && enemyCoins >= 1) || freePlay;
|
|
|
|
if (playVsPlayerButton != null)
|
|
playVsPlayerButton.interactable = bothHaveEnough;
|
|
}
|
|
}
|
|
|