Skip to content

Game.Simulation.GroundPollutionSystem

Assembly: Assembly-CSharp (in-game)
Namespace: Game.Simulation

Type: class

Base: CellMapSystem, IJobSerializable

Summary:
Manages the ground pollution cell map used by the simulation. This system holds a 256×256 cell texture of GroundPollution values, updates (fades) pollution over time using a Burst-compiled IJob, provides sampling helpers (including bilinear interpolation), and ties into the SimulationSystem frame index and PollutionParameterData singleton. The system schedules a PollutionFadeJob each update tick to reduce pollution values according to configured parameters and per-frame randomness.


Fields

  • public static readonly int kTextureSize = 256
    Defines the pollution texture width and height (map is kTextureSize × kTextureSize = 256×256 cells). Used by sampling and for creating the cell textures.

  • public static readonly int kUpdatesPerDay = 128
    Number of update steps per simulated day. Used in calculations for fade amount and to compute the update interval.

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem instance (retrieved in OnCreate) used to read the current simulation frame index (m_SimulationSystem.frameIndex) for deterministic randomness in the job scheduling.

  • private EntityQuery m_PollutionParameterGroup
    EntityQuery used to obtain the PollutionParameterData singleton. The system requires this query for updates (RequireForUpdate is called).

  • private struct PollutionFadeJob (private nested, Burst compiled)
    A private nested IJob (annotated with [BurstCompile]) that performs the per-cell pollution fade. It contains:

  • NativeArray<GroundPollution> m_PollutionMap — the cell map to mutate.
  • PollutionParameterData m_PollutionParameters — parameters (e.g., m_GroundFade) driving fade amount.
  • RandomSeed m_Random — seed provider used to create a Random instance per-frame for randomized rounding.
  • uint m_Frame — frame index used to derive deterministic Random instance. The job iterates the map and reduces each cell's m_Pollution by a randomized rounded amount: RoundToIntRandom((float)m_GroundFade / kUpdatesPerDay), clamped to zero.

Properties

  • public int2 TextureSize => new int2(kTextureSize, kTextureSize)
    Returns the cell map texture size as an int2 (256, 256). Useful when interacting with other systems that need the map resolution.

Constructors

  • public GroundPollutionSystem()
    Default constructor. The system relies on OnCreate to perform initialization.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval for the system in simulation ticks: 262144 / kUpdatesPerDay. This controls how often the system is scheduled in a full simulation tick cycle relative to its updates-per-day setting.

  • public static float3 GetCellCenter(int index)
    Utility to get the world-space center for a given cell index. Delegates to CellMapSystem.GetCellCenter with kTextureSize.

  • public static GroundPollution GetPollution(float3 position, NativeArray<GroundPollution> pollutionMap)
    Samples pollution at an arbitrary world position. Uses:

  • CellMapSystem.GetCell to locate the integer cell,
  • CellMapSystem.GetCellCoords to compute fractional coordinates within the cell,
  • Bilinear interpolation across the four surrounding cell values to return a sampled GroundPollution with interpolated m_Pollution (rounded to nearest int). Returns a default GroundPollution (all-zero) if position maps outside the texture bounds.

  • [Preserve] protected override void OnCreate()
    Initialization:

  • Calls base.OnCreate().
  • Retrieves the SimulationSystem instance (m_SimulationSystem = base.World.GetOrCreateSystemManaged()) for frame index access.
  • Calls CreateTextures(kTextureSize) (inherited from CellMapSystem) to create the underlying cell textures.
  • Sets up m_PollutionParameterGroup = GetEntityQuery(ComponentType.ReadOnly()) and calls RequireForUpdate(m_PollutionParameterGroup) so the system runs only when the PollutionParameterData singleton is present.

  • [Preserve] protected override void OnUpdate()
    Schedules the PollutionFadeJob:

  • Constructs PollutionFadeJob with the map (m_Map from base), PollutionParameterData singleton, a RandomSeed.Next() instance, and the current simulation frame index.
  • Schedules the job with JobHandle.CombineDependencies(m_WriteDependencies, m_ReadDependencies, base.Dependency) and adds the resulting dependency as a writer via AddWriter(base.Dependency).
  • Combines read/write dependencies into base.Dependency for proper synchronization with other systems.

  • private struct PollutionFadeJob.Execute()
    The job Execute method runs on worker threads (Burst):

  • Creates a Unity.Mathematics.Random from m_Random and m_Frame.
  • Iterates all cells in m_PollutionMap and, for each cell with m_Pollution > 0, reduces pollution by a randomized rounded amount based on m_PollutionParameters.m_GroundFade / kUpdatesPerDay, clamped to zero, then writes the new value back into the array.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
    CreateTextures(kTextureSize);
    m_PollutionParameterGroup = GetEntityQuery(ComponentType.ReadOnly<PollutionParameterData>());
    RequireForUpdate(m_PollutionParameterGroup);
}

Additional notes for modders: - Use GetPollution(position, pollutionMap) to sample pollution at arbitrary positions; ensure you pass the correct NativeArray (for read access within jobs or main thread with correct dependencies). - The system uses deterministic randomness derived from the SimulationSystem frame index so results are stable across simulation frames. - To affect fade behavior, modify PollutionParameterData.m_GroundFade (this system reads that singleton via its EntityQuery). - The job is Burst-compiled for performance and scheduled with explicit read/write dependency handling; when interacting with this map from other jobs/systems be sure to manage JobHandles and call AddWriter/AddReader as appropriate (following pattern used here).