118 lines
3.0 KiB
C#
118 lines
3.0 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using GeometryTD.CustomEvent;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class TowerSelectItem : MonoBehaviour
|
|
{
|
|
[SerializeField] private Image _icon;
|
|
|
|
[SerializeField] private TMP_Text _price;
|
|
|
|
[SerializeField] private CommonButton _button;
|
|
|
|
[SerializeField] private TowerIconArea _towerIconArea;
|
|
|
|
private TowerSelectItemContext _context;
|
|
|
|
private Sprite _defaultIcon;
|
|
|
|
private bool _hasCachedDefaultIcon;
|
|
|
|
public void OnInit(TowerSelectItemContext context)
|
|
{
|
|
CacheDefaultIcon();
|
|
_context = context;
|
|
|
|
if (context.ActionType == CombatSelectActionType.BuildTower)
|
|
{
|
|
_icon?.gameObject.SetActive(false);
|
|
_towerIconArea?.OnInit(_context.TowerIconAreaContext);
|
|
|
|
if (_price != null)
|
|
{
|
|
_price.text = context.PriceText ?? string.Empty;
|
|
}
|
|
|
|
if (_button != null)
|
|
{
|
|
_button.Interactive = context.IsInteractable;
|
|
}
|
|
}
|
|
else if (context.ActionType is CombatSelectActionType.UpgradeTower or CombatSelectActionType.DestroyTower)
|
|
{
|
|
if (_icon != null)
|
|
{
|
|
_icon.sprite = _context.Icon ?? _defaultIcon;
|
|
_icon.gameObject.SetActive(true);
|
|
}
|
|
|
|
_towerIconArea?.OnReset();
|
|
|
|
if (_price != null)
|
|
{
|
|
_price.text = context.PriceText ?? string.Empty;
|
|
}
|
|
|
|
if (_button != null)
|
|
{
|
|
_button.Interactive = context.IsInteractable;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_icon?.gameObject.SetActive(false);
|
|
_towerIconArea?.OnReset();
|
|
}
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_context = null;
|
|
CacheDefaultIcon();
|
|
|
|
if (_icon != null)
|
|
{
|
|
_icon.sprite = _defaultIcon;
|
|
_icon.enabled = _icon.sprite != null;
|
|
}
|
|
|
|
_towerIconArea.OnReset();
|
|
|
|
if (_price != null)
|
|
{
|
|
_price.text = string.Empty;
|
|
}
|
|
|
|
if (_button != null)
|
|
{
|
|
_button.Interactive = false;
|
|
}
|
|
}
|
|
|
|
public void OnClick()
|
|
{
|
|
if (_context == null || !_context.IsInteractable)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(
|
|
this,
|
|
CombatSelectItemClickEventArgs.Create(_context.ActionType, _context.ActionIndex));
|
|
}
|
|
|
|
private void CacheDefaultIcon()
|
|
{
|
|
if (_hasCachedDefaultIcon || _icon == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_defaultIcon = _icon.sprite;
|
|
_hasCachedDefaultIcon = true;
|
|
}
|
|
}
|
|
} |