A Perk class for a game, named Near-Death Experience. While the player is at 1 HP or below it applies an XP gain multiplier modifier, toggles UI display state and updates each tick.
using System;
using Sandbox;
[Perk( Rarity.Legendary, includedAtStart: false, locked: true, alwaysOfferDebug: false )]
public class PerkOnly1HpXpGain : Perk
{
private enum Mod { XpGainMultiplier };
private bool _isActive;
static PerkOnly1HpXpGain()
{
Register<PerkOnly1HpXpGain>(
name: "Near-Death Experience",
imagePath: "textures/icons/vector/only_1hp_xp_gain.png",
description: level => $"+{GetValue( level, Mod.XpGainMultiplier, true )}% xp gained while at 1 hp",
upgradeDescription: level => $"+{GetValue( level - 1, Mod.XpGainMultiplier, true )}%→{GetValue( level, Mod.XpGainMultiplier, true )}% xp gained\nwhile at 1 hp"
);
}
public override void Start()
{
base.Start();
ShouldUpdate = true;
DisplayCooldownColor = new Color( 0.1f, 0.3f, 1f, 0.25f );
}
public override void Refresh()
{
base.Refresh();
if ( !(Player.Health > 1f) )
Enable();
}
public override void Update( float dt )
{
base.Update( dt );
if( _isActive )
{
if ( Player.Health > 1f )
Disable();
}
else
{
if ( !(Player.Health > 1f) )
Enable();
}
}
void Enable()
{
Player.Modify( this, PlayerStat.XpGainMultiplier, GetValue( Level, Mod.XpGainMultiplier ), ModifierType.Mult );
_isActive = true;
DisplayCooldown = 1f;
DisplayText = $"{GetValue( Level, Mod.XpGainMultiplier, true )}%";
}
void Disable()
{
Player.StopModifying( this, PlayerStat.XpGainMultiplier );
_isActive = false;
DisplayCooldown = 0f;
DisplayText = " ";
}
private static float GetValue( int level, Mod mod, bool isPercent = false )
{
switch ( mod )
{
case Mod.XpGainMultiplier:
default:
return isPercent
? 4f + 8f * level
: 1f + 0.04f + 0.08f * level;
}
}
}