A physics hazard component for a falling anvil. On collisions above a speed threshold it plays an impact sound, adds upward and outward velocity to a CharacterController, applies an impulse to a Rigidbody, and reports the hit to Killfeed as "HEAVY ANVIL".
using Sandbox;
public sealed class HeavyAnvil : Component, Component.ICollisionListener
{
[Property] public float PushResistanceMultiplier { get; set; } = 0.25f;
[Property] public float CrushSpeedThreshold { get; set; } = 300f;
[Property] public float CrushImpactForce { get; set; } = 75000f;
[Property] public SoundEvent ImpactSound { get; set; }
public void OnCollisionStart( Collision collision )
{
if ( collision.Contact.Speed.Length < CrushSpeedThreshold ) return;
var targetObj = collision.Other.GameObject?.Root;
if ( !targetObj.IsValid() || targetObj == GameObject.Root ) return;
if ( ImpactSound != null )
{
Sound.Play( ImpactSound, WorldPosition );
}
var cc = targetObj.Components.Get<CharacterController>();
if ( cc.IsValid() )
{
var dir = (targetObj.WorldPosition - WorldPosition).Normal;
dir.z = 0.5f;
cc.Velocity += dir * 900f;
Killfeed.Instance?.AnnounceHazard( targetObj.Name, "HEAVY ANVIL" );
}
var rb = targetObj.Components.Get<Rigidbody>();
if ( rb.IsValid() )
{
var dir = (targetObj.WorldPosition - WorldPosition).Normal;
dir.z = 0.5f;
rb.ApplyImpulse( dir * CrushImpactForce );
}
}
}