Skip to content

Game.Simulation.CollapsedBuildingSystem

Assembly:
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
Handles the simulation logic for collapsed/destroyed buildings. Runs on an update interval (64) and processes entities that have a Destroyed component (and optionally Building or Extension components). The system: - Advances/updates the Destroyed.m_Cleared timer, - Triggers rescue requests (FireRescueRequest) for collapsed buildings that need rescue, - Adds or removes components such as RescueTarget, InterpolatedTransform, Updated, Deleted as buildings transition through cleared / deletion states, - Uses an EndFrameBarrier command buffer to enqueue structural changes safely from a parallel job (CollapsedBuildingJob).


Fields

  • private const uint UPDATE_INTERVAL = 64u
    Interval (in system ticks/frames) at which the system runs. The system overrides GetUpdateInterval to return this value.

  • private EndFrameBarrier m_EndFrameBarrier
    Reference to the EndFrameBarrier system used to create an EntityCommandBuffer for making entity/component changes at the end of the frame from a job.

  • private EntityQuery m_CollapsedQuery
    Query used to select entities with Destroyed and optionally Building/Extension components, excluding Deleted and Temp entities.

  • private EntityArchetype m_RescueRequestArchetype
    Archetype used to create rescue request entities (ServiceRequest + FireRescueRequest + RequestGroup) when a new rescue needs to be spawned.

  • private TypeHandle __TypeHandle
    Internal struct holding EntityTypeHandle / ComponentTypeHandle / ComponentLookup / BufferLookup handles used by the job. Assigned in OnCreateForCompiler.

Properties

  • None (no public properties).

Constructors

  • public CollapsedBuildingSystem()
    Default constructor. (Preserved by attribute in source — no custom construction logic besides base initialization.)

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 64 — the update interval for this system.

  • [Preserve] protected override void OnCreate()
    Initializes the system:

  • Obtains the EndFrameBarrier system.
  • Builds the m_CollapsedQuery (All: Destroyed; Any: Building or Extension; None: Deleted, Temp).
  • Creates the m_RescueRequestArchetype (ServiceRequest, FireRescueRequest, RequestGroup).
  • Calls RequireForUpdate(m_CollapsedQuery) so the system only runs when there are matching entities.
  • Performs an assertion.

  • [Preserve] protected override void OnUpdate()
    Creates and schedules the CollapsedBuildingJob in parallel:

  • Populates the CollapsedBuildingJob with Entity/Component type handles and lookups obtained via InternalCompilerInterface and the TypeHandle stored in the system.
  • Uses m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter() for structural changes.
  • Schedules the job with JobChunkExtensions.ScheduleParallel over m_CollapsedQuery.
  • Passes the produced dependency to the EndFrameBarrier (AddJobHandleForProducer).

  • protected override void OnCreateForCompiler()
    Compiler helper that calls __AssignQueries and assigns type handles via __TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal helper (used by compiled code / source generator). In this class it only constructs and disposes an EntityQueryBuilder(Allocator.Temp) — placeholder behavior in generated code.

Nested types and job details (collapsed behavior performed inside the job):

  • private struct CollapsedBuildingJob : IJobChunk
  • Purpose: iterates matching archetype chunks and applies the collapsed-building logic per-entity in parallel.
  • Read-only inputs: EntityTypeHandle, RescueTarget, ServiceUpgrade, Extension, Owner, Attached, PrefabRef component type handles; component lookups for FireRescueRequest, Area, PrefabRef, ObjectGeometryData, BuildingData; buffer lookup for SubArea.
  • Read-write: Destroyed component type handle.
  • Extra: EntityArchetype m_RescueRequestArchetype and EntityCommandBuffer.ParallelWriter m_CommandBuffer for creating rescue requests and making structural changes.

Main logic implemented in Execute: - If entities in chunk have RescueTarget components: - For each entity where Destroyed.m_Cleared < 1: call RequestRescueIfNeeded to ensure a FireRescueRequest exists. - If Destroyed.m_Cleared >= 1, remove the RescueTarget component via command buffer. - Otherwise (no RescueTarget present in chunk): - For each entity: - Determine whether the building (or its owner) requires a road (via Prefab building data flags). This affects how m_Cleared gets clamped when crossing zero. - If Destroyed.m_Cleared < 0: - Increment m_Cleared by ~1.0666667f per update. If it crosses >= 0: - Set m_Cleared to 1 or 0 depending on whether building requires a road. - Remove InterpolatedTransform, add Updated via command buffer. - Else if Destroyed.m_Cleared is between 0 and 1 and entity is not an Extension: - If building requires road: add a RescueTarget and request rescue via RequestRescueIfNeeded. - Else set m_Cleared to 1 (cleared). - After processing cleared timers, for entities where Destroyed.m_Cleared >= 1 and not a ServiceUpgrade/Extension: - If object geometry flags indicate Overridable and entity is not Attached: add Deleted component. - Else if owner area exists: - Add Deleted component. - If the entity has SubAreas buffer, mark each subarea's Area component with Updated via command buffer.

  • private void RequestRescueIfNeeded(int jobIndex, Entity entity, RescueTarget rescueTarget)

    • Creates a rescue request entity (m_RescueRequestArchetype) and sets FireRescueRequest and RequestGroup components only if the rescueTarget.m_Request entity does not already have a FireRescueRequest component (checked via m_FireRescueRequestData lookup).
    • The created FireRescueRequest uses priority/time (10f) and type Disaster; RequestGroup set to 4.
  • private struct TypeHandle
    Holds all the EntityTypeHandle / ComponentTypeHandle / ComponentLookup / BufferLookup fields used by the job and provides __AssignHandles(ref SystemState state) to initialize them.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // obtain the end-of-frame barrier to apply structural changes from the job
    m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();

    // create the query used by this system: must have Destroyed and optionally Building/Extension
    m_CollapsedQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[] { ComponentType.ReadOnly<Destroyed>() },
        Any = new ComponentType[] { ComponentType.ReadOnly<Building>(), ComponentType.ReadOnly<Extension>() },
        None = new ComponentType[] { ComponentType.ReadOnly<Deleted>(), ComponentType.ReadOnly<Temp>() }
    });

    // archetype used for creating rescue request entities
    m_RescueRequestArchetype = EntityManager.CreateArchetype(
        ComponentType.ReadWrite<ServiceRequest>(),
        ComponentType.ReadWrite<FireRescueRequest>(),
        ComponentType.ReadWrite<RequestGroup>());

    RequireForUpdate(m_CollapsedQuery);
}

Notes and implementation details: - The system runs on a 64-tick interval and schedules the CollapsedBuildingJob in parallel; changes to entity structure are done through an EndFrameBarrier command buffer to avoid race conditions. - Destroyed.m_Cleared is used as a state/timer for the collapse/clearing progression: negative values are advanced until they reach >= 0, then further logic (rescue request or immediate clearing) is applied depending on prefab flags (e.g., RequireRoad). - The system interacts with many other components (RescueTarget, FireRescueRequest, PrefabRef, BuildingData, ObjectGeometryData, SubArea/Area) to decide when to spawn rescue service requests and when to delete or mark entities updated.