2026-04-18 12:19:27 +03:00

127 lines
4.2 KiB
C#

using PrizeSystem;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
//Это как шкала че есть в витрине чего нет.
public class VitrinaView : MonoBehaviour
{
private static VitrinaView instance;
public static VitrinaView Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<VitrinaView>();
instance.Deactivate();
}
return instance;
}
}
[SerializeField] private List<Image> Cells;
[SerializeField] private Sprite AvailableCell;
[SerializeField] private Sprite AvailableEmptyCell;
[SerializeField] private Sprite NotAvailableCell;
[SerializeField] private Color AvailableColor;
[SerializeField] private Color AvailableEmptyColor;
[SerializeField] private Color NotAvailableColor;
public void ActivateIfNeed()
{
if (GameManager.instance.gameMode == GameMode.pvp)
{
Deactivate();
return;
}
bool hasPrize = CharacterController.CurrentCharacter.HasPrize;
if (hasPrize && CharacterController.CurrentCharacter.prize is VitrinaPrize vitrinaPrize)
{
gameObject.SetActive(true);
// Устанавливаем номера ячеек и базовое состояние
for (int i = 0; i < Cells.Count; i++)
{
int boxNumber = i + 1;
var text = Cells[i].GetComponentInChildren<TextMeshProUGUI>();
if (text != null)
{
text.text = boxNumber.ToString(); // Устанавливаем номер ячейки
text.color = NotAvailableColor;
}
Cells[i].sprite = NotAvailableCell;
Cells[i].color = Color.white;
}
SetPrizesAsync(vitrinaPrize);
return;
}
Deactivate();
}
public async void SetPrizesAsync(VitrinaPrize vitrinaPrize)
{
try
{
int[] readyBoxes = await PrizeStandController.GetReadyBoxesAsync();
int[] boxesToGive = vitrinaPrize?.Boxes ?? new int[0];
HashSet<int> readyBoxesSet = new HashSet<int>(readyBoxes);
HashSet<int> boxesToGiveSet = new HashSet<int>(boxesToGive);
// Проходим по всем ячейкам (индексы 0-based, номера ячеек 1-based)
for (int i = 0; i < Cells.Count; i++)
{
int boxNumber = i + 1;
var text = Cells[i].GetComponentInChildren<TextMeshProUGUI>();
// Убеждаемся, что номер ячейки всегда отображается
if (text != null && string.IsNullOrEmpty(text.text))
{
text.text = boxNumber.ToString();
}
if (boxesToGiveSet.Contains(boxNumber))
{
// Ячейка должна быть выдана, проверяем доступность
if (readyBoxesSet.Contains(boxNumber))
{
Cells[i].sprite = AvailableCell;
if (text != null) text.color = AvailableColor;
}
else
{
Cells[i].sprite = AvailableEmptyCell;
if (text != null) text.color = AvailableEmptyColor;
}
}
else
{
// Ячейка не входит в список для выдачи
Cells[i].sprite = NotAvailableCell;
if (text != null)
{
text.color = NotAvailableColor;
}
Cells[i].color = NotAvailableColor;
}
}
}
catch (Exception ex)
{
Debug.LogError($"Ошибка при установке призов: {ex.Message}");
}
}
public void Deactivate()
{
gameObject.SetActive(false);
}
}