Tutorial 5: Items and rooms again
... 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, ifw.light_switch.switchedon
is true, thenworld.LIGHT_FULL
is the result, otherwiseworld.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...
Tutorial
- First steps
- Rooms and Exits
- Items
- Templates
- Items and rooms again
- More items
- Locks
- Commands
- Complex mechanisms
- Uploading
QuestJS Basics
- General
- Settings
- Attributes for items
- Attributes for rooms
- Attributes for exits
- Naming Items and Rooms
- Restrictions, Messages and Reactions
- Creating objects on the fly
- String Functions
- Random Functions
- Array/List Functions
- The
respond
function - Other Functions
The Text Processor
Commands
- Introduction
- Basic commands (from the tutorial)
- Complex commands
- Example of creating a command (implementing SHOOT GUN AT HENRY)
- More on commands
- Shortcut for commands
- Modifying existing commands
- Custom parser types
- Note on command results
- Meta-Commands
- Neutral language (including alternatives to "you")
- The parser
- Command matching
Templates for Items
- Introduction
- Takeable
- Openable
- Container and surface
- Locks and keys
- Wearable
- Furniture
- Button and Switch
- Readable
- Edible
- Vessel (handling liquids)
- Components
- Countable
- Consultable
- Rope
- Backscene (walls, etc.)
- Merchandise (including how to create a shop)
- Shiftable (can be pushed from one room to another)
See also:
- Custom templates (and alternatives)
Handing NPCs
- Introduction
- Attributes
- Allowing the player to give commands
- Conversations
- Simple TALK TO
- SAY
- ASK and TELL
- Dynamic conversations with TALK TO
- Following an agenda
- Reactions
- Giving
- Followers
- Changing the player point-of-view
The User Experience (UI)
The main screen
- Basics
- Printing Text Functions
- Special Text Effects
- Output effects (including pausing)
- Hyperlinks
- User Input
The Side Panes
Multi-media (sounds, images, maps, etc.)
- Images
- Sounds
- Youtube Video (Contribution by KV)
- Adding a map
- Node-based maps
- Image-based maps
- Hex maps
- Adding a playing board
- Roulette!... in a grid
Dialogue boxes
- Character Creation
- Other example dialogs [See also "User Input"]
Other Elements
- Toolbar (status bar across the top)
- Custom UI Elements
Role-playing Games
- Introduction
- Getting started
- Items
- Characters (and Monsters!)
- Attributes for characters
- Attacking and guarding
- Skills and Spells
- Limiting Magic
- Effects
- The Attack Object
- Quests for Quest
- User Interface
Web Basics
- HTML (the basic elements of a web page)
- CSS (how to style web pages)
- SVG (scalable vector graphics)
- Colours
- JavaScript
- Regular Expressions
How-to
Time
- Events (and Turnscripts)
- Date and Time (including custom calendars)
- Timed Events (i.e., real time, not game time)
Items
- Phone a Friend
- Using the USE verb
- Display Verbs
- Change Listeners
- Ensembles (grouping items)
Locations
- Large, open areas
- Region,s with sky, walls, etc.
- Dynamic Room Descriptions
- Transit system (lifts/elevators, buses, trains, simple vehicles)
- Rooms split into multiple locations
Exits
- Alternative Directions (eg, port and starboard)
- Destinations, Not Directions
Meta
- Customise Help
- Provide hints
- Include Achievements
- Add comments to your code
-
End The Game (
io.finish
)
Meta: About The Whole Game
- Translate from Quest 5
- Authoring Several Games at Once
- Chaining Several Games Together
- Competition Entry
- Walk-throughs
- Unit testing
- Debugging (trouble-shooting)
Releasing Your Game
Reference
- The Language File
- List of settings
- Scope
- The Output Queue
- Security
- Implementation notes (initialisation order, data structures)
- Files
- Code guidelines
- Save/load
- UNDO
- The editor
- The Cloak of Darkness
- Versions
- Quest 6 or QuestJS
- The other Folders
- Choose your own adventure