Player/Player.Local.cs

Partial class for the Player type containing local-player management. It stores a static Local reference for the owning (non-proxy) player, sets it via SetLocal, clears it in OnDestroy, and stops a skid sound when the player is destroyed.

using Sandbox;

namespace BrickJam;

public sealed partial class Player
{
	/// <summary>The local (owning, non-proxy) player, or null. Replaces <c>Game.LocalPawn as Player</c>.</summary>
	public static Player Local { get; private set; }

	internal void SetLocal()
	{
		if ( !IsProxy )
			Local = this;
	}

	protected override void OnDestroy()
	{
		if ( Local == this )
			Local = null;

		// Stop the looping drift sound so it doesn't keep playing if the player is destroyed mid-skid.
		StopSkidSound();
	}
}