Player/PlayerPawn.Zones.cs

Partial PlayerPawn class extension that tracks which Zone instances the player is currently inside. It stores a local list of Zone, exposes it as an IEnumerable, updates the list by querying Zone.GetAt with the player's world position, and provides a helper GetZone<T> to fetch a component of type T from the zones.


namespace KOTH;

// tony: Not a huge fan of this, can't we be reacting to trigger events instead of tracing against spheres for every player every update?
// or just do this on the local player, and tell the host about what zones they're in?
// travis : I AGREE
partial class PlayerPawn
{
	private readonly List<Zone> _zones = new();

	/// <summary>
	/// Which <see cref="Zone"/>s is the player currently standing in.
	/// </summary>
	public IEnumerable<Zone> Zones => _zones;

	/// <summary>
	/// Update which <see cref="Zone"/>s the player is standing in.
	/// </summary>
	private void UpdateZones()
	{
		_zones.Clear();
		_zones.AddRange(Zone.GetAt(WorldPosition));
	}

	public T GetZone<T>()
	{
		return Zones.Select(x => x.Components.Get<T>()).FirstOrDefault(x => x is not null);
	}
}