unitStatus/UnitStatusShield.cs

A UnitStatus subclass that represents a temporary shield state for a unit. It sets shield state on init, cancels the status when damaged above a threshold, and on removal optionally spawns a visual ring and applies area-of-effect damage/repel to nearby entities.

NetworkingFile Access
using System;
using Sandbox;

public class UnitStatusShield : UnitStatus
{
	public float EnemyAoeDamage { get; set; }
	public const float ENEMY_AOE_RADIUS = 90f;

	public UnitStatusShield()
	{

	}

	public override void Init( Unit unit )
	{
		base.Init( unit );

		Unit.SetStateShield( true );
	}

	public override void OnHurt( float damage, Player playerSource, Enemy enemySource, DamageType damageType, bool isSelfInflicted )
	{
		base.OnHurt( damage, playerSource, enemySource, damageType, isSelfInflicted );

		if ( damageType == DamageType.Self )
			return;

		if ( Player.IsValid() && damage < Player.Stats[PlayerStat.ShieldMinDmg] )
			return;

		Unit.RemoveUnitStatus( this );
	}

	public override void OnRemove( bool playEffects = true )
	{
		Unit.SetStateShield( false, playEffects );

		if( EnemyAoeDamage > 0f )
		{
			Manager.Instance.SpawnRingRpc( Unit.Position2D, ENEMY_AOE_RADIUS * 1.08f, new Color( 1f, 0.7f, 0f, 0.6f ), lifetime: Game.Random.Float( 0.25f, 0.3f ), path: "ring_spiky_2" );

			Manager.Instance.AffectInRadius( Unit.Position2D, ENEMY_AOE_RADIUS, EnemyAoeDamage, repelRadius: ENEMY_AOE_RADIUS * 1.2f, repelForce: 120f, playerSource: null, damageType: DamageType.Aoe, options: RepelOptions.DamagePlayers | RepelOptions.RepelPlayers | RepelOptions.RepelEnemies | RepelOptions.RepelItems );
		}
	}

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

	}
}