Tuesday 12 March 2013

Which way should I be facing!?

Aaaaaaaaaaand we're back, I've been spending most of my time doing other assignments so I've only just found some time to write a post. Last post I showed a picture of a dungeon path that recognised how many rooms were adjacent to it. Thats all good but how do we utilise the information about adjacent rooms and use it to line up and remove unecessary doors?

Well firstly I used some mathematical methods I learnt in GCSE! Binary Logic and Pascal's Triangle. I know there are 4 different relative door positions:

North, South, East, West.

4^2 tells us the number of combinations there are which is 16. If I include the option of 0 adjacent doors as a 0 value that makes 5 options, the 5th row of pascals triangle tells me the distribution of possibilities:

1 4 6 4 1

Now let's check if my GCSE maths was actually useful by doing some logic. Each bit in these calculations represent a possible room or door position.

nsew
0000
0001 w
0010 e
0011 ew
0100 s
0101 sw
0110 se
0111 swe
1000 n
1001 nw
1010 ne
1011 new
1100 ns
1101 nsw
1110 nse
1111 nsew

Posibilities :

0 Doors/Rooms : 1
1 Door/Rooms : 4
2 Doors/Rooms : 6
3 Doors/Rooms: 4
4 Doors/Rooms : 1

So now I know the properties of each room, but how do I program my script to utilise this? Well my script can already figure out how many adjacent rooms there are, and thanks to Pascal and Binary Logic we know that each number of adjacent rooms have certain possibilities and we know the combinations of these possibilities, so first we need to allow our script to represent these combinations.

I created 4 new Boolean variable types; Nbool, Sbool, Ebool, and Wbool. These types represent the bits from earlier and are all initialised as false at the start of the path calculation loop. If a room is adjacent, one of these Boolean values is set to true depending on its' relative co-ordinate.

Next I'll perform a check inside the if statements from before. If for example, Nbool and Sbool are true, this would mean adjacentCount is 2.

if(adjacentCount == 2)
{
      if(Nbool == true & Sbool == true)
      {
              //Load a room with a door the North and South side.
      }
}

Using other relevant if statements I can place every room appropriately.

On the Unity side of things this became somewhat of an issue due to run time rotation transformations but eventually it lead to me defining public variables for every possible room (all 15 of them) and setting up pre-transformed prefabs for all of them. Here's what i got.


And there you have it.... wait what?!



Well it looks like Darksteel set me up with some dodgy prefabs! It's fine though I hard-coded them into the correct position. I'll get some replacements eventually but this will have to do for now. :)

Here's what i got after hard-coding the transformations.


No comments:

Post a Comment