227 lines
5.6 KiB
C#
227 lines
5.6 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;
|
|
public Image costNumberImage; // 费用数字图片
|
|
|
|
[Header("费用数字精灵")]
|
|
public Sprite[] costNumberSprites; // 数字_0 到 数字_8,对应费用 1-9
|
|
|
|
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 (characterData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 决战模式:有布阵管理器时走选兵逻辑,不消耗金币。
|
|
if (BattleSetupManager.Instance != null)
|
|
{
|
|
BattleSetupManager.Instance.SelectCharacter(characterData);
|
|
return;
|
|
}
|
|
|
|
// 建筑模式:检查建筑费用是否足够
|
|
if (GoldSystem.Instance == null || GoldSystem.Instance.CurrentGold < characterData.buildingCost)
|
|
{
|
|
Debug.Log($"金币不足,需要{characterData.buildingCost}");
|
|
return;
|
|
}
|
|
|
|
// 触发建筑放置模式
|
|
if (BuildingPlacementManager.Instance != null)
|
|
{
|
|
BuildingPlacementManager.Instance.StartPlacingBuilding(characterData);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("找不到 BuildingPlacementManager");
|
|
}
|
|
}
|
|
|
|
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 (characterData == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// 决战模式按人口判断。
|
|
if (BattleSetupManager.Instance != null)
|
|
{
|
|
return BattleSetupManager.Instance.CanAfford(characterData);
|
|
}
|
|
|
|
// 建筑模式按金币判断。
|
|
if (GoldSystem.Instance == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return GoldSystem.Instance.CurrentGold >= characterData.buildingCost;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
// 更新费用数字精灵
|
|
UpdateCostNumberSprite();
|
|
}
|
|
|
|
private void UpdateCostNumberSprite()
|
|
{
|
|
if (costNumberImage == null || characterData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int cost = characterData.manaCost;
|
|
|
|
// 费用范围 1-9,对应精灵索引 0-8
|
|
if (cost >= 1 && cost <= 9 && costNumberSprites != null && costNumberSprites.Length >= cost)
|
|
{
|
|
costNumberImage.sprite = costNumberSprites[cost - 1];
|
|
costNumberImage.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
// 超出范围或未配置精灵数组,隐藏图片
|
|
costNumberImage.enabled = false;
|
|
}
|
|
}
|
|
} |