Game.GameModeGovernmentSubsidiesSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
Manages government subsidy calculations for the Government Subsidies game mode. The system reads ModeSettingData and the player's current money to determine whether government subsidies are enabled and, if so, computes a monthly subsidy amount and a per-update (per-day) subsidy cover. It depends on CityServiceBudgetSystem to get total city service expenses and on CitySystem to access the city entity and PlayerMoney. The system only runs when the relevant ModeSettingData component exists and the mode indicates subsidies are enabled.
Fields
-
public static readonly System.Int32 kUpdatesPerDay
Constant value (128) used to convert the monthly subsidy into a per-update (per-day) amount. The system's update interval is also computed relative to this value. -
private System.Int32 m_LastSubsidyCoverPerDay
Stores the last computed subsidy cover amount applied per update (effectively per day when combined with kUpdatesPerDay). Updated during OnUpdate when a subsidy is applicable. -
private System.Int32 m_MonthlySubsidy
Holds the computed monthly subsidy amount for the current update. Reset to 0 each OnUpdate before recalculation. -
private ICityServiceBudgetSystem m_CityServiceBudgetSystem
Reference to the CityServiceBudgetSystem used to obtain total city service expenses (used in subsidy calculation). -
private CitySystem m_CitySystem
Reference to the CitySystem used to get the city entity and the PlayerMoney component. -
private EntityQuery m_GameModeSettingQuery
EntityQuery used to find the ModeSettingData singleton. The system requires this query for updates (RequireForUpdate is used in OnCreate).
Properties
-
public System.Int32 LastSubsidyCoverPerDay { get; }
Returns the integer per-update subsidy cover that was last computed (m_LastSubsidyCoverPerDay). This is suitable for applying a per-tick/per-day subsidy amount. -
public System.Int32 monthlySubsidy { get; }
Returns the integer monthly subsidy computed for the current update (m_MonthlySubsidy). It is reset and recomputed each OnUpdate and represents the total subsidy amount for the month based on the current city expenses and player money.
Constructors
public GameModeGovernmentSubsidiesSystem()
Default constructor. The system performs setup in OnCreate rather than in the constructor; the constructor is present for ECS/system instantiation and is marked with [Preserve] for AOT/serialization compatibility.
Methods
-
public override System.Int32 GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval as 262144 / kUpdatesPerDay. This determines how frequently the system's OnUpdate is invoked relative to the game's update ticks. The divisor using kUpdatesPerDay ties update frequency to the per-day logic. -
public System.Boolean GetGovernmentSubsidiesEnabled()
Returns the system's Enabled flag (base.Enabled). The flag is set in OnGameLoaded based on ModeSettingData (whether subsidies are enabled for the current game mode). -
protected override void OnCreate()
Initializes system references and query requirements: - Gets CityServiceBudgetSystem and CitySystem via World.GetOrCreateSystemManaged.
-
Creates an EntityQuery for ModeSettingData and calls RequireForUpdate with it so the system only runs if the ModeSettingData singleton exists.
-
protected override void OnGameLoaded(Context serializationContext)
Called when a save/game is loaded. Resets m_MonthlySubsidy to 0 and reads ModeSettingData (if present). Sets base.Enabled to true only if ModeSettingData.m_Enable and ModeSettingData.m_EnableGovernmentSubsidies are both true; otherwise disables the system. -
protected override void OnUpdate()
Main logic to compute subsidies. Behavior: - Resets m_MonthlySubsidy to 0.
- Returns early if ModeSettingData is missing or if there is no city entity.
- Reads ModeSettingData singleton and PlayerMoney from the city entity.
- If player money is below ModeSettingData.m_MoneyCoverThreshold.x, calculates a coverage fraction:
- num = thresholdMax - thresholdMin (m_MoneyCoverThreshold.x - m_MoneyCoverThreshold.y)
- coverageFraction = clamp(1 - (money - thresholdMin) / num, 0, 1) * (m_MaxMoneyCoverPercentage / 100f)
- monthlySubsidy = abs((int)(coverageFraction * totalExpenses)) where totalExpenses is obtained from the CityServiceBudgetSystem.
- m_LastSubsidyCoverPerDay = monthlySubsidy / kUpdatesPerDay
- If player money is above threshold, subsidies are not applied (monthlySubsidy remains 0).
Notes on the computation: - The subsidy scales linearly between the two threshold values (m_MoneyCoverThreshold.y and m_MoneyCoverThreshold.x). - m_MaxMoneyCoverPercentage (from ModeSettingData) caps the maximum portion of total expenses the government will cover. - Absolute value and integer cast convert the computed float to an integer monthly subsidy.
Usage Example
// Example: reading current subsidy values from another system (e.g., in another GameSystemBase)
protected override void OnUpdate()
{
var subsidySystem = World.GetExistingSystemManaged<GameModeGovernmentSubsidiesSystem>();
if (subsidySystem != null && subsidySystem.GetGovernmentSubsidiesEnabled())
{
int monthly = subsidySystem.monthlySubsidy;
int perDay = subsidySystem.LastSubsidyCoverPerDay;
// Use monthly and perDay values, for example log, display or apply them as needed.
}
}
Additional notes: - The system depends on the ModeSettingData component and PlayerMoney component; if these are missing the system is effectively inactive. - The system is designed to be data-driven: changing ModeSettingData values (thresholds, max cover percentage, enable flags) will alter subsidy behavior without code changes.