Building in Columns and Light

Will help in generating light and shadows.

Before implementing this post, make sure you have a collection class for storing block type data in you Chunk class!

Steps for adding light:

—Make a ColumnMap

—Make a HeightMap

—Build chunks in columns

—Make a collection class for light level data.

—Make a class finds how much light there is at each block in a column.

 

ColumnMap

This post is written with the assumption that your world has a definable surface (i.e. it’s resolvable to a height map). Having a surface let’s us identify blocks as being aboveground and underground.

(If you don’t have a surface that resolves to a height map in your world and want to add ambient light to it, you’ll need to devise some other way of defining where the light is. Maybe, your air blocks have MAX_LIGHT if they don’t have a solid block above them for (say) 10 blocks and no blocks with less than MAX_LIGHT 10 blocks above and the same condition for any two of the four XZ directions? Else they have light equal to their lightest XZ neighbor minus 1?)

Later on, having a surface will also help us decide where to put structures.

Since we want to add light levels to any chunks we generate, we have to know where the surface is for those chunks. If we build our chunks in a vertical column, we will know that we can also add light to those chunks after we are done building them.

To build chunks column-wise, we need a way to represent the idea of a column’s state—EMPTY, BUILDING, BUILT—in the program.

Also, we need a class that can keep track of those states and tell us which columns to build next.

To embody a column’s build status use an enum:

And why not call it ColumnStatus or BuildStatus?

Give it the values:

EMPTY, BUILDING, BUILT;

Then, make a ColumnMap class.

Which will own:

(private final) HashMap<Coord2, ColumnStatus> columns.

(Wait, do you have Coord2 yet? Its just like Coord3 but in only 2 dimensions.)

And has some methods for getting and setting the ColumnStatus at a given coord2. For example:

public void setBuilt(Coord2 column) {
columns.put(column, ColumnStatus.BUILT);
}

public ColumnStatus get(Coord2 column) { return columns.get(column); }

 

HeightMap

Will be a backed by a HashMap<Coord2, ByteArray2D>. Make a ByteArray2D class that’s backed by an array of bytes Chunk.XLENGTH * Chunk.ZLENGTH long.

Build in Columns

LightMap

LightComputer

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation