Game.Prefabs.TaxableResourceData
Assembly:
Namespace: Game.Prefabs
Type: struct
Base: IComponentData, IQueryTypeParameter
Summary:
Represents which tax areas a particular resource (or prefab) is taxable in. The struct stores a compact bitmask (byte) where each bit corresponds to a TaxAreaType. It is intended for use with Unity's ECS (implements IComponentData) and as a query parameter (IQueryTypeParameter). The design favors minimal memory usage and O(1) containment checks, but limits the number of distinct tax areas to the number of bits in a byte (8).
Fields
public byte m_TaxAreas
Stores the bitmask representing enabled tax areas. Each bit corresponds to a TaxAreaType as encoded by the private GetBit method. A set bit (1) means the resource is taxable in that area.
Properties
- None
Constructors
public TaxableResourceData(IEnumerable<TaxAreaType> taxAreas)
Initializes the bitmask from an enumeration of TaxAreaType values. Iterates the provided taxAreas and sets the corresponding bits in m_TaxAreas. If the provided collection is empty, m_TaxAreas will be 0 (no tax areas).
Methods
-
public bool Contains(TaxAreaType areaType)
Checks whether the bit corresponding to areaType is set in m_TaxAreas. Returns true if the resource is taxable in the given area; otherwise false. This is an O(1) bitwise test. -
private static int GetBit(TaxAreaType areaType)
Returns an integer with a single bit set for the provided TaxAreaType. The implementation uses 1 << (areaType - 1), so it assumes TaxAreaType values are 1-based (i.e., TaxAreaType value 1 maps to bit 0). Be cautious: values too large (>= 9) will overflow the byte capacity when mapped into m_TaxAreas.
Usage Example
// Example: create a TaxableResourceData that is taxable in two areas
var areas = new[] { TaxAreaType.City, TaxAreaType.Suburban };
var taxable = new TaxableResourceData(areas);
// Check if taxable in an area
if (taxable.Contains(TaxAreaType.City))
{
// handle taxable logic for City area
}
Notes and recommendations: - Because m_TaxAreas is a byte, you can represent up to 8 distinct tax areas. If the game defines more than 8 TaxAreaType values, this struct will not be able to represent them all simultaneously without changing the storage type. - The GetBit implementation subtracts 1 from the TaxAreaType value before shifting. Ensure the enum values are defined accordingly (starting at 1) to avoid off-by-one mapping errors. - This struct is appropriate for use as a component in Unity.Entities-based systems where compact storage and fast checks are desirable.