using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class BuffSelectionController : MonoBehaviour { private static BuffSelectionController _instance; public static BuffSelectionController Instance { get { if (_instance == null) { _instance = FindObjectOfType(); if (_instance == null) { var canvas = GameObject.Find("TowerDefenseCanvas"); var targetObj = canvas != null ? canvas : new GameObject("BuffSelectionControllerManager"); _instance = targetObj.AddComponent(); } } return _instance; } } public static float KillGoldMultiplier = 1.0f; // 全局击杀金币收益倍率 public enum BuffType { AttackPower, // 1. 攻击力 (+6% / +12% / +20%) MaxHealth, // 2. 最大生命值 (+10% / +18% / +30%) AttackSpeed, // 3. 攻击速度 (+5% / +10% / +16%) CritRate, // 4. 暴击率 (+6% / +12% / +20%) Lifesteal, // 5. 通用吸血 (+4% / +8% / +14%) SkillCharge, // 6. 技能充能 (+8% / +15% / +25%) SummonerCD, // 7. 召唤师技能CD (-5% / -10% / -18%) SummonerPower, // 8. 召唤师技能威力 (+8% / +15% / +25%) AttackRange, // 9. 攻击射程 (+0.5米 / +1.0米 / +1.8米) DamageReduction, // 10. 伤害减免 (+5% / +10% / +16%) Shield, // 11. 护盾生成 (15% / 25% / 40% MaxHP) KillGold, // 12. 击杀金币 (+15% / +30% / +50%) GoldBonus, // 特殊 1: 招财进宝 (+30 / +60 / +100 金币) EmergencyHeal // 特殊 2: 紧急救急 (恢复 25% / 40% / 60% HP) } public enum BuffQuality { Common, // 初级 (白色 85%) Rare, // 中级 (紫色 10%) Epic, // 高级 (金色 5%) Legendary // 顶级 (红色/酒红 预留) } public class BuffOption { public BuffType type; public BuffQuality quality; public int targetHeroIndex; // -1 表示【全体英雄】或【全局】,0~4 表示指定单体英雄 public string targetName; public float rawValue; // 单体原始加成数值 (如 0.06f) public float finalValue; // 计算 40% 折扣后的最终加成数值 public string titleText; public string descText; public Color qualityColor; public string qualityName; public Sprite iconSprite; // 英雄头像 / 全体头像 } [Header("UI 节点引用")] public GameObject buffSelectionWindow; public Transform cardsContainer; public Button rollButton; // 刷新按钮 public int rollCost = 2; // 每次刷新消耗金币 [Header("品质边框 Sprites")] public Sprite commonBorderSprite; // 铜 (初级) public Sprite rareBorderSprite; // 银 (中级) public Sprite epicBorderSprite; // 金 (高级) public Sprite legendaryBorderSprite; // 红 (顶级) [Header("头像 Sprites")] public Sprite allIconSprite; // 全体/全局头像 (all.png) private bool isWindowShowing = false; // 3 个卡槽各自独立追踪的品质与递进未升阶计数 private BuffQuality[] slotQualities = new BuffQuality[3]; private int[] slotNonUpgradeCounts = new int[3]; private void Awake() { if (_instance == null) { _instance = this; } } /// /// 触发 3 选 1 增益弹窗(自动暂停游戏) /// public void ShowBuffSelection() { if (isWindowShowing) return; EnsureUIReferences(); if (buffSelectionWindow == null || cardsContainer == null) { Debug.LogWarning("BuffSelectionController: UI 节点缺失,无法弹出 3 选 1。"); return; } // 初始弹窗时重置 3 个卡槽的累积计数,并按初始基础概率独立抽品质 (初级 85% / 中级 10% / 高级 5%) for (int i = 0; i < 3; i++) { slotNonUpgradeCounts[i] = 0; float roll = Random.value; if (roll < 0.85f) { slotQualities[i] = BuffQuality.Common; } else if (roll < 0.95f) { slotQualities[i] = BuffQuality.Rare; } else { slotQualities[i] = BuffQuality.Epic; } } // 生成 3 个卡槽的增益选项 List options = GenerateOptionsForSlots(); RenderOptionsUI(options); // 绑定刷新按钮事件 if (rollButton != null) { rollButton.onClick.RemoveAllListeners(); rollButton.onClick.AddListener(OnRollButtonClicked); } buffSelectionWindow.SetActive(true); isWindowShowing = true; Time.timeScale = 0f; // 暂停游戏让玩家做选择 } /// /// 点击刷新按钮 (耗费 2 金币),3 个卡槽独立计算升阶概率并重新生成选项 /// private void OnRollButtonClicked() { if (GoldManager.Instance != null && !GoldManager.Instance.TrySpendGold(rollCost)) { Debug.LogWarning($"金币不足 {rollCost},无法刷新!"); return; } // 3 个卡槽独立判定升级逻辑 for (int i = 0; i < 3; i++) { BuffQuality currentQ = slotQualities[i]; bool upgraded = false; BuffQuality targetQ = currentQ; if (currentQ == BuffQuality.Common) { // 初级 升 中级 10% 每次 +10% (第2次20%、第3次30%...) float chance = 0.10f + slotNonUpgradeCounts[i] * 0.10f; if (Random.value < chance) { targetQ = BuffQuality.Rare; upgraded = true; } } else if (currentQ == BuffQuality.Rare) { // 中级 升 高级 3% 每次 +5% (第2次8%、第3次13%...) float chance = 0.03f + slotNonUpgradeCounts[i] * 0.05f; if (Random.value < chance) { targetQ = BuffQuality.Epic; upgraded = true; } } else if (currentQ == BuffQuality.Epic) { // 高级 升 顶级 1% (预留) 每次 +2% float chance = 0.01f + slotNonUpgradeCounts[i] * 0.02f; if (Random.value < chance) { targetQ = BuffQuality.Legendary; upgraded = true; } } if (upgraded) { slotQualities[i] = targetQ; slotNonUpgradeCounts[i] = 0; // 刷出升阶,该槽位的追加概率重置 } else { if (currentQ != BuffQuality.Legendary) { slotNonUpgradeCounts[i]++; // 未升阶,追加概率增加 } } } Debug.Log($"玩家消耗 {rollCost} 金币刷新卡牌! 当前槽位品质: [{slotQualities[0]}], [{slotQualities[1]}], [{slotQualities[2]}]"); // 重新渲染 3 个卡槽 List newOptions = GenerateOptionsForSlots(); RenderOptionsUI(newOptions); } private void RenderOptionsUI(List options) { int cardCount = cardsContainer.childCount; for (int i = 0; i < cardCount; i++) { var card = cardsContainer.GetChild(i); if (i < options.Count) { card.gameObject.SetActive(true); SetupCardUI(card, options[i], i); } else { card.gameObject.SetActive(false); } } } private float GetNextUpgradeChance(int slotIndex) { if (slotIndex < 0 || slotIndex >= 3) return 0f; BuffQuality currentQ = slotQualities[slotIndex]; int count = slotNonUpgradeCounts[slotIndex]; if (currentQ == BuffQuality.Common) { return 0.10f + count * 0.10f; // 10% + n * 10% } else if (currentQ == BuffQuality.Rare) { return 0.03f + count * 0.05f; // 3% + n * 5% } else if (currentQ == BuffQuality.Epic) { return 0.01f + count * 0.02f; // 1% + n * 2% } return 0f; // 顶级无升级 } private void SetupCardUI(Transform cardTransform, BuffOption option, int slotIndex) { Text buffNameText = cardTransform.Find("BuffName")?.GetComponent(); Text descText = cardTransform.Find("Description")?.GetComponent(); Text upgradeRateText = cardTransform.Find("UpgradeRateText")?.GetComponent(); if (buffNameText != null) { buffNameText.text = $"[{option.qualityName}] {option.titleText}"; buffNameText.color = option.qualityColor; } if (descText != null) { descText.text = option.descText; } if (upgradeRateText != null) { if (option.quality == BuffQuality.Legendary) { upgradeRateText.text = "已达最高品质"; } else { float chance = GetNextUpgradeChance(slotIndex); upgradeRateText.text = $"升阶概率: {Mathf.Min(100f, chance * 100f):F0}%"; } } // 设置目标英雄 / 全体 Icon 头像 Image iconImage = cardTransform.Find("Icon")?.GetComponent(); if (iconImage != null && option.iconSprite != null) { iconImage.sprite = option.iconSprite; iconImage.enabled = true; } // 设置品质边框 (仅更换 buffImage 节点的源图像,不修改缩放和位置) Image borderImage = cardTransform.Find("buffImage")?.GetComponent(); if (borderImage != null) { Sprite borderSprite = GetQualityBorderSprite(option.quality); if (borderSprite != null) { borderImage.sprite = borderSprite; } } // 卡牌整体点击选定 Button selectBtn = cardTransform.GetComponent