172 lines
3.9 KiB
C#
172 lines
3.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[RequireComponent(typeof(Button))]
|
|
public class SummonCardButton : MonoBehaviour
|
|
{
|
|
[Header("角色数据")]
|
|
public CharacterData characterData;
|
|
|
|
[Header("卡槽配置")]
|
|
[Min(1)]
|
|
public int summonSlot = 1;
|
|
|
|
[Header("UI 引用")]
|
|
public Image cooldownMask;
|
|
public Text cooldownText;
|
|
public Image cardIcon;
|
|
public Text characterNameText;
|
|
public Text manaCostText;
|
|
|
|
private Button button;
|
|
private MainCharacterCombat combat;
|
|
private float cooldownTimer;
|
|
private bool isOnCooldown;
|
|
|
|
private void Awake()
|
|
{
|
|
button = GetComponent<Button>();
|
|
combat = FindObjectOfType<MainCharacterCombat>();
|
|
button.onClick.AddListener(OnClick);
|
|
|
|
isOnCooldown = false;
|
|
cooldownTimer = 0f;
|
|
|
|
UpdateCardUI();
|
|
UpdateCooldownUI();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (button != null)
|
|
{
|
|
button.onClick.RemoveListener(OnClick);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isOnCooldown)
|
|
{
|
|
cooldownTimer -= Time.deltaTime;
|
|
if (cooldownTimer <= 0f)
|
|
{
|
|
isOnCooldown = false;
|
|
cooldownTimer = 0f;
|
|
}
|
|
|
|
UpdateCooldownUI();
|
|
}
|
|
|
|
UpdateButtonState();
|
|
}
|
|
|
|
public void OnClick()
|
|
{
|
|
if (isOnCooldown || characterData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (combat == null)
|
|
{
|
|
combat = FindObjectOfType<MainCharacterCombat>();
|
|
}
|
|
|
|
if (combat != null && combat.RequestSummon(summonSlot))
|
|
{
|
|
StartCooldown();
|
|
}
|
|
}
|
|
|
|
private void StartCooldown()
|
|
{
|
|
if (characterData != null)
|
|
{
|
|
isOnCooldown = true;
|
|
cooldownTimer = characterData.cooldownTime;
|
|
UpdateCooldownUI();
|
|
}
|
|
}
|
|
|
|
private void UpdateCooldownUI()
|
|
{
|
|
bool canAfford = CanAfford();
|
|
|
|
if (cooldownMask != null)
|
|
{
|
|
// CD中显示CD进度,能量不足时显示满遮罩
|
|
if (isOnCooldown)
|
|
{
|
|
cooldownMask.fillAmount = cooldownTimer / GetCooldownTime();
|
|
cooldownMask.enabled = true;
|
|
}
|
|
else if (!canAfford)
|
|
{
|
|
// 能量不足:显示满遮罩
|
|
cooldownMask.fillAmount = 1f;
|
|
cooldownMask.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
// 可用状态:隐藏遮罩
|
|
cooldownMask.fillAmount = 0f;
|
|
cooldownMask.enabled = false;
|
|
}
|
|
}
|
|
|
|
if (cooldownText != null)
|
|
{
|
|
cooldownText.text = isOnCooldown ? Mathf.CeilToInt(cooldownTimer).ToString() : "";
|
|
cooldownText.enabled = isOnCooldown;
|
|
}
|
|
}
|
|
|
|
private void UpdateButtonState()
|
|
{
|
|
if (button == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
button.interactable = !isOnCooldown && CanAfford();
|
|
}
|
|
|
|
private bool CanAfford()
|
|
{
|
|
if (ManaSystem.Instance == null || characterData == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return ManaSystem.Instance.CurrentManaInt >= characterData.manaCost;
|
|
}
|
|
|
|
private float GetCooldownTime()
|
|
{
|
|
return characterData != null ? characterData.cooldownTime : 5f;
|
|
}
|
|
|
|
private void UpdateCardUI()
|
|
{
|
|
if (characterData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (cardIcon != null && characterData.cardIcon != null)
|
|
{
|
|
cardIcon.sprite = characterData.cardIcon;
|
|
}
|
|
|
|
if (characterNameText != null)
|
|
{
|
|
characterNameText.text = characterData.characterName;
|
|
}
|
|
|
|
if (manaCostText != null)
|
|
{
|
|
manaCostText.text = characterData.manaCost.ToString();
|
|
}
|
|
}
|
|
} |