Game.Simulation.TrafficAmbienceSystem
Assembly: Game (Assembly-CSharp)
Namespace: Game.Simulation
Type: class
Base: CellMapSystem
Summary:
TrafficAmbienceSystem maintains a 2D cell map (texture) of ambient traffic values used by the simulation and other systems (e.g., audio/visual ambience). It runs a Burst-compiled parallel job each update to move per-cell accumulators into the current traffic value, creates/owns the cell textures, and exposes sampling helpers for querying traffic at world positions (bilinear interpolation and a weighted neighborhood sample). The system relies on the CellMapSystem base for common map/cell utilities and for dependency management (m_Map, m_ReadDependencies, m_WriteDependencies, CreateTextures, etc.).
Fields
-
public static readonly int kTextureSize = 64
Defines the dimension of the traffic ambience texture (64x64 cells). Used for indexing, scheduling and creating textures. -
public static readonly int kUpdatesPerDay = 1024
Controls how many updates of this map occur per in-game day. Used in GetUpdateInterval to space updates across the simulation. -
private struct TrafficAmbienceUpdateJob
(nested) —public NativeArray<TrafficAmbienceCell> m_TrafficMap
Burst-compiled IJobParallelFor that operates on the NativeArray backing the cell map. The job replaces each cell's m_Traffic value with the accumulated value (m_Accumulator) and resets m_Accumulator to 0.
Properties
public int2 TextureSize => new int2(kTextureSize, kTextureSize);
Convenience property returning the texture/map size as an int2. Consumers can use this to query dimensions without hardcoding the constant.
Constructors
public TrafficAmbienceSystem()
Default constructor; no special initialization beyond inherited behavior. The system performs its runtime setup in OnCreate (CreateTextures call).
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the interval between updates (in simulation ticks/units) for this system. It returns 262144 / kUpdatesPerDay. Explanation: 262,144 is the base cell map resolution used by the base CellMapSystem (512*512), and dividing by kUpdatesPerDay spaces the updates over the in-game day according to the chosen update frequency. -
public static float3 GetCellCenter(int index)
Static helper that forwards to CellMapSystem.GetCellCenter using this system's kTextureSize. Returns the world-space center position of the cell at the given linear index. -
public static TrafficAmbienceCell GetTrafficAmbience2(float3 position, NativeArray<TrafficAmbienceCell> trafficAmbienceMap, float maxPerCell)
Samples the traffic value at a world position using a weighted average over a 5x5 neighborhood centered on the target cell. Weighting uses inverse squared distance (with a minimum distance of 1 to avoid division by zero). Each neighbor's contribution is clamped by maxPerCell before accumulation to limit the influence of any single cell. The result.m_Traffic contains the normalized weighted average. -
public static TrafficAmbienceCell GetTrafficAmbience(float3 position, NativeArray<TrafficAmbienceCell> trafficAmbienceMap)
Performs bilinear interpolation of the four surrounding cells to produce a smooth traffic value at a position. If the position maps outside the 0..kTextureSize-1 range, the method returns a zeroed TrafficAmbienceCell. Uses CellMapSystem helpers to map positions to cell coordinates and indices. -
protected override void OnCreate()
Called when the system is created. Calls base.OnCreate() and CreateTextures(kTextureSize) to initialize the underlying textures/storage sized to kTextureSize. -
protected override void OnUpdate()
Builds and schedules the TrafficAmbienceUpdateJob (Burst-compiled). The job is scheduled as an IJobParallelFor across kTextureSize * kTextureSize (4096) elements with a batch size of kTextureSize (64). The system combines this job with existing read/write dependencies using JobHandle.CombineDependencies, registers itself as a writer with AddWriter(base.Dependency), and sets the system's base.Dependency appropriately. The job copies each cell's accumulator into its traffic value and resets the accumulator. This ensures updates run in parallel and are correctly synchronized with other systems accessing the map.
Notes on threading and safety: - The update job mutates the NativeArray backing the map; dependency combination and AddWriter are used to ensure safe scheduling relative to other readers/writers. - The nested job is Burst-compiled for performance. - Sampling helpers expect a NativeArray snapshot (read-only) and do not schedule jobs themselves.
- Implements IJobSerializable (marker/interface for serialization support in the game's entity serialization system); the file does not show explicit serialization code, but the interface indicates the system participates in save/load behavior.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
CreateTextures(kTextureSize);
}
Additional notes and tips: - Use GetTrafficAmbience for smooth, cheap sampling (bilinear). Use GetTrafficAmbience2 when you need a wider neighborhood sampling and want to cap individual cell influence (pass an appropriate maxPerCell). - If you add new writers/readers to m_Map, ensure you correctly combine JobHandles with m_ReadDependencies/m_WriteDependencies and call AddWriter to maintain proper synchronization. - kTextureSize is relatively small (64x64) to keep the ambience map cheap to update; if you change it, update batch sizes and derived calculations accordingly.