ExplosionKiller.cs
using System.Numerics;
using Sandbox;
public sealed class ExplosionKiller : Component
{
/// <summary>
/// When a player leaves this area, kill them
/// </summary>
[Property]
public float KillRange { get; set; } = 1000f;
[Property]
public GameObject Explosion;
[Property]
public SoundEvent ExplosionSound;
TimeSince lastCheck;
protected override void OnEnabled()
{
base.OnEnabled();
lastCheck = 0;
}
protected override void OnFixedUpdate()
{
base.OnFixedUpdate();
if (lastCheck > 1)
{
var players = Scene.GetAll<PlayerController>().ToArray();
Game.Random.Shuffle( players );
foreach (var player in players)
{
if (player.WorldPosition.Distance(Vector3.Zero) > KillRange && player.GetComponent<Health>().IsAlive)
{
var dtag = new TagSet();
dtag.Add( "explosion" );
player.GetComponent<IDamageable>().OnDamage( new DamageInfo()
{
Damage = 1000,
Tags = dtag,
} );
Explosion.Clone(player.WorldPosition);
Sound.Play( ExplosionSound, player.WorldPosition );
lastCheck = 0.2f;
return;
}
}
lastCheck = 0;
}
}
protected override void DrawGizmos()
{
base.DrawGizmos();
Gizmo.Draw.Color = Color.Red;
Gizmo.Draw.LineCircle( Vector3.Zero, Vector3.Up, Vector3.Forward, KillRange, 0, 360, 64 );
}
}