Skip to content

Game.UI.InGame.LevelSection

Assembly:
Assembly-CSharp

Namespace:
Game.UI.InGame

Type:
class

Base:
InfoSectionBase

Summary:
LevelSection is an Info UI section used in the in-game building info panel to display building level information. It determines the current building level, the maximum achievable level for the building's lot/zone, and shows progress when a building is under construction or leveling up. The class uses Unity Entities (ECS) queries and a Burst-compiled IJobChunk (CalculateMaxLevelJob) to compute the maximum level across matching spawnable building prefabs. It reads components such as BuildingData, SpawnableBuildingData, ZoneData, BuildingPropertyData, BuildingCondition and buffers like CityModifier. The class allocates a small NativeArray to return job results and disposes it on destroy.


Fields

  • private EntityQuery m_SpawnableBuildingQuery
    Used to query entities that have BuildingData and SpawnableBuildingData. This query is scheduled against by CalculateMaxLevelJob to find other spawnable building prefabs for computing max level.

  • private EntityQuery m_CityQuery
    Query used to fetch the city modifier singleton entity / buffer (CityModifier) when computing leveling costs.

  • private NativeArray<int> m_Result
    A persistent NativeArray (length 1) used to store the computed maximum level result produced by the CalculateMaxLevelJob. Allocated in OnCreate and disposed in OnDestroy.

  • private TypeHandle __TypeHandle
    Holds ComponentTypeHandle instances (for BuildingData and SpawnableBuildingData) assigned via __AssignHandles to support IJobChunk access. This is a compiler-helper structure used when scheduling the job.

Properties

  • protected override string group => "LevelSection"
    Identifier for this info section used by the UI system.

  • private int level { get; set; }
    Current building level (value read from the selected prefab's SpawnableBuildingData).

  • private int maxLevel { get; set; }
    Computed maximum level available for the building's lot size and zone prefab. Determined via the CalculateMaxLevelJob or falls back to the current level.

  • private bool isUnderConstruction { get; set; }
    True when the selected building entity is currently under construction (UnderConstruction component present with m_NewPrefab == Entity.Null).

  • private float progress { get; set; }
    Progress percentage. For under-construction it is based on UnderConstruction.m_Progress; when leveling, it's computed as (condition / levelingCost) * 100.

  • private Entity zone { get; set; }
    Zone prefab entity associated with the building (from SpawnableBuildingData.m_ZonePrefab).

  • protected override bool displayForUnderConstruction => true
    Indicates that this section should be displayed for buildings under construction.

Constructors

  • public LevelSection()
    Default constructor. (No custom initialization; OnCreate handles ECS-related setup.)

Methods

  • protected override void Reset()
    Resets internal state used for UI display: level = 0, maxLevel = 0, isUnderConstruction = false, progress = 0f, zone = Entity.Null.

  • [Preserve] protected override void OnCreate()
    Initializes queries and persistent native memory:

  • Builds m_SpawnableBuildingQuery for BuildingData + SpawnableBuildingData.
  • Builds m_CityQuery for CityModifier.
  • Allocates m_Result = new NativeArray(1, Allocator.Persistent). Also calls base.OnCreate().

  • [Preserve] protected override void OnDestroy()
    Disposes the m_Result NativeArray and calls base.OnDestroy().

  • private bool Visible()
    Determines whether this info section should be visible for the current selection. Checks entity/prefab components:

  • Returns true only when selectedPrefab has BuildingData and SpawnableBuildingData, selectedEntity has Renter, and selectedEntity is not Abandoned and selectedPrefab does not have SignatureBuildingData.
  • Used to set base.visible.

  • [Preserve] protected override void OnUpdate()
    Main update step executed every frame (or UI refresh):

  • Sets base.visible = Visible().
  • If visible and the selectedEntity has an UnderConstruction component with m_NewPrefab == Entity.Null, sets isUnderConstruction and progress (from UnderConstruction.m_Progress) and returns.
  • Otherwise, reads BuildingData and SpawnableBuildingData from selectedPrefab and schedules CalculateMaxLevelJob to compute the maximum level for the building's lot size & zone. The job is scheduled with m_SpawnableBuildingQuery and completed synchronously (Complete()) to make the result immediately available in m_Result.

  • protected override void OnProcess()
    Fills UI display data based on the computed values:

  • Reads SpawnableBuildingData to set zone.
  • Reads ZoneData for the zone prefab and sets tooltip keys based on AreaType (Residential, Commercial, Industrial/Office).
  • If isUnderConstruction is true, adds "UnderConstruction" tooltip and returns.
  • Reads BuildingPropertyData and BuildingCondition to compute leveling progress if current level < maxLevel:
    • Uses BuildingUtils.GetLevelingCost(areaType, buildingPropertyData, level, cityModifiers) to compute required condition.
    • progress = condition / levelingCost * 100 (or 100 if levelingCost <= 0).
  • Sets level and maxLevel (max of computed result and current level).

  • public override void OnWriteProperties(IJsonWriter writer)
    Serializes the section properties to JSON for the UI:

  • Writes "zone" (prefab name via m_PrefabSystem.GetPrefabName(zone))
  • Writes "level", "maxLevel", "isUnderConstruction", and "progress".

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler helper used by generated code; currently creates and disposes a temporary EntityQueryBuilder. Kept for compiler/runtime compatibility.

  • protected override void OnCreateForCompiler()
    Called by compiled pipelines to assign component type handles and queries. Calls __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef).

  • [Preserve] public LevelSection()
    Public parameterless constructor (already listed above).

Nested / Job types:

  • [BurstCompile] private struct CalculateMaxLevelJob : IJobChunk
    Burst-compiled IJobChunk that looks through chunks of entities with BuildingData and SpawnableBuildingData to find the highest SpawnableBuildingData.m_Level that matches both:
  • The same zone prefab entity (m_ZonePrefabEntity)
  • The same lot size (m_LotSize) It writes the found maximum level into m_Result[0]. Uses ComponentTypeHandle and ComponentTypeHandle to access chunk native arrays.

Main behavior: - For each chunk, gets native arrays for BuildingData and SpawnableBuildingData. - Iterates spawnable array and, when spawnable.m_Level > m_Result[0] and spawnable.m_ZonePrefab == m_ZonePrefabEntity and building.m_LotSize.Equals(m_LotSize), updates m_Result[0] to spawnable.m_Level.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // The base implementation already sets up queries and allocates m_Result.
    // Additional custom initialization can go here.
}

// Example: read level info after update/process in a derived section or system:
protected override void OnProcess()
{
    base.OnProcess();

    // After base processing, you can read the values (these are private in the original class;
    // if you expose them in a derived class, you'd access level/maxLevel/progress).
    // This snippet illustrates the intended use:
    // writer.PropertyName("level"); writer.Write(level);
    // writer.PropertyName("maxLevel"); writer.Write(maxLevel);
}

Notes and implementation details: - The class uses synchronous job completion (Complete()) in OnUpdate to ensure m_Result is available immediately for OnProcess. This simplifies UI flow but can block the main thread if many chunks exist; consider scheduling asynchronously if latency becomes a concern. - m_Result must be disposed in OnDestroy to avoid native memory leaks (this is handled in the class). - The CalculateMaxLevelJob relies on exact lot-size matching via BuildingData.m_LotSize.Equals and matching the zone prefab entity reference. - Tooltip keys (e.g., "Residential", "Commercial", "Office", "Industrial", "UnderConstruction") are added to base.tooltipKeys and m_InfoUISystem.tooltipTags for UI presentation.