Game.Simulation.NetXPSystem
Assembly:
Namespace: Game.Simulation
Type: class NetXPSystem
Base: GameSystemBase
Summary:
NetXPSystem is a simulation system that calculates experience point (XP) gains when net entities (roads, tracks, waterways, pipelines, powerlines, etc.) are created or deleted. It collects created and deleted net entity lists via EntityQuery, schedules a Burst IJob (NetXPJob) that inspects prefab data, edges and curves to compute per-type XP contributions, picks the largest contribution and enqueues an XPGain into the central XPSystem queue. The system uses ComponentLookup handles cached in an internal TypeHandle and normalizes rewards by a constant length factor.
Fields
-
private static readonly float kXPRewardLength
Constant (112f) used to normalize XP reward by net piece length (reward per kXPRewardLength units). -
private XPSystem m_XPSystem
Reference to the game's XPSystem. Used to get a queue writer and to register the job dependency so XP gains are enqueued safely. -
private EntityQuery m_CreatedNetQuery
EntityQuery matching net entities that were just created (Edge + PrefabRef + Curve + Created, excluding Temp/Deleted). Used to obtain the created entities this frame. -
private EntityQuery m_DeletedNetQuery
EntityQuery matching net entities that were just deleted (Edge + PrefabRef + Curve + Deleted, excluding Temp/Created). Used to obtain the deleted entities this frame. -
private TypeHandle __TypeHandle
Internal struct that stores ComponentLookup handles (PlaceableNetData, Elevation, RoadData, TrackData, WaterwayData, PipelineData, PowerLineData, PrefabRef, Edge, Curve). __AssignHandles fills these lookups from a SystemState. -
private struct NetXPJob
(nested)
A Burst-compiled IJob that: - Uses ComponentLookup
to read PlaceableNetData, Elevation, RoadData, TrackData, WaterwayData, PipelineData, PowerLineData, PrefabRef, Edge, Curve. - Receives deferred job arrays of created and deleted entities.
- Counts XP via CountXP (checks prefab XP reward, elevation bonus, curve length).
-
Subtracts deleted XP from created XP, chooses the maximum category using NetXPs.GetMaxValue, and enqueues an XPGain into the provided XP queue if > 0.
-
private struct NetXPs
(nested)
Struct that accumulates float XP totals per net category (roads, trains, trams, subways, waterways, pipes, powerlines). Provides Clear, operator + / -, and GetMaxValue which returns the maximum accumulated XP and its XPReason. -
private struct TypeHandle
(nested)
Holds ComponentLookupfields and provides __AssignHandles(ref SystemState) to initialize them via state.GetComponentLookup ().
Properties
- None (the system uses fields and internal handles; no public properties are declared)
Constructors
public NetXPSystem()
Default parameterless constructor (marked [Preserve]). No custom initialization beyond what the base GameSystemBase does; actual setup happens in OnCreate.
Methods
protected override void OnCreate()
Sets up the EntityQueries:- m_CreatedNetQuery: Edge, PrefabRef, Curve, Created, excludes Temp/Deleted
-
m_DeletedNetQuery: Edge, PrefabRef, Curve, Deleted, excludes Temp/Created Retrieves the XPSystem instance from the World (m_XPSystem = base.World.GetOrCreateSystemManaged
()) and calls RequireForUpdate(m_CreatedNetQuery) to ensure the system runs only when there are created net entities. Marked [Preserve]. -
protected override void OnUpdate()
Main scheduling logic: - Converts the created/deleted queries to NativeList
asynchronously (ToEntityListAsync), receiving dependencies. - Constructs a NetXPJob with component lookups obtained through InternalCompilerInterface.GetComponentLookup(ref __TypeHandle..., ref base.CheckedStateRef).
- Sets m_CreatedEntities/m_DeletedEntities to deferred job arrays from the NativeLists.
- Obtains the XP queue and its dependency via m_XPSystem.GetQueue(out deps).
- Schedules the job combining all dependencies and sets base.Dependency to the returned JobHandle.
- Calls m_XPSystem.AddQueueWriter(base.Dependency) to register the writer dependency.
-
Disposes the NativeLists with base.Dependency to ensure correct lifetime ordering.
-
protected override void OnCreateForCompiler()
Internal/compile-time helper that calls __AssignQueries and __TypeHandle.__AssignHandles to initialize compiler-required state. Intended for internal/IL2CPP usage. -
private void __AssignQueries(ref SystemState state)
Compiler helper (used in OnCreateForCompiler). In this implementation it creates and immediately disposes an EntityQueryBuilder (likely a placeholder to satisfy code-gen requirements). -
(Nested NetXPJob members)
private float GetElevationBonus(Edge edge, ComponentLookup<Elevation> elevations, bool isRoad)
Returns elevation-based bonus (returns 1f if both start and end elevations have positive values for roads; otherwise 0f).private NetXPs CountXP(ref NativeArray<Entity> entities)
Iterates entities, retrieves prefab via PrefabRef, checks PlaceableNetData.m_XPReward and component types to categorize and accumulate XP scaled by prefab reward, elevation bonus, curve length and kXPRewardLength.public void Execute()
Entry point for the job: counts created XP, subtracts deleted XP if any, calls GetMaxValue to determine max XP and reason, and enqueues an XPGain if integer XP > 0.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_CreatedNetQuery = GetEntityQuery(
ComponentType.ReadOnly<Edge>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<Curve>(),
ComponentType.ReadOnly<Created>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>());
m_DeletedNetQuery = GetEntityQuery(
ComponentType.ReadOnly<Edge>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<Curve>(),
ComponentType.ReadOnly<Deleted>(),
ComponentType.Exclude<Created>(),
ComponentType.Exclude<Temp>());
m_XPSystem = base.World.GetOrCreateSystemManaged<XPSystem>();
RequireForUpdate(m_CreatedNetQuery);
}
Notes and implementation details:
- The system relies on component data on the prefab entity (PlaceableNetData, RoadData, TrackData, etc.) and on runtime components on the net entity (Edge, Curve, PrefabRef).
- XP calculation details:
- Only prefabs with PlaceableNetData.m_XPReward > 0 contribute.
- Reward per entity = (m_XPReward + elevationBonus) * curveLength / kXPRewardLength.
- Elevation bonus applies only to roads when both start and end elevation vectors have positive x and y.
- For tracks, TrackData.m_TrackType is used to distinguish Train/Tram/Subway.
- After summing created minus deleted contributions, the system selects the category with the highest XP and enqueues a single XPGain containing the floored integer amount and the XPReason.
- The job uses ComponentLookup