Doors!!!

lildude2022002 I have looked at all the tutorials...believe me. I just can't figure it out. The one tutorial is confusing because its rooms are different. Could someone please post the code for putting in a door and using it in the first room. I also would like to know how to get from room 3 back to room 2 and my "ego" coming through the door.

Thanks!
Joel Let me see what I can do with this. Implementing doors isn't particularly difficult, although it can be pretty tedious. Obviously, you'll need your picture and your door view drawn. I'm going to talk through the steps needed to use the door, then show the code afterward. Of course, whenever I use something like "o1" or "f200" below, you need to make sure that you replace it with something that doesn't cause any conflicts in your own program. The first thing you'll need to actually make the door work is to actually have an object to work with.

#define oDoor o1

Then, in your "new room" section, you need to do all the initialization stuff for a door. The first thing you need to do is  make sure your door's view is loaded. Do this by calling load.view() with whatever the view number of the door is (load.view(5), for example). Then activate the door object with a call to animate.obj(oDoor); Set the view and loop for the door (taken directly from the View resource...if you need help on this, ask for further clarification...i'm going to assume the door's animation is stored in view 5, loop 2). This is done with calls to set.view() and set.loop(). It's probably a good idea to call ignore.objs() and ignore.blocks() for the door, otherwise you might have problems trying to position it. You'll also want to call stop.cycling() so that the door doesn't sit there opening over and over again. The code for all that is this:

load.view(5);
animate.obj(oDoor);
set.view(oDoor, 5);
set.loop(oDoor, 2);
ignore.objs(oDoor);
ignore.blocks(oDoor);
stop.cycling(oDoor);

After you do that, you need to position the door in the room. You might have to experiment a little to get the position right. I suggest you pay attention to where you think you should put the door by looking for the x and y coordinates with PicEdit. Position the door with a call to position. I'm going to assume the door will go at (50, 130).  Then set the priority of the door to 4 so that it doesn't get drawn over anything. It should be considered part of the background until it's interacted with (of course, you can ignore this if you WANT the door to get drawn over stuff sometimes). Then you need to draw the door on the screen with a call to draw().  The code for this stuff is this:

position(oDoor, 50, 130);
set.priority(oDoor, 4);
draw(oDoor);

To let ego interact with the door, you'll need some additional flags. Place these at the beginning of the file:

#define fDoorDoneClosing f200
#define fEgoThroughDoor f201

In the new room section, put the following code.

reset(fDoorDoneClosing);
reset(fEgoThroughDoor);

To let ego open a door, you need to determine a rectangle where it's acceptable for ego to open the door. This you'll have to do yourself. I can't tell you what the rectangle will be. For this example I'll use a rectangle with its top-left at (45, 130) and its bottom-right at (55, 140). That's not meant to be a guideline...it's just a rectangle. If the player types "open door" and ego is in this rectangle, you want a new room to come up...if not, you want the game to say "You're not close enough." The code for this would go in your input parsing section, and would look like:

if (said("open", "door"))
{
   if (posn(ego, 45, 130, 55, 140))
   {
       new.room(3);
   }
   else
   {
       print("You\'re not close enough.");
   }
}

I'll come back and describe walking through the door when coming back from room 3 later, or someone else can pick it up. It involves the two flags that were defined. You want to position ego inside the door frame, tell it to ignore.objs() and move.obj() on ego so that it's walking outside of the door. You also want to set the door's cel to the last cel in its animation and do a reverse.loop() after ego has walked out of the door.
Joel Okay, moving ego through the door.

In the new room section you'll have to have code that looks something like the following:

if (prev_room_no == 3)
{
   stop.motion(ego);
   program.control();
   prevent.input();
   set.cel(oDoor, 5);
   position(ego, 45, 120);
   ignore.objs(ego);
   ignore.blocks(ego);
   move.obj(ego, 50, 120, 1, fEgoThroughDoor);
}

What that does is keeps the player from moving ego, sets the current cel of the door object to the last cel in its loop (so that the door is standing open), positions ego inside the door frame, and then tells it to walk 5 pixels to the right. Obviously, this code would be for a door that's on the left side of the room. You'll have to experiment to find out what exact coordinates work.

Outside of the new room section, you'll need an if statement to test when ego has finished walking through the door.

if (fEgoThroughDoor)
{
   reverse.loop(oDoor, fDoorDoneClosing);
}

That code basically tells the door to close, assuming that the normal order of the loop is the door opening. If you drew a loop where the door is closing, then you want to ignore the set.cel() thing above and use end.of.loop(). You'll need another if statement to test when the door has finished closing.

if (fDoorDoneClosing)
{
   observe.objs(ego);
   observe.blocks(ego);
   player.control();
   accept.input();
}

That gives control back to the player. And that's all.