perks/PerkRerollRefund.cs

A Perk class for an Epic rarity perk named "Free Spins" that gives a chance to refund a reroll. It defines the UI registration, visual highlight settings, the refund chance scaling by level, and logic to grant either a reroll item or increment available rerolls when a reroll occurs.

NetworkingFile Access
using System;
using Sandbox;

[Perk( Rarity.Epic, locked: true, alwaysOfferDebug: false )]
public class PerkRerollRefund : Perk
{
	private enum Mod { RefundChance };

	//private float _delayTimer;
	//private int _numChances;

	//private const float DELAY_TIME = 0.25f;

	static PerkRerollRefund()
	{
		Register<PerkRerollRefund>(
			name: "Free Spins",
			imagePath: "textures/icons/vector/reroll_refund.png",
			description: level => $"{GetValue( level, Mod.RefundChance, true )}% chance for +1 reroll-item\nwhen you reroll",
			upgradeDescription: level => $"{GetValue( level - 1, Mod.RefundChance, true )}%→{GetValue( level, Mod.RefundChance, true )}% chance for +1 reroll-item\nwhen you reroll"
		);
	}

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

		//ShouldUpdate = true;

		HighlightColor = new Color( 0.75f, 0.75f, 1f );
		HighlightDuration = 0.5f;
		HighlightOpacity = 4.5f;
	}

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

	}

	private static float GetValue( int level, Mod mod, bool isPercent = false )
	{
		switch ( mod )
		{
			case Mod.RefundChance:
			default:
				return isPercent
					? (10f + 10f * level)
					: (0.10f + 0.10f * level);
		}
	}

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

		//if ( _numChances == 0 )
		//	return;

		////_delayTimer += dt;
		//_delayTimer += RealTime.Delta;
		//if ( _delayTimer > DELAY_TIME )
		//{
		//	if ( Game.Random.Float( 0f, 1f ) < GetValue( Level, Mod.RefundChance ) )
		//	{
		//		var pos = Player.Position2D + Utils.GetRandomVector() * Game.Random.Float( 1f, 5f );
		//		Manager.Instance.SpawnItemRpc( "reroll_item", pos, Utils.GetRandomVector() );

		//		//Player.NumRerollAvailable++;
		//		//Player.ForEachPerk( perk => perk.OnGainReroll( 1 ) );
		//		//Manager.Instance.SpawnFloaterTextRpc( Player.WorldPosition.WithZ( 65f ), "FREE REROLL!", new Color( 0.5f, 1f, 0.5f ), size: 1.2f, floaterType: FloaterType.PositiveMessage );

		//		Manager.Instance.PlaySfxUI( "reroll", Utils.Map( Player.NumRerollAvailable, 0, 20, 0.9f + 0.4f, 1.4f + 0.4f, EasingType.QuadIn ), volume: 0.8f );
		//		_delayTimer = 0f;

		//		Highlight();
		//	}

		//	_numChances--;
		//}
	}

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

		//_numChances++;
		//_delayTimer = 0f;

		if ( Game.Random.Float( 0f, 1f ) < GetValue( Level, Mod.RefundChance ) )
		{
			if( Manager.Instance.IsUnpausedChoosing )
			{
				Player.GiveItemRpc( "reroll_item", Utils.GetRandomVectorInCone( -Player.FacingDir ) );
			}
			else
			{
				Player.NumRerollAvailable++;
				Player.ForEachPerk( perk => perk.OnGainReroll( 1 ) );
			}

			Manager.Instance.SpawnFloaterText( Player.WorldPosition.WithZ( 65f ), "FREE REROLL!", new Color( 0.5f, 1f, 0.5f ), size: 1.2f, floaterType: FloaterType.PositiveMessage );

			Manager.Instance.PlaySfxUI( "reroll", Utils.Map( Player.NumRerollAvailable, 0, 20, 0.9f + 0.4f, 1.4f + 0.4f, EasingType.QuadIn ), volume: 0.8f );
			//_delayTimer = 0f;

			Highlight();
		}
	}
}