perks/PerkClickAdd.cs

A game perk class named PerkClickAdd. It implements a mythic rarity perk that grants the player a count of "click-add" charges, lets the player spend a charge to give themselves a perk item in a random cone, updates display UI elements (text, highlight, cooldown) and syncs a Player.Stat counter.

Native Interop
using System;
using System.Numerics;
using Sandbox;

[Perk( Rarity.Mythic, alwaysOfferDebug: false )]
public class PerkClickAdd : Perk
{
	public override float ImportanceMultiplier => 0.1f;

	static PerkClickAdd()
	{
		Register<PerkClickAdd>(
			name: "Reinforced Learning",
			imagePath: "textures/icons/vector/click_add.png",

			// todo: should the amount say it's increasing each level?
			description: level => $"Left-click {level} of your perks\nto get {(level == 1 ? "it" : "them")}",
			upgradeDescription: level => $"Left-click {level - 1}→{level} of your perks\nto get them"
		);
	}

	public override void Start()
	{
		base.Start();

		HighlightColor = new Color( 0.6f, 0.7f, 1f );
		HighlightDuration = 0.25f;
		HighlightOpacity = 2f;
	}

	public override void IncreaseLevel()
	{
		base.IncreaseLevel();

		ShouldUpdate = true;
		DisplayCooldown = 1f;

		Player.Stats[PlayerStat.ClickAddPerk] += 1f;

		DisplayText = $"{(int)Player.Stats[PlayerStat.ClickAddPerk]}";
	}

	public override void Update( float dt )
	{
		base.Update( dt );

		DisplayCooldownColor = new Color( 0.3f, 0.3f, 1f, Utils.Map( Utils.FastSin( RealTime.Now * 12f ), -1f, 1f, 0f, 1f ) );
	}

	public override void Remove( bool restart = false )
	{
		base.Remove( restart );

		Player.Stats[PlayerStat.ClickAddPerk] = 0f;
	}

	public void Activate( TypeDescription type )
	{
		Player.GivePerkItem( type, dir: Utils.GetRandomVectorInCone( -Player.FacingDir ) );

		Manager.Instance.PlaySfxUI( "click", pitch: 1.2f, volume: 0.75f );

		Player.Stats[PlayerStat.ClickAddPerk] -= 1f;

		if( (int)Player.Stats[PlayerStat.ClickAddPerk] <= 0 )
		{
			ShouldUpdate = false;
			DisplayCooldown = 0f;
			DisplayText = " ";
		}
		else
		{
			DisplayText = $"{(int)Player.Stats[PlayerStat.ClickAddPerk]}";
		}

		Highlight();

		Player.GetPerk( type )?.Highlight();
	}
}