A legendary perk class 'PerkOnly1HpDodge' named Edge Dancer. It grants extra dodge attempts while the player's health is at 1 HP by adding/removing a PlayerStat modifier and updates a small UI display state.
using System;
using Sandbox;
[Perk( Rarity.Legendary, includedAtStart: false, locked: true, alwaysOfferDebug: false )]
public class PerkOnly1HpDodge : Perk
{
private enum Mod { ExtraAttempts };
private bool _isActive;
static PerkOnly1HpDodge()
{
Register<PerkOnly1HpDodge>(
name: "Edge Dancer",
imagePath: "textures/icons/vector/only_1hp_dodge.png",
description: level => $"+{GetValue( level, Mod.ExtraAttempts )} dodge {(level == 1 ? "attempt" : "attempts")} while at 1 hp",
upgradeDescription: level => $"+{GetValue( level - 1, Mod.ExtraAttempts )}→{GetValue( level, Mod.ExtraAttempts )} dodge attempts\nwhile at 1 hp"
);
}
public override void Start()
{
base.Start();
ShouldUpdate = true;
DisplayCooldownColor = new Color( 0.3f, 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.DodgeExtraAttempts, GetValue( Level, Mod.ExtraAttempts ), ModifierType.Add );
_isActive = true;
DisplayCooldown = 1f;
DisplayText = $"✅";
}
void Disable()
{
Player.StopModifying( this, PlayerStat.DodgeExtraAttempts );
_isActive = false;
DisplayCooldown = 0f;
DisplayText = " ";
}
private static float GetValue( int level, Mod mod, bool isPercent = false )
{
switch ( mod )
{
case Mod.ExtraAttempts:
default:
return level;
}
}
}