Class CellRegionLayer
Represents a terrain layer that manages cell region assignments and region-related data for a hex-based terrain. Provides functionality to assign, query, and serialize region information for each cell, as well as to calculate region membership and sizes.
Inherited Members
Namespace: Fwt.HexTerrains.Regions.Data
Assembly: fwt.hexterrains.dll
Syntax
public class CellRegionLayer : HexTerrainLayer, IDisposable, ISerializableTerrainLayer
Remarks
This layer maintains mappings between cells and their assigned regions, and supports efficient queries and updates of region data. It is typically used in scenarios where terrain needs to be partitioned into logical regions, such as countries or provinces. The layer supports serialization and deserialization of region data for persistence. Thread safety and job scheduling are handled internally to support parallel operations when calculating region cells and sizes.
Fields
CellRegionMap
DataLayer that maps each cell to its assigned region index.
Declaration
public ColorMapCellValueDataLayer_Int CellRegionMap
Field Value
| Type | Description |
|---|---|
| ColorMapCellValueDataLayer_Int |
RegionCells
MultiHashMap DataLayer that stores lists of cell indices for each region. Key = region index, Value = cell index.
Declaration
public NativeParallelMultiHashMapDataLayer<int, uint> RegionCells
Field Value
| Type | Description |
|---|---|
| NativeParallelMultiHashMapDataLayer<int, uint> |
RegionSizes
DataLayer that stores the size (number of cells) of each region.
Declaration
public NativeListDataLayer<uint> RegionSizes
Field Value
| Type | Description |
|---|---|
| NativeListDataLayer<uint> |
Methods
CalcRegionsCells(JobHandle)
Schedules a job to recalculate the mapping of terrain cells to regions, updating the region cell data as needed.
Declaration
public virtual JobHandle CalcRegionsCells(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency | A job handle representing dependencies that must be completed before this job can run. |
Returns
| Type | Description |
|---|---|
| JobHandle | A job handle that represents the scheduled calculation job and its dependencies. If no calculation is needed, returns the input dependency handle. |
Remarks
This method only schedules the calculation if the region map is marked as dirty. If the region map or region cell data is unavailable, or if no recalculation is required, the method returns the original dependency handle without scheduling a new job.
CalcRegionsSizes(JobHandle)
Schedules a job to calculate the sizes of all regions, updating the region sizes data as needed.
Declaration
public virtual JobHandle CalcRegionsSizes(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency | A job handle representing the dependency chain that must be completed before this job can run. |
Returns
| Type | Description |
|---|---|
| JobHandle | A job handle that represents the scheduled calculation job and its dependencies. If no calculation is needed, returns the input dependency handle. |
Remarks
This method only schedules the calculation if either the region map or region sizes data is marked as dirty. The returned job handle should be used to ensure that dependent jobs are scheduled after the region sizes have been updated.
CalculateColorMap(JobHandle)
Schedules a job to calculate the color map for the region layer.
Declaration
public override JobHandle CalculateColorMap(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency | A job handle representing the dependency chain that must be completed before this job can run. |
Returns
| Type | Description |
|---|---|
| JobHandle | A job handle that represents the scheduled calculation job and its dependencies. If no calculation is needed, returns the input dependency handle. |
Overrides
Cleanup()
Clears all dirty flags from the region layer and its associated data layers.
Declaration
public override void Cleanup()
Overrides
CleanupAsync(JobHandle)
Asynchronously clears all dirty flags from the region layer and its associated data layers.
Declaration
public override JobHandle CleanupAsync(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency |
Returns
| Type | Description |
|---|---|
| JobHandle |
Overrides
CompleteAllJobs()
Completes all jobs that affect data layers.
Declaration
public override void CompleteAllJobs()
Overrides
DeserializeLayer(BinaryReader, HexTerrainSettings)
Deserializes the layer's data from the specified binary reader.
Declaration
public virtual bool DeserializeLayer(BinaryReader reader, HexTerrainSettings terrainSettings)
Parameters
| Type | Name | Description |
|---|---|---|
| BinaryReader | reader | The binary reader from which the layer data will be read |
| HexTerrainSettings | terrainSettings | terrain settings to (re)initilize a layer with to fit the serialized data |
Returns
| Type | Description |
|---|---|
| bool | true if the data was successfully deserialized; otherwise, false. |
Dispose()
Releases all resources used by the current instance of the class, including associated region data.
Declaration
public override void Dispose()
Overrides
Remarks
Call this method when the object is no longer needed to free unmanaged resources and perform other cleanup operations. After calling Dispose, the object should not be used.
GetCellRegion(int)
Returns the region index assigned to the specified cell or null if cellIndex is out of bounds or region map is not initialized.
Declaration
public virtual int? GetCellRegion(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | Cell index to get value from |
Returns
| Type | Description |
|---|---|
| int? | Value from specified cell or null if cellIndex is out of bounds or region map is not initialized |
Init(HexTerrainSettings)
Initializes the data layers required for region mapping using the specified terrain settings.
Declaration
public override void Init(HexTerrainSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| HexTerrainSettings | settings | The settings that define the configuration and cell count for the hex terrain. |
Overrides
Remarks
This method prepares the region map, region cell associations, and region size data layers for use. It should be called before performing operations that depend on these data layers. Calling this method multiple times is safe; data layers will only be initialized if they have not already been created.
Init(HexTerrainSettings, ICellRegionConfig)
Initializes the terrain with the specified settings and applies region-specific configuration if provided.
Declaration
public virtual void Init(HexTerrainSettings settings, ICellRegionConfig args)
Parameters
| Type | Name | Description |
|---|---|---|
| HexTerrainSettings | settings | The settings to use for initializing the terrain. Cannot be null. |
| ICellRegionConfig | args | The region configuration to apply during initialization. If null, no region-specific configuration is applied. |
SerializeLayer(BinaryWriter)
Serializes the current layer's data to the specified binary writer.
Declaration
public virtual bool SerializeLayer(BinaryWriter writer)
Parameters
| Type | Name | Description |
|---|---|---|
| BinaryWriter | writer | The binary writer to which the layer data will be written. |
Returns
| Type | Description |
|---|---|
| bool | true if the layer was successfully serialized; otherwise, false. |
SetAllCellsRegion(int)
Sets the specified region index for all cells in the region map.
Declaration
public virtual bool SetAllCellsRegion(int regionIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | regionIndex | The region index to assign to every cell. Must be a valid region index as defined by the region map. |
Returns
| Type | Description |
|---|---|
| bool | true if values was succesfully assigned; otherwise, false |
Remarks
If the region map is not initialized, this method performs no action.
SetAllDirty(bool)
Marks all regions and their associated color map chunks as dirty or clean, depending on the specified value.
Declaration
public override void SetAllDirty(bool isDirty)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | isDirty | A value indicating whether to mark all chunks as dirty (true) or clean (false). |
Overrides
SetCellRegion(int, int)
Assigns the specified region to the cell at the given index.
Declaration
public virtual bool SetCellRegion(int cellIndex, int regionIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell to update. Must be within the valid range of cell indices. |
| int | regionIndex | The index of the region to assign to the cell. |
Returns
| Type | Description |
|---|---|
| bool | true if the region was successfully assigned to the cell; otherwise, false. |