A game perk class PerkRerolls that awards reroll and banish item pickups when leveled. It increments counters on IncreaseLevel, spawns items periodically while counters remain by calling Player.GiveItemRpc, plays a nearby sound, and stops updating when used up.
using System;
using Sandbox;
[Perk( Rarity.Common, alwaysOfferDebug: false )]
public class PerkRerolls : Perk
{
private enum Mod { NumReroll };
private TimeSince _timeSinceItem;
private int _numRerolls;
private int _numBanish;
static PerkRerolls()
{
Register<PerkRerolls>(
name: "Tuning Tools",
imagePath: "textures/icons/vector/rerolls.png",
description: level => $"+{(int)GetValue( level, Mod.NumReroll )} reroll-item ___ +1 banish-item",
upgradeDescription: level => $"+{(int)GetValue( level, Mod.NumReroll )} reroll-item ___ +1 banish-item"
);
}
public override void Start()
{
base.Start();
}
public override void IncreaseLevel()
{
base.IncreaseLevel();
_numRerolls += 4;
_numBanish += 1;
_timeSinceItem = 0f;
ShouldUpdate = true;
}
public override void Refresh()
{
base.Refresh();
}
public override void Update( float dt )
{
base.Update( dt );
if ( (_numRerolls > 0 || _numBanish > 0 ) && _timeSinceItem > 0.25f )
{
if( _numRerolls > 0 )
{
var dir = Utils.GetRandomVectorInCone( -Player.FacingDir );
Player.GiveItemRpc( "reroll_item", dir );
_numRerolls--;
}
else if( _numBanish > 0 )
{
var dir = Utils.GetRandomVectorInCone( -Player.FacingDir );
Player.GiveItemRpc( "banish_item", dir );
_numBanish--;
}
Manager.Instance.PlaySfxNearbyRpc( "scuffle", Player.Position2D, pitch: Utils.Map( _numBanish + _numRerolls, 5, 1, 0.8f, 0.95f ), volume: 0.85f, maxDist: 300f );
_timeSinceItem = 0f;
if ( !(_numRerolls > 0 || _numBanish > 0) )
ShouldUpdate = false;
}
}
private static float GetValue( int level, Mod mod, bool isPercent = false )
{
switch ( mod )
{
case Mod.NumReroll:
default:
//return 1 + level;
return 4;
}
}
}