Skip to content

Game.Prefabs.LeisureProviderData

Assembly:
Namespace: Game.Prefabs

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
LeisureProviderData is a small ECS component that describes a leisure-service provider in the simulation. It stores an efficiency metric, the resource type associated with the provider, and the leisure category exposed to agents. The struct implements custom binary serialization/deserialization to pack its fields into compact primitive representations (int, sbyte, byte) using EconomyUtils helpers for resource indexing. It is intended for use with Unity Entities (DOTS) and the game's serialization system.


Fields

  • public System.Int32 m_Efficiency
    Holds the provider's efficiency value (int). Used by simulation logic to scale service output or cost. Serialized first as a 32-bit int.

  • public Game.Economy.Resource m_Resources
    Represents the resource category used/produced by this provider. When serializing, this is converted to an sbyte index via EconomyUtils.GetResourceIndex and written as an sbyte. On deserialization EconomyUtils.GetResource(value) is used to restore the enum/value.

  • public Game.Agents.LeisureType m_LeisureType
    Specifies the category/type of leisure service provided (e.g., park, entertainment). Serialized as a single byte (cast from the enum underlying value).

Properties

  • None

Constructors

  • public LeisureProviderData()
    No explicit constructors are defined; the struct uses the default parameterless constructor provided by C#. Fields should be initialized explicitly when creating instances if non-default values are required.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component to a binary writer in the following order:
  • m_Efficiency as a 32-bit int.
  • m_Resources converted to an sbyte index via EconomyUtils.GetResourceIndex and written as sbyte.
  • m_LeisureType cast to byte and written as byte. This compact representation is used by the game's serialization pipeline.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads fields from a binary reader in the same order they were written:

  • reads an int into m_Efficiency.
  • reads an sbyte resource index and converts it to Resource using EconomyUtils.GetResource.
  • reads a byte and casts it to LeisureType to populate m_LeisureType. The method uses ref assignment for the efficiency field and expects the reader to support the required primitive reads.

Notes / Considerations: - The serialization assumes the resource index fits into an sbyte and the leisure type fits into a byte. These casts can truncate or produce invalid values if enum definitions change between versions; consider adding versioning checks if modifying serialization layout. - Because this is an IComponentData, instances are intended to be stored on entities (EntityManager / ECS workflows). IQueryTypeParameter allows the type to be used in query construction patterns.

Usage Example

// Create and add the component to an entity (EntityManager used as example)
var data = new Game.Prefabs.LeisureProviderData
{
    m_Efficiency = 85,
    m_Resources = Game.Economy.Resource.Water,   // example enum member
    m_LeisureType = Game.Agents.LeisureType.Park // example enum member
};

entityManager.AddComponentData(entity, data);

// Example: manual serialization using the game's IWriter (pseudo-code)
using (var writer = new SomeBinaryWriter())
{
    data.Serialize(writer);
    // writer now contains a compact representation: int, sbyte, byte
}

// Example: manual deserialization
var loaded = new Game.Prefabs.LeisureProviderData();
using (var reader = new SomeBinaryReader(serializedBytes))
{
    loaded.Deserialize(reader);
    // loaded now has the same field values restored
}