Skip to content

GameModeNaturalResourcesAdjustSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
System that adjusts natural resource and groundwater values according to mode settings. It can apply a one-time initial boost when a new game is created and performs periodic refill of natural resources each update interval. The system uses Burst-compiled IJobParallelFor jobs to process cell maps for NaturalResourceCell and GroundWater efficiently and integrates with the NaturalResourceSystem and GroundWaterSystem to acquire and publish write access to their data. It is enabled/disabled based on ModeSettingData (m_Enable and m_EnableAdjustNaturalResources).


Fields

  • public static readonly int kUpdatesPerDay
    Constant number of update ticks considered per in-game day (128). Used to convert per-day refill percentages into per-update amounts.

  • private NaturalResourceSystem m_NaturalResourceSystem
    Reference to the NaturalResourceSystem used to access natural resource cell data and register writers for jobs.

  • private GroundWaterSystem m_GroundWaterSystem
    Reference to the GroundWaterSystem used to access groundwater cell data and register writers for jobs.

  • private EntityQuery m_GameModeSettingQuery
    EntityQuery to obtain the ModeSettingData singleton used to determine whether the system is enabled and to read refill/boost settings.

  • private struct BoostInitialNaturalResourcesJob (nested)
    Burst-compiled IJobParallelFor that multiplies NaturalResourceCell base values (fertility, ore, oil) by a boost multiplier when starting a new game. Caps values at ushort max (65535).

  • private struct BoostInitialGroundWaterJob (nested)
    Burst-compiled IJobParallelFor that multiplies GroundWater fields (m_Amount, m_Polluted, m_Max) by a boost multiplier when starting a new game. Caps values at short max (32767).

  • private struct RefillNaturalResourcesJob (nested)
    Burst-compiled IJobParallelFor that reduces the "used" amount of ore and oil on each cell according to ModeSettingData percent refill per day, scaled by kUpdatesPerDay.

Properties

  • None.

Constructors

  • public GameModeNaturalResourcesAdjustSystem()
    Default constructor (preserved). Initializes the managed system; heavy initialization happens in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval in ticks for this system. Implementation returns 262144 / kUpdatesPerDay, so the system runs periodically to simulate daily refill cadence.

  • [Preserve] protected override void OnCreate()
    Initializes references to NaturalResourceSystem and GroundWaterSystem and creates an EntityQuery for ModeSettingData. Calls RequireForUpdate on the query so the system only runs when mode settings exist.

  • protected override void OnGameLoaded(Context serializationContext)
    Called after game load. If there is no ModeSettingData the system disables itself. If the ModeSettingData indicates adjustments are enabled (m_Enable and m_EnableAdjustNaturalResources), the system enables itself and, if this is a new game (serializationContext.purpose == Purpose.NewGame), applies an initial boost by calling BoostStartGameNaturalResources with m_InitialNaturalResourceBoostMultiplier.

  • [Preserve] protected override void OnUpdate()
    Runs each enabled update interval. If ModeSettingData enables adjustments, schedules the RefillNaturalResourcesJob on the NaturalResourceSystem cell map to decrease used oil/ore by the configured percent-per-day (converted into per-update amount). The scheduled JobHandle is registered with the NaturalResourceSystem via AddWriter.

  • private void BoostStartGameNaturalResources(float boostMultiplier)
    Schedules BoostInitialNaturalResourcesJob on the NaturalResourceSystem cell map and BoostInitialGroundWaterJob on the GroundWaterSystem cell map to apply the provided multiplier to base natural resource and groundwater values. Each scheduled job's handle is added to the corresponding system with AddWriter.

  • Job Execute details (each in their respective nested structs):

  • BoostInitialNaturalResourcesJob.Execute(int index): multiplies fertility.m_Base, ore.m_Base, oil.m_Base by m_BoostMultiplier, clamps to 65535, and writes back.
  • BoostInitialGroundWaterJob.Execute(int index): multiplies m_Amount, m_Polluted, m_Max by m_BoostMultiplier, clamps to 32767, and writes back.
  • RefillNaturalResourcesJob.Execute(int index): reduces oil.m_Used and ore.m_Used by (base * percentRefillPerDay / 100) / kUpdatesPerDay, with a min of 0, then writes back.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Acquire systems and require ModeSettingData for this system to run
    m_NaturalResourceSystem = World.GetOrCreateSystemManaged<NaturalResourceSystem>();
    m_GroundWaterSystem = World.GetOrCreateSystemManaged<GroundWaterSystem>();
    m_GameModeSettingQuery = EntityManager.CreateEntityQuery(ComponentType.ReadOnly<ModeSettingData>());
    RequireForUpdate(m_GameModeSettingQuery);
}

// OnGameLoaded will automatically apply an initial boost on new game when enabled in ModeSettingData
// and OnUpdate will perform the periodic refill according to ModeSettingData settings.