Tutorial 2 ‐ Rooms and exits - ThePix/QuestJS GitHub Wiki

...Continued from First steps.

A room is the same as a location; it is not necessarily a chamber in a building. I tend to use the words interchangeably.

Room descriptions

If you look back at the room, you will see it has a "desc" attribute; the description the user sees when entering the room.

In QuestJS terminology, the "user" is the person sat at a computer playing the game, whilst the "player" is the person in the game world that you user controls.

As with the "examine" attribute of the player, this can be a string or a function.

A second room

Add another room to the "data.js" file:

createRoom("kitchen", {
  desc:'A clean room.',
  west:new Exit("lounge"),
  afterFirstEnter:function() {
    msg("A fresh smell here!")
  },
})

Quest 6 has a msg function for printing, just like Quest 5. The "function" keyword introduces the next bit as code, like a script in Quest 5.

We have an exit, heading to the lounge, which is a new object of the type Exit. We also need to create an exit going the other way:

createRoom("lounge", {
  east:new Exit("kitchen"),
  desc:"A smelly room with an old settee and a tv.",
})

Now if you reload the page, you can move from one room to the other.

Scripts

You may have noticed that the kitchen has a script (or a function in the usual JavaScript terminology) that fires when the player first enters the room. It was defined like this

  afterFirstEnter:function() {
    msg("A fresh smell here!")
  },

Again, this is a name-value pair in the dictionary, with the name "afterFirstEnter", followed by a colon, as usual. Then we have the "function" keyword, followed by the parameters in curved brackets. There are no parameters in this case, but we still need the brackets. The code itself goes inside the curly braces, and has a single line - the msg function that is used to print some text.

There are five attributes for rooms that can be given scripts that will run as the player moves about:

  afterExit
  beforeEnter
  beforeFirstEnter
  afterEnter
  afterFirstEnter

A Third Room, Using a Link

Let us create a garage:

createRoom("garage", {
  desc:'An empty garage.',
})

We now want an exit from the kitchen to the garage. Most of the time you will be adding exits that go both ways, so Quest has a shortcut to handle that. Set it up as a Link in one room, and Quest will created the return exit for you.

createRoom("kitchen", {
  desc:'A clean room.',
  west:new Exit("lounge"),
  north:new Link("garage"),
  afterEnterFirst:function() {
    msg("A fresh smell here!")
  }
})

Note that the return exit can only be a simple exit and must be in the opposite direction. If you want to do anything else, just use an Exit for each direction.

Locking the Door

We will have this new door locked, to illustrate how that is done. Exits are not saved when the user saves the game, so we do not set the exit itself to be locked, but instead a flag on the room. Add this extra line:

createRoom("kitchen", {
  desc:'A clean room.',
  west:new Exit("lounge"),
  north:new Link("garage"),
  afterEnterFirst:function() {
    msg("A fresh smell here!")
  }
  exit_locked_north:true,
})

The exit will sometimes be locked and sometimes unlocked. For now, just try changing the "locked" attribute between true and false to confirm it works - we will look at unlocking it in the game later.

Continue to items...