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; private Animator _animator; private bool _hidden = true; private readonly Dictionary _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(); 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(); } 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; _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; } } }