Game.Simulation.ElectricityTradeSystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase, IDefaultSerializable, ISerializable
Summary:
ElectricityTradeSystem collects electricity flows that cross trade nodes (outside connections), accumulates total exported and imported electricity, and enqueues fee events for outside electricity trade. It uses two Burst jobs: SumJob (IJobChunk) to read ConnectedFlowEdge buffers and ElectricityFlowEdge components to accumulate per-thread export/import counts, and ElectricityTradeJob (IJob) to convert those counts to monetary cost using OutsideTradeParameterData and push ServiceFeeSystem.FeeEvent entries. The system runs on a coarse interval (update interval 128, offset 126) and persists the last-frame export/import totals for save/load.
Fields
-
private ElectricityFlowSystem m_ElectricityFlowSystem
Reference to the ElectricityFlowSystem used to obtain source/sink node Entities for determining direction of flow. -
private EntityQuery m_TradeNodeGroup
EntityQuery selecting TradeNode entities that also have ElectricityFlowNode and ConnectedFlowEdge; used to schedule the SumJob over trade nodes. -
private ServiceFeeSystem m_ServiceFeeSystem
Reference to the ServiceFeeSystem used to obtain a fee queue writer and to register the writer's dependency. -
private NativePerThreadSumInt m_Export
Per-thread accumulator used during the SumJob to sum exported electricity units; persisted across updates and reset each update. -
private NativePerThreadSumInt m_Import
Per-thread accumulator used during the SumJob to sum imported electricity units; persisted across updates and reset each update. -
private int m_LastExport
Last recorded total export count (snapshot saved/serialized). Exposed via the export property. -
private int m_LastImport
Last recorded total import count (snapshot saved/serialized). Exposed via the import property. -
private TypeHandle __TypeHandle
Compiler-generated struct containing BufferTypeHandle and ComponentLookup handles used by the scheduled job. Managed/initialized in OnCreateForCompiler. -
private EntityQuery __query_1233563293_0
Compiler-generated EntityQuery used to obtain the OutsideTradeParameterData singleton.
Properties
-
public int export { get }
Returns m_LastExport — the last update's total exported electricity count. Useful for UI or stats. -
public int import { get }
Returns m_LastImport — the last update's total imported electricity count.
Constructors
public ElectricityTradeSystem()
Default constructor (preserved). Initialization is performed in OnCreate rather than the constructor.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 128 — this system runs every 128 ticks (coarse periodic update). -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns 126 — the offset into the update schedule. -
protected override void OnCreate()
Initializes references to ElectricityFlowSystem and ServiceFeeSystem, builds the trade node EntityQuery, requires OutsideTradeParameterData for updates, and allocates the NativePerThreadSumInt accumulators (m_Export and m_Import) with Allocator.Persistent. -
protected override void OnDestroy()
Disposes m_Export and m_Import and calls base.OnDestroy to free resources. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes m_LastExport and m_LastImport to the provided writer for save persistence. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads m_LastExport and m_LastImport from the provided reader during load. -
public void SetDefaults(Context context)
Resets m_LastExport and m_LastImport to zero. Called when initializing default data for a new game/context. -
protected override void OnUpdate()
Main runtime logic: - Copies current accumulator counts to m_LastExport/m_LastImport and resets accumulators.
- If there are trade nodes, schedules SumJob (parallel JobChunk) that iterates ConnectedFlowEdge buffers and ElectricityFlowEdge components to increment per-thread export/import accumulators based on source/sink node comparison.
- Schedules ElectricityTradeJob which computes trade amounts and costs from the accumulated counts and the OutsideTradeParameterData singleton, enqueueing ServiceFeeSystem.FeeEvent entries for export/import fees. It also wires dependencies with the ServiceFeeSystem fee queue.
-
Registers the fee queue writer dependency with ServiceFeeSystem.
-
private void __AssignQueries(ref SystemState state)
Compiler-generated: builds the query that fetches the OutsideTradeParameterData singleton (including systems) and stores it in __query_1233563293_0. -
protected override void OnCreateForCompiler()
Compiler helper called during system creation to assign query and type handles using the system state.
Inner job descriptions (nested private structs):
- SumJob (IJobChunk)
- Reads ConnectedFlowEdge buffers and ElectricityFlowEdge components. For each connected edge on a trade node, checks whether the flow edge represents an export (edge.End == sink node) or import (edge.Start == source node), asserts non-negative flow, and adds the integer flow amount to the per-thread m_Export or m_Import accumulators.
- ElectricityTradeJob (IJob)
- Converts accumulated counts to normalized amounts (divides by 2048f), multiplies by prices from OutsideTradeParameterData to calculate costs, and enqueues ServiceFeeSystem.FeeEvent entries for positive export/import costs. Uses a fee queue provided by ServiceFeeSystem and respects job dependencies.
Usage Example
// Example: get the system from the World and read the last export/import totals.
var tradeSystem = World.DefaultGameObjectInjectionWorld
.GetExistingSystemManaged<Game.Simulation.ElectricityTradeSystem>();
if (tradeSystem != null)
{
int lastExport = tradeSystem.export;
int lastImport = tradeSystem.import;
// Use the values for UI, stats, or logging.
}