Game.UI.InGame.CityInfoUISystem
Assembly:
Namespace: Game.UI.InGame
Type: class
Base: UISystemBase, IDefaultSerializable, ISerializable
Summary:
CityInfoUISystem provides UI bindings for the in-game "City Info" panel (group "cityInfo"). It reads demand and happiness data from various simulation systems, smooths demand values for UI presentation, and exposes demand/factor arrays (as JSON) for the UI. It also handles serialization of its internal smoothing state so UI values persist across saves/loads.
Fields
-
private struct TypeHandle
A small compiler-generated helper struct used to cache ComponentLookup handles used by this system (currently stores a read-only ComponentLookup). Contains a method to assign the handles from a SystemState. -
public const string kGroup
Constant "cityInfo" — the UI binding group name used when registering bindings. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to query the current frame index. -
private ResidentialDemandSystem m_ResidentialDemandSystem
Reference to the system that provides residential demand and density-specific demand factors. -
private CommercialDemandSystem m_CommercialDemandSystem
Reference to the commercial demand system. -
private IndustrialDemandSystem m_IndustrialDemandSystem
Reference to the industrial demand system (including industrial, storage and office demand data). -
private CitySystem m_CitySystem
Reference to the CitySystem; used to locate the city entity for population/happiness data. -
private CitizenHappinessSystem m_CitizenHappinessSystem
Reference to the system that calculates happiness factors and values. -
private RawValueBinding m_ResidentialLowFactors
RawValueBinding instance for writing low-density residential demand factors to the UI (as JSON). -
private RawValueBinding m_ResidentialMediumFactors
RawValueBinding instance for medium-density residential demand factors. -
private RawValueBinding m_ResidentialHighFactors
RawValueBinding instance for high-density residential demand factors. -
private RawValueBinding m_CommercialFactors
RawValueBinding instance for commercial demand factors. -
private RawValueBinding m_IndustrialFactors
RawValueBinding instance for industrial demand factors. -
private RawValueBinding m_OfficeFactors
RawValueBinding instance for office demand factors. -
private RawValueBinding m_HappinessFactors
RawValueBinding instance for happiness factors. -
private float m_ResidentialLowDemand
Smoothed value (0..1) representing low-density residential demand used for UI display. -
private float m_ResidentialMediumDemand
Smoothed value for medium-density residential demand. -
private float m_ResidentialHighDemand
Smoothed value for high-density residential demand. -
private float m_CommercialDemand
Smoothed commercial demand value. -
private float m_IndustrialDemand
Smoothed industrial demand value. -
private float m_OfficeDemand
Smoothed office demand value. -
private uint m_LastFrameIndex
Frame index last time the system updated smoothing; used to advance smoothing only when simulation frame changes. -
private int m_AvgHappiness
Cached average citizen happiness (integer) used by UI binding. -
private UIUpdateState m_UpdateState
Helper that tracks when raw bindings should be updated; used to batch/limit expensive updates to UI JSON bindings. -
private TypeHandle __TypeHandle
Instance of the compiler-generated TypeHandle struct used to hold component lookups required for computing happiness factors.
Properties
-
private float m_ResidentialLowDemandBindingValue { get; }
Returns m_ResidentialLowDemand snapped to 0.001 increments via MathUtils.Snap — value exposed to bindings. -
private float m_ResidentialMediumDemandBindingValue { get; }
Returns m_ResidentialMediumDemand snapped to 0.001. -
private float m_ResidentialHighDemandBindingValue { get; }
Returns m_ResidentialHighDemand snapped to 0.001. -
private float m_CommercialDemandBindingValue { get; }
Returns m_CommercialDemand snapped to 0.001. -
private float m_IndustrialDemandBindingValue { get; }
Returns m_IndustrialDemand snapped to 0.001. -
private float m_OfficeDemandBindingValue { get; }
Returns m_OfficeDemand snapped to 0.001.
Constructors
public CityInfoUISystem()
Default constructor; marked with [Preserve] attribute in the source. Standard managed system construction; most initialization happens in OnCreate.
Methods
-
[Preserve] protected override void OnCreate()
Initializes system references (simulation and demand/happiness/city systems), registers UI getter bindings (for demand and happiness) and RawValueBindings (for factor arrays), and initializes the UIUpdateState. Called when the system is created. -
protected override void OnGameLoaded(Context serializationContext)
Called after a game is loaded — forces a UI update (m_UpdateState.ForceUpdate) so bindings reflect loaded state immediately. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the current smoothing state and average happiness to the provided writer. Serializes: residential low/medium/high, commercial, industrial, office demands, last frame index, and avg happiness (depending on version). -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads saved smoothing state. Handles version differences: - If save version >= residentialDemandSplitUI, reads separate low/medium/high residential values.
- Otherwise reads a single residential demand and splits it evenly across densities.
- Reads commercial, industrial, office demands, last frame index.
-
Reads average happiness if version >= populationComponent.
-
public void SetDefaults(Context context)
Sets smoothing fields and last frame index to default zeros. Used to initialize state for new games or when resetting. -
[Preserve] protected override void OnUpdate()
Main update method: - Advances smoothing of demand values when simulation frame has progressed, using AdvanceSmoothDemand.
- Updates m_AvgHappiness by reading Population component if present; otherwise defaults to 50.
-
Advances UIUpdateState and, when it indicates, updates RawValueBindings so JSON factor arrays are refreshed.
-
private static float AdvanceSmoothDemand(float current, int target, uint delta)
Smoothing helper that moves the current smoothed value toward target/100 with different up/down speeds per simulation delta (clamped). Formula uses small step sizes to create a smooth UI change. -
public void RequestUpdate()
Forces UI update (calls m_UpdateState.ForceUpdate()). Useful when external code wants to immediately refresh factor arrays in the UI. -
private void WriteResidentialLowFactors(IJsonWriter writer)
Fetches low-density residential demand factors from the ResidentialDemandSystem (returns a NativeArrayand JobHandle), then writes them via WriteDemandFactors. -
private void WriteResidentialMediumFactors(IJsonWriter writer)
Fetches medium-density residential demand factors and writes them. -
private void WriteResidentialHighFactors(IJsonWriter writer)
Fetches high-density residential demand factors and writes them. -
private void WriteCommercialFactors(IJsonWriter writer)
Fetches commercial demand factors and writes them. -
private void WriteIndustrialFactors(IJsonWriter writer)
Fetches industrial demand factors and writes them. -
private void WriteOfficeFactors(IJsonWriter writer)
Fetches office demand factors and writes them. -
private void WriteDemandFactors(IJsonWriter writer, NativeArray<int> factors, JobHandle deps)
Common helper that: - Completes the provided JobHandle (deps.Complete()) to ensure factor data is ready.
- Converts the NativeArray
into a NativeList via FactorInfo.FromFactorArray(…, Allocator.Temp). - Sorts the list and writes up to the top N factors (N determined by the writer call), using FactorInfo.WriteDemandFactor to generate output JSON objects.
-
Disposes of the temporary list in a finally block.
-
private void WriteHappinessFactors(IJsonWriter writer)
Collects happiness factors: - Builds a NativeList
(capacity 25). - Queries for HappinessFactorParameterData; if present, gets the singleton buffer and uses CitizenHappinessSystem.GetHappinessFactor for each factor index.
- Adds non-zero factors to the list, sorts, then writes up to 10 top happiness factors via FactorInfo.WriteHappinessFactor.
- Disposes the temporary list in a finally block.
-
Uses internal component lookup for Locked via the compiler-assigned __TypeHandle.
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler-generated method that creates and disposes an EntityQueryBuilder — placeholder for query initialization. -
protected override void OnCreateForCompiler()
Called by generated code paths; calls __AssignQueries and assigns component lookup handles (via __TypeHandle.__AssignHandles).
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Force an immediate UI update after the game loads / when the system is created.
RequestUpdate();
}
Notes and implementation details: - Demand values are stored and updated as smoothed floats in 0..1 range (targets are integers divided by 100). - RawValueBindings produce JSON arrays for the UI and are updated via UIUpdateState to limit frequency of potentially expensive queries. - The Write*Factors methods may complete Unity JobHandles and allocate temporary Native containers; they ensure proper disposal in finally blocks. - Serialization respects internal game version differences so older saves map correctly to the split-residential-demand UI introduced in later versions.