Game.Simulation.WaterDamageSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: public class
Base: GameSystemBase
Summary:
WaterDamageSystem processes entities that have been marked as Flooded. On a periodic update (interval = 64), it runs a Burst-compiled IJobChunk (WaterDamageJob) that:
- Samples water surface and terrain height to compute effective flood depth for each flooded entity.
- Calculates flood damage rate using DisasterConfigurationData, object geometry (size), and structural integrity.
- Applies city-level modifiers to disaster damage rate if the entity is a building.
- Either increments a Damaged component (and possibly issues a Destroy event when fully damaged) or creates a Damage event entity.
- Adds or removes in-game notification icons via IconCommandSystem for water damage and destruction.
The system depends on CitySystem, TerrainSystem, WaterSystem, EndFrameBarrier (for command buffering), and IconCommandSystem, and uses an EventHelpers.StructuralIntegrityData helper to query structural integrity. The per-entity work is performed in a parallel job and reads water/terrain data via the respective systems' reader APIs.
Fields
-
private const uint UPDATE_INTERVAL = 64u
Update interval (ticks/frames) for the system; GetUpdateInterval returns 64. -
private IconCommandSystem m_IconCommandSystem
Reference to the IconCommandSystem used to add/remove notification icons for damage and destruction. -
private CitySystem m_CitySystem
Reference to CitySystem; used to obtain the city entity for applying city modifiers. -
private TerrainSystem m_TerrainSystem
Reference to TerrainSystem; used to obtain terrain height data required to compute flood depth. -
private WaterSystem m_WaterSystem
Reference to WaterSystem; used to obtain water surface data required to compute flood depth. -
private EndFrameBarrier m_EndFrameBarrier
Command buffer barrier used to create damage/destroy event entities at the end of the frame. -
private EntityQuery m_FloodedQuery
Query matching entities that are Flooded (and not Deleted/Temp); used to schedule the job only when there are flooded entities. -
private EntityQuery m_FireConfigQuery
Query used to read FireConfigurationData (structural integrity helper uses this). -
private EntityQuery m_DisasterConfigQuery
Query used to read DisasterConfigurationData (flood damage rates, notification prefabs, etc). -
private EntityArchetype m_DamageEventArchetype
Archetype for creating Damage event entities. -
private EntityArchetype m_DestroyEventArchetype
Archetype for creating Destroy event entities. -
private EventHelpers.StructuralIntegrityData m_StructuralIntegrityData
Helper struct used to obtain structural integrity values for prefabs/entities. -
private TypeHandle __TypeHandle
Generated container for the Entity/Component type handles used by the job; assigned in OnCreateForCompiler. -
private struct WaterDamageJob
(nested)
Burst-compiled IJobChunk that performs the per-chunk/per-entity flood damage logic (see Methods). -
private struct TypeHandle
(nested)
Generated struct that stores EntityTypeHandle, ComponentTypeHandle and ComponentLookup/BufferLookup instances and assigns them from a SystemState.
Properties
- None (no public properties exposed by this system)
Constructors
public WaterDamageSystem()
Default constructor. The system uses the OnCreate lifecycle method to initialize dependencies, queries and archetypes.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval. Implementation returns 64 (UPDATE_INTERVAL). -
[Preserve] protected override void OnCreate()
Initializes system references and queries: - Gets or creates IconCommandSystem, CitySystem, TerrainSystem, WaterSystem, and EndFrameBarrier.
- Creates entity queries for Flooded, FireConfigurationData, and DisasterConfigurationData.
- Creates archetypes for Damage and Destroy event entities.
- Initializes m_StructuralIntegrityData.
-
Calls RequireForUpdate(m_FloodedQuery) so the system only updates when there are flooded entities.
-
[Preserve] protected override void OnUpdate()
Main scheduling method: - Reads singleton FireConfigurationData and DisasterConfigurationData.
- Updates m_StructuralIntegrityData with the current fire config.
- Obtains water surface data (with a reader JobHandle) and terrain height data.
- Builds and schedules the WaterDamageJob as a parallel IJobChunk, wiring in component type handles, lookups, command buffers, and configuration data.
- Registers job handles as readers with TerrainSystem/WaterSystem/IconCommandSystem and adds the job handle to the EndFrameBarrier as a producer.
-
Assigns the returned JobHandle to base.Dependency.
-
protected override void OnCreateForCompiler()
Compiler helper that assigns query/type handles from the generated TypeHandle instance. Called as part of system creation in the compiled environment. -
private void __AssignQueries(ref SystemState state)
Compiler-generated stub for query assignment (no-op in this compiled code). -
private struct WaterDamageJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Per-chunk execution logic (Burst compiled): - Iterates entities in chunk that have Flooded component (and optionally Damaged, Building, Destroyed).
- Computes flood depth via GetFloodDepth (samples water surface depth and terrain height).
- Computes raw flood damage rate using DisasterConfigurationData and object geometry height (reduces damage for taller objects).
- Applies prefab structural integrity and city modifiers (CityModifierType.DisasterDamageRate) if entity is a building.
- Caps per-step damage and either:
- Increments an existing Damaged component and, if total damage reaches 1, creates a Destroy event entity and updates notification icons.
- Or creates a Damage event entity when no Damaged component exists.
- Adds/removes Flooded component and water damage notification icons based on current flood depth.
-
Uses EntityCommandBuffer.ParallelWriter and IconCommandBuffer to enqueue changes/symbol updates.
-
private float GetFloodDepth(float3 position)
(inside WaterDamageJob)
Samples Water surface depth and terrain height to determine effective flood depth at a world position. Returns value > 0 when flooding is significant (>0.5f after combining surface depth and terrain offset), otherwise 0.
Usage Example
Demonstrates a simple pattern: add a Flooded component to an entity (e.g., a building) so WaterDamageSystem will process it on its next update. In real mods you typically don't instantiate the system directly — the ECS world manages systems — but you can add components to entities so the system will act on them.
// Assuming 'entityManager' is available and 'buildingEntity' is a target entity
// and Flooded is a blittable component with fields m_Depth (float) and m_Event (Event).
var flood = new Flooded { m_Depth = 1.0f, m_Event = new Event(/* event data */) };
if (!entityManager.HasComponent<Flooded>(buildingEntity))
{
entityManager.AddComponentData(buildingEntity, flood);
}
else
{
entityManager.SetComponentData(buildingEntity, flood);
}
// The WaterDamageSystem will sample water/terrain, compute damage, and produce Damage/Destroy events
// and notifications on its next scheduled update (system update interval = 64).
Notes: - The system relies on DisasterConfigurationData and FireConfigurationData singletons; ensure those exist in the world (they are typically provided by the game). - The job is Burst-compiled and uses parallel command buffering (EndFrameBarrier) and IconCommandBuffer for icon changes — ensure you respect these systems' reader/writer contracts if you schedule custom jobs that interact with the same data.