Game.CondemnedBuildingSystem
Assembly: Game (Assembly-CSharp)
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
CondemnedBuildingSystem is a simulation system that processes buildings marked with the Condemned component and randomly marks a subset of them for removal by adding a Deleted component. It runs as an ECS system using a parallel IJobChunk (CondemnedBuildingJob) and issues structural changes via an EndFrameBarrier’s EntityCommandBuffer (parallel writer). The system filters entities on an UpdateFrame shared component so it only runs on specific frames, and it uses a RandomSeed per-chunk to decide which condemned buildings to delete.
Fields
-
private const uint UPDATE_INTERVAL = 1024u
This constant is declared in the class but not used by the current implementation. It likely represents an intended larger interval for some update logic, but GetUpdateInterval currently returns 64 and the shared-component filter uses a different interval. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem in the same world. Used to obtain the current simulation frame index for computing the UpdateFrame filter. -
private EndFrameBarrier m_EndFrameBarrier
Reference to an EndFrameBarrier system used to create an EntityCommandBuffer. The system schedules its CondemnedBuildingJob and uses the barrier to enqueue structural changes (adding Deleted) safely at the end of the frame. -
private EntityQuery m_CondemnedQuery
EntityQuery selecting entities to process. It's configured to include Condemned, Building and UpdateFrame components and to exclude Destroyed, Deleted and Temp components. RequireForUpdate is called with this query so the system only updates when matching entities exist. -
private TypeHandle __TypeHandle
A small wrapper storing an EntityTypeHandle for internal use. It is assigned in OnCreateForCompiler via __AssignHandles and is used to get entity arrays inside the job scheduling.
Properties
- This system exposes no public properties.
Constructors
public CondemnedBuildingSystem()
Default constructor. The system relies on OnCreate to initialize its references and queries. The constructor is annotated with [Preserve] in the source to avoid stripping by AOT/code stripping.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 64. This method controls the system's declared update interval (used by surrounding scheduling mechanisms). Note: there is an unused UPDATE_INTERVAL constant (1024) declared in the class. -
protected override void OnCreate()
Initializes the system: - Acquires references to SimulationSystem and EndFrameBarrier from the World.
- Builds the m_CondemnedQuery to match Condemned + Building + UpdateFrame, excluding Destroyed, Deleted and Temp.
-
Calls RequireForUpdate(m_CondemnedQuery) so the system only runs when matching entities exist. The method is annotated with [Preserve].
-
protected override void OnUpdate()
Main scheduling logic: - Resets any filters on m_CondemnedQuery.
- Computes an UpdateFrame shared component using SimulationUtils.GetUpdateFrameWithInterval with the simulation frame index, the update interval returned by GetUpdateInterval (64), and a sub-interval of 16. This restricts which subset of matched entities are processed this update.
- Creates and schedules a parallel CondemnedBuildingJob that:
- Gets the entity array for the chunk.
- Initializes a Random per-chunk from RandomSeed.Next().
- For each entity in the chunk, picks a random int in [0,4). If result == 0 (25% chance), adds a Deleted component via the parallel ECB writer.
-
Adds the returned JobHandle to the EndFrameBarrier (AddJobHandleForProducer) to ensure the barrier waits for the producer job, and attaches the job handle to base.Dependency.
-
private void __AssignQueries(ref SystemState state)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Compiler helper used during generated OnCreateForCompiler; currently creates and disposes an empty EntityQueryBuilder (placeholder in this generated code). -
protected override void OnCreateForCompiler()
Compiler-time initialization helper: - Calls __AssignQueries to setup any generated queries.
-
Calls __TypeHandle.__AssignHandles to obtain the EntityTypeHandle from the system state. This method is marked [Preserve] in the source.
-
Nested type:
CondemnedBuildingJob : IJobChunk
(private struct)
Job executed per chunk (Burst-compiled): - Fields:
- [ReadOnly] EntityTypeHandle m_EntityType
- [ReadOnly] RandomSeed m_RandomSeed
- EntityCommandBuffer.ParallelWriter m_CommandBuffer
- Behavior:
- Retrieves the chunk's Entity array.
- Creates a Random using the RandomSeed and the unfiltered chunk index.
- Iterates entities in the chunk and with 25% chance adds a Deleted component using m_CommandBuffer.AddComponent(unfilteredChunkIndex, entity, default(Deleted)).
-
Note: Job implements IJobChunk.Execute and forwards to its strongly-typed Execute method.
-
Nested type:
TypeHandle
Small helper struct that contains an EntityTypeHandle and a method __AssignHandles to obtain it from SystemState. Used to pass the EntityTypeHandle into the job via InternalCompilerInterface.GetEntityTypeHandle.
Usage Example
// This system is an ECS system and is created/managed by the World.
// Example: what the system does internally when scheduling the job (simplified).
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_SimulationSystem = World.GetOrCreateSystemManaged<SimulationSystem>();
m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_CondemnedQuery = GetEntityQuery(
ComponentType.ReadOnly<Condemned>(),
ComponentType.ReadOnly<Building>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<Destroyed>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>()
);
RequireForUpdate(m_CondemnedQuery);
}
// The job scheduled in OnUpdate will add a Deleted component to ~25% of matched Condemned buildings:
// m_CommandBuffer.AddComponent(unfilteredChunkIndex, entity, default(Deleted));
Notes and Modding Tips: - The system uses an EndFrameBarrier and an EntityCommandBuffer parallel writer to add Deleted safely; do not attempt structural changes from a job without an ECB. - The random selection is per-chunk and seeded via RandomSeed.Next(); results will vary across runs/frames. - UpdateFrame filtering means only a subset of condemned buildings are processed each tick; customize the interval logic in SetSharedComponentFilter if you need different pacing. - The declared UPDATE_INTERVAL constant is unused — if you intend to use it to control frequency, update GetUpdateInterval or the shared-component filter usage accordingly.