-
Notifications
You must be signed in to change notification settings - Fork 17
How to spit
How might we implement spitting out an item, such as some gum?
We will create the gum first. The important bit is flagging it as spittable.
createItem("gum", TAKEABLE(), {
loc:"lounge",
spittable:true,
examine: "Just some gum",
pronouns:lang.pronouns.massnoun,
})
The player can put the gum in her mouth, so we need a mouth object, and it needs to be a container. We can also make it a component of the player object (which is called "me"). Here is a first go:
createItem("mouth", COMPONENT('me'), CONTAINER(), {
loc:"me",
owner:'me',
examine: "Teeth and a tongue.",
})
We need to refine that so only spittable things can go in the moth, and only one at a time. This version therefore has a "testDropIn" function. I have also added a "content" function, which will give us what is currently in there, or null if there is nothing, as this is something we will want to check a few times.
createItem("mouth", COMPONENT('me'), CONTAINER(), {
loc:"me",
owner:'me',
examine: "Teeth and a tongue.",
content:function() {
const contents = this.getContents()
return contents.length > 0 ? contents[0] : null
},
testDropIn:function(options) {
if (!options.item.spittable) {
return falsemsg("{nv:char:cannot:true} put {nm:item:the} in your mouth.", options)
}
options.otherItem = this.content()
if (options.otherItem) {
options.otherItem = contents[0]
return falsemsg("{nv:char:have:true} {nm:otherItem:the} in {pa:char} mouth already.", options)
}
return true
},
})
We then need to update the inventory command. How do we want to do that? Do we want to have a bit tagged on the end if there is something in the players mouth? If so, we can just modify the existing script.
findCmd('Inv').script = function() {
const listOfOjects = player.getContents(world.INVENTORY)
msg(lang.inventory_prefix + " " + formatList(listOfOjects, {article:INDEFINITE, lastSep:lang.list_and, modified:true, nothing:lang.list_nothing, loc:player.name}) + ".", {char:player})
const options = {item:this.content(), char:player}
if (options.item) msg("{nv:char:have:true} {nm:item:a} in {pa:char} mouth.", options)
return settings.lookCountsAsTurn ? world.SUCCESS : world.SUCCESS_NO_TURNSCRIPTS
}
Finally, we need a spit command. Note that a Regex is "non-greedy" as there is a question mark after the plus sign. This means the bare minimum will be matched and stops the OUT getting added to the item name.
In the objects list, "attName" is used to tell the parser to give priority to items that are flagged as spittable.
In the script, we just check the item is in the mouth, and if so, proceed to spit.
new Cmd('Spit', {
regex:/^spit(?: out|) (.+?)(?: out|)$/,
objects:[
{scope:parser.isPresent, attName:"spittable"}
],
script:function(objects) {
const item = objects[0][0]
if (item.loc !== 'mouth') return failedmsg("You can't spit something that is not in your mouth")
msg("You spit out {nm:item:the}.", {item:item})
item.loc = player.loc
return world.SUCCESS
},
})
An alternative approach would be to have a SPITTABLE template. We could then handle most of the command in there. We can also combine it with the TAKEABLE template, as everything that the player can spit can also be taken - and there should be no conflicts with other templates that also combine with TAKEABLE.
Here is the template:
const SPITTABLE = function() {
const res = Object.assign({}, TAKEABLE_DICTIONARY)
res.spittable = true
res.spit = function(options) {
if (this.loc !== 'mouth') return failedmsg("You can't spit something that is not in your mouth")
msg("{nv:char:spit:true} out {nm:item:the}.", options)
this.loc = player.loc
return true
}
return res
}
Our command is not just this:
new Cmd('Spit', {
regex:/^spit(?: out|) (.+?)(?: out|)$/,
objects:[
{scope:parser.isPresent, attName:"spittable"}
],
defmsg:"That's not something you can spit.",
})
And the gum is flagged as spittable like this:
createItem("gum", SPITTABLE(), {
loc:"lounge",
examine: "Just some gum",
pronouns:lang.pronouns.massnoun,
})
What if you want your NPCs to be spitting gum? You would have to give every NPC their own mouth object.
Or we could just have one mouth object and fake it...
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
respondfunction - 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
- Vari-verbs (for verbs that are almost synonyms)
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
- Construction
- 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
- TALK and DISCUSS
- Following an agenda
- Reactions
- Giving
- Followers
- Visibility
- 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!)
- Spawning Monsters and Items)
- Systema Naturae
- Who, When and How NPCs Attack
- Attributes for characters
- Attacking and guarding
- Communicating monsters
- Skills and Spells
- Limiting Magic
- Effects
- The Attack Object
- [Extra utility functions](https://github.com/ThePix/QuestJS/wiki/RPG-Library-%E2%80%90-Extra Functions)
- Randomly Generated Dungeon
- 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)
- How to spit
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
- Create rooms on the fly
- Handling weather
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