Skip to content

Game.Simulation.FloodCheckSystem

Assembly: Assembly-CSharp.dll
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
FloodCheckSystem periodically scans placeable buildings/objects to detect if they are flooded and, when appropriate, schedules Submerge events. It runs a Burst-compiled IJobChunk (FloodCheckJob) over a target EntityQuery, samples water surface depth and terrain height, and — for objects that are not explicitly marked as submersible or floating — creates Submerge events via an EndFrameBarrier command buffer. The system also inspects active WaterLevelChange events (e.g., tsunami wave events of Sine type) to find a related flood event for more accurate submerge event attribution. The system updates every 16 simulation frames and uses TerrainSystem and WaterSystem read access to sample heights and surface data.


Fields

  • private const uint UPDATE_INTERVAL = 16u
    Describes the update interval used by the system (16 simulation frames). This value is used by GetUpdateInterval to control how often chunks are processed.

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem (provides current frame index and general simulation timing).

  • private TerrainSystem m_TerrainSystem
    Reference to the TerrainSystem used to obtain terrain height data required when deciding if an object is submerged.

  • private WaterSystem m_WaterSystem
    Reference to the WaterSystem used to obtain water surface data and wave parameters (WaveSpeed, kMapSize, etc.).

  • private EndFrameBarrier m_EndFrameBarrier
    Used to create an EntityCommandBuffer. The system writes Submerge entities into this barrier so they are created at the end of the frame in a thread-safe manner.

  • private EntityQuery m_TargetQuery
    Query selecting candidate objects to check for flooding (ReadOnly Building, excluding Placeholder/Flooded/Deleted/Temp).

  • private EntityQuery m_WaterLevelChangeQuery
    Query selecting WaterLevelChange entities (with Duration) used by FloodCheckJob.FindFloodEvent to associate submerge events to a water level change (for tsunamis/waves).

  • private EntityArchetype m_SubmergeArchetype
    Archetype used to create Submerge event entities (contains Game.Common.Event and Submerge components).

  • private TypeHandle __TypeHandle
    Generated holder for Entity/Component type handles and ComponentLookup handles used by the job; assigned in OnCreateForCompiler / internal initialization.

  • (Nested type) FloodCheckJob (private struct)
    Burst-compiled IJobChunk that performs the actual per-chunk checking. Holds component type handles & lookups and a ParallelWriter command buffer. Key behaviors:

  • Uses a SharedComponent UpdateFrame to spread checks across frames (m_UpdateFrameIndex).
  • Iterates transforms and prefab references; calls IsFlooded(position, out depth).
  • Skips objects that are allowed to submerge or are floating/swaying (via ObjectGeometryData/PlaceableObjectData flags).
  • Calls FindFloodEvent to try to associate a WaterLevelChange event (e.g., tsunami); otherwise leaves event null.
  • Creates Submerge entities via command buffer with the target, event, and computed depth.
  • IsFlooded: samples water surface depth and adds terrain height difference; threshold > 0.5f used to consider an object flooded.
  • FindFloodEvent: inspects WaterLevelChange chunks, filters by Duration and Sine type, computes proximity to a theoretical wave line and timing using WaterSystem.WaveSpeed and other timing math to pick the most relevant event.

  • (Nested type) TypeHandle (private struct)
    Generated helper for obtaining type handles and ComponentLookup handles used by the FloodCheckJob. Assigned via __AssignHandles(ref SystemState).

Properties

  • None (no public properties on this system)

Constructors

  • public FloodCheckSystem()
    Default constructor (preserved). Initialization of fields happens in OnCreate / OnCreateForCompiler.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the system update interval (16). This controls how often this system will run; consistent with UPDATE_INTERVAL.

  • [Preserve] protected override void OnCreate()
    Initializes references and queries:

  • Retrieves SimulationSystem, TerrainSystem, WaterSystem, and EndFrameBarrier from the World.
  • Constructs target (m_TargetQuery) and water level change (m_WaterLevelChangeQuery) queries.
  • Creates the Submerge archetype used to instantiate Submerge events.
  • Calls RequireForUpdate(m_TargetQuery) so the system only runs when there are targets.

  • [Preserve] protected override void OnUpdate()
    Builds and schedules the Burst-compiled FloodCheckJob:

  • Computes m_UpdateFrameIndex by shifting SimulationSystem.frameIndex >> 4 and masking (distributes load across 16 subframes).
  • Gets type handles and ComponentLookup references (via InternalCompilerInterface wrappers around __TypeHandle).
  • Obtains Terrain height data and Water surface data (WaterSystem.GetSurfaceData(out deps)).
  • Builds a list of WaterLevelChange archetype chunks asynchronously (ToArchetypeChunkListAsync).
  • Creates a parallel command buffer via EndFrameBarrier.CreateCommandBuffer().AsParallelWriter().
  • Schedules the job in parallel over m_TargetQuery and combines dependencies appropriately.
  • Registers readers and producer job handles with TerrainSystem, WaterSystem, and EndFrameBarrier so the systems synchronize correctly.

  • protected override void OnCreateForCompiler()
    Internal helper called by generated code/path to assign queries and type handles for compilation-time generated code paths. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Generated placeholder for query assignment used by the compiler/backing code. Currently contains minimal logic (EntityQueryBuilder transient use).

  • (Nested job methods inside FloodCheckJob)

  • Execute(in ArchetypeChunk chunk, ...) — chunk processing entry that checks UpdateFrame shared component and iterates chunk entities.
  • FindFloodEvent(Entity entity, float3 position) — finds the best matching WaterLevelChange event (tsunami/wave) based on distance from the wave front and timing; returns Entity.Null if none found.
  • IsFlooded(float3 position, out float depth) — samples water depth and terrain height; uses 0.5f thresholds to decide flooding and returns computed depth.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // Typical initialization performed by the system:
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
    m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
    m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();
    m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();

    // Query for buildings / placeable objects that may flood
    m_TargetQuery = GetEntityQuery(
        ComponentType.ReadOnly<Building>(),
        ComponentType.Exclude<Placeholder>(),
        ComponentType.Exclude<Flooded>(),
        ComponentType.Exclude<Deleted>(),
        ComponentType.Exclude<Temp>());

    // Query for water level change events (tsunamis, waves)
    m_WaterLevelChangeQuery = GetEntityQuery(
        ComponentType.ReadOnly<WaterLevelChange>(),
        ComponentType.ReadOnly<Duration>(),
        ComponentType.Exclude<Deleted>(),
        ComponentType.Exclude<Temp>());

    // Archetype for generating Submerge events
    m_SubmergeArchetype = base.EntityManager.CreateArchetype(
        ComponentType.ReadWrite<Game.Common.Event>(),
        ComponentType.ReadWrite<Submerge>());

    RequireForUpdate(m_TargetQuery);
}

Notes and implementation details: - Flood detection uses two thresholds of 0.5f: first on sampled water depth, then after adding the terrain height delta to the object's Y position. Only if both checks exceed 0.5f is an object considered flooded and a Submerge event created. - Objects marked with ObjectGeometryData.CanSubmerge or PlaceableObjectData having PlacementFlags.Floating | PlacementFlags.Swaying are skipped (floating/swavging objects are not submerged by this system). - FindFloodEvent attempts to prefer an InDanger-associated event (if the entity already references one). Otherwise it selects the most relevant WaterLevelChange (Sine type) by computing distance to a wave line and comparing a smoothstep-based proximity score. - The job is scheduled in parallel using ScheduleParallel and uses an EndFrameBarrier command buffer's ParallelWriter to create Submerge entities safely from worker threads. - System registers readers with TerrainSystem and WaterSystem and ties produced job handle to EndFrameBarrier so cross-system dependencies are respected.