Game.UI.InGame.AverageHappinessSection
Assembly: Assembly-CSharp
Namespace: Game.UI.InGame
Type: class
Base: InfoSectionBase
Summary:
{{ This UI info section computes and exposes the average citizen happiness for a selected Building, Household or District. It queries game parameter singletons (citizen happiness, garbage, healthcare, park, education, telecom, tax/service fee) and uses several runtime systems (pollution, telecom coverage, tax, city) plus ECS queries and Component/Buffer lookups. The heavy lifting is done by two Burst-compiled jobs (CountHappinessJob and CountDistrictHappinessJob) which aggregate citizen counts, total happiness and factor breakdowns. The results are processed into a CitizenHappiness value and a sorted list of FactorInfo entries for presentation and serialization. }}
Fields
-
private GroundPollutionSystem m_GroundPollutionSystem
{{ Reference to the system providing ground pollution data (used as read-only map for happiness factor calculation). }} -
private NoisePollutionSystem m_NoisePollutionSystem
{{ Reference to the system providing noise pollution data (read-only). }} -
private AirPollutionSystem m_AirPollutionSystem
{{ Reference to the system providing air pollution data (read-only). }} -
private TelecomCoverageSystem m_TelecomCoverageSystem
{{ Reference to the telecom coverage system (used to get telecom coverage CellMapData for jobs). }} -
private TaxSystem m_TaxSystem
{{ Reference used to read current tax rates for happiness calculations. }} -
private CitySystem m_CitySystem
{{ Reference providing the city Entity (m_City) and service fee buffers used to compute relative fees. }} -
private EntityQuery m_DistrictBuildingQuery
{{ EntityQuery used to drive the district-mode IJobChunk scheduling (filters buildings in a district). }} -
public NativeArray<int> m_Results
{{ NativeArray of length 3 used by the jobs to store aggregated results: [visibleFlag, citizenCount, totalHappiness]. }} -
private NativeArray<int2> m_Factors
{{ NativeArray storing per-factor accumulators (count and summed effect) used by BuildingHappiness.GetResidentialBuildingHappinessFactors to collect factor contributions. }} -
protected EntityQuery m_CitizenHappinessParameterQuery
{{ Query for the CitizenHappinessParameterData singleton. }} -
protected EntityQuery m_GarbageParameterQuery
{{ Query for the GarbageParameterData singleton. }} -
protected EntityQuery m_HappinessFactorParameterQuery
{{ Query for HappinessFactorParameterData items (buffer lookup is used in jobs). }} -
protected EntityQuery m_HealthcareParameterQuery
{{ Query for HealthcareParameterData singleton. }} -
protected EntityQuery m_ParkParameterQuery
{{ Query for ParkParameterData singleton. }} -
protected EntityQuery m_EducationParameterQuery
{{ Query for EducationParameterData singleton. }} -
protected EntityQuery m_TelecomParameterQuery
{{ Query for TelecomParameterData singleton. }} -
private TypeHandle __TypeHandle
{{ Internal generated struct that stores Entity/Component/Buffer handles for efficient job binding (assigned in OnCreateForCompiler). }} -
private EntityQuery __query_1244325462_0
{{ Internal EntityQuery built for ServiceFeeParameterData (used to compute relative service fees). }}
Properties
-
private CitizenHappiness averageHappiness { get; set; }
{{ Holds the computed average happiness (converted to CitizenHappiness enum/value) after processing job results in OnProcess. Used for display/serialization. }} -
private NativeList<FactorInfo> happinessFactors { get; set; }
{{ Collected and sorted list of factor contributions (FactorInfo) derived from m_Factors. Exposed via OnWriteProperties to the UI/json writer. }}
Constructors
public AverageHappinessSection()
{{ Default constructor. Most setup happens in overridden OnCreate; constructor only exists to satisfy Unity/managed construction. }}
Methods
-
protected override void OnCreate()
{{ Initializes system references (pollution/telecom/tax/city), builds queries (district/building/parameter queries), allocates persistent NativeArray m_Factors and m_Results and NativeList for happinessFactors. This is where the section obtains the systems it needs and prepares buffers for job execution. Marked [Preserve]. }} -
protected override void OnDestroy()
{{ Disposes persistent NativeArray and NativeList resources (m_Results, m_Factors, happinessFactors) and calls base.OnDestroy. Marked [Preserve]. }} -
protected override void Reset()
{{ Clears accumulators: zeroes m_Factors, resets averageHappiness to default, clears happinessFactors and zeroes m_Results. Called when the section resets state between selections or refreshes. }} -
protected override void OnUpdate()
{{ Main update that: - Reads parameter singletons (citizen happiness, garbage, healthcare, park, education, telecom, service fee).
- Gets pollution/coverage buffers from respective systems (blocking until data is ready).
- Computes relative electricity/water fees.
- Decides whether selectedEntity is a District/Area combination; if yes schedules CountDistrictHappinessJob via JobChunkExtensions on m_DistrictBuildingQuery; otherwise schedules CountHappinessJob for a single entity.
-
Jobs populate m_Results and m_Factors; the code sets base.visible based on m_Results[0]. This method binds many ComponentLookup/TypeHandles from the internal __TypeHandle. Marked [Preserve]. }}
-
protected override void OnProcess()
{{ Called after jobs complete. It: - Reads m_Results to compute average happiness (uses CitizenUIUtils.GetCitizenHappiness on the per-citizen average).
- Iterates m_Factors and converts accumulated (count,sum) into rounded per-factor values, adds non-zero factors to happinessFactors and sorts them.
-
Adds tooltip keys depending on whether the selected entity is a Building, Household or District. This prepares data for display and serialization. }}
-
public override void OnWriteProperties(IJsonWriter writer)
{{ Serializes the computed averageHappiness and up to the top 10 happinessFactors into the provided JSON writer. This method is used by the UI to send section data to the frontend. }} -
private static bool TryAddPropertyHappiness(ref int happiness, ref int citizenCount, Entity entity, ComponentLookup<Household> householdFromEntity, ComponentLookup<Citizen> citizenFromEntity, ComponentLookup<HealthProblem> healthProblemFromEntity, BufferLookup<Renter> renterFromEntity, BufferLookup<HouseholdCitizen> householdCitizenFromEntity)
{{ Utility that attempts to walk renters->households->household citizens for a property entity and accumulate happiness/ citizenCount. Returns true if at least one household/renter was processed. It skips dead citizens via CitizenUtils.IsDead (consults HealthProblem). Used by both jobs to gather per-property citizen happiness contributions. }} -
private void __AssignQueries(ref SystemState state)
{{ Internal method that builds the __query_1244325462_0 EntityQuery (ServiceFeeParameterData) used by OnUpdate. This is part of generated/compiled glue to safely acquire queries in compiled code. }} -
protected override void OnCreateForCompiler()
{{ Internal glue invoked by compiled code to assign queries and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles). Marked [Preserve] in the source. }} -
CountHappinessJob (private nested struct : IJob, BurstCompile)
{{ Burst-compiled job that runs for a single selected entity (building or household). If the entity is a residential building it invokes BuildingHappiness.GetResidentialBuildingHappinessFactors and then calls TryAddPropertyHappiness to accumulate citizens/happiness; if the selected entity is a household it iterates household citizens. Writes results into provided NativeArrays (m_Results and m_Factors). Uses many ComponentLookup/BufferLookup handles passed in during scheduling. }} -
CountDistrictHappinessJob (public nested struct : IJobChunk, BurstCompile)
{{ Burst-compiled chunk job that iterates building chunks filtered by district, checks buildings that belong to the selected district and are spawnable residential prefabs, uses TryAddPropertyHappiness to accumulate citizen counts/happiness for each building and calls BuildingHappiness.GetResidentialBuildingHappinessFactors to aggregate factors into m_Factors. Accumulates chunk results into m_Results. Designed for efficient district-wide aggregation. }}
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Example: this class already initializes systems/queries and persistent buffers.
// If subclassing, call base.OnCreate() first so m_Factors/m_Results and system references are available.
// You can also override OnProcess to read computed averageHappiness/happinessFactors for custom UI logic.
}