Rampage/Humans/PopulaceFx.cs
using System;
using System.Collections.Generic;

/// <summary>
/// The small trimmings of the town's slapstick: its sounds, the stars that circle a dazed head,
/// and the one prop each person carries.
/// </summary>
public static class PopulaceFx
{
	// ---- Sounds ------------------------------------------------------------------------------------
	// Every name used: whack, whack_big, wilhelm, yelp, thud_land, bounce, stuck_twang, unstuck_pop,
	// stars, shield_clang, grab, indignity, scream, stampede, police_whistle, riot_drum, army_bugle,
	// mib_sting, news_sting, band_march, band_toot, salute, lump_scatter, fat_boing, fat_launch,
	// fat_whistle, fat_land.

	const int MaxSoundsPerFrame = 6;
	static int _soundFrame = -1, _soundsThisFrame;

	/// <summary>Plays sounds/rampage/&lt;name&gt;.sound at a spot. Quiet if the sound does not exist yet, and never more than a few in one frame.</summary>
	public static SoundHandle Sound( string name, Vector3 position, float volume = 1f, float pitch = 1f )
	{
		if ( string.IsNullOrEmpty( name ) ) return null;
		int frame = (int)(RealTime.Now * 60f);
		if ( frame != _soundFrame ) { _soundFrame = frame; _soundsThisFrame = 0; }
		if ( ++_soundsThisFrame > MaxSoundsPerFrame ) return null;

		// Each name is looked up once (see Rampage.PlaySound); a missing sound is just silence.
		var handle = Rampage.PlaySound( name, position );
		if ( handle is null ) return null;
		handle.Volume *= volume;
		handle.Pitch *= pitch;
		return handle;
	}

	// ---- Props -------------------------------------------------------------------------------------

	static Model _box, _ball;
	public static Model Box => _box ??= Model.Load( "models/dev/box.vmdl" );        // a 50 unit cube
	public static Model Ball => _ball ??= Model.Load( "models/dev/sphere.vmdl" );   // radius 32

	/// <summary>A tinted box or ball under a parent, in the parent's frame. Sizes are in inches.</summary>
	public static GameObject Block( GameObject parent, string name, Vector3 position, Vector3 size, Color tint, bool ball = false )
	{
		var go = new GameObject( parent, true, name );
		go.Flags |= GameObjectFlags.NotSaved;
		var renderer = go.Components.Create<ModelRenderer>();
		renderer.RenderType = ModelRenderer.ShadowRenderType.Off;
		Shape( go, renderer, position, size, tint, ball );
		return go;
	}

	/// <summary>Makes an existing block into this prop (a stand-in's prop is reused from person to person).</summary>
	public static void Shape( GameObject go, ModelRenderer renderer, Vector3 position, Vector3 size, Color tint, bool ball )
	{
		go.LocalPosition = position;
		go.LocalRotation = Rotation.Identity;
		go.LocalScale = ball ? size / 64f : size / 50f;
		renderer.Model = ball ? Ball : Box;
		renderer.Tint = tint;
	}

	// ---- Stars -------------------------------------------------------------------------------------

	const int MaxStars = 28;
	static readonly Color StarYellow = new( 1f, 0.9f, 0.15f );
	static readonly Stack<GameObject> _starPool = new();
	static int _starsMade;

	/// <summary>A ring of little yellow stars to circle a dazed head. Empty if they are all in use.</summary>
	public static GameObject TakeStars( GameObject root )
	{
		while ( _starPool.Count > 0 )
		{
			var pooled = _starPool.Pop();
			if ( !pooled.IsValid() ) continue;
			pooled.Enabled = true;
			return pooled;
		}
		if ( _starsMade >= MaxStars || !root.IsValid() ) return null;
		_starsMade++;

		var ring = new GameObject( root, true, "Stars" );
		ring.Flags |= GameObjectFlags.NotSaved;
		for ( int i = 0; i < 4; i++ )
		{
			float angle = i * MathF.PI * 0.5f;
			Block( ring, "Star", new Vector3( MathF.Cos( angle ) * 13f, MathF.Sin( angle ) * 13f, 0f ), new Vector3( 4.5f ), StarYellow, true );
		}
		return ring;
	}

	public static void ReturnStars( GameObject ring )
	{
		if ( !ring.IsValid() ) return;
		ring.Enabled = false;
		_starPool.Push( ring );
	}

	/// <summary>Circles the stars over a head, bobbing a little.</summary>
	public static void SpinStars( GameObject ring, Vector3 head, float now )
	{
		if ( !ring.IsValid() ) return;
		ring.WorldPosition = head + Vector3.Up * (4f + MathF.Sin( now * 5f ) * 1.5f);
		ring.WorldRotation = Rotation.FromYaw( now * 300f ) * Rotation.FromPitch( 12f );
	}

	/// <summary>Forgets the pool (the scene it was made in is gone).</summary>
	public static void Reset()
	{
		_starPool.Clear();
		_starsMade = 0;
	}
}