Skip to content

Game.Simulation.GroundPollution

Assembly: Game
Namespace: Game.Simulation

Type: struct (GroundPollution)

Base: System.ValueType
Implements: IPollution, IStrideSerializable, ISerializable

Summary:
Represents ground pollution as a 16-bit signed integer (short) with helper methods for accumulation and version-aware serialization/deserialization. The struct clamps additions to prevent overflow and contains logic to remain compatible with multiple on-disk formats by reading/writing an extra short in older versions. GetStride reports the serialized byte size depending on the context version.


Fields

  • public short m_Pollution
    Holds the current pollution value for the ground cell. Values are managed as a signed 16-bit integer. The Add method ensures the value is clamped to at most 32767 to avoid overflow.

Properties

  • (none)

Constructors

  • public GroundPollution()
    Default value-type constructor. m_Pollution will be 0 unless initialized otherwise.

Methods

  • public void Add(short amount)
    Adds the specified amount to m_Pollution and clamps the result to a maximum of 32767. Uses Unity.Mathematics.math.min for clamping.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the current m_Pollution value to the provided writer. Writer must implement IWriter.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads m_Pollution from the provided reader. For compatibility with older save formats, if reader.context.version is >= Version.groundPollutionDelta and < Version.removeGroundPollutionDelta, an additional short is read and discarded (legacy delta field).

  • public int GetStride(Context context)
    Returns the number of bytes used by this struct in serialized form for the given context.version. Returns 4 bytes for contexts older than Version.removeGroundPollutionDelta (legacy format included an extra short) and 2 bytes for newer contexts.

Usage Example

// Create and modify
var gp = new Game.Simulation.GroundPollution();
gp.Add(150); // increments m_Pollution by 150, clamped to 32767

// Serialize (example; actual writer obtained from serialization system)
IWriter writer = /* obtain writer from framework */;
gp.Serialize(writer);

// Deserialize (example; actual reader/context obtained from serialization system)
IReader reader = /* obtain reader */;
gp.Deserialize(reader);

// Get stride for a context (to know serialized size)
Context ctx = /* obtain context with ctx.version set */;
int sizeInBytes = gp.GetStride(ctx);

Notes: - The struct is small and designed to be stride-serializable for efficient bulk storage. - Version checks reference Version.groundPollutionDelta and Version.removeGroundPollutionDelta to maintain backward/forward compatibility when the on-disk layout changed (legacy delta value).