mirror of
https://github.com/ryzij/Cube-Tower-Defense.git
synced 2026-07-14 01:56:57 +00:00
86 lines
2.4 KiB
C#
86 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[RequireComponent(typeof(Animator))]
|
|
public class TurretsShopViewer : MonoBehaviour
|
|
{
|
|
[SerializeField] private TurretsShop _shop;
|
|
[SerializeField] private Wallet _wallet;
|
|
[SerializeField] private Button _buttonPrefab;
|
|
[SerializeField] private List<BlockScript> _ignoreBlocks;
|
|
|
|
private Animator _animator;
|
|
private bool _hidden = true;
|
|
private readonly Dictionary<Button, TurretShopItem> _buttons = new();
|
|
|
|
private static readonly int _hideAnimation = Animator.StringToHash("HideTurretShopPanelAnimation");
|
|
private static readonly int _showAnimation = Animator.StringToHash("ShowTurretShopPanelAnimation");
|
|
|
|
private void Start()
|
|
{
|
|
foreach (var item in _shop.TurretShopItems)
|
|
{
|
|
var button = Instantiate(_buttonPrefab, transform);
|
|
var icon = button.GetComponentInChildrenWithoutParent<Image>();
|
|
if (icon != null && item.Icon != null)
|
|
{
|
|
icon.gameObject.SetActive(true);
|
|
icon.sprite = item.Icon;
|
|
}
|
|
|
|
button.onClick.AddListener(() => _shop.BuyTurret(item));
|
|
_buttons.Add(button, item);
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
_animator = GetComponent<Animator>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
_wallet.OnMoneyChanged += OnMoneyChanged;
|
|
_shop.OnBlockSelected += OnBlockSelected;
|
|
_shop.OnBlockDeselected += OnBlockDeselected;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_wallet.OnMoneyChanged -= OnMoneyChanged;
|
|
_shop.OnBlockSelected -= OnBlockSelected;
|
|
_shop.OnBlockDeselected -= OnBlockDeselected;
|
|
}
|
|
|
|
private void OnBlockSelected(BlockScript block)
|
|
{
|
|
if (!_hidden)
|
|
return;
|
|
|
|
// Чтобы анимация не воспроизводилась когда не надо
|
|
if (_ignoreBlocks.Count > 0 && _ignoreBlocks.Contains(block))
|
|
{
|
|
_ignoreBlocks.Clear();
|
|
return;
|
|
}
|
|
|
|
_animator.CrossFade(_showAnimation, 0.1f);
|
|
_hidden = false;
|
|
}
|
|
|
|
private void OnBlockDeselected()
|
|
{
|
|
_animator.CrossFade(_hideAnimation, 0.1f);
|
|
_hidden = true;
|
|
}
|
|
|
|
private void OnMoneyChanged(int money)
|
|
{
|
|
foreach (var (button, item) in _buttons)
|
|
{
|
|
button.interactable = money >= item.Price;
|
|
}
|
|
}
|
|
}
|