HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Method for obtaining adjacent chunk positions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
adjacentobtainingchunkpositionsmethodfor

Problem

I'm currently in the process of making a 2D tile based game. This game has an "infinite" world that can be traversed and modified as needed. I am creating the chunks with a mesh and I apply a texture to them. The chunks texture updates every time I set a tile with a new Sprite.

Each time I set a new tile, the surrounding tiles from the same chunk recalculate based on their adjacent tiles and they set their sprite accordingly (Auto-tiling). This works perfectly fine and I have no issues, unless the tile is along the edge of the chunk.

If the tile is along the edge of the chunk, it updates the texture of the chunk perfectly but the adjacent chunk textures aren't updated. Now, I am using the below code to obtain the adjacent chunks based on the tiles position. This works, however it is extremely ugly and unreadable.

```
if (tileX == 0 || tileY == 0 || tileX == chunkSize - 1 || tileY == chunkSize - 1)
{
var chunksToUpdate = new List ();
var position = chunk.Position;

if (tileX == 0 && tileY == 0)
{
chunksToUpdate.Add (new Vector3Int (position.X - chunkSize, position.Y, position.Z)); //left
chunksToUpdate.Add (new Vector3Int (position.X, position.Y - chunkSize, position.Z)); //down
chunksToUpdate.Add (new Vector3Int (position.X - chunkSize, position.Y - chunkSize, position.Z)); //downleft

}
else if (tileX == 0 && tileY == chunkSize - 1)
{
chunksToUpdate.Add (new Vector3Int (position.X - chunkSize, position.Y, position.Z)); //left
chunksToUpdate.Add (new Vector3Int (position.X, position.Y + chunkSize, position.Z)); //up
chunksToUpdate.Add (new Vector3Int (position.X - chunkSize, position.Y + chunkSize, position.Z)); //upleft

}
else if (tileX == chunkSize - 1 && tileY == 0)
{
chunksToUpdate.Add (new Vector3Int (position.X + chunkSize, position.Y, position.Z)); //right
chunksToUpdate.Add (new Vector3Int (position.X, position.Y - chunkSize, position.Z)); //down

Solution

Well, I suggest next approach

if (tileX != 0 && tileY != 0 && tileX != chunkSize - 1 && tileY != chunkSize - 1)
        {
            return;
        }

        var leftChunk = tileX == 0;
        var rightChunk = tileX == chunkSize - 1;
        var downChunk = tileY == 0;
        var upChunk = tileY == chunkSize - 1;

        if (leftChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X - chunkSize, position.Y, position.Z)); // Left
            if (downChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X - chunkSize, position.Y - chunkSize, position.Z)); // DownLeft
            }
            else if (upChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X - chunkSize, position.Y + chunkSize, position.Z)); // UpLeft
            }
        }
        else if (rightChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X + chunkSize, position.Y, position.Z)); // Right
            if (upChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X + chunkSize, position.Y + chunkSize, position.Z)); // UpRight
            }
            else if (downChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X + chunkSize, position.Y - chunkSize, position.Z)); // DownRight
            }
        }
        if (downChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X, position.Y - chunkSize, position.Z)); // Down
        }
        else if (upChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X, position.Y + chunkSize, position.Z)); // Up
        }


It makes code a little bit more readable by using bool named variables and also has less code lines and if else branching.

Moreover, it is often (almost every time) better to have less nesting via inverting if statement like I did. So basically I quit if it is not an edge tile.

Code Snippets

if (tileX != 0 && tileY != 0 && tileX != chunkSize - 1 && tileY != chunkSize - 1)
        {
            return;
        }

        var leftChunk = tileX == 0;
        var rightChunk = tileX == chunkSize - 1;
        var downChunk = tileY == 0;
        var upChunk = tileY == chunkSize - 1;


        if (leftChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X - chunkSize, position.Y, position.Z)); // Left
            if (downChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X - chunkSize, position.Y - chunkSize, position.Z)); // DownLeft
            }
            else if (upChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X - chunkSize, position.Y + chunkSize, position.Z)); // UpLeft
            }
        }
        else if (rightChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X + chunkSize, position.Y, position.Z)); // Right
            if (upChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X + chunkSize, position.Y + chunkSize, position.Z)); // UpRight
            }
            else if (downChunk)
            {
                chunksToUpdate.Add(new Vector3Int(position.X + chunkSize, position.Y - chunkSize, position.Z)); // DownRight
            }
        }
        if (downChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X, position.Y - chunkSize, position.Z)); // Down
        }
        else if (upChunk)
        {
            chunksToUpdate.Add(new Vector3Int(position.X, position.Y + chunkSize, position.Z)); // Up
        }

Context

StackExchange Code Review Q#118498, answer score: 2

Revisions (0)

No revisions yet.