A unit status component representing 'fear' applied to a Unit. It sets the unit's fear state on init, clears it on removal, expires after Lifetime, and can spawn a grenade via the PlayerSource when the unit dies based on a chance.
using System;
using Sandbox;
public class UnitStatusFear : UnitStatus
{
public Enemy EnemySource { get; set; } // todo: never set
public Player PlayerSource { get; set; }
// todo: have an effect on players
public UnitStatusFear()
{
}
public override void Init( Unit unit )
{
base.Init( unit );
Unit.SetStateFear( true );
}
public override void Update( float dt )
{
base.Update( dt );
if ( !Unit.IsValid() )
return;
if ( ElapsedTime > Lifetime )
Unit.RemoveUnitStatus( this );
}
public override void Die( Player player )
{
base.Die( player );
if ( PlayerSource.IsValid() && Game.Random.Float( 0f, 1f ) < PlayerSource.GetSyncStat(PlayerStat.FearDropGrenadeChance) )
{
var pos = Unit.Position2D + Utils.GetRandomVector() * Game.Random.Float( 0.01f, Unit.Radius );
var velocity = (pos - Unit.Position2D).Normal * Game.Random.Float( 0f, 500f );
PlayerSource.SpawnBombRpc( pos, velocity, damage: 30f );
}
}
public override void OnRemove( bool playEffects = true )
{
Unit.SetStateFear( false );
}
public override void Refresh()
{
base.Refresh();
}
}