Grid obstacle detection

Hi all,

I am having a problem with obstacle detection on my grid. My grid size differs depending on the current level of the game. Currently my grid is instantiated piece by piece and placed inside a List<>. Attached to the grid pieces is this class:

public class gridtile : MonoBehaviour
{
	// This class will be used so we can modify the isOccupied flag
	// if the grid section is occupied, the cube cant move onto it
	public bool isOccupied = false;
	public bool haslightsOn = false;
	
	public gridtile north;
	public gridtile south;
	public gridtile east;
	public gridtile west;
}

Once the obstacles have spawned on a part of the grid the isOccupied flag changes from false to true. I am trying to figure out how to initialize the north, south etc pieces dependant on the current tile my player is on. I think this sort of looks like a linked list. My player object moves from grid piece to grid piece. The player class also has a data member called gridtile currentTile.
I am not sure also how to “read ahead” in the list so that for example this code may work:

if (currentTile.north.isOccupied)
{
    canMoveNorth = false;
}

This should get you going. Note that you’ll have to add null checks when checking if you can move, since the ones around the outside will not have adjacent tiles (you could wrap them though if you want?)

Block[] blocks = new Blocks[4,4];

for(int i = 0; i < 4; i++) {
    for(int j = 0; j < 4; j++) {
        blocks[i,j] = new Block();
    }
}

for(int i = 0; i < 4; i++) {
    for(int j = 0; j < 4; j++) {
        if(i + 1 < 4) blocks[i,j].North = blocks[i +1,j];
        if(i - 1 >= 0) blocks[i,j].South = blocks[i - 1,j];
        if(j + 1 < 4) blocks[i,j].East = blocks[i,j + 1];
        if(j - 1 >= 0) blocks[i,j].West = blocks[i,j - 1];
    }
}

I’m a little disappointed we aren’t making massive irregularly shaped continents with portals though :frowning: