Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
102 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Creating Virtual Worlds


Section 6 - Redefine the World

Now I will show you what true powers lie in this concept. :o)

Let's pretend you want to add 4 rooms instead of the empty space at the right lower corner, also room 2 should be only half the size it is now.

If you had build your level/world the traditional way, you would be screwed. You would have to delete room 1 and redo it, and you would have to move the door and redo room 2.

Not so with our concept. Simply add 4 cells to the lower room instead of the empty one:

$lower = cell ( name => "lower part", size => 16, div => inY(),
   cells => [ $room2 ], );                                # line 1
addCells ($lower, 4, "4,4", "room##num##" );              # line 2 

In line 1 we split it into Y, give it a name etc. You should be familiar with this already. In line 2 we use the function addCells to add 4 cells. This is a bit easier then writing the same cell definition four times. In the upcoming GUI, you would select "Split into cells" and then enter 4. First parameter is the cell to add cells to, the next is the number of cells to add. The third and fourth parameter are the names (comma separated) and the sizes (also comma separated). If you don't give enough names/sizes, the last one will be repeated until all are defined. The ##num## will be replaced later on with an actual number, you will see this frequently in my examples to generate unique names.

Whew! That's nearly all. The only thing left is to increase the world size, because otherwise each of the new rooms would be only 4 feet wide.

$bunch = worldAsBunch ( 0, 0, 0, 80, 32, 16);            # line 3

Now you may start wondering about the size of the rooms and the world, and if all this calculating isn't a bit too complicated, and how you calculate the world size, anyway. Also, we haven't yet made room 2 half the size as it should be.

Well, to make it short, the cells' sizes are not absolute sizes. They are relative sizes. Also the thing placement coordinates are relative. It all boils down to Einstein, afterall :o)

To calculate a cell's/room's actual size, all the sizes of the subcells are taken, summed up and then each of the cells' sizes is divided by this sum.

So, two cells with size 16 will be equal in size, and get exactly one half of the size of the mother cell, whatever that size may be. If they both have 4 as a size, the result will be the same, no matter how big the mother cell is. If one of the cells is 16, and the other is 32, then the second one will be always twice the size of the first cell. You can also specify (absolute) minimum and maximum values, so that a hallway will never grow, for instance, larger than 16 foot, no matter how big you make your world.

Since our room 2 currently gets 1/2 of the size of the lower part, and we want it only 1/4 (half the size it is now), we can't just set is size to 8. It would get then 8/(16+8) and thats 1/3 of the size. If you like fractions, you can set the room 2 size to 1/4, and the lower_right cell's size to 3/4 if you like. 4 and 12 work, too. Just pick the method you like.

$room2->{size} = 4;                                      # line 4

Whew! You have just resized/move almost anything in your world! Now, wasn't that easy?

Let me show you one more thing. Of course we need doors to the new rooms. Each of them should have a door on the east side. We could now add a door to each of them. But remember how I talked about inheritance? Well, just give the mother a door and all the children will inherit it. Not quite how it works in real life, though.

But one more problem remains. Since all the rooms in the lower cell are contained in one cell, room 2 will inherit the door, too. This will not look nice, since there is no room west of room 2 where the door could lead. We can do two things to overcome this. We could split the lower cell into two parts, room2 and a cell containing all the other rooms. This may become neccessary. But for now we can just give room2 the "noinherit" property and it won't inherit any things.

$mydoor->{inherit} = true();                            # line 5
$room2->{noinherit} = true();                           # line 6
$lower->{things} = [ openDoor(aEast(),$mydoor) ];       # line 7


6: 6 rooms, with a corner and a lot of doors between them

In line 5 we set the flag inherit of our door to true, so sub cells will inherit it. Also, this will avoid the mother cell getting the door, too. In line 6 we disable inheritance for room 2, so that it doesn't get the door. In line 7 we simple add the door to our lower side as the only thing.

Remember, the doors will move to the proper places if you resize the rooms!

That's all, folks!



Links, routes, etc.


Contents
  Introduction
  What you need
  How it was...
  A single room
  Two rooms
  Three rooms over the edge
  Another brick in the wall
  Doorways to Heaven
  Redefine the world
  Links, routes, etc.
  More ideas
  Afterword

  Printable version
  Discuss this article