using PrizeSystem; using System; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; //Это как шкала че есть в витрине чего нет. public class VitrinaView : MonoBehaviour { public static VitrinaView instance; [SerializeField] private List 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); Deactivate(); } public void ActivateIfNeed() { bool hasPrize = CharacterController.CurrentCharacter.HasPrize; if (hasPrize && CharacterController.CurrentCharacter.prize is VitrinaPrize vitrinaPrize) { gameObject.SetActive(true); foreach (var cell in Cells) { cell.sprite = NotAvailableCell; cell.GetComponentInChildren().color = NotAvailableColor; } 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 readyBoxesSet = new HashSet(readyBoxes); // Создаем HashSet для быстрого поиска ячеек, которые нужно выдать HashSet boxesToGiveSet = new HashSet(boxesToGive); // Проходим по всем ячейкам (индексы 0-based, номера ячеек 1-based) for (int i = 0; i < Cells.Count; i++) { int boxNumber = i + 1; // Преобразуем индекс в номер ячейки (1-based) if (boxesToGiveSet.Contains(boxNumber)) { // Ячейка должна быть выдана, проверяем доступность if (readyBoxesSet.Contains(boxNumber)) { Cells[i].sprite = AvailableCell; // Доступна для выдачи Cells[i].GetComponentInChildren().color = AvailableColor; } else { Cells[i].sprite = AvailableEmptyCell; // Недоступна для выдачи Cells[i].GetComponentInChildren().color = AvailableEmptyColor; } } else { // Ячейка не входит в список для выдачи Cells[i].sprite = NotAvailableCell; Cells[i].GetComponentInChildren().color = NotAvailableColor; } } } catch (Exception ex) { Debug.LogError($"Ошибка при установке призов: {ex.Message}"); } } public void Deactivate() { gameObject.SetActive(false); } }