Skip to content

Game.Simulation.TransportLineSystem

Assembly: Game
Namespace: Game.Simulation

Type: class

Base: GameSystemBase, IDefaultSerializable, ISerializable

Summary:
Manages transport lines (routes) and their vehicles. Runs on a fixed update interval (256 frames) and: - Calculates route durations and per-segment RouteInfo updates. - Computes required vehicle counts and intervals for each transport line. - Requests new vehicles when needed and tracks outstanding vehicle requests. - Schedules vehicle state changes (abandon/cancel abandon) via a work queue. - Updates notification icons for lines lacking vehicles. - Tracks maximum observed transport speed for passenger and cargo vehicles (exposed via GetMaxTransportSpeed and serialized). Internally it uses Unity.Entities jobs (TransportLineTickJob and VehicleActionJob) and writes commands through an EndFrameBarrier and an IconCommandSystem.


Fields

  • public const uint UPDATE_INTERVAL = 256u
    Defines the system update interval (returns 256 in GetUpdateInterval). The system is intended to tick every 256 frames.

  • private EntityQuery m_LineQuery
    EntityQuery used to find entities that represent routes/transport lines (Route + TransportLine + RouteWaypoint + PrefabRef; excludes Temp/Deleted).

  • private EntityArchetype m_VehicleRequestArchetype
    Archetype used to create ServiceRequest entities for requesting new vehicles.

  • private NativeArray<float> m_MaxTransportSpeed
    Length-2 NativeArray storing max observed transport speeds. Index 0 = passenger transport max, index 1 = cargo transport max. Persistent allocation released on OnDestroy.

  • private JobHandle m_MaxTransportSpeedDeps
    JobHandle used to track dependencies for jobs that write into m_MaxTransportSpeed. Completed when GetMaxTransportSpeed is called.

  • private TimeSystem m_TimeSystem
    Cached reference to the TimeSystem (used to determine day/night for route activation).

  • private EndFrameBarrier m_EndFrameBarrier
    Cached EndFrameBarrier system used to create parallel command buffers for job producers.

  • private IconCommandSystem m_IconCommandSystem
    Cached IconCommandSystem used to add/remove UI notifications for lines.

  • private TypeHandle __TypeHandle
    Struct holding cached ComponentTypeHandle/BufferTypeHandle/ComponentLookup used by jobs; assigned on creation.


Properties

  • None (no public properties exposed by this system)

Constructors

  • public TransportLineSystem()
    Default constructor. Initialization happens in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the system update interval (256). Used by the engine to schedule the system.

  • protected override void OnCreate()
    Initializes the system: gets required systems (TimeSystem, EndFrameBarrier, IconCommandSystem), creates the m_LineQuery, creates the m_VehicleRequestArchetype, allocates m_MaxTransportSpeed, and asserts invariants. Also prepares type handles for job use (via OnCreateForCompiler/__AssignQueries/__AssignHandles in generated code paths).

  • protected override void OnDestroy()
    Disposes m_MaxTransportSpeed and calls base.OnDestroy.

  • protected override void OnUpdate()
    Main runtime logic: zeroes m_MaxTransportSpeed entries, and if there are transport lines:

  • Creates a NativeQueue actionQueue for vehicle actions.
  • Prepares and schedules TransportLineTickJob (parallel chunk job) to:
    • Refresh route/segment RouteInfo and durations,
    • Calculate vehicle counts and intervals,
    • Check vehicles and requests,
    • Enqueue vehicle actions (abandon/cancel),
    • Possibly create ServiceRequest entities for new vehicles,
    • Update icon notifications and m_MaxTransportSpeed.
  • Schedules VehicleActionJob (single-threaded job) that dequeues actionQueue and applies AbandonRoute/CancelAbandon flags to vehicles.
  • Wires up job dependencies, disposes the queue, and registers producer handles with EndFrameBarrier/IconCommandSystem.

  • public void GetMaxTransportSpeed(out float maxPassengerTransportSpeed, out float maxCargoTransportSpeed)
    Completes the jobs that may write m_MaxTransportSpeed, then returns the stored max passenger and cargo transport speeds. Safe to call from main thread to read final values after scheduled jobs complete.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes the two m_MaxTransportSpeed values via the provided writer.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes the two m_MaxTransportSpeed values from the provided reader into m_MaxTransportSpeed.

  • public void SetDefaults(Context context)
    Sets default values for m_MaxTransportSpeed (both entries default to 277.77777f).

  • public static int CalculateVehicleCount(float vehicleInterval, float lineDuration)
    Utility: returns the number of vehicles required for a line by rounding lineDuration / max(1, vehicleInterval) and clamping to at least 1.

  • public static float CalculateVehicleInterval(float lineDuration, int vehicleCount)
    Utility: returns interval between vehicles as lineDuration / max(1, vehicleCount).

  • protected override void OnCreateForCompiler() and private void __AssignQueries(ref SystemState state)
    Generated/compile-time helper methods used to assign queries and handle caches; these ensure the job TypeHandle is initialized in burst-compatible form.

Nested/related job and helper types (not exhaustive): - TransportLineTickJob (IJobChunk, BurstCompile) — main per-line processing job (see OnUpdate scheduling). Handles RouteInfo updates, vehicle count calculations, request creation, and enqueues VehicleAction items. - VehicleActionJob (IJob, BurstCompile) — single-threaded job that processes the queued VehicleAction items and toggles abandon flags on vehicle components. - SortedVehicle, VehicleAction, VehicleActionType — small helper types used inside jobs for sorting and action queuing. - TypeHandle — cached component/buffer/lookups used by jobs; __AssignHandles fills them from SystemState.


Usage Example

// Example: reading max transport speeds from another system/mod
protected override void OnUpdate()
{
    var transportLineSystem = World.GetExistingSystemManaged<Game.Simulation.TransportLineSystem>();
    if (transportLineSystem != null)
    {
        transportLineSystem.GetMaxTransportSpeed(out float maxPassenger, out float maxCargo);
        // Use values (e.g. for diagnostics, balancing, UI)
        UnityEngine.Debug.Log($"Max passenger speed: {maxPassenger}, Max cargo speed: {maxCargo}");
    }
}