Weapons/ToolGun/Modes/Balloon/BalloonEntity.cs

A game entity component for a balloon that responds to damage by popping. It triggers a pop visual effect with tint and adjusted particle settings, plays a pop sound, increments the attacker's balloon.popped stat, and then destroys the GameObject.

Networking
public sealed class BalloonEntity : Component, Component.IDamageable
{
	[Property] public PrefabFile PopEffect { get; set; }
	[Property] public SoundEvent PopSound { get; set; }
	[RequireComponent] public Prop Prop { get; set; }

	void IDamageable.OnDamage( in DamageInfo damage )
	{
		if ( IsProxy ) return;

		damage.Attacker?.GetComponent<Player>()?.PlayerData?.AddStat( "balloon.popped" );

		Pop();
	}

	[Rpc.Host]
	private void Pop()
	{
		if ( PopEffect.IsValid() )
		{
			var effect = GameObject.Clone( PopEffect, new CloneConfig { Transform = WorldTransform, StartEnabled = false } );

			foreach ( var tintable in effect.GetComponentsInChildren<ITintable>( true ) )
			{
				tintable.Color = Prop.Tint;
			}

			// Beef up the gib burst
			foreach ( var gibs in effect.GetComponentsInChildren<ParticleEffect>( true ).Where( x => x.GameObject.Name == "Gibs" ) )
			{
				gibs.MaxParticles = 40;

				foreach ( var emitter in gibs.GetComponents<ParticleConeEmitter>( true ) )
				{
					emitter.Burst = 35;
				}
			}

			effect.NetworkSpawn( true, null );
		}

		if ( PopSound is null )
		{
			PopSound = ResourceLibrary.Get<SoundEvent>( "entities/balloon/sounds/balloon_pop.sound" );
		}

		if ( PopSound is not null )
		{
			Sound.Play( PopSound, WorldPosition );
		}

		GameObject.Destroy();
	}
}