Game.Simulation.AttractionSystem
Assembly:
Game (inferred from project)
Namespace:
Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
AttractionSystem updates the per-building AttractivenessProvider component based on prefab AttractionData, building efficiency, installed upgrades, park maintenance state, and terrain attractiveness (including height). It runs on a fixed interval (GetUpdateInterval returns 16) and schedules a parallel IJobChunk (AttractivenessJob) that computes and writes rounded attractiveness values for entities matching the building query. The system also interacts with TerrainAttractivenessSystem and TerrainSystem to sample terrain- and height-based modifiers, and reads AttractivenessParameterData from a settings query.
Fields
-
private SimulationSystem m_SimulationSystem
{{ Reference to the global SimulationSystem; used to obtain the current simulation frame index for update scheduling and intervals. }} -
private TerrainAttractivenessSystem m_TerrainAttractivenessSystem
{{ Used to read the TerrainAttractiveness cell map data required to evaluate terrain-based attractiveness modifiers. The system provides the CellMapDataused by the job. }} -
private TerrainSystem m_TerrainSystem
{{ Used to obtain height data (TerrainHeightData) and to register CPU read dependencies for height sampling. }} -
private EntityQuery m_BuildingGroup
{{ EntityQuery selecting building entities that should have their attractiveness updated. Built to require AttractivenessProvider (RW), PrefabRef (RO) and UpdateFrame (shared) while excluding Destroyed, Deleted and Temp. Used as the query scheduled with the AttractivenessJob. }} -
private EntityQuery m_SettingsQuery
{{ EntityQuery used to fetch the singleton AttractivenessParameterData settings. }} -
private TypeHandle __TypeHandle
{{ A private struct instance that caches ComponentTypeHandle / BufferTypeHandle / SharedComponentTypeHandle and ComponentLookup references for the job and for compiler-time initialization. Populated in OnCreateForCompiler. }}
Properties
- (none)
{{ AttractionSystem exposes no public properties. Internal type handles and queries are private. }}
Constructors
public AttractionSystem()
{{ Default constructor. The system uses OnCreate / OnUpdate lifecycle methods to initialize and schedule work; no custom construction logic beyond the default is present. }}
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
{{ Returns 16. This determines the update frequency used by the system (used together with SimulationUtils.GetUpdateFrameWithInterval when calculating which update frame index the job should process). The parameter phase is available but the implementation ignores it and always returns 16. }} -
public static void SetFactor(NativeArray<int> factors, AttractivenessFactor factor, float attractiveness)
{{ Utility helper to modify a NativeArrayof attractiveness factors. If the NativeArray is created and has length 5, stores Mathf.RoundToInt(attractiveness) at the index corresponding to the provided AttractivenessFactor. Useful for building a set of per-factor modifiers to pass into terrain evaluation or other systems that expect a 5-element factor array. Note: AttractivenessFactor enum includes Count; typical valid indices are Efficiency, Maintenance, Forest, Beach, Height (5 entries). }} -
[Preserve] protected override void OnCreate()
{{ System initialization: acquires references to SimulationSystem, TerrainAttractivenessSystem, and TerrainSystem from World; creates the settings query (AttractivenessParameterData singleton) and the building entity query (m_BuildingGroup) which selects entities that will have attractiveness computed. This is the main setup for the system. }} -
[Preserve] protected override void OnUpdate()
{{ Main scheduling method. Computes a target updateFrameIndex using SimulationUtils.GetUpdateFrameWithInterval and the system's update interval. Prepares an AttractivenessJob with: - component handles and lookups (AttractivenessProvider, PrefabRef, Efficiency buffers, Signature, Park, InstalledUpgrade, Transform),
- terrain map and height data (m_TerrainAttractivenessSystem.GetData and m_TerrainSystem.GetHeightData),
- attraction parameters (AttractivenessParameterData),
-
the update frame index. It schedules the job in parallel over m_BuildingGroup (JobChunkExtensions.ScheduleParallel) and combines dependencies with the terrain/height read handles. Finally, it registers the dependency with TerrainSystem and TerrainAttractivenessSystem so they know about CPU readers. The job computes attractiveness per-entity by reading AttractionData (from the prefab), applying upgrade modifiers, optionally multiplying by efficiency (if no Signature present), applying park maintenance modifier (if entity is a park and ParkData exists), and applying terrain/height-based modifier via TerrainAttractivenessSystem.EvaluateAttractiveness, rounding the final float to an integer and writing it to AttractivenessProvider. }}
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
{{ Internal helper stub used by the generated/compiled system code path; currently creates and disposes an EntityQueryBuilder(Allocator.Temp). Present to satisfy compiler-generated code paths and to be called from OnCreateForCompiler. }} -
protected override void OnCreateForCompiler()
{{ Compiler-time initialization entrypoint that calls __AssignQueries and invokes __TypeHandle.__AssignHandles to populate the cached ComponentTypeHandle/BufferTypeHandle/SharedComponentTypeHandle and ComponentLookup entries against the system state. This ensures the type handles are initialized for Burst/Jobs usage. }} -
(Nested)
AttractivenessJob : IJobChunk
(private, BurstCompile)
{{ The job executed over building archetype chunks. Key behavior: - Skips chunks whose shared UpdateFrame component index doesn't match the system's m_UpdateFrameIndex (so it runs only on entities for the targeted update frame).
- Reads PrefabRef to get the prefab Entity for lookups.
- Reads AttractionData from component lookup for the prefab; applies installed upgrades via UpgradeUtils.CombineStats when present.
- Starts with prefab AttractionData.m_Attractiveness, scales by BuildingUtils.GetEfficiency(bufferAccessor,i) unless the building has a Signature component (flag).
- If the entity has Park component and ParkData exists for the prefab, applies a maintenance factor (0.8 + 0.2 * (park.m_Maintenance / parkData.m_MaintenancePool)) to the attractiveness value.
- If the entity has a Transform component, samples terrain attractiveness/height and multiplies attractiveness by (1 + 0.01 * EvaluateAttractiveness(...)).
- Rounds the result to an int and writes it into AttractivenessProvider.m_Attractiveness. The job uses read-only component lookups for AttractionData, ParkData and PrefabRef, reads buffers (Efficiency, InstalledUpgrade), and reads/writes AttractivenessProvider. It is Burst-compiled. }}
Usage Example
// Example: adjust a single factor in a 5-element factor array and call the static helper.
using Unity.Collections;
using Game.Simulation;
var factors = new NativeArray<int>(5, Allocator.Temp);
AttractionSystem.SetFactor(factors, AttractionSystem.AttractivenessFactor.Height, 12.5f);
// Use 'factors' as needed by terrain evaluation, then dispose.
factors.Dispose();
{{ Notes: - The system expects AttractivenessParameterData to exist as a singleton; ensure the settings entity is created. - SetFactor is a simple helper that only writes into a 5-sized NativeArray; ensure size matches expected factors. - Terrain and height reads are registered as dependencies so any systems writing to terrain/height data will be respected by the job scheduling. }}