107 lines
3.6 KiB
C#
107 lines
3.6 KiB
C#
using PrizeSystem;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.Serialization;
|
||
|
||
public class AvailablePrizesTable : MonoBehaviour
|
||
{
|
||
public event Action<Prize> OnPrizeConfigured;
|
||
|
||
[SerializeField] private PrizeButton _prizeButtonPrefab;
|
||
[SerializeField] private RectTransform _buttonsRoot;
|
||
[SerializeField, FormerlySerializedAs("countablePrizeConfigurator")] private CountablePrizeConfigurator _countablePrizeConfigurator;
|
||
[SerializeField, FormerlySerializedAs("qrPrizeConfigurator")] private QrPrizeConfigurator _qrPrizeConfigurator;
|
||
[SerializeField, FormerlySerializedAs("vitrinaPrizeConfigurator")] private VitrinaPrizeConfigurator _vitrinaPrizeConfigurator;
|
||
[SerializeField, FormerlySerializedAs("root")] private RectTransform _configuratorsRoot;
|
||
|
||
private readonly List<PrizeButton> _prizeButtons = new();
|
||
|
||
public IReadOnlyList<PrizeButton> PrizeButtons => _prizeButtons;
|
||
public PrizeButton PrizeButtonPrefab => _prizeButtonPrefab;
|
||
|
||
public void Refresh(PrizeSet prizeSet)
|
||
{
|
||
ClearButtons();
|
||
foreach (PrizeType type in GetMissingPrizeTypes(prizeSet.Prizes))
|
||
{
|
||
PrizeButton button = Instantiate(_prizeButtonPrefab, _buttonsRoot);
|
||
button.Configure(type);
|
||
button.OnPrize += OpenConfigurator;
|
||
_prizeButtons.Add(button);
|
||
}
|
||
}
|
||
|
||
public static List<PrizeType> GetMissingPrizeTypes(IReadOnlyCollection<Prize> prizes)
|
||
{
|
||
PrizeType[] allTypes =
|
||
{
|
||
PrizeType.Hopper,
|
||
PrizeType.Marbles,
|
||
PrizeType.Ticket,
|
||
PrizeType.Qr,
|
||
PrizeType.VitrinaBox
|
||
};
|
||
|
||
bool hasCountablePrize = prizes.Any(prize => prize is CountablePrize);
|
||
HashSet<PrizeType> existingTypes = prizes
|
||
.Where(prize => prize is not EmptyPrize)
|
||
.Select(prize => prize.PrizeType)
|
||
.ToHashSet();
|
||
|
||
return allTypes
|
||
.Where(type => IsCountable(type) ? !hasCountablePrize : !existingTypes.Contains(type))
|
||
.ToList();
|
||
}
|
||
|
||
private void OpenConfigurator(PrizeType type)
|
||
{
|
||
BasePrizeConfigurator prefab = GetConfiguratorPrefab(type);
|
||
if (prefab == null)
|
||
{
|
||
Debug.LogError($"Не найден конфигуратор для приза {type}");
|
||
return;
|
||
}
|
||
|
||
BasePrizeConfigurator configurator = Instantiate(prefab, _configuratorsRoot);
|
||
configurator.OnPrizeConfigured += CompleteConfiguration;
|
||
configurator.Configure(type);
|
||
ControlsPerformer.Instance.SetControlable(configurator);
|
||
}
|
||
|
||
private BasePrizeConfigurator GetConfiguratorPrefab(PrizeType type)
|
||
{
|
||
return type switch
|
||
{
|
||
PrizeType.Hopper => _countablePrizeConfigurator,
|
||
PrizeType.Marbles => _countablePrizeConfigurator,
|
||
PrizeType.Ticket => _countablePrizeConfigurator,
|
||
PrizeType.VitrinaBox => _vitrinaPrizeConfigurator,
|
||
PrizeType.Qr => _qrPrizeConfigurator,
|
||
_ => null
|
||
};
|
||
}
|
||
|
||
private void CompleteConfiguration(Prize prize)
|
||
{
|
||
OnPrizeConfigured?.Invoke(prize);
|
||
}
|
||
|
||
private void ClearButtons()
|
||
{
|
||
_prizeButtons.Clear();
|
||
for (int i = _buttonsRoot.childCount - 1; i >= 0; i--)
|
||
{
|
||
Transform child = _buttonsRoot.GetChild(i);
|
||
child.SetParent(null, false);
|
||
Destroy(child.gameObject);
|
||
}
|
||
}
|
||
|
||
private static bool IsCountable(PrizeType type)
|
||
{
|
||
return type is PrizeType.Hopper or PrizeType.Marbles or PrizeType.Ticket;
|
||
}
|
||
}
|