ShopUpgrade.razor
@using Sandbox;
@using Sandbox.UI;
@inherits Panel
@namespace Sandbox

<root>
	<div class="itemrow">
		<div class="description @(Upgrade.IsSpecial ? "special" : "")">
			@Upgrade.Name
		</div>
		<div class="cost @(CantAfford() ? "cantafford" : "canafford")">
			@Upgrade.Cost
		</div>
		<div>
			<button disabled=@CantAfford() onclick=@((e) => Buy())>BUY</button>
		</div>
	</div>
</root>

@code
{

	[Property]
	public Upgrade Upgrade { get; set; }

	public List<Upgrade> UpgradeList { get; set; }

	public int Currency { get; set; }

	public SoundEvent Purchase { get; set; }

	private bool CantAfford()
	{
		return Currency < Upgrade.Cost;
	}

	private void Buy()
	{
		if (CantAfford()) return;

		Currency -= Upgrade.Cost;
		Upgrade.Action();
		UpgradeList.Remove(Upgrade);
		Sound.Play(Purchase);
	}

	/// <summary>
	/// the hash determines if the system should be rebuilt. If it changes, it will be rebuilt
	/// </summary>
	protected override int BuildHash() => System.HashCode.Combine( Upgrade, Currency );
}