Skip to content

Game.Simulation.CityDangerLevelSystem

Assembly:
Assembly-CSharp

Namespace:
Game.Simulation

Type:
class

Base:
GameSystemBase

Summary:
Aggregates danger-level information reported by event entities and writes the maximum danger level into the single City DangerLevel component. The system collects Game.Events.DangerLevel components across entities (in parallel, using a Burst-compiled IJobChunk), reduces them with a NativeAccumulator, and then writes the final maximum value into the city's Game.City.DangerLevel component via a separate job. The system is scheduled to run infrequently (GetUpdateInterval returns 128), and it requires the Game.City.DangerLevel component to exist for updates.


Fields

  • private CitySystem m_CitySystem
    Reference to the CitySystem used to obtain the city entity (m_CitySystem.City) where the aggregated danger value is written.

  • private EntityQuery m_DangerLevelQuery
    EntityQuery used to find entities that have Game.Events.DangerLevel components (and are not Deleted). If the query is empty the chunk job is skipped.

  • private TypeHandle __TypeHandle
    Internal struct that holds component type handles and component lookups used by the jobs. It contains:

  • a read-only ComponentTypeHandle and
  • a ComponentLookup for writing.

  • (nested) private struct DangerLevelJob : IJobChunk
    Burst-compiled job that iterates over chunks of Game.Events.DangerLevel and accumulates the maximum danger value into a NativeAccumulator. Uses ComponentTypeHandle and a ParallelWriter to the accumulator.

  • (nested) private struct UpdateCityJob : IJob
    Job that reads the accumulated MaxFloat result and writes the final float into the Game.City.DangerLevel component on the city entity. Uses ComponentLookup and an Entity for the target city.

  • (nested) private struct TypeHandle
    Holds component handles/lookups and provides an __AssignHandles method that binds them from a SystemState.

Properties

  • None (no public properties exposed by this system)

Constructors

  • public CityDangerLevelSystem()
    Default parameterless constructor. The system further initializes references in OnCreate/OnCreateForCompiler.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase) : System.Int32
    Returns 128. The system is intended to run every 128 frames (or ticks) for the given update phase.

  • [Preserve] protected override void OnCreate() : System.Void
    Creates/gets the CitySystem, sets up the m_DangerLevelQuery to read Game.Events.DangerLevel and exclude Deleted entities, and calls RequireForUpdate() to ensure the CityDangerLevel component exists and the system will be scheduled only when relevant.

  • [Preserve] protected override void OnUpdate() : System.Void
    Main update logic:

  • Allocates a NativeAccumulator (Allocator.TempJob).
  • If m_DangerLevelQuery is not empty, schedules a Burst-compiled DangerLevelJob (IJobChunk) in parallel to read Game.Events.DangerLevel components and accumulate the maximum danger value.
  • Schedules an UpdateCityJob (IJob) that writes the accumulator's result into the city's Game.City.DangerLevel component.
  • Disposes the NativeAccumulator with the combined job dependency to ensure safe deallocation after jobs complete.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Compiler-generated helper that (here) creates and disposes an EntityQueryBuilder (used by the generated code path). Present to satisfy compiler/IL post-processing.

  • protected override void OnCreateForCompiler() : System.Void
    Compiler-time initialization invoked by generated code. Calls __AssignQueries and __TypeHandle.__AssignHandles to bind component handles/lookups.

  • private struct TypeHandle.__AssignHandles(ref SystemState state) : System.Void
    Assigns the ComponentTypeHandle (read-only) and ComponentLookup (read/write) from the provided SystemState.

  • Nested job method details:

  • DangerLevelJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Iterates the chunk's Game.Events.DangerLevel components and calls m_Result.Accumulate(new MaxFloat(value)) for each entry.
  • UpdateCityJob.Execute()
    Reads m_Result.GetResult().m_Value and writes it to m_DangerLevel[m_City] (Game.City.DangerLevel.m_DangerLevel).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Initialize references and queries similar to the system's implementation:
    m_CitySystem = World.GetOrCreateSystemManaged<CitySystem>();
    m_DangerLevelQuery = GetEntityQuery(
        ComponentType.ReadOnly<Game.Events.DangerLevel>(),
        ComponentType.Exclude<Deleted>());

    // Ensure the city's DangerLevel component exists and this system runs only when relevant.
    RequireForUpdate<Game.City.DangerLevel>();
}

Notes and implementation tips: - The system uses Burst-compiled jobs and a NativeAccumulator to perform a parallel reduction (max) over event danger levels, so the DangerLevelJob is efficient for large numbers of event entities. - The accumulator is disposed with the job dependency to avoid race conditions and to ensure the disposal occurs after all jobs that use it finish. - Because the final write to the city component is done in a separate IJob, the system ensures jobs are correctly chained via base.Dependency. - GetUpdateInterval(phase) returning 128 means the system runs infrequently; if you need more responsive updates, change the interval or trigger updates differently.