Saturday 9 March 2013

Knowing what's around you!

So I have all these beautiful room prefabs but my script doesn't know when to use the appropriate one! Not only that but it doesn't know which way the prefab needs to be facing! Fortunately I have a plan to get this working.

My script needs to figure out how many rooms are adjacent to the room in question. To do this I need to know the properties of an adjacent room. An adjacent room will have the same co-ordinates as the room in question except it's X or Y value will be 1 higher or 1 lower. Now how do I get my script to check this? Firstly I start off by choosing a room to check, this can be done using a for loop.

for(int i = 0; i <= roomCount; ++i)
{
      Xcheck = Xarray[i];
      Ycheck = Yarray[i];
}

This loop will fetch me every room co-ordinate in the arrays for checking, but I want to check every room values against every other room in the arrays! To do this I embed another for loop inside the for loop I just created.

for(int i = 0; i <= roomCount; ++i)
{
        Xcheck = Xarray[i];
        Ycheck = Yarray[i];
        for(int j = 0; j <= roomCount; ++j)
       {
                if(Xcheck == Xarray[j]+1)
               {
               This room is adjacent
               adjacentCount++;
               }
       }
}

I then introduce several if statements to check if the room's co-ordinates determined by j are adjacent, if the if statement passes then an integer variable called adjacentCount is incremented. When it comes to drawing the room I can then use another if statement such as "if(adjacentCount == 4)" then load a 4 doors prefab. adjacentCount is then reset when a new room is being checked.

I introduced these changes to my script as well as colour changes to each prefab so they are more visible and here is my result.

Red Room = 1 Door
Blue Room = 2 Doors
Green Room = 3 Doors
White Room = 4 Doors


No comments:

Post a Comment