Skip to content

Game.Simulation.WaterPipePollutionSystem

Assembly:
Game (assembly inferred from project structure)

Namespace:
Game.Simulation

Type:
class

Base:
GameSystemBase

Summary:
WaterPipePollutionSystem is an ECS system responsible for updating pollution values in the water pipe network. It runs at a fixed interval (64 frames) and schedules two Burst-compiled IJobChunk jobs: - NodePollutionJob: computes per-node fresh pollution by averaging pollution from incoming flows or applying stale-water purification when no inflow is present. - EdgePollutionJob: updates per-edge fresh pollution based on connected node pollution values, the flow direction, a source node and a "purify" flag controlled by simulation parameters.

The system reads WaterPipeParameterData (singleton) to control timing and purification rates, and depends on WaterPipeFlowSystem for the current source node. It uses EntityQuery to select nodes and edges, and uses component type handles and lookups for safe Burst job access.


Fields

  • private const int kUpdateInterval = 64
    This constant defines the update interval (in frames) for the system. The system's GetUpdateInterval returns this value so the system runs once every 64 simulation frames.

  • private WaterPipeFlowSystem m_WaterPipeFlowSystem
    Reference to the WaterPipeFlowSystem instance in the same World; used to access the current source node for edge pollution propagation.

  • private SimulationSystem m_SimulationSystem
    Reference to the central SimulationSystem; used to access simulation-wide state such as the frame index to determine purification scheduling.

  • private EntityQuery m_NodeQuery
    EntityQuery selecting water pipe nodes (WaterPipeNode) with their ConnectedFlowEdge buffers; used to schedule NodePollutionJob.

  • private EntityQuery m_EdgeQuery
    EntityQuery selecting water pipe edges (WaterPipeEdge); used to schedule EdgePollutionJob.

  • private EntityQuery m_ParameterQuery
    EntityQuery selecting WaterPipeParameterData singleton; used to fetch parameters like m_StaleWaterPipePurification and m_WaterPipePollutionSpreadInterval.

  • private TypeHandle __TypeHandle
    Internal struct instance that caches Unity ECS type handles and lookups for use when scheduling jobs (EntityTypeHandle, BufferTypeHandle, ComponentTypeHandle, ComponentTypeHandle, and ComponentLookup<...>). Populated by __AssignHandles during compiler-time setup.

Properties

  • None (this system exposes no public properties)

Constructors

  • public WaterPipePollutionSystem()
    Default constructor. Marked with [Preserve] in other lifecycle methods to avoid stripping; the system is created and managed by the ECS World (via GetOrCreateSystemManaged).

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the system update interval in frames. Implementation returns 64 (kUpdateInterval), causing the system to run once every 64 frames.

  • [Preserve] protected override void OnCreate()
    Initializes the system:

  • Acquires references to WaterPipeFlowSystem and SimulationSystem from the World.
  • Builds EntityQueries for nodes, edges and the parameter singleton (WaterPipeParameterData).
  • Calls RequireForUpdate for node, edge and parameter queries so the system only runs if those exist.

  • [Preserve] protected override void OnUpdate()
    Main update method that runs on the configured interval:

  • Reads WaterPipeParameterData singleton.
  • Computes a purify flag using m_SimulationSystem.frameIndex and the parameter spread interval (to stagger purification).
  • Schedules NodePollutionJob (Burst-compiled, parallel) over m_NodeQuery. NodePollutionJob:
    • Reads each node's ConnectedFlowEdge buffer and looks up edges to compute average incoming fresh pollution (weighted by fresh flow).
    • If no incoming fresh flow, decays the node's fresh pollution by stale purification value.
  • Schedules EdgePollutionJob (Burst-compiled, parallel) over m_EdgeQuery, dependent on the node job. EdgePollutionJob:
    • For each edge, determines fresh pollution based on start/end node pollution and flow direction, skipping the edge if it starts at the source node.
    • Applies the purify flag to optionally skip overwriting when purification is active.
  • Updates base.Dependency with the final job handle.

  • private void __AssignQueries(ref SystemState state)
    Internal helper used by the compiler-time creation hook. In this implementation it constructs (and immediately disposes) an EntityQueryBuilder to ensure query assignment side-effects are executed during OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    Compiler-generated hook that calls __AssignQueries and populates cached type handles via __TypeHandle.__AssignHandles. This supports the job scheduling code that uses InternalCompilerInterface to fetch the prepared handles.

  • Nested/Burst jobs (summaries)

  • NodePollutionJob : IJobChunk
    • Fields: EntityTypeHandle m_EntityType (readonly), BufferTypeHandle m_FlowConnectionType (readonly), ComponentTypeHandle m_NodeType, ComponentLookup m_FlowEdges (readonly), float m_StaleWaterPipePurification.
    • Execute: For every node in the chunk, iterates its ConnectedFlowEdge buffer, sums flows and weighted pollution from connected edges, sets node.m_FreshPollution to the weighted average when there is incoming fresh flow; otherwise reduces node.m_FreshPollution by m_StaleWaterPipePurification clamped at zero.
  • EdgePollutionJob : IJobChunk
    • Fields: ComponentTypeHandle m_FlowEdgeType, ComponentLookup m_FlowNodes (readonly), Entity m_SourceNode, bool m_Purify.
    • Execute: For every edge in the chunk, reads start/end node pollution and sets edge.m_FreshPollution according to flow direction. Edges that start at the source node are skipped. If m_Purify is true, edges may avoid being overwritten when computed pollution is zero (per code logic).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_WaterPipeFlowSystem = base.World.GetOrCreateSystemManaged<WaterPipeFlowSystem>();
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
    m_NodeQuery = GetEntityQuery(ComponentType.ReadWrite<WaterPipeNode>(),
                                 ComponentType.ReadOnly<ConnectedFlowEdge>(),
                                 ComponentType.Exclude<Deleted>());
    m_EdgeQuery = GetEntityQuery(ComponentType.ReadWrite<WaterPipeEdge>(),
                                 ComponentType.Exclude<Deleted>());
    m_ParameterQuery = GetEntityQuery(ComponentType.ReadOnly<WaterPipeParameterData>());

    RequireForUpdate(m_NodeQuery);
    RequireForUpdate(m_EdgeQuery);
    RequireForUpdate(m_ParameterQuery);
}

Notes and implementation details: - The system relies heavily on Burst-compiled IJobChunk implementations for performance in large networks. - It uses ComponentLookup (ComponentLookup / ComponentLookup<...>(isReadOnly: true)) to access components from within jobs without manual chunk joins. - The purify scheduling uses simulation frame index / 64 % spreadInterval to stagger pollution propagation vs purification (as implemented). - This system requires the WaterPipeParameterData singleton and WaterPipeFlowSystem to exist; otherwise it will not run (RequireForUpdate).