// Assets/_Project/Scripts/Gameplay/Draft/TowerUpgradeDraftOption.cs using UnityEngine; using TD.Towers; namespace TD.Gameplay.Draft { /// /// Draft choice — "unlock an upgrade". Adds to the player's /// set, letting them spend gold converting a placed /// into it. The base tower stays buildable. /// /// /// Unlock, not swap. This option used to replace the base tower in the player's /// deck, which made the draft pick itself the whole upgrade — free, instant, and retroactive to /// every tower built afterward. The 2.0 model splits it: the draft grants access, gold /// grants the tower. So the deck is untouched here and already-placed towers are /// upgraded one at a time, each paid for. /// /// Prerequisites fall out of the tree. An upgrade is offerable only once its /// parent is reachable — either the parent is a base tower in the player's deck, or it's an /// upgrade node they've already unlocked. That single rule is what gates deep nodes behind /// shallow ones without any explicit prerequisite list: authoring the tree in /// is enough. /// [CreateAssetMenu(fileName = "TowerUpgradeOption", menuName = "TD/Draft/Tower Upgrade Option")] public class TowerUpgradeDraftOption : DraftOption { [Header("Payload")] [Tooltip("The tower this upgrade branches from — the tree node that must already be " + "reachable (in the player's deck, or itself an unlocked upgrade) for this " + "option to be offered.")] public TowerDefinition BaseTower; [Tooltip("The tower this option unlocks as an upgrade target. Must be in the " + "TowerPlacementManager catalog (that's where its TowerTypeId comes from) and " + "should be listed in BaseTower's UpgradePaths so the conversion is legal.")] public TowerDefinition UpgradedTower; public override bool IsValidFor(ulong clientId) { var deck = PlayerTowerDeck.GetForClient(clientId); var upgrades = PlayerTowerUpgrades.GetForClient(clientId); var pm = TowerPlacementManager.Instance; if (deck == null || upgrades == null || pm == null) return false; if (BaseTower == null || UpgradedTower == null) return false; if (!pm.TryGetTypeId(BaseTower, out int baseTypeId)) return false; if (!pm.TryGetTypeId(UpgradedTower, out int upgradedTypeId)) return false; // Already unlocked — offering it again would waste the pick. if (upgrades.Contains(upgradedTypeId)) return false; // The parent must be reachable: a base tower they can build, or an upgrade they've // already taken. This is the whole prerequisite mechanism. return deck.Contains(baseTypeId) || upgrades.Contains(baseTypeId); } public override bool ServerApply(ulong clientId) { var upgrades = PlayerTowerUpgrades.GetForClient(clientId); var pm = TowerPlacementManager.Instance; if (upgrades == null || pm == null || UpgradedTower == null) return false; if (!pm.TryGetTypeId(UpgradedTower, out int upgradedTypeId)) { Debug.LogError($"[TowerUpgradeDraftOption] '{UpgradedTower.name}' is not in the " + $"tower catalog; cannot apply. Add it to " + $"TowerPlacementManager.towerDefinitions."); return false; } return upgrades.ServerUnlock(upgradedTypeId); } /// Inherits the upgraded tower's icon unless this option assigns an override. public override Sprite ResolveIcon() => Icon != null ? Icon : (UpgradedTower != null ? UpgradedTower.Icon : null); } }