Skip to content

Game.Simulation.GroundWaterPollutionSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
GroundWaterPollutionSystem is a simulation system that applies ground pollution to groundwater cells. It schedules a Burst-compiled IJob (PolluteGroundWaterJob) that reads the GroundPollution map and updates the GroundWater map: for each groundwater cell it samples pollution at the cell center and, if positive, increases the cell's m_Polluted value by (pollution.m_Pollution / 200), clamped to the groundwater amount. The system obtains references to GroundWaterSystem and GroundPollutionSystem on creation and registers read/write dependencies when scheduling the job. It runs periodically with an update interval of 128 and an offset of 64.


Fields

  • private struct PolluteGroundWaterJob
    Burst-compiled nested IJob that performs the per-cell pollution transfer from the pollution map to the groundwater map.
  • public NativeArray<GroundWater> m_GroundWaterMap — writable array of groundwater cells; updated by the job.
  • [ReadOnly] public NativeArray<GroundPollution> m_PollutionMap — read-only array of pollution cells used to sample pollution.
  • Execute behavior: iterates all groundwater cells, obtains pollution at the cell center via GroundPollutionSystem.GetPollution(GroundWaterSystem.GetCellCenter(i), m_PollutionMap). If pollution.m_Pollution > 0, increases the groundwater cell's m_Polluted by pollution.m_Pollution / 200, clamped to the cell's m_Amount, and writes the updated GroundWater back into m_GroundWaterMap.

  • private GroundWaterSystem m_GroundWaterSystem
    Holds a reference to the world's GroundWaterSystem. It is set during OnCreate and used in OnUpdate to obtain a writable map and to register the system as a writer for that map.

  • private GroundPollutionSystem m_GroundPollutionSystem
    Holds a reference to the world's GroundPollutionSystem. It is set during OnCreate and used in OnUpdate to obtain a read-only pollution map and to register the system as a reader for that map.

Properties

  • This type exposes no public properties.

Constructors

  • public GroundWaterPollutionSystem()
    Default constructor. Marked with [Preserve] in source. No custom initialization beyond what base constructor does; the actual system references are populated in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 128. This indicates how frequently (in simulation ticks / frames according to the engine's scheduling) the system is updated.

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns 64. The offset is used to stagger system updates across phases / frames.

  • [Preserve] protected override void OnCreate()
    Called when the system is created. Retrieves and stores references to the GroundWaterSystem and GroundPollutionSystem via base.World.GetOrCreateSystemManaged(). No job scheduling occurs here.

  • [Preserve] protected override void OnUpdate()
    Creates a PolluteGroundWaterJob instance and populates its NativeArray fields by requesting maps from the GroundWaterSystem (writable) and GroundPollutionSystem (read-only). It collects the dependency JobHandles returned by those GetMap calls, combines them with base.Dependency, schedules the job, stores the returned JobHandle in base.Dependency, and registers the GroundWaterSystem as a writer and the GroundPollutionSystem as a reader by calling AddWriter and AddReader with the scheduled JobHandle. This ensures proper dependency chaining and thread-safety for map access.

  • private struct PolluteGroundWaterJob.Execute()
    Implements the job's work. For each groundwater cell:

  • Query pollution using GroundPollutionSystem.GetPollution(...) with the cell center.
  • If pollution.m_Pollution > 0, compute increment = pollution.m_Pollution / 200 and set m_Polluted = (short)math.min(m_Amount, m_Polluted + increment).
  • Write the modified GroundWater back to the m_GroundWaterMap.

Notes: - The job uses Unity.Burst for performance and Unity.Jobs with NativeArray data. Proper dependency combination and registering of readers/writers ensure safe access to the shared maps. - The integer division by 200 and cast to short imply coarse, integer-based pollution accumulation.

Usage Example

[Preserve]
protected override void OnUpdate()
{
    JobHandle dep1, dep2;
    var jobData = new PolluteGroundWaterJob
    {
        m_GroundWaterMap = m_GroundWaterSystem.GetMap(readOnly: false, out dep1),
        m_PollutionMap = m_GroundPollutionSystem.GetMap(readOnly: true, out dep2)
    };

    // Combine current dependency with map dependencies and schedule the job
    base.Dependency = jobData.Schedule(JobHandle.CombineDependencies(base.Dependency, dep1, dep2));

    // Register read/write access so other systems know about the scheduled dependency
    m_GroundWaterSystem.AddWriter(base.Dependency);
    m_GroundPollutionSystem.AddReader(base.Dependency);
}

This system is managed by the simulation's world and will run automatically according to its interval/offset. If you create custom systems that read or write the same maps, use AddReader/AddWriter and proper JobHandle dependency chaining in the same pattern to avoid race conditions.