Game.ObjectPolluteSystem
Assembly:
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
System that updates Plant pollution values based on ground and air pollution maps. It queries all pollutable objects (Plant + Transform) that are scheduled for the current update frame, reads pollution data from the GroundPollutionSystem and AirPollutionSystem, and schedules a Burst-compiled IJobChunk (ObjectPolluteJob) to apply pollution accumulation/fade to each Plant component. The system uses an UpdateFrame shared-component filter so plants are updated incrementally across frames (controlled by kUpdatesPerDay).
Fields
-
public static readonly int kUpdatesPerDay
Constant controlling how many sub-updates per day the system uses. Default value is 32. Used to divide per-day pollution changes into discrete update steps. -
private GroundPollutionSystem m_GroundPollutionSystem
Reference to the world's GroundPollutionSystem used to obtain the ground pollution map (NativeArray) for read access. -
private AirPollutionSystem m_AirPollutionSystem
Reference to the world's AirPollutionSystem used to obtain the air pollution map (NativeArray) for read access. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current frameIndex so the system can compute which UpdateFrame to process. -
private EntityQuery m_PollutionParameterQuery
Query used to obtain the singleton PollutionParameterData (pollution multipliers and fade values) required by the job. -
private EntityQuery m_PollutableObjectQuery
Query that matches pollutable objects: components required are Plant, Transform, and UpdateFrame. Excludes Deleted and Temp. This query is filtered by UpdateFrame to process only a subset each frame. -
private TypeHandle __TypeHandle
Internal struct caching EntityTypeHandle and ComponentTypeHandle instances. Used to obtain handles for scheduling the IJobChunk safely across frames.
Properties
- This system does not expose public properties.
Constructors
public ObjectPolluteSystem()
Default constructor. Marked with [Preserve] in source; no special runtime initialization (actual setup happens in OnCreate).
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval used by the engine scheduling. Implementation returns 262144 / (kUpdatesPerDay * 16). This value determines the cadence at which the system is asked to run relative to the engine update phases. -
[Preserve] protected override void OnCreate()
Initializes references and queries: - Acquires GroundPollutionSystem, AirPollutionSystem, and SimulationSystem from the World.
- Creates m_PollutionParameterQuery to read PollutionParameterData singleton.
-
Builds m_PollutableObjectQuery to match Plant + Transform + UpdateFrame and exclude Deleted & Temp. Called once when the system is created.
-
[Preserve] protected override void OnUpdate()
Main update logic: - Computes the UpdateFrame value to process for this execution via SimulationUtils.GetUpdateFrame using m_SimulationSystem.frameIndex and kUpdatesPerDay.
- Resets and applies a shared-component filter on m_PollutableObjectQuery so only entities with the computed UpdateFrame are processed.
- Obtains read-only native maps from GroundPollutionSystem and AirPollutionSystem via GetMap(readOnly: true, out JobHandle dependencies).
- Reads the PollutionParameterData singleton from m_PollutionParameterQuery.
- Constructs and schedules a Burst-compiled ObjectPolluteJob via JobChunkExtensions.ScheduleParallel, combining dependency JobHandles.
- Registers the scheduled JobHandle as a reader with both pollution systems (m_GroundPollutionSystem.AddReader(jobHandle) and m_AirPollutionSystem.AddReader(jobHandle)).
- Stores the job handle to base.Dependency so the system dependency chain is kept.
The scheduled job reads Transform and Plant components and updates Plant.m_Pollution using values sampled from the ground and air pollution maps and the polluter parameters. The update uses math.saturate to clamp pollution to [0,1] and divides change by kUpdatesPerDay to spread changes across updates.
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler helper used during build-time initialization. In the current implementation it constructs and disposes a temporary EntityQueryBuilder (no runtime queries are assigned here). -
protected override void OnCreateForCompiler()
Compiler-time initialization hook. Calls __AssignQueries and __TypeHandle.__AssignHandles to populate cached component and entity type handles used by the job scheduling logic. -
Inner type:
ObjectPolluteJob
(private struct, [BurstCompile], implements IJobChunk) - Fields:
EntityTypeHandle m_EntityType
(ReadOnly)ComponentTypeHandle<Transform> m_TransformType
(ReadOnly)ComponentTypeHandle<Plant> m_PlantType
(RW)NativeArray<GroundPollution> m_GroundPollutionMap
(ReadOnly)NativeArray<AirPollution> m_AirPollutionMap
(ReadOnly)PollutionParameterData m_PollutionParameters
- Execute behavior:
- Iterates over entities in the ArchetypeChunk.
- For each entity it reads the Transform.m_Position and samples ground/air pollution maps via GroundPollutionSystem.GetPollution / AirPollutionSystem.GetPollution.
- Updates Plant.m_Pollution with: saturate(current + ((plantGroundMultiplier * groundPollution) + (plantAirMultiplier * airPollution) - plantFade) / kUpdatesPerDay).
- Writes the modified Plant component back to the chunk.
-
The job is scheduled in parallel and is burst-compiled for performance.
-
Inner type:
TypeHandle
(private struct) - Holds cached handles:
- EntityTypeHandle __Unity_Entities_Entity_TypeHandle
- ComponentTypeHandle
__Game_Objects_Plant_RW_ComponentTypeHandle - ComponentTypeHandle
__Game_Objects_Transform_RO_ComponentTypeHandle
- Method __AssignHandles(ref SystemState state) populates these handles from the provided SystemState. This reduces overhead when scheduling the job.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by this system:
// - cache references to GroundPollutionSystem / AirPollutionSystem / SimulationSystem
// - create EntityQueries for pollution parameters and pollutable objects
m_GroundPollutionSystem = World.GetOrCreateSystemManaged<GroundPollutionSystem>();
m_AirPollutionSystem = World.GetOrCreateSystemManaged<AirPollutionSystem>();
m_SimulationSystem = World.GetOrCreateSystemManaged<SimulationSystem>();
m_PollutionParameterQuery = GetEntityQuery(ComponentType.ReadOnly<PollutionParameterData>());
m_PollutableObjectQuery = GetEntityQuery(
ComponentType.ReadOnly<Plant>(),
ComponentType.ReadOnly<Transform>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>()
);
}
Notes and remarks - The system relies on UpdateFrame shared-component filtering so plants are updated incrementally. kUpdatesPerDay controls how much of daily pollution change is applied each time an entity is processed. - The job reads pollution maps returned by the GroundPollutionSystem and AirPollutionSystem. Those maps are provided as NativeArray and may produce dependency JobHandles; those are combined with the system dependency when scheduling. - The internal job is Burst-compiled and runs in parallel; ensure component access annotations (read/write) match actual usage to avoid race conditions. - The Plant.m_Pollution value is clamped using math.saturate to keep it in [0,1].