Tutorial 5 ‐ Items and rooms again - ThePix/QuestJS GitHub Wiki

... Continued from Templates.

So we will now look at how to create a more complex system. We will add a trapdoor to the kitchen; when it is open the player will be able to go down into the basement. We will say the basement is dark, and the player needs a torch to turn on the light down there.

With a complex system, it is best to work in steps, and get each bit working at each step. And it is often easiest to work in reverse order, making sure the last bit works first. We will start with the light in the basement.

The basement and switch look like this:

createRoom("basement", {
  desc:"A dank room, with piles of crates everywhere.",
  darkDesc:"It is dark, but you can just see the outline of the trapdoor above you.",
  up:new Exit('kitchen'),
  lightSource:function() {
    return w.light_switch.switchedon ? world.LIGHT_FULL : world.LIGHT_NONE;
  },
})

createItem("light_switch", SWITCHABLE(true), { 
  loc:"basement",
  alias:"light switch",
  examine:"A switch, presumably for the light.",
})

JavaScript has a conditional operator denoted by a question mark and a colon. If the condition before the question mark is true, the value left on the colon is returned, otherwise the value on the right is. In the above code, if w.light_switch.switchedon is true, then world.LIGHT_FULL is the result, otherwise world.LIGHT_NONE is the result.

You also need an exit to the kitchen: down:new Exit('basement'), (if you are not sure how, we will get to that later).

The light switch gets the SWITCHABLE template, and is sent true so the switch will be on at the start, so we can see if it works. It is otherwise pretty simple.

The basement has a "lightSource" function. By default a room's "lightSource" function just returns world.LIGHT_FULL - the room is lit. For the basement, it returns world.LIGHT_FULL if the switch is on, and world.LIGHT_NONE otherwise (these are integer constants, which allows for a range of lighting levels, but currently only two states are available).

There is also a "darkDesc", a description that will get used when the room is dark.

You should be able to go into the basement, take a look around, and then turn off the light, and it will be dark... At which point you are stuck, because you cannot see the exit.

We need to update the basement, giving the exit a dictionary so we can flag that the exit is "illuminated".

createRoom("basement", {
  desc:"A dank room, with piles of crates everywhere.",
  darkDesc:"It is dark, but you can just see the outline of the trapdoor above you.",
  up:new Exit('kitchen', {
    illuminated:true,
  }),
  lightSource:function() {
    return w.light_switch.switchedon ? world.LIGHT_FULL : world.LIGHT_NONE;
  },
})

We also need an exit down from the kitchen to the basement, with a trapdoor object.

createRoom("kitchen", {
  desc:'A clean room, a clock hanging on the wall. There is a sink in the corner.',
  west:new Exit("lounge"),
  down:new Exit('basement', {
    isHidden:function() { return w.trapdoor.closed; },
  }),
  north:new Exit("garage"),
  afterFirstEnter:function() {
    msg("A fresh smell here!");
  },
})

createItem("trapdoor", OPENABLE(false), {
  loc:"kitchen",
  examine:"A small trapdoor in the floor.",
})

The trapdoor is very simple, it just uses the OPENABLE template. We want it to start closed, so send if false.

The new bit for the kitchen is the exit. As before, we want to do something special with the exit, so we give the Exit object its own dictionary. In this case, we will link it to the trapdoor, so it is hidden when the trapdoor is closed.

Lastly, we need to set the basement to be dark from the start, which is done by setting the parameter for the SWITCHABLE temple to false:

createItem("light_switch", SWITCHABLE(false), { 

In the next section we will implement a torch.

Continues with More items...