Finding neighbouring grid points in dynamic 2D grids

Tondy

New member
Joined
Aug 14, 2021
Messages
1
Programming Experience
5-10
Hello,

So I am working on a mini game that dynamically creates quads of 2 * 2 or 2 * 4 or 4 * 4. The player is then prompted to tap on a random neighbouring quad starting from the bottom right. The neighbouring quad needs to be chess move king distance from the current grid point. New neighbouring grid points are prompted through out the game. How could I write code that finds neighbouring quads from a given current quad point?

This is the current code creating grid point :
C#:
public void CreateQuadArray()
{
    totalQuads = quadRows * quadColumns;

    gridPoints = new Vector3[quadRows, quadColumns];

    currentGridPoint = new int[quadRows, quadColumns];

    for (int row = 0; row < gridPoints.GetLength(0); row++)
    {
        gridPoints[row, 0] = new Vector3(quadRowOffSet * row,0,0);

        for (int col = 0; col < gridPoints.GetLength(1); col++)
        {
            gridPoints[row, col] = new Vector3(quadRowOffSet * row, 0, quadColumnOffSet * col);

            MakeQuad(gridPoints[row, col]);
        }
    }
}

public void MakeQuad(Vector3 quadPosition)
{

    var Quad = Instantiate(Resources.Load("Prefabs/Quad"), quadPosition, Quaternion.Euler(90,0,0)) as GameObject;
    var quadIndex = UnityEngine.Random.Range(0, totalQuads);

    Quad.name = stepCountQuads.quads[quadIndex].QuadTitle.ToString();
    Quad.GetComponent<QuadController>().QuadID = stepCountQuads.quads[quadIndex].QuadId;
    Quad.GetComponent<QuadController>().QuadMaterial = stepCountQuads.quads[quadIndex].QuadMaterial;
}
 
Last edited by a moderator:
Welcome to the forums. In the future, please use code tags around your code to make it easier for others to read your post.

My recommendation is to generate the entire field of play ahead of time, but only reveal parts of the field like most games that use "fog-of-war" to incrementally reveal a map. That way determining adjacency is a matter of adding or subtracting from the ordinates of the current position.
 
Back
Top Bottom