Skip to content

Game.Simulation.WaterSourceInitializeSystem

Assembly: Game
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
Initializes WaterSourceData components for newly created water-source entities by copying default values from the associated prefab data and computing initial multipliers where appropriate. The system targets entities that have WaterSourceData and PrefabRef and have been marked Created, while excluding Temp entities. Initialization is performed in a Burst-compiled IJobChunk (scheduled in parallel) to populate m_Amount, m_Radius, m_Multiplier and m_Polluted on each WaterSourceData instance, using prefab data (Game.Prefabs.WaterSourceData) and the entity Transform when needed.


Fields

  • private EntityQuery m_WaterSourceQuery
    Used to query entities that require initialization: those with WaterSourceData, PrefabRef and Created, excluding Temp. The query is created in OnCreate() and passed to RequireForUpdate so the system runs only when matching entities exist.

  • private TypeHandle __TypeHandle
    Holds cached ComponentTypeHandle/ComponentLookup instances used by the job. The generated TypeHandle struct provides methods to assign the required handles from a SystemState and avoids recomputing these handles every frame.

Properties

  • (none)

Constructors

  • public WaterSourceInitializeSystem()
    Default parameterless constructor. Marked with [Preserve] attribute in the source so the runtime/linker won’t strip the type.

Methods

  • protected override void OnCreate()
    Creates the EntityQuery (m_WaterSourceQuery) that matches WaterSourceData (read-only), PrefabRef (read-only), Created (read-only), and excludes Temp. Calls RequireForUpdate(m_WaterSourceQuery) so this system only updates when matching entities exist.

  • protected override void OnUpdate()
    Builds and schedules the InitializeWaterSourcesJob (Burst-compiled) as a parallel job using JobChunkExtensions.ScheduleParallel. The job is provided component type handles and a prefab-data lookup via InternalCompilerInterface helper calls that use the cached __TypeHandle entries. The scheduled job fills/updates WaterSourceData for each matching entity chunk.

  • protected override void OnCreateForCompiler()
    Compiler-time helper invoked to setup internal state for the generated system. Assigns query-related data and component handles by calling __AssignQueries and __TypeHandle.__AssignHandles with the system's CheckedStateRef.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Generated helper that (in this implementation) creates and immediately disposes an EntityQueryBuilder (placeholder for query assignment). Present for compiler/codegen reasons.

  • private struct TypeHandle
    Generated struct that caches:

  • ComponentTypeHandle (read/write)
  • ComponentTypeHandle (read-only)
  • ComponentTypeHandle (read-only)
  • ComponentLookup (read-only) It contains __AssignHandles(ref SystemState state) which obtains these handles/lookups from the provided SystemState (used in OnCreateForCompiler / OnUpdate).

  • [BurstCompile] private struct InitializeWaterSourcesJob : IJobChunk
    Burst-compiled job responsible for per-chunk initialization. Fields:

  • ComponentTypeHandle<PrefabRef> m_PrefabRefType (ReadOnly)
  • ComponentTypeHandle<WaterSourceData> m_SourceType
  • ComponentTypeHandle<Transform> m_TransformType (ReadOnly)
  • ComponentLookup<Game.Prefabs.WaterSourceData> m_PrefabSourceDatas (ReadOnly)

Behavior (Execute): - Retrieves native arrays for WaterSourceData, PrefabRef and Transform from the chunk. - For each entity in the chunk: - Looks up the prefab's Game.Prefabs.WaterSourceData via m_PrefabSourceDatas using the PrefabRef. - Copies m_Amount and m_Radius from the prefab to the entity's WaterSourceData. - If the entity's m_ConstantDepth is not 2 or 3, computes m_Multiplier by calling WaterSystem.CalculateSourceMultiplier with the entity's WaterSourceData and its Transform.m_Position. - Sets m_Polluted based on prefab.m_InitialPolluted. - Writes the modified WaterSourceData back into the native array. - The job implements IJobChunk.Execute and delegates to its Execute method.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Query for entities that contain WaterSourceData and a PrefabRef, are newly created, but not Temp.
    m_WaterSourceQuery = GetEntityQuery(
        ComponentType.ReadOnly<WaterSourceData>(),
        ComponentType.ReadOnly<PrefabRef>(),
        ComponentType.ReadOnly<Created>(),
        ComponentType.Exclude<Temp>()
    );
    RequireForUpdate(m_WaterSourceQuery);
}

[Preserve]
protected override void OnUpdate()
{
    // Prepare the job with component type handles / lookups (obtained via the generated TypeHandle helpers)
    var jobData = new InitializeWaterSourcesJob
    {
        m_SourceType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Simulation_WaterSourceData_RW_ComponentTypeHandle, ref base.CheckedStateRef),
        m_PrefabRefType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Prefabs_PrefabRef_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        m_TransformType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Objects_Transform_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        m_PrefabSourceDatas = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Prefabs_WaterSourceData_RO_ComponentLookup, ref base.CheckedStateRef)
    };

    // Schedule the initialization in parallel for matching chunks
    base.Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_WaterSourceQuery, base.Dependency);
}

Notes and implementation details: - The system uses Burst for the chunk job (BurstCompile). - It relies on a prefab data lookup (ComponentLookup) to copy default values from prefabs. - The multiplier computation is skipped when WaterSourceData.m_ConstantDepth equals 2 or 3 (these magic values indicate constant-depth behaviors where multiplier is not derived from the transform). - The system is compiler-generated (attributes like [CompilerGenerated] and generated TypeHandle wiring) to integrate with the DOTS/ECS codegen patterns used in the project.