LayerProcGen v0.1.0
Layer-based infinite procedural generation
Loading...
Searching...
No Matches
LayerChunk< L, C >abstract

In a layer-and-chunk pair of classes, the chunk inherits from this class. More...

Inherits AbstractLayerChunk.

Public Member Functions

virtual void Create (int level, bool destroy)
 Create or destroy the specified level of this chunk.
 
virtual void Reset ()
 Called by the pool when this IPoolable object is returned to the pool.
 
override string ToString ()
 Example output: "[TerrainChunk (3,-4) level 0]".
 

Properties

override AbstractChunkBasedDataLayer abstractLayer [get]
 Needed for C# covariance reasons. The layer property can be used instead.
 
GridBounds bounds [get]
 The bounds in world space units of this chunk.
 
Point index [get, set]
 The coordinate index this chunk is for.
 
layer [get]
 The layer for this chunk type.
 
int level = -1 [get, set]
 The level the chunk is currently generated up to (zero-based).
 
Point worldOffset [get]
 The position in world space units of the lower left corner of this chunk.
 

Detailed Description

In a layer-and-chunk pair of classes, the chunk inherits from this class.

Template Parameters
LThe corresponding layer class.
CThe chunk class itself.

A layer contains a grid of chunks and takes care of generating and destroying them as appropriate. Each chunk generates and destroys its own data in its Create method.

Type Constraints
L :ChunkBasedDataLayer 
L :L 
L :C 
L :new() 
C :LayerChunk 
C :L 
C :C 
C :new() 

Member Function Documentation

◆ Create()

virtual void Create ( int level,
bool destroy )
virtualinherited

Create or destroy the specified level of this chunk.

Parameters
levelThe level of the chunk to create or destroy.
destroyTrue if destroying, false if creating.

The central method for procedural generation of the chunk.

If the chunk has only one level, the following is a useful pattern for the method. Placing the destruction code before the creation code will tend to place lines of code related to resource use closer to each other.

public override void Create(int level, bool destroy) {
if (destroy) {
// Destroy data for this chunk (or return to pools where applicable).
}
else {
// Generate data for this chunk.
}
}
virtual void Create(int level, bool destroy)
Create or destroy the specified level of this chunk.
Definition LayerChunk.cs:212
int level
The level the chunk is currently generated up to (zero-based).
Definition LayerChunk.cs:74

If the chunk has multiple levels, the following is a useful pattern for the method:

public override void Create(int level, bool destroy) {
if (level == 0) {
if (destroy) {
// Destroy data for level 0 of this chunk (or return to pools where applicable).
}
else {
// Generate data for level 0 of this chunk.
}
}
if (level == 1) {
if (destroy) {
// Destroy data for level 1 of this chunk (or return to pools where applicable).
}
else {
// Generate data for level 1 of this chunk.
}
}
}

◆ Reset()

virtual void Reset ( )
virtualinherited

Called by the pool when this IPoolable object is returned to the pool.

AbstractLayerChunk resets internal chunk state in this method. Derived classes generally don't need to override this method as cleanup should happen in the Create method when the destroy parameter is true. If overriding this method anyway, ensure the the base method is called.

Implements IPoolable.

◆ ToString()

override string ToString ( )
inherited

Example output: "[TerrainChunk (3,-4) level 0]".

Returns

Property Documentation

◆ abstractLayer

override AbstractChunkBasedDataLayer abstractLayer
get

Needed for C# covariance reasons. The layer property can be used instead.

Implements ILC.

◆ bounds

GridBounds bounds
getinherited

The bounds in world space units of this chunk.

Based on worldOffset and layer.chunkSize.

Implements ILC.

◆ index

Point index
getsetinherited

The coordinate index this chunk is for.

A 2D index where consecutive chunks have consecutive indexes. The chunk with its lower left corner at the world origin will have index (0, 0). Its neighbor to the left will have index (-1, 0) and to the right (1, 0) etc.

◆ layer

L layer
get

The layer for this chunk type.

Typically the chunk will often reference its layer to get the chunkSize property, and other chunk-related properties specified in a layer due to them being identical for all chunks of that layer.

◆ level

int level = -1
getsetinherited

The level the chunk is currently generated up to (zero-based).

This property is not updated to a higher level until the call to Create has finished, so do not check it inside the Create method. Use the level parameter of the Create method instead.

◆ worldOffset

Point worldOffset
getinherited

The position in world space units of the lower left corner of this chunk.

Based on index * layer.chunkSize.