278 lines
9.0 KiB
C#
278 lines
9.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TcgEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using TcgEngine.Client;
|
|
|
|
namespace TcgEngine.UI
|
|
{
|
|
/// <summary>
|
|
/// Scripts to display all stats of a card,
|
|
/// is used by other script that display cards like BoardCard, and HandCard, CollectionCard..
|
|
/// </summary>
|
|
|
|
public class CardUI : MonoBehaviour, IPointerClickHandler
|
|
{
|
|
public Image card_image;
|
|
public Image frame_image;
|
|
public Image team_icon;
|
|
public Image rarity_icon; // 稀有度
|
|
public Image attack_icon;
|
|
public Image hp_icon;
|
|
public Image cost_icon;
|
|
public Text attack;
|
|
public Text hp;
|
|
public Text cost;
|
|
|
|
public Text card_title;
|
|
public Text card_text;
|
|
|
|
public TraitUI[] stats;
|
|
|
|
public UnityAction<CardUI> onClick;
|
|
public UnityAction<CardUI> onClickRight;
|
|
|
|
private CardData card;
|
|
private VariantData variant;
|
|
|
|
void Awake()
|
|
{
|
|
|
|
}
|
|
|
|
public void SetCard(Card card)
|
|
{
|
|
if (card == null)
|
|
return;
|
|
|
|
SetCard(card.CardData, card.VariantData);
|
|
|
|
if (cost != null)
|
|
cost.text = card.GetMana().ToString();
|
|
if (cost != null && card.CardData.IsDynamicManaCost())
|
|
cost.text = "X";
|
|
if (attack != null)
|
|
attack.text = card.GetAttack().ToString();
|
|
if (hp != null)
|
|
hp.text = card.GetHP().ToString();
|
|
|
|
foreach (TraitUI stat in stats)
|
|
stat.SetCard(card);
|
|
}
|
|
|
|
public void SetCard(CardData card, VariantData variant)
|
|
{
|
|
if (card == null)
|
|
return;
|
|
|
|
bool isSameCard = (this.card == card) && (this.variant == variant);
|
|
this.card = card;
|
|
this.variant = variant;
|
|
|
|
if (!isSameCard)
|
|
{
|
|
if (card_image != null)
|
|
{
|
|
card_image.enabled = true;
|
|
card_image.sprite = card.GetFullArt(variant);
|
|
}
|
|
if (frame_image != null)
|
|
frame_image.sprite = (variant != null) ? variant.frame : frame_image.sprite;
|
|
if (card_title != null)
|
|
card_title.text = card.GetTitle().ToUpper();
|
|
if (card_text != null)
|
|
card_text.text = card.GetDesc();
|
|
}
|
|
|
|
if (attack_icon != null)
|
|
attack_icon.enabled = card.IsCharacter();
|
|
if (attack != null)
|
|
attack.enabled = card.IsCharacter();
|
|
if (hp_icon != null)
|
|
hp_icon.enabled = card.IsBoardCard() || card.IsEquipment();
|
|
if (hp != null)
|
|
hp.enabled = card.IsBoardCard() || card.IsEquipment();
|
|
if (cost_icon != null)
|
|
cost_icon.enabled = card.type != CardType.Hero;
|
|
if (cost != null)
|
|
cost.enabled = card.type != CardType.Hero;
|
|
|
|
if (cost != null)
|
|
cost.text = card.mana.ToString();
|
|
if (cost != null && card.IsDynamicManaCost())
|
|
cost.text = "X";
|
|
if (attack != null)
|
|
attack.text = card.attack.ToString();
|
|
if (hp != null)
|
|
hp.text = card.hp.ToString();
|
|
|
|
if (!isSameCard)
|
|
{
|
|
if (team_icon != null)
|
|
{
|
|
Sprite teamSprite = (card.team != null) ? card.team.icon : null;
|
|
team_icon.sprite = teamSprite;
|
|
team_icon.enabled = teamSprite != null;
|
|
}
|
|
}
|
|
|
|
if (!isSameCard)
|
|
{
|
|
if (rarity_icon != null)
|
|
{
|
|
Sprite raritySprite = (card.rarity != null) ? card.rarity.icon : null;
|
|
rarity_icon.sprite = raritySprite;
|
|
rarity_icon.enabled = raritySprite != null && card.type != CardType.Hero;
|
|
}
|
|
}
|
|
|
|
if (stats != null)
|
|
{
|
|
foreach (TraitUI stat in stats)
|
|
{
|
|
if (stat != null)
|
|
stat.SetCard(card);
|
|
}
|
|
}
|
|
|
|
if (!gameObject.activeSelf)
|
|
gameObject.SetActive(true);
|
|
}
|
|
|
|
public void SetCard(CardData card)
|
|
{
|
|
if (card == null)
|
|
return;
|
|
|
|
bool isSameCard = (this.card == card);
|
|
|
|
this.card = card;
|
|
|
|
if (!isSameCard)
|
|
{
|
|
if (card_image != null)
|
|
{
|
|
card_image.enabled = true;
|
|
card_image.sprite = card.GetVerticalArt();
|
|
}
|
|
if (team_icon != null)
|
|
{
|
|
Sprite teamSprite = (card.team != null) ? card.team.icon : null;
|
|
team_icon.sprite = teamSprite;
|
|
team_icon.enabled = teamSprite != null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetHP(int hp_value)
|
|
{
|
|
if (hp != null)
|
|
hp.text = hp_value.ToString();
|
|
}
|
|
|
|
public void SetMaterial(Material mat)
|
|
{
|
|
if (card_image != null)
|
|
card_image.material = mat;
|
|
if (frame_image != null)
|
|
frame_image.material = mat;
|
|
if (team_icon != null)
|
|
team_icon.material = mat;
|
|
if (rarity_icon != null)
|
|
rarity_icon.material = mat;
|
|
if (attack_icon != null)
|
|
attack_icon.material = mat;
|
|
if (hp_icon != null)
|
|
hp_icon.material = mat;
|
|
if (cost_icon != null)
|
|
cost_icon.material = mat;
|
|
}
|
|
|
|
public void SetOpacity(float opacity)
|
|
{
|
|
if (card_image != null)
|
|
card_image.color = new Color(card_image.color.r, card_image.color.g, card_image.color.b, opacity);
|
|
if (frame_image != null)
|
|
frame_image.color = new Color(frame_image.color.r, frame_image.color.g, frame_image.color.b, opacity);
|
|
if (team_icon != null)
|
|
team_icon.color = new Color(team_icon.color.r, team_icon.color.g, team_icon.color.b, opacity);
|
|
if (rarity_icon != null)
|
|
rarity_icon.color = new Color(rarity_icon.color.r, rarity_icon.color.g, rarity_icon.color.b, opacity);
|
|
if (attack_icon != null)
|
|
attack_icon.color = new Color(attack_icon.color.r, attack_icon.color.g, attack_icon.color.b, opacity);
|
|
if (hp_icon != null)
|
|
hp_icon.color = new Color(hp_icon.color.r, hp_icon.color.g, hp_icon.color.b, opacity);
|
|
if (cost_icon != null)
|
|
cost_icon.color = new Color(cost_icon.color.r, cost_icon.color.g, cost_icon.color.b, opacity);
|
|
if (attack != null)
|
|
attack.color = new Color(attack.color.r, attack.color.g, attack.color.b, opacity);
|
|
if (hp != null)
|
|
hp.color = new Color(hp.color.r, hp.color.g, hp.color.b, opacity);
|
|
if (cost != null)
|
|
cost.color = new Color(cost.color.r, cost.color.g, cost.color.b, opacity);
|
|
if (card_title != null)
|
|
card_title.color = new Color(card_title.color.r, card_title.color.g, card_title.color.b, opacity);
|
|
if (card_text != null)
|
|
card_text.color = new Color(card_text.color.r, card_text.color.g, card_text.color.b, opacity);
|
|
}
|
|
#region ================== 对象池友好接口 ==================
|
|
|
|
/// <summary>
|
|
/// 重置数据并隐藏
|
|
/// </summary>
|
|
public void ResetLine()
|
|
{
|
|
card = null;
|
|
variant = null;
|
|
Hide();
|
|
if (card_image) card_image.enabled = false;
|
|
if (frame_image) frame_image.enabled = false;
|
|
if (team_icon) team_icon.enabled = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 展开用户卡组信息
|
|
/// </summary>
|
|
public void Refresh(CardData card, VariantData variant)
|
|
{
|
|
SetCard(card);
|
|
}
|
|
|
|
|
|
#endregion
|
|
public void Hide()
|
|
{
|
|
if (gameObject.activeSelf)
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (eventData.button == PointerEventData.InputButton.Left)
|
|
{
|
|
if (onClick != null)
|
|
onClick.Invoke(this);
|
|
}
|
|
|
|
if (eventData.button == PointerEventData.InputButton.Right)
|
|
{
|
|
if (onClickRight != null)
|
|
onClickRight.Invoke(this);
|
|
}
|
|
}
|
|
|
|
public CardData GetCard()
|
|
{
|
|
return card;
|
|
}
|
|
|
|
public VariantData GetVariant()
|
|
{
|
|
return variant;
|
|
}
|
|
}
|
|
}
|