A UnitStatus subclass that grants temporary invincibility. It sets the unit invincible on Init, removes the status after Lifetime expires, and clears invincibility on removal.
using System;
using Sandbox;
public class UnitStatusInvincible : UnitStatus
{
public UnitStatusInvincible()
{
}
public override void Init( Unit unit )
{
base.Init( unit );
Unit.SetStateInvincible( true );
}
public override void Update( float dt )
{
base.Update( dt );
if ( !Unit.IsValid() )
return;
if ( Lifetime > 0f && ElapsedTime > Lifetime )
Unit.RemoveUnitStatus( this );
}
public override void OnRemove( bool playEffects = true )
{
Log.Info( $"removing invincibility from {Unit} playEffects: {playEffects}" );
Unit.SetStateInvincible( false, playEffects );
}
public override void Refresh()
{
base.Refresh();
}
}