chip-gameV3/Assets/AvailablePrizesTable.cs

145 lines
4.6 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.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class AvailablePrizesTable : MonoBehaviour
{
public PrizeButton _prizeButtonPrefab;
[SerializeField] RectTransform _buttonsRoot;
public List<PrizeButton> prizeButtons;
public CountablePrizeConfigurator countablePrizeConfigurator;
public QrPrizeConfigurator qrPrizeConfigurator;
public VitrinaPrizeConfigurator vitrinaPrizeConfigurator;
public PrizeSet cachedSet;
public RectTransform root;
public PrizesSelector selector;
public void Refresh(PrizeSet set)
{
cachedSet = set;
ClearButtons();
prizeButtons = new();
foreach (var type in GetMissingPrizeTypes(set.Prizes))
{
var button = Instantiate(_prizeButtonPrefab, _buttonsRoot);
button.Configure(type);
button.OnPrize += OnPrizeTypeSelected;
prizeButtons.Add(button);
}
}
private void OnPrizeTypeSelected(PrizeType type)
{
print($"Выбран приз {type}");
BasePrizeConfigurator window = type switch
{
PrizeType.Hopper => countablePrizeConfigurator,
PrizeType.Marbles => countablePrizeConfigurator,
PrizeType.Ticket => countablePrizeConfigurator,
PrizeType.VitrinaBox => vitrinaPrizeConfigurator,
PrizeType.Qr => qrPrizeConfigurator
};
var configurator = Instantiate(window, root);
configurator.Configure(type);
configurator.OnPrizeConfigured += Configurator_OnPrizeConfigured;
ControlsPerformer.Instance.SetControlable(configurator);
}
private void Configurator_OnPrizeConfigured(Prize prize)
{
cachedSet.Prizes.Add(prize);
Refresh(cachedSet);
selector.skipFrame = true;
}
private void ClearButtons()
{
for (int i = 0; i < _buttonsRoot.childCount; i++)
{
var go = _buttonsRoot.GetChild(i).gameObject;
Destroy(go);
}
}
public static List<PrizeType> GetMissingPrizeTypes(List<Prize> prizes)
{
// Все типы призов (кроме None и Empty)
var allTypes = new List<PrizeType>
{
PrizeType.Hopper,
PrizeType.Marbles,
PrizeType.Ticket,
PrizeType.Qr,
PrizeType.VitrinaBox
};
// Определяем, есть ли в списке хоть один CountablePrize
bool hasCountablePrize = prizes.Any(p => p is CountablePrize);
// Если есть CountablePrize, все счетные типы считаются добавленными
var countableTypes = new HashSet<PrizeType>
{
PrizeType.Hopper,
PrizeType.Marbles,
PrizeType.Ticket
};
// Собираем существующие типы
var existingTypes = new HashSet<PrizeType>();
foreach (var prize in prizes)
{
// Если это EmptyPrize - пропускаем (не влияет на существующие типы)
if (prize is EmptyPrize) continue;
// Если это CountablePrize и у нас уже есть флаг, что счетные есть
if (prize is CountablePrize)
{
// Добавляем все счетные типы, если их еще нет
if (hasCountablePrize)
{
foreach (var type in countableTypes)
{
existingTypes.Add(type);
}
}
else
{
// На случай, если hasCountablePrize false (но такого быть не может)
existingTypes.Add(prize.PrizeType);
}
}
else
{
// Для не-счетных призов добавляем их конкретный тип
existingTypes.Add(prize.PrizeType);
}
}
// Если есть CountablePrize, но не все типы добавлены, добавляем все счетные
if (hasCountablePrize)
{
foreach (var type in countableTypes)
{
existingTypes.Add(type);
}
}
// Возвращаем типы, которых нет в существующих
return allTypes
.Where(type => !existingTypes.Contains(type))
.ToList();
}
}