Game.Simulation.WaterDangerSystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
System that detects and marks static objects (primarily buildings) endangered by water level change events (e.g., tsunami/flood-like events). Runs a Burst-compiled IJobChunk (WaterDangerJob) on a fixed interval (64 simulation frames) that:
- Queries active WaterLevelChange event entities and their durations.
- Computes prediction distances based on wave speed, event direction and city modifiers (warning time).
- Searches the game's static-object quad-tree for buildings within the danger cone/line using a spatial iterator (EndangeredStaticObjectIterator).
- Creates Endanger event entities (Event + Endanger archetype) for endangered targets and updates per-event DangerLevel component on the event entity.
The job is scheduled in parallel and uses the SearchSystem's static object search tree and the EndFrameBarrier command buffer to produce Endanger entities safely for later consumption.
Fields
-
private const uint UPDATE_INTERVAL = 64u
Interval (in simulation frames) at which the system runs / updates (returns 64 from GetUpdateInterval). -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem to read the current simulation frame (frameIndex). -
private CitySystem m_CitySystem
Reference to the CitySystem used to obtain the city's Entity (for city modifiers, etc). -
private SearchSystem m_ObjectSearchSystem
Reference to the SearchSystem used to obtain the static-object NativeQuadTree for spatial queries. -
private EndFrameBarrier m_EndFrameBarrier
Barrier used to create a parallel EntityCommandBuffer for producing Endanger event entities safely to be played back at end frame. -
private EntityQuery m_WaterLevelChangeQuery
Query used to find active WaterLevelChange event entities (excludes Deleted and Temp). -
private EntityArchetype m_EndangerArchetype
Archetype for created Endanger event entities. Constructed as (Game.Common.Event, Endanger). -
private TypeHandle __TypeHandle
Internal container for Entity/component type handles used while scheduling the chunk job. Assigned in OnCreateForCompiler.
Properties
- None. (The system uses private fields and schedules a job; component read/write access is performed inside WaterDangerJob.)
Constructors
public WaterDangerSystem()
Default constructor (calls base GameSystemBase constructor). The system sets up required queries and systems in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval for the system. This implementation returns 64 (the value of UPDATE_INTERVAL), so the system runs every 64 simulation frames. -
[Preserve] protected override void OnCreate()
Initializes references to SimulationSystem, CitySystem, SearchSystem and EndFrameBarrier; creates the water-level-change EntityQuery; creates the Endanger archetype; and calls RequireForUpdate(m_WaterLevelChangeQuery) so the system only updates when WaterLevelChange entities exist. -
[Preserve] protected override void OnUpdate()
Builds and schedules the Burst-compiled WaterDangerJob in parallel across WaterLevelChange event entities. It: - Prepares type handles and component lookups.
- Retrieves a read-only static-object NativeQuadTree from SearchSystem (and collects a dependency JobHandle).
- Schedules the job via JobChunkExtensions.ScheduleParallel and sets up dependencies.
-
Registers the job as a reader of the static tree and with the EndFrameBarrier (to ensure the command buffer playback respects job completion).
-
protected override void OnCreateForCompiler()
Compiler helper that assigns internal queries and type handles via __AssignQueries and __TypeHandle.__AssignHandles. Called by the ECS codegen/runtime. -
private void __AssignQueries(ref SystemState state)
Internal helper used by OnCreateForCompiler to assign queries (empty here, placeholder for generated code patterns).
Inner types (summary of important nested logic)
- private struct WaterDangerJob : IJobChunk
Burst-compiled chunk job that processes WaterLevelChange entities. For each WaterLevelChange event in the chunk it:
- Reads PrefabRef, Duration, WaterLevelChange components.
- Looks up WaterLevelChangeData from the event's prefab.
- If the event is active/impending and has danger flags, it computes prediction distances and uses the static object quad-tree to find buildings inside the threat area.
- Updates the event's DangerLevel component (to indicate whether danger is currently active) and creates Endanger entities for individual endangered objects (via EndFrameBarrier's parallel command buffer).
Important methods inside WaterDangerJob:
- Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Main IJobChunk entry point; iterates components in the chunk and either schedules endangered object searches or updates DangerLevel values.
-
FindEndangeredObjects(int jobIndex, Entity eventEntity, Duration duration, WaterLevelChange waterLevelChange, WaterLevelChangeData waterLevelChangeData)
Computes the prediction window, wave distances (using WaterSystem.WaveSpeed), warning time modifiers (city modifiers), forms a start line for the approaching wave, constructs an EndangeredStaticObjectIterator, and runs the quad-tree iteration to produce Endanger entities. -
private static Bounds1 GetDistanceBounds(Bounds2 bounds, Line2 line)
Utility that computes min/max distances from a rectangular bounds to a line, accounting for intersection (returns distance bounds; if the line intersects the rectangle the min is floored to 0). -
private struct EndangeredStaticObjectIterator : INativeQuadTreeIterator<Entity, QuadTreeBoundsXZ>, IUnsafeQuadTreeIterator<Entity, QuadTreeBoundsXZ>
Iterator used by the native quad-tree to test and handle static objects that intersect the danger prediction. For each candidate: - Checks vertical level vs danger height.
- Ensures the building component exists on the candidate.
- Modifies DangerFlags for emergency shelters (Evacuate -> StayIndoors for shelters).
- Checks existing InDanger component to avoid producing duplicate/less severe Endanger events.
-
Creates an Endanger entity through the parallel EntityCommandBuffer with computed EndFrame and flags.
-
private struct TypeHandle
Helper for storing EntityTypeHandle / ComponentTypeHandle / ComponentLookup / BufferLookup for use in the chunk job. __AssignHandles(...) fills these from a SystemState; used to prepare the job's handles.
Usage Example
// Example: create a WaterLevelChange event entity so WaterDangerSystem will process it.
// NOTE: this is illustrative — actual prefab references and values need to match game data.
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
// Create an event entity with the components the system expects:
Entity waterEvent = em.CreateEntity(
typeof(PrefabRef),
typeof(WaterLevelChange),
typeof(Duration),
typeof(Game.Events.DangerLevel)
);
// Assign a prefab ref (must point to a WaterLevelChange prefab entity)
em.SetComponentData(waterEvent, new PrefabRef { m_Prefab = someWaterLevelChangePrefabEntity });
// Configure direction and danger height for the wave:
em.SetComponentData(waterEvent, new WaterLevelChange {
m_Direction = new float2(1f, 0f), // example direction
m_DangerHeight = 5.0f
});
// Set start/end frames for the event (frames in simulation)
uint now = (uint)World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<SimulationSystem>().frameIndex;
em.SetComponentData(waterEvent, new Duration { m_StartFrame = now + 60u, m_EndFrame = now + 3600u });
// The WaterDangerSystem (running every 64 frames) will evaluate this event, compute danger zones,
// update its DangerLevel and emit Endanger entities for affected buildings.