145 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>();
CharacterEventBus.Instance.OnCharacterAppeared.AddListener(instance.ActivateIfNeed);
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;
//private void Start()
//{
// instance = this;
// CharacterEventBus.Instance.OnCharacterAppeared.AddListener(ActivateIfNeed);
//}
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();
// Получаем ячейки, которые нужно выдать из VitrinaPrize
int[] boxesToGive = vitrinaPrize?.Boxes ?? new int[0];
// Создаем HashSet для быстрого поиска готовых ячеек
HashSet<int> readyBoxesSet = new HashSet<int>(readyBoxes);
// Создаем HashSet для быстрого поиска ячеек, которые нужно выдать
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);
}
}