Runtime Spawner 1.5.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.RegionPopulationController Class Referencesealed

Central, director-driven controller that scales per-tile LocalAreaSpawners and resolves their spawn lists from a global RegionSpawnLibrary. More...

Inheritance diagram for MegaCrush.Spawner.RegionPopulationController:

Classes

interface  IRegionTagProvider
 Optional provider interface for region tags. Implement on any component under a region root to supply semantic tags (e.g., "industrial", "residential"). If not present, the Unity tag is used. More...
 

Public Member Functions

void SetLibrary (RegionSpawnLibrary lib)
 Assigns the global library at runtime (optional).
 
void ApplyStepFromDirector (int step, int stepsCount, float localDensityOverride=-1f)
 Applies a new step from the SpawnDirector (or a custom driver) and recomputes all region targets and spawn lists. If localDensityOverride is >= 0, it is used directly; otherwise localDensityByStep is evaluated.
 
void RegisterRegion (LocalAreaSpawner region)
 Registers a region with this controller (normally handled automatically by RuntimeSpawner events). Safe to call redundantly.
 
void UnregisterRegion (LocalAreaSpawner region)
 Unregisters a region from this controller (normally handled automatically by RuntimeSpawner events).
 

Public Attributes

bool autoDiscoverAtStart = false
 
float enableThreshold = 0.40f
 
AnimationCurve localDensityByStep = AnimationCurve.Linear(0, 0.8f, 1, 1.7f)
 
bool useJitter = true
 
bool logChanges = false
 

Private Member Functions

void Awake ()
 
void OnEnable ()
 
void OnDisable ()
 
void OnValidate ()
 
void OnRegionRegistered (LocalAreaSpawner region)
 Handles region registration from RuntimeSpawner.
 
void OnRegionUnregistered (LocalAreaSpawner region)
 Handles region unregistration from RuntimeSpawner.
 
void OnDirectorStepChanged (int step)
 Handles director step changes (if bound).
 
void RecomputeAll (float localDensityOverride)
 Recomputes targets and spawn lists for all tracked regions using the current step.
 
void RecomputeRegion (LocalAreaSpawner region, float localDensityOverride)
 Recomputes a single region’s enable/disable state, target min/max, and spawn list.
 
void RefreshRegionEntries (LocalAreaSpawner region, int step, int stepsCount)
 Builds a region’s LocalAreaSpawner.CustomRegionSpawners from the global RegionSpawnLibrary using tags and step ranges. If no rules match, applies fallback.
 

Static Private Member Functions

static string[] GetTags (LocalAreaSpawner region)
 Returns semantic tags for a region using IRegionTagProvider if present; otherwise falls back to the GameObject's Unity Tag (if not "Untagged"). Returns an empty array if none.
 
static bool TagsMatch (string[] have, string[] all, string[] any)
 Returns true if have satisfies "all" and/or "any" tag filters.
 
static float Hash01 (string s)
 Stable 0..1 hash for strings (FNV-1a based, reduced).
 

Private Attributes

RuntimeSpawner spawner
 
SpawnDirector director
 
RegionSpawnLibrary library
 
readonly List< LocalAreaSpawner_regions = new()
 
int _step = 0
 
int _stepsCount = 1
 

Detailed Description

Central, director-driven controller that scales per-tile LocalAreaSpawners and resolves their spawn lists from a global RegionSpawnLibrary.

This component is part of the Runtime Spawner package and is intended to be placed once in your gameplay scene. It listens for region lifecycle events from RuntimeSpawner and (optionally) step changes from SpawnDirector. For each active region, it:

  • Enables/disables the region based on a step-driven density curve.
  • Computes per-region target min/max population and writes them via LocalAreaSpawner.DesiredRangeOverride (non-destructive, runtime-only).
  • Builds LocalAreaSpawner.CustomRegionSpawners from RegionSpawnLibrary rules (tags + step ranges), optionally appending or replacing existing entries.
  • Pre-warms pools for resolved entries via RuntimeSpawner.EnsurePoolForEntry.

Requirements:

  • RuntimeSpawner must raise onRegionRegistered and onRegionUnregistered events.
  • LocalAreaSpawner exposes two runtime knobs consumed by this controller:
    • public (int min, int max) DesiredRangeOverride { get; set; } — set to (-1,-1) to ignore.
    • public float DensityMult { get; set; } — informational; controller bakes scaling into the override.
  • RegionSpawnLibrary is a ScriptableObject that defines rule-based spawn entries (filtered by tile tags and step ranges) and a fallback list. See that type for authoring details.
  • To supply tile tags without coupling, a region may implement IRegionTagProvider (package interface) on any component under the region root. If none is present, the GameObject's Unity Tag is used (if not "Untagged").

Usage:

  • Drop one instance into the scene, assign spawner and library.
  • (Optional) Assign director to auto-apply step changes. Otherwise, call ApplyStepFromDirector(int,int,float) from your own system.
  • For procedural tiles, simply instantiate/despawn LocalAreaSpawners — registration is automatic via RuntimeSpawner events.

Member Function Documentation

◆ ApplyStepFromDirector()

void MegaCrush.Spawner.RegionPopulationController.ApplyStepFromDirector ( int step,
int stepsCount,
float localDensityOverride = -1f )

Applies a new step from the SpawnDirector (or a custom driver) and recomputes all region targets and spawn lists. If localDensityOverride is >= 0, it is used directly; otherwise localDensityByStep is evaluated.

Parameters
step0-based step index to apply.
stepsCountTotal number of steps (≥ 1) used to normalize the curve.
localDensityOverrideOptional override for density multiplier; pass < 0 to use the curve.

◆ Awake()

void MegaCrush.Spawner.RegionPopulationController.Awake ( )
private

◆ GetTags()

static string[] MegaCrush.Spawner.RegionPopulationController.GetTags ( LocalAreaSpawner region)
staticprivate

Returns semantic tags for a region using IRegionTagProvider if present; otherwise falls back to the GameObject's Unity Tag (if not "Untagged"). Returns an empty array if none.

◆ Hash01()

static float MegaCrush.Spawner.RegionPopulationController.Hash01 ( string s)
staticprivate

Stable 0..1 hash for strings (FNV-1a based, reduced).

◆ OnDirectorStepChanged()

void MegaCrush.Spawner.RegionPopulationController.OnDirectorStepChanged ( int step)
private

Handles director step changes (if bound).

◆ OnDisable()

void MegaCrush.Spawner.RegionPopulationController.OnDisable ( )
private

◆ OnEnable()

void MegaCrush.Spawner.RegionPopulationController.OnEnable ( )
private

◆ OnRegionRegistered()

void MegaCrush.Spawner.RegionPopulationController.OnRegionRegistered ( LocalAreaSpawner region)
private

Handles region registration from RuntimeSpawner.

◆ OnRegionUnregistered()

void MegaCrush.Spawner.RegionPopulationController.OnRegionUnregistered ( LocalAreaSpawner region)
private

Handles region unregistration from RuntimeSpawner.

◆ OnValidate()

void MegaCrush.Spawner.RegionPopulationController.OnValidate ( )
private

◆ RecomputeAll()

void MegaCrush.Spawner.RegionPopulationController.RecomputeAll ( float localDensityOverride)
private

Recomputes targets and spawn lists for all tracked regions using the current step.

Parameters
localDensityOverrideIf >= 0, forces a fixed density multiplier for this pass.

◆ RecomputeRegion()

void MegaCrush.Spawner.RegionPopulationController.RecomputeRegion ( LocalAreaSpawner region,
float localDensityOverride )
private

Recomputes a single region’s enable/disable state, target min/max, and spawn list.

◆ RefreshRegionEntries()

void MegaCrush.Spawner.RegionPopulationController.RefreshRegionEntries ( LocalAreaSpawner region,
int step,
int stepsCount )
private

Builds a region’s LocalAreaSpawner.CustomRegionSpawners from the global RegionSpawnLibrary using tags and step ranges. If no rules match, applies fallback.

◆ RegisterRegion()

void MegaCrush.Spawner.RegionPopulationController.RegisterRegion ( LocalAreaSpawner region)

Registers a region with this controller (normally handled automatically by RuntimeSpawner events). Safe to call redundantly.

◆ SetLibrary()

void MegaCrush.Spawner.RegionPopulationController.SetLibrary ( RegionSpawnLibrary lib)

Assigns the global library at runtime (optional).

◆ TagsMatch()

static bool MegaCrush.Spawner.RegionPopulationController.TagsMatch ( string[] have,
string[] all,
string[] any )
staticprivate

Returns true if have satisfies "all" and/or "any" tag filters.

◆ UnregisterRegion()

void MegaCrush.Spawner.RegionPopulationController.UnregisterRegion ( LocalAreaSpawner region)

Unregisters a region from this controller (normally handled automatically by RuntimeSpawner events).

Member Data Documentation

◆ _regions

readonly List<LocalAreaSpawner> MegaCrush.Spawner.RegionPopulationController._regions = new()
private

◆ _step

int MegaCrush.Spawner.RegionPopulationController._step = 0
private

◆ _stepsCount

int MegaCrush.Spawner.RegionPopulationController._stepsCount = 1
private

◆ autoDiscoverAtStart

bool MegaCrush.Spawner.RegionPopulationController.autoDiscoverAtStart = false

◆ director

SpawnDirector MegaCrush.Spawner.RegionPopulationController.director
private

◆ enableThreshold

float MegaCrush.Spawner.RegionPopulationController.enableThreshold = 0.40f

◆ library

RegionSpawnLibrary MegaCrush.Spawner.RegionPopulationController.library
private

◆ localDensityByStep

AnimationCurve MegaCrush.Spawner.RegionPopulationController.localDensityByStep = AnimationCurve.Linear(0, 0.8f, 1, 1.7f)

◆ logChanges

bool MegaCrush.Spawner.RegionPopulationController.logChanges = false

◆ spawner

RuntimeSpawner MegaCrush.Spawner.RegionPopulationController.spawner
private

◆ useJitter

bool MegaCrush.Spawner.RegionPopulationController.useJitter = true

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