Game.FlipTrafficHandednessSystem
Assembly: Assembly-CSharp
Namespace: Game.Net
Type: class
Base: GameSystemBase
Summary:
FlipTrafficHandednessSystem is an ECS system that flips the traffic handedness for net (road) entities whose prefab geometry declares the FlipTrafficHandedness flag. It schedules a Burst-compiled IJobChunk (FlipOnewayRoadsJob) over road edge entities to modify edge endpoints, bezier curves, elevations, upgrades, build orders and several dynamic buffers (connected nodes, service coverage, resource availability) so that left/right handedness is inverted consistently. The system requires entities that have a Road and Edge component and only runs when such entities exist.
Fields
-
private Unity.Entities.EntityQuery m_RoadEdgeQuery
Used to select road edge entities (entities with Road and Edge components). The query is required for update (RequireForUpdate) so the system runs only when relevant road-edge entities exist. -
private FlipTrafficHandednessSystem.TypeHandle __TypeHandle
Holds component and buffer type handles / lookups used by the scheduled job. Populated in OnCreateForCompiler / __AssignHandles and used to obtain ComponentTypeHandle / BufferTypeHandle instances for the job before scheduling.
Properties
- (none)
Constructors
public FlipTrafficHandednessSystem()
Default constructor. The system is preserved for the runtime ([Preserve] on lifecycle methods). Initialization of queries and type handles is performed in OnCreate / OnCreateForCompiler.
Methods
protected override void OnCreate()
Sets up the entity query used by the system:- Creates m_RoadEdgeQuery via GetEntityQuery(ComponentType.ReadOnly
(), ComponentType.ReadOnly ()). -
Calls RequireForUpdate(m_RoadEdgeQuery) so the system only updates when matching entities exist.
-
protected override void OnUpdate()
Constructs a FlipOnewayRoadsJob and fills its ComponentTypeHandle / BufferTypeHandle fields via InternalCompilerInterface.GetComponentTypeHandle and GetBufferTypeHandle, using the prepared __TypeHandle entries. Schedules the job in parallel with JobChunkExtensions.ScheduleParallel and assigns its JobHandle to base.Dependency. -
protected override void OnCreateForCompiler()
Compiler-time helper called during codegen; invokes __AssignQueries and assigns handles via __TypeHandle.__AssignHandles(ref base.CheckedStateRef). Keeps the system ready for the job handle retrieval used in OnUpdate. -
private void __AssignQueries(ref SystemState state)
A short helper used at compile-time; currently only creates and disposes an EntityQueryBuilder(Allocator.Temp). Present for generated/compiled ECS plumbing.
Nested types (summaries):
private struct FlipOnewayRoadsJob : IJobChunk
Burst-compiled job that performs the handedness flip per-chunk. Important fields:- ReadOnly ComponentTypeHandle
m_PrefabRefType - ReadOnly ComponentLookup
m_PrefabGeometryData (lookup to check GeometryFlags) - ComponentTypeHandle
m_EdgeType - ComponentTypeHandle
m_CurveType - ComponentTypeHandle
m_ElevationType - ComponentTypeHandle
m_UpgradedType - ComponentTypeHandle
m_BuildOrderType - BufferTypeHandle
m_ConnectedNodeType - BufferTypeHandle
m_ServiceCoverageType - BufferTypeHandle
m_ResourceAvailabilityType
Behavior (Execute): - Iterates chunk entities and reads their PrefabRef. Uses m_PrefabGeometryData to check if the prefab's NetGeometryData has the GeometryFlags.FlipTrafficHandedness flag. - If the flag is not set: if Upgraded component exists, it calls NetUtils.FlipUpgradeTrafficHandedness(ref value.m_Flags) to adjust upgrade flags and writes it back. - If the flag is set: flips data to invert handedness: - Swaps Edge.m_Start and Edge.m_End. - Inverts Curve.m_Bezier via MathUtils.Invert(...). - Swaps Elevation.m_Elevation components (m_Elevation = m_Elevation.yx) if Elevation exists. - Swaps BuildOrder.m_Start and m_End if BuildOrder exists. - For ConnectedNode buffers: updates m_CurvePosition to math.saturate(1f - m_CurvePosition). - For ServiceCoverage buffers: swaps coverage vector components (m_Coverage = m_Coverage.yx). - For ResourceAvailability buffers: swaps availability vector components (m_Availability = m_Availability.yx).
The job is implemented using IJobChunk.Execute with an explicit forwarding method.
private struct TypeHandle
Utility struct that stores ComponentTypeHandle and BufferTypeHandle fields used to source handles for the job. Provides:- __AssignHandles(ref SystemState state): populates each handle via state.GetComponentTypeHandle / state.GetComponentLookup / state.GetBufferTypeHandle. These handles are then transferred into the job using InternalCompilerInterface helpers at runtime.
Remarks / Implementation notes: - The job is Burst-compiled for performance ([BurstCompile] attribute). - The flip is controlled by GeometryFlags.FlipTrafficHandedness on the prefab's NetGeometryData. If set, the system performs a symmetric inversion of edge endpoints, curves and related per-edge/buffer data so the road geometry and associated per-side data are consistent after flipping handedness. - The system uses InternalCompilerInterface to safely get runtime handles for use with the generated job struct. - Care is taken to only access optional components/buffers if the chunk contains them (checks on .Length and use of respective accessors).
Usage Example
// The system runs as part of the ECS world. Example of how it initializes its query:
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_RoadEdgeQuery = GetEntityQuery(ComponentType.ReadOnly<Road>(), ComponentType.ReadOnly<Edge>());
// Only run when road edges exist
RequireForUpdate(m_RoadEdgeQuery);
}
// The system's OnUpdate schedules the FlipOnewayRoadsJob. No further action is required by mods:
// as long as net prefabs have their NetGeometryData.GeometryFlags set to FlipTrafficHandedness,
// this system will flip endpoints, curves, elevations, build orders and per-edge buffers
// to invert traffic handedness appropriately.
{{ This system is intended for internal use by the net/road pipeline. When authoring or modifying net prefabs, set GeometryFlags.FlipTrafficHandedness in NetGeometryData to trigger handedness inversion on existing entities. Be mindful that this job mutates several components and dynamic buffers; ensure mods interacting with those components are compatible (ordering or synchronization) with this system. }}