start of draft option upgrades

This commit is contained in:
Ian Woods 2026-07-22 00:00:05 -07:00
parent e78ae0cda1
commit db9c828e63
7 changed files with 235 additions and 2 deletions

View file

@ -20,8 +20,9 @@ namespace TD.Gameplay
/// same constraint on a struct); <c>byte</c> already does, for free.</para> /// same constraint on a struct); <c>byte</c> already does, for free.</para>
/// ///
/// <para><b>Granted via draft.</b> <see cref="TD.Gameplay.Draft.BuilderEffectDraftOption"/> /// <para><b>Granted via draft.</b> <see cref="TD.Gameplay.Draft.BuilderEffectDraftOption"/>
/// calls <see cref="ServerGrantEffect"/> when a player picks it. Effects are permanent for /// calls <see cref="ServerGrantEffect"/> when a player picks it. Effects are otherwise
/// the match — there is no revoke.</para> /// permanent for the match — the only way one leaves the granted set is
/// <see cref="ServerUpgradeEffect"/> swapping it for its upgraded replacement.</para>
/// ///
/// <para><b>Query, don't snapshot.</b> Consumers (e.g. <see cref="WaveManager"/> awarding /// <para><b>Query, don't snapshot.</b> Consumers (e.g. <see cref="WaveManager"/> awarding
/// kill gold) query this manager live at the point of use rather than towers caching their /// kill gold) query this manager live at the point of use rather than towers caching their
@ -117,5 +118,24 @@ namespace TD.Gameplay
grantedKinds.Add(value); grantedKinds.Add(value);
return true; return true;
} }
/// <summary>
/// Server-only: replaces a previously granted effect with a different one (an upgrade
/// pick). No-op if the old effect isn't currently granted or the new one already is.
/// Returns true if the swap happened.
/// </summary>
public bool ServerUpgradeEffect(BuilderEffectKind oldKind, BuilderEffectKind newKind)
{
if (!IsServer) return false;
byte newValue = (byte)newKind;
if (grantedKinds.Contains(newValue)) return false;
byte oldValue = (byte)oldKind;
if (!grantedKinds.Remove(oldValue)) return false;
grantedKinds.Add(newValue);
return true;
}
} }
} }

View file

@ -0,0 +1,42 @@
// Assets/_Project/Scripts/Gameplay/Draft/BuilderEffectUpgradeDraftOption.cs
using UnityEngine;
using TD.Core;
using TD.Gameplay.BuilderEffects;
namespace TD.Gameplay.Draft
{
/// <summary>
/// Draft choice — "upgrade a builder effect you already have". Replaces
/// <see cref="BaseKind"/> with <see cref="UpgradedKind"/> in the player's
/// <see cref="BuilderUpgradeManager"/>.
/// </summary>
/// <remarks>
/// Mirrors <see cref="BuilderEffectDraftOption"/>, but swaps rather than adds. Only offered
/// while the player currently has <see cref="BaseKind"/> granted — once taken,
/// <see cref="BaseKind"/> is removed, which automatically makes this option (and any sibling
/// upgrade branching off the same base) invalid for future drafts.
/// </remarks>
[CreateAssetMenu(fileName = "BuilderEffectUpgradeOption",
menuName = "TD/Draft/Builder Effect Upgrade Option")]
public class BuilderEffectUpgradeDraftOption : DraftOption
{
[Header("Payload")]
[Tooltip("The builder effect this option upgrades from.")]
public BuilderEffectKind BaseKind;
[Tooltip("The builder effect this option upgrades to.")]
public BuilderEffectKind UpgradedKind;
public override bool IsValidFor(ulong clientId)
{
var upgrades = BuilderUpgradeManager.GetForClient(clientId);
return upgrades != null && upgrades.PlayerHasEffect(BaseKind);
}
public override bool ServerApply(ulong clientId)
{
var upgrades = BuilderUpgradeManager.GetForClient(clientId);
return upgrades != null && upgrades.ServerUpgradeEffect(BaseKind, UpgradedKind);
}
}
}

View file

@ -0,0 +1,52 @@
// Assets/_Project/Scripts/Gameplay/Draft/BuilderSpellUpgradeDraftOption.cs
using UnityEngine;
using TD.Core;
using TD.Gameplay.BuilderSpells;
namespace TD.Gameplay.Draft
{
/// <summary>
/// Draft choice — "upgrade a builder spell you already have". Replaces
/// <see cref="BaseKind"/> with <see cref="UpgradedKind"/> in the player's
/// <see cref="PlayerSpellLoadout"/>, in place — the upgraded spell keeps the same hotkey
/// slot the base spell occupied.
/// </summary>
/// <remarks>
/// Mirrors <see cref="BuilderSpellDraftOption"/>, but swaps rather than adds (so it doesn't
/// consume a slot). Only offered while the player currently has <see cref="BaseKind"/>
/// granted — once taken, <see cref="BaseKind"/> is removed, which automatically makes this
/// option (and any sibling upgrade branching off the same base) invalid for future drafts.
/// </remarks>
[CreateAssetMenu(fileName = "BuilderSpellUpgradeOption",
menuName = "TD/Draft/Builder Spell Upgrade Option")]
public class BuilderSpellUpgradeDraftOption : DraftOption
{
[Header("Payload")]
[Tooltip("The builder spell this option upgrades from.")]
public BuilderSpellKind BaseKind;
[Tooltip("The builder spell this option upgrades to.")]
public BuilderSpellKind UpgradedKind;
public override bool IsValidFor(ulong clientId)
{
var loadout = PlayerSpellLoadout.GetForClient(clientId);
return loadout != null && loadout.PlayerHasSpell(BaseKind);
}
public override bool ServerApply(ulong clientId)
{
var loadout = PlayerSpellLoadout.GetForClient(clientId);
return loadout != null && loadout.ServerUpgradeSpell(BaseKind, UpgradedKind);
}
/// <summary>Inherits the upgraded spell's icon (resolved from the pool by
/// <see cref="UpgradedKind"/>) unless this option assigns an override.</summary>
public override Sprite ResolveIcon()
{
if (Icon != null) return Icon;
var def = BuilderSpellPool.Instance != null ? BuilderSpellPool.Instance.Get(UpgradedKind) : null;
return def != null ? def.Icon : null;
}
}
}

View file

@ -0,0 +1,70 @@
// Assets/_Project/Scripts/Gameplay/Draft/TowerUpgradeDraftOption.cs
using UnityEngine;
using TD.Towers;
namespace TD.Gameplay.Draft
{
/// <summary>
/// Draft choice — "upgrade a tower you already have". Replaces <see cref="BaseTower"/>
/// with <see cref="UpgradedTower"/> in the player's <see cref="PlayerTowerDeck"/>: every
/// tower of that type placed from now on is the upgraded version. Already-placed towers
/// are unaffected.
/// </summary>
/// <remarks>
/// Mirrors <see cref="NewTowerDraftOption"/>, but swaps rather than adds. Only offered
/// while the player currently has <see cref="BaseTower"/> unlocked — once taken,
/// <see cref="BaseTower"/> is removed from the deck, which automatically makes this option
/// (and any sibling upgrade branching off the same base) invalid for future drafts.
/// </remarks>
[CreateAssetMenu(fileName = "TowerUpgradeOption", menuName = "TD/Draft/Tower Upgrade Option")]
public class TowerUpgradeDraftOption : DraftOption
{
[Header("Payload")]
[Tooltip("The tower this option upgrades from. Must be present in the player's deck " +
"for this option to be offered.")]
public TowerDefinition BaseTower;
[Tooltip("The tower this option upgrades to. Must also be present in the " +
"TowerPlacementManager catalog (that's where its TowerTypeId comes from).")]
public TowerDefinition UpgradedTower;
public override bool IsValidFor(ulong clientId)
{
var deck = PlayerTowerDeck.GetForClient(clientId);
var pm = TowerPlacementManager.Instance;
if (deck == null || pm == null || BaseTower == null || UpgradedTower == null) return false;
if (!pm.TryGetTypeId(BaseTower, out int baseTypeId)) return false;
return deck.Contains(baseTypeId);
}
public override bool ServerApply(ulong clientId)
{
var deck = PlayerTowerDeck.GetForClient(clientId);
var pm = TowerPlacementManager.Instance;
if (deck == null || pm == null || BaseTower == null || UpgradedTower == null) return false;
if (!pm.TryGetTypeId(BaseTower, out int baseTypeId))
{
Debug.LogError($"[TowerUpgradeDraftOption] '{BaseTower.name}' is not in the tower " +
$"catalog; cannot apply. Add it to TowerPlacementManager.towerDefinitions.");
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 deck.ServerUpgradeTower(baseTypeId, upgradedTypeId);
}
/// <summary>Inherits the upgraded tower's icon unless this option assigns an override.</summary>
public override Sprite ResolveIcon()
=> Icon != null ? Icon : (UpgradedTower != null ? UpgradedTower.Icon : null);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aeec22f152a57ef5798eae035c091543

View file

@ -139,6 +139,28 @@ namespace TD.Gameplay
return true; return true;
} }
/// <summary>
/// Server-only: replaces a previously granted spell with a different one (an upgrade
/// pick), in place — the upgraded spell keeps the base spell's hotkey slot and starts
/// off cooldown. No-op if the old spell isn't currently granted or the new one already
/// is. Returns true if the swap happened.
/// </summary>
public bool ServerUpgradeSpell(BuilderSpellKind oldKind, BuilderSpellKind newKind)
{
if (!IsServer) return false;
if (PlayerHasSpell(newKind)) return false;
for (int i = 0; i < spells.Count; i++)
{
if (spells[i].Kind == oldKind)
{
spells[i] = SpellSlot.CreateReady(newKind);
return true;
}
}
return false;
}
// ----- Cast RPC ----------------------------------------------------- // ----- Cast RPC -----------------------------------------------------
/// <summary> /// <summary>

View file

@ -146,5 +146,30 @@ namespace TD.Gameplay
unlockedTypeIds.Add(towerTypeId); unlockedTypeIds.Add(towerTypeId);
return true; return true;
} }
/// <summary>
/// Server-only: replaces a previously unlocked tower type with a different one (an
/// upgrade pick) — every tower of that type placed from now on uses the new definition.
/// Already-placed towers keep whatever type they were placed with; this only changes
/// what's placeable going forward. No-op if the old type isn't currently unlocked or
/// the new one already is. Returns true if the swap happened.
/// </summary>
public bool ServerUpgradeTower(int oldTypeId, int newTypeId)
{
if (!IsServer) return false;
if (newTypeId <= 0) return false;
if (Contains(newTypeId)) return false;
for (int i = 0; i < unlockedTypeIds.Count; i++)
{
if (unlockedTypeIds[i] == oldTypeId)
{
unlockedTypeIds.RemoveAt(i);
unlockedTypeIds.Add(newTypeId);
return true;
}
}
return false;
}
} }
} }