Runtime Spawner 1.3.0
Generic Runtime spawn and instance pooling system for spawning random AI agents around a map. Works with ANY AI system easily.
Loading...
Searching...
No Matches
MegaCrush.Spawner.SpawnHintRegistry Class Reference

Lightweight spatial index for EnemySpawnHintPoint. More...

Static Public Member Functions

static void Configure (float cellSize)
 Configures the spatial grid cell size and rebuilds the internal buckets without losing current registrations.
 
static void Register (EnemySpawnHintPoint h)
 Registers a hint with the registry and inserts it into the appropriate grid bucket.
 
static void Unregister (EnemySpawnHintPoint h)
 Unregisters a hint from the registry, removes it from its grid bucket and clears any reservation.
 
static IEnumerable< EnemySpawnHintPointQueryCircle (Vector3 center, float radius)
 Enumerates all registered hints whose positions fall within a radius of center .
 
static bool IsReserved (EnemySpawnHintPoint h)
 Returns true if the hint is currently soft-reserved.
 
static bool TryReserve (EnemySpawnHintPoint h, float seconds)
 Attempts to soft-reserve a hint for a duration.
 
static int DebugCountAll ()
 Returns the number of currently registered hints.
 
static void ScanSceneForHints ()
 Clears and repopulates the registry by scanning the open scene(s) for EnemySpawnHintPoint.
 

Static Private Member Functions

static void Rebuild ()
 Rebuilds all grid buckets from the current s_All set using the active s_Cell size.
 
static void AddToGrid (EnemySpawnHintPoint h)
 Adds a hint to the grid bucket for its world position.
 
static void RemoveFromGrid (EnemySpawnHintPoint h)
 Removes a hint from the grid bucket matching its world position.
 
static Vector2Int WorldToCell (Vector3 p)
 Converts a world position to a grid cell coordinate using the active cell size on X/Z.
 

Static Private Attributes

static float s_Cell = 16f
 Current grid cell size (in meters). Hints are bucketed by this size in X/Z.
 
static readonly HashSet< EnemySpawnHintPoints_All = new()
 Master set of all registered hints (alive & valid).
 
static readonly Dictionary< Vector2Int, List< EnemySpawnHintPoint > > s_Grid = new()
 Grid buckets mapping cell coords to hint lists. Keys are discretized X/Z cells via WorldToCell(Vector3).
 
static readonly Dictionary< EnemySpawnHintPoint, float > s_ReservedUntil = new()
 Soft reservation expiry times per hint (in Time.time seconds). Hints are considered reserved while now <= until.
 

Detailed Description

Lightweight spatial index for EnemySpawnHintPoint.

This registry keeps a grid-bucketed lookup of all registered hint points so spawn systems can query nearby hints efficiently at spawn time. It performs no per-frame work; all operations are explicit (register/unregister/query).

  • Call Configure(float) to set the spatial cell size (defaults to 16m).
  • Call Register(EnemySpawnHintPoint) / Unregister(EnemySpawnHintPoint) from hint components' enable/disable.
  • Use QueryCircle(Vector3, float) to iterate hints within a radius.
  • Soft-reserve hints with TryReserve(EnemySpawnHintPoint, float) to avoid piling spawns on one spot.

The registry is intended for main-thread usage only (Unity API calls).

Member Function Documentation

◆ AddToGrid()

static void MegaCrush.Spawner.SpawnHintRegistry.AddToGrid ( EnemySpawnHintPoint h)
staticprivate

Adds a hint to the grid bucket for its world position.

Parameters
hHint to bucket.

◆ Configure()

static void MegaCrush.Spawner.SpawnHintRegistry.Configure ( float cellSize)
static

Configures the spatial grid cell size and rebuilds the internal buckets without losing current registrations.

Parameters
cellSizeCell size in meters; clamped to ≥ 2.

Calling this at runtime is safe; the registry will re-bucket existing hints based on the new size.

◆ DebugCountAll()

static int MegaCrush.Spawner.SpawnHintRegistry.DebugCountAll ( )
static

Returns the number of currently registered hints.

◆ IsReserved()

static bool MegaCrush.Spawner.SpawnHintRegistry.IsReserved ( EnemySpawnHintPoint h)
static

Returns true if the hint is currently soft-reserved.

Parameters
hHint to check.
Returns
true if reserved and the reservation has not yet expired; otherwise false.

◆ QueryCircle()

static IEnumerable< EnemySpawnHintPoint > MegaCrush.Spawner.SpawnHintRegistry.QueryCircle ( Vector3 center,
float radius )
static

Enumerates all registered hints whose positions fall within a radius of center .

Parameters
centerQuery center (world space).
radiusQuery radius in meters.
Returns
An enumeration of EnemySpawnHintPoint within the circle. This may include reserved hints — callers can filter using IsReserved(EnemySpawnHintPoint) / TryReserve(EnemySpawnHintPoint, float).

The query first bounds grid iteration to cells overlapping the circle’s AABB, then filters by exact circle distance.

◆ Rebuild()

static void MegaCrush.Spawner.SpawnHintRegistry.Rebuild ( )
staticprivate

Rebuilds all grid buckets from the current s_All set using the active s_Cell size.

◆ Register()

static void MegaCrush.Spawner.SpawnHintRegistry.Register ( EnemySpawnHintPoint h)
static

Registers a hint with the registry and inserts it into the appropriate grid bucket.

Parameters
hHint to register (ignored if null).

Typically called from EnemySpawnHintPoint.OnEnable().

◆ RemoveFromGrid()

static void MegaCrush.Spawner.SpawnHintRegistry.RemoveFromGrid ( EnemySpawnHintPoint h)
staticprivate

Removes a hint from the grid bucket matching its world position.

Parameters
hHint to remove.

◆ ScanSceneForHints()

static void MegaCrush.Spawner.SpawnHintRegistry.ScanSceneForHints ( )
static

Clears and repopulates the registry by scanning the open scene(s) for EnemySpawnHintPoint.

Intended for editor-time utilities and diagnostics. Uses Object.FindObjectsByType<T>(FindObjectsSortMode).

◆ TryReserve()

static bool MegaCrush.Spawner.SpawnHintRegistry.TryReserve ( EnemySpawnHintPoint h,
float seconds )
static

Attempts to soft-reserve a hint for a duration.

Parameters
hHint to reserve.
secondsReservation duration in seconds. Values ≤ 0 will expire immediately (effectively no reservation).
Returns
true if the hint was not already reserved and is now reserved; otherwise false.

Reservations are advisory and only affect systems that consult the registry (e.g., spawn placement heuristics).

◆ Unregister()

static void MegaCrush.Spawner.SpawnHintRegistry.Unregister ( EnemySpawnHintPoint h)
static

Unregisters a hint from the registry, removes it from its grid bucket and clears any reservation.

Parameters
hHint to unregister (ignored if null).

Typically called from EnemySpawnHintPoint.OnDisable().

◆ WorldToCell()

static Vector2Int MegaCrush.Spawner.SpawnHintRegistry.WorldToCell ( Vector3 p)
staticprivate

Converts a world position to a grid cell coordinate using the active cell size on X/Z.

Parameters
pWorld position.
Returns
Grid cell index (x/z) for the given position.

Member Data Documentation

◆ s_All

readonly HashSet<EnemySpawnHintPoint> MegaCrush.Spawner.SpawnHintRegistry.s_All = new()
staticprivate

Master set of all registered hints (alive & valid).

◆ s_Cell

float MegaCrush.Spawner.SpawnHintRegistry.s_Cell = 16f
staticprivate

Current grid cell size (in meters). Hints are bucketed by this size in X/Z.

◆ s_Grid

readonly Dictionary<Vector2Int, List<EnemySpawnHintPoint> > MegaCrush.Spawner.SpawnHintRegistry.s_Grid = new()
staticprivate

Grid buckets mapping cell coords to hint lists. Keys are discretized X/Z cells via WorldToCell(Vector3).

◆ s_ReservedUntil

readonly Dictionary<EnemySpawnHintPoint, float> MegaCrush.Spawner.SpawnHintRegistry.s_ReservedUntil = new()
staticprivate

Soft reservation expiry times per hint (in Time.time seconds). Hints are considered reserved while now <= until.


The documentation for this class was generated from the following file: