Skip to content

Game.Buildings.WaterPoweredInitializeSystem

Assembly:
Game (inferred from namespace and project structure)

Namespace:
Game.Buildings

Type:
public class WaterPoweredInitializeSystem

Base:
GameSystemBase

Summary:
System that initializes and updates WaterPowered components for buildings by sampling connected net segments (subnets) and their curves, and by querying terrain heights and water surface velocities. It schedules a Burst-compiled IJobChunk (WaterPoweredInitializeJob) that iterates matching entities, samples each connected subnet's curves at multiple points, measures terrain and water heights and velocities, and accumulates metrics (length, height and a flow/estimate) into the WaterPowered component. The system retrieves TerrainHeightData and WaterSurfaceData from the TerrainSystem and WaterSystem and registers readers for correct dependency management. It only runs for entities matching an EntityQuery (Updated + WaterPowered, excluding ServiceUpgrade and Deleted).


Fields

  • private TerrainSystem m_TerrainSystem
    Holds a reference to the world's TerrainSystem. Used to get TerrainHeightData for sampling terrain heights in the job.

  • private WaterSystem m_WaterSystem
    Holds a reference to the world's WaterSystem. Used to get WaterSurfaceData (velocities surface data) for sampling water heights/velocities.

  • private EntityQuery m_WaterPoweredQuery
    Query used to select entities to process: entities with Updated and WaterPowered components, excluding ServiceUpgrade and Deleted. The system is required to update only when this query has matching entities.

  • private TypeHandle __TypeHandle
    Container for the ComponentTypeHandle and ComponentLookup handles used by the job. Populated in OnCreateForCompiler and assigned via __AssignHandles. Encapsulates the handles for Created, Temp, SubNet buffer, WaterPowered and several ComponentLookup types used in the job.

  • Nested types:

  • private struct WaterPoweredInitializeJob (BurstCompile)
    The job that executes per chunk to compute water-related metrics for WaterPowered components. See Methods for details.
  • private struct TypeHandle
    Helper struct that stores component handles/lookup instances and exposes __AssignHandles to initialize them from a SystemState.

Properties

  • None (no public properties on this system)
    This system exposes no public properties; its behaviour is driven by OnCreate/OnUpdate and the internal job.

Constructors

  • public WaterPoweredInitializeSystem()
    Default constructor annotated with [Preserve]. No special parameters. Initialization of handles and queries occurs in OnCreate/OnCreateForCompiler.

Methods

  • protected override void OnCreate() : System.Void
    Initializes m_TerrainSystem and m_WaterSystem via World.GetOrCreateSystemManaged(), constructs the m_WaterPoweredQuery (Updated + WaterPowered, exclude ServiceUpgrade and Deleted) and calls RequireForUpdate(m_WaterPoweredQuery) so the system only runs when there are matching entities.

  • protected override void OnUpdate() : System.Void
    Creates and schedules the Burst-compiled WaterPoweredInitializeJob using JobChunkExtensions.ScheduleParallel and the query m_WaterPoweredQuery. It populates the job's component type handles and component lookups via InternalCompilerInterface.GetComponentTypeHandle / GetBufferTypeHandle / GetComponentLookup and passes TerrainHeightData and WaterSurfaceData from m_TerrainSystem and m_WaterSystem. The WaterSystem returns WaterSurfaceData plus an out JobHandle (deps) which is combined with base.Dependency before scheduling. After scheduling, the system registers the returned job handle with the TerrainSystem (AddCPUHeightReader) and WaterSystem (AddSurfaceReader) to declare read access, and sets base.Dependency to the scheduled job handle.

  • protected override void OnCreateForCompiler() : System.Void
    Method used by the compiled system wiring; calls __AssignQueries and assigns component handles in __TypeHandle via __AssignHandles. Typically generated/used during compilation to ensure handles are set for safety checks.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Internal helper that currently constructs and disposes an EntityQueryBuilder. Present to satisfy compiler-generated setup; effectively a placeholder to bind queries when compiled.

  • private struct WaterPoweredInitializeJob : IJobChunk (nested)

  • Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : System.Void
    The main chunk-execution entrypoint. Reads WaterPowered components and SubNet buffers for each chunk. For chunks that also have Temp or Created components, it iterates WaterPowered elements, resets m_Length, m_Height and m_Estimate, calls CalculateWaterPowered to process connected subnets, then divides m_Height by max(1, m_Length) to normalize height.
  • private void CalculateWaterPowered(ref WaterPowered waterPowered, DynamicBuffer subNets) : System.Void
    Iterates all SubNet entries in the buffer, uses the SubNet entity to fetch PrefabRef and various component lookups (Curve, Composition, PlaceableNetData, NetCompositionData, TerrainComposition). If the subnet and prefab data meet requirements (including the net placement flags having FlowLeft or FlowRight), it calls the curve-based overload below to sample the curve geometry.
  • private void CalculateWaterPowered(ref WaterPowered waterPowered, Curve curve, PlaceableNetData placeableData, NetCompositionData compositionData, TerrainComposition terrainComposition) : System.Void
    For a given curve, computes an integer sample count proportional to curve length and waterSurfaceData.scale.x, then loops sample points:
    • Computes world position (Bezier position) and tangent at sample parameter t.
    • Chooses a "right" or "left" direction based on placement flags (FlowLeft/FlowRight) to project flow into a lateral direction.
    • Offsets sample worldPosition.y by compositionData.m_SurfaceHeight.min + terrainComposition.m_MaxHeightOffset.y.
    • Calls WaterUtils.SampleHeight to obtain terrainHeight, waterHeight and waterDepth at that position.
    • Calls WaterUtils.SampleVelocity to obtain surface velocity vector.
    • Accumulates:
      • num2: sum of max(0, worldPosition.y - terrainHeight) (i.e., elevation above terrain)
      • num3: contribution to flow estimate: dot(velocity, lateralDir) * waterDepth * max(0, worldPosition.y - waterHeight)
    • Increases waterPowered.m_Length by curve.m_Length and accumulates weighted m_Height and m_Estimate by curve.m_Length / num. The math effectively estimates potential water-driven effect along net curves based on local water velocity and depth with weighting by how much of the structure is above terrain and by curve length.
  • void IJobChunk.Execute(...) : System.Void
    Explicit interface implementation calling the job's Execute method.
  • Notes: The job uses several ComponentTypeHandle and ComponentLookup objects and is Burst compiled. It reads many components and writes WaterPowered.

  • protected override void OnCreateForCompiler() : System.Void
    (Already listed above) Called to prepare compiler-time handles; ensures __AssignQueries and __AssignHandles run when systems are compiled.

Additional private helper: - private void __AssignQueries(ref SystemState state) : System.Void
(mentioned above) creates a temporary EntityQueryBuilder and disposes it; boilerplate for compiler wiring.

Usage Example

// This system is a GameSystemBase-managed system and runs automatically when added to the World.
// Typical flow shown (simplified) by what the system already does:

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
    m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();

    // Query: Updated + WaterPowered, exclude ServiceUpgrade, Deleted
    m_WaterPoweredQuery = GetEntityQuery(
        ComponentType.ReadOnly<Updated>(),
        ComponentType.ReadOnly<WaterPowered>(),
        ComponentType.Exclude<ServiceUpgrade>(),
        ComponentType.Exclude<Deleted>()
    );
    RequireForUpdate(m_WaterPoweredQuery);
}

[Preserve]
protected override void OnUpdate()
{
    // The system builds a WaterPoweredInitializeJob, obtains TerrainHeightData and
    // WaterSurfaceData from the TerrainSystem and WaterSystem, schedules the job and
    // registers readers so the terrain/water systems know the job will read their data.
    // See source file for the full scheduling call and dependency handling.
}

Notes and Implementation Details: - The heavy lifting is done in a Burst-compiled IJobChunk that is scheduled in parallel across chunks. - The job uses ComponentLookup (read-only) for multiple net/prefab related components and BufferAccessor for SubNets. - TerrainHeightData and WaterSurfaceData are acquired from their systems and require the job handle to be registered as a reader via m_TerrainSystem.AddCPUHeightReader and m_WaterSystem.AddSurfaceReader to ensure correct synchronization. - The system only processes entities that are "Updated" and have a WaterPowered component (and not ServiceUpgrade/Deleted). The job only runs per chunk when Temp or Created components are present in that chunk, causing it to reinitialize/reset WaterPowered values. - Burst, math, and native collections are used for performance.