Class: Game
Defined in: adventure/Game.js, line 6
Public Constructor:
var MyGame = new adventurejs.Game( "MyGame", "MyGameDisplay" )
Game is used to construct a new game instance. This instance contains the entire game and all of its methods. If you open the browser inspector window, go to the console, and type "MyGame" (or whatever game_name you have provided), you can see the properties scoped to the Game object. Every object in the game is given a reference back to the main Game object. This means that you can run multiple games in a single HTML page.
Example:
A minimal game defines at least one Room and one Player Character. If you don't provide either of these, one will be created automatically.
var MyGame = new adventurejs.Game( "MyGame", "MyGameDisplay" )
.set({
title: "Welcome Game",
author: "Ivan Cockrum",
description: "This is my great game! Thanks for playing!",
version: "0.0.1",
});
MyGame.createAsset({
class: "Room",
name: "Welcome Room",
descriptions:{ look: "You've entered the Welcome Room. " },
});
MyGame.createAsset({
class: "Player",
name: "Guest",
is: { active: true, },
place: { in: "Welcome Room" },
});
Private Constructor:
var foo = new adventurejs.Game(game_nameopt, displayElIdopt)
Parameters:
-
game_name
String <optional>
Default value: ""
A string that matches the name of the var used to store the new Game instance. Used to give the game a reference back to itself, relative to the browser window. If no name is provided, the instance name will default to "MyGame". The chief reason you might want to use a custom game_name is if you want to run multiple instance of adventure.js on a single page; give them distinct names to prevent them from colliding. You also may want to refer to your Game instance if you write any custom javascript. -
displayElId
String <optional>
Default value: ""
A string that matches the ID of an existing DOM element into which to insert the game Display. If no ID is provided, a DOM element will be created with an ID of "MyGameDisplay" and class of "game_display", and appended to .
Index
Methods:
- $directions
- $exits
- appendToOutput
- appendToOutput
- arrayToLowerCase
- clearInput
- combineVerbs
- convertTemperature
- createAsset
- createCompass
- createGlobalCeilings
- createGlobalExits
- createGlobalFloors
- createGlobalScenery
- createGlobalWalls
- createImageDock
- createImageLookup
- createInventoryDock
- createVerb
- createVerbDock
- diff
- disableAllVerbsBut
- disableVerbs
- disableVerbs
- enableVerbs
- getAsset
- getCurrentRoom
- getCurrentRoomExits
- getCurrentRoomFloor
- getCustomSuccessMessage
- getExitFromDirection
- getExtendedExitName
- getGlobalAssetDescription
- getInput
- getLastTurn
- getPlayer
- getPrintableObjectList
- getStringLookup
- getStringOrArrayOrFunction
- getSubstanceContainers
- getWordCount
- isFalseOrNull
- isIdInMixedArray
- listCurrentRoomContents
- log
- patchVerb
- play
- prependToOutput
- printCurrentRoom
- printCurrentRoomContents
- printCurrentRoomExits
- printInput
- printPlayerInventory
- printRoomZoneEvents
- printWithInput
- propercase
- registerInterval
- replaceVerb
- sendToInput
- sendToParser
- set
- setGlobalDescriptions
- setPlayerRoom
- stringToArray
- substituteCustomTemplates
- substituteHTMLTags
- tryTravel
- unregisterInterval
- unregisterInterval
- unregisterInterval
- updateDisplayCompasses
- updateDisplayInventory
- updateDisplayRoom
- updateDisplayVerbs
- validateClassList
- worldClone
- worldConstructAsset
- worldCreateDeferredAssets
- worldDiff
- worldInitializeAssets
- worldMerge
- worldRestore
- worldSave
- worldSortLookupValues
- worldValidateAssets
- worldValidatePrecursor
Properties:
Methods Collapse all |
Defined in: adventure/game/_directions.js, line 9
Returns:
Object
Defined in: adventure/game/_exits.js, line 9
Returns:
Object
Defined in: adventure/Game.js, line 590
Parameters:
-
msg
String
Returns:
boolean
Defined in: adventure/Game.js, line 605
Parameters:
-
msg
String
Returns:
boolean
Defined in: adventure/static/arrayToLowerCase.js, line 4
Parameters:
-
array
Array
Returns:
Array
Defined in: adventure/Game.js, line 718
Parameters:
-
verbs
object -
intoVerb
object
Returns:
object
Defined in: adventure/static/convertTemperature.js, line 4
Todos: Finish writing this.
Parameters:
-
temperature
Number | String
In Celsius. -
parent_id
String
The ID of the game object the temperature applies to.
Returns:
Boolean
Defined in: adventure/game/createAsset.js, line 8
Parameters:
-
asset
Object
A generic object with properties to copy into a new classed object.
MyGame.createAsset({ "class":"foo", "name":"bar", [...] })
createAsset() is a constructor function available to authors, for creating all instances of Asset and its subclasses, which includes most of the objects you'd put in a game world.
The function takes one parameter: a generic object containing properties to be passed into a class instance. For most asset classes, the only required properties are 'class' and 'name'. You'd likely also want to specify 'location', though that's not a required property. Otherwise, an author only needs to define the properties they wish to revise away from default settings.
For example, to create a simple brass key with default settings and put it in a particular location:
MyGame.createAsset({
"class":"Key",
"name":"brass key",
place: { in: "desk drawer" },
});
At runtime, createAsset() returns a new instance of the given class, uses the given name to create an id, validates the object properties, initializes the new object, and then adds it to the game world. Failure to pass any of these steps will throw an error to the console.
For more examples of how to use createAsset, see How to Create Assets and other How To docs under Get Started.
Returns:
Object
A new Asset of the class specified in asset.class.
Defined in: adventure/game/createCompass.js, line 8
Parameters:
-
properties
Object
A generic object with properties used to construct a compass.
Returns:
Object
A new compass object.
Defined in: adventure/game/createGlobalCeilings.js, line 9
Defined in: adventure/game/createGlobalFloors.js, line 9
Defined in: adventure/game/createGlobalScenery.js, line 9
Defined in: adventure/game/createImageDock.js, line 8
Parameters:
-
properties
Object
A generic object with properties used to construct an image dock.
Returns:
Object
A new image dock object.
Defined in: adventure/game/createImageLookup.js, line 8
Parameters:
-
asset
Object
A generic object with properties to copy into the image library.
MyGame.createImageLookup({
images: [
{ id: "desert", image: "images/backgrounds/desert.jpg" },
{ id: "sea", image: "images/backgrounds/sea.jpg" },
{ id: "mountains", image: "images/backgrounds/mountains.jpg" },
{ id: "dungeon", image: "images/backgrounds/dungeon.jpg" },
{ id: "throneroom", image: "images/backgrounds/throneroom.jpg" },
],
});
The image library is accessible at game.image_lookup.
Returns:
Object
A reference to the image lookup.
Defined in: adventure/game/createInventoryDock.js, line 8
Parameters:
-
properties
Object
A generic object with properties used to construct an inventory dock.
Returns:
Object
A new inventory dock object.
Defined in: adventure/Game.js, line 690
Parameters:
-
verb
object
Returns:
object
Defined in: adventure/game/createVerbDock.js, line 8
Parameters:
-
properties
Object
A generic object with properties used to construct the verb dock.
Returns:
Object
A new verb dock object.
Defined in: adventure/static/diff.js, line 5
Parameters:
-
baseline_object
Object
Old object -
updated_object
Object
New object
Returns:
Object
Defined in: adventure/Game.js, line 664
Parameters:
-
verbs
object
Returns:
object
Defined in: adventure/Game.js, line 651
Parameters:
-
verbs
object
Returns:
object
Defined in: adventure/Game.js, line 733
Parameters:
-
disabled_verbs
object
Returns:
object
Defined in: adventure/Game.js, line 677
Parameters:
-
verbs
object
Returns:
object
Defined in: adventure/game/getAsset.js, line 9
Parameters:
-
identifier
String
Name or ID of a game object. -
params
Object
Used to check for 'prefer_substance' in cases of looking for substances in containers.
Returns:
String
An object ID.
Defined in: adventure/game/getCurrentRoom.js, line 9
Returns:
Object
Defined in: adventure/game/getCurrentRoomExits.js, line 9
Returns:
String
A formatted list of exits.
Defined in: adventure/game/getCurrentRoomFloor.js, line 9
Returns:
Object
Defined in: adventure/game/getCustomSuccessMessage.js, line 9
Parameters:
-
asset
Object
A game asset. -
verbName
String
A verb name.
Returns:
Boolean
Defined in: adventure/game/getExitFromDirection.js, line 9
Parameters:
-
direction
String
Returns:
Object
An exit.
Defined in: adventure/game/getExtendedExitName.js, line 9
Parameters:
-
exit
Object -
destination
Object
Returns:
String
Defined in: adventure/game/getGlobalAssetDescription.js, line 9
Parameters:
-
asset
Object | String
Global assets are a bit (perhaps overly) complicated. They exist as singular objects in the game world, and they can be enabled/disabled and have their descriptions overridden per room and per zone.
For example:
MyGame.world.global_air
is an assetMyGame.world.room.room_scenery.global_air
is a simple object that can be used to determine whether
a global asset is available in a given room. MyGame.world.zone.zone_scenery.global_air
is a simple object that can be used to determine whether
a global asset is available in a given zone. Returns:
String
A description of the specified asset id.
Defined in: adventure/game/getInput.js, line 9
Returns:
Object
Defined in: adventure/game/getLastTurn.js, line 9
Returns:
Object
Defined in: adventure/game/getPlayer.js, line 9
Returns:
Object
Defined in: adventure/game/getPrintableObjectList.js, line 9
Parameters:
-
objects
Object
An array of object ID strings. -
exclusions
Object
An array of of object ID strings to exclude.
Returns:
String
A formatted string.
Defined in: adventure/Game.js, line 638
Parameters:
-
verbs
object
Returns:
object
Defined in: adventure/game/getStringOrArrayOrFunction.js, line 9
Parameters:
-
obj
String | Array | function | Boolean | null
Can be string or array or function. -
scope
Object
Optional reference to set scope to an object. -
params
Object
Optional params to pass to a function.
Get string or array or function. Because Javascript is untyped, we can pass any kind of value in a variable. We take advantage of that here to provide flexibility to authors in properties that print a string back to the player.
- Strings will be printed as is.
- Arrays can be set to provide a string from a randomized index, or a sequential index that increments each time it's called.
- Functions can use their own internal logic to return a string, allowing for dynamic state-based descriptions, such as whether an Asset is open or closed.
Example:
MyGame.createAsset({
class: "Desk",
name: "desk",
descriptions: { look: "An old school wooden desk. ", },
});
MyGame.createAsset({
class: "Drawer",
name: "drawer",
descriptions: {
look: function(){
return "The drawer is $( drawer is open or closed ). ";
},
},
});
MyGame.createAsset({
class: "Blotter",
name: "blotter",
descriptions: {
look: [
{randomize: true},
"The words 'live and let die' are scrawled on the blotter. ",
"The desk blotter has 'born to bleed' carved into it. ",
"You see 'zep rulez!' scratched in to the desk blotter. ",
],
},
});
For more information, see How to Use String|Array|Function.
Properties that call getStringOrArrayOrFunction
- verb subscription with_success: asset.get[in]direct_object_of_verb[verb].with_success
- verb subscription then_destroy: asset.get[in]direct_object_of_verb[verb].then_destroy
- all descriptions: asset.description and asset.descriptions[any]
- room events: room.room_events
- zone events: room.zone.zone_events
- custom vars: MyGame.world._vars[ property ]
- constraint message: character.constrained_msg
Returns:
String
Defined in: adventure/game/getSubstanceContainers.js, line 9
Parameters:
-
asset_id
String -
selects
Array
Returns:
Array
Defined in: adventure/game/getWordCount.js, line 8
getWordCount() is a utility method to get a count of all the unique words the game "knows".
Returns:
Array
Defined in: adventure/static/isFalseOrNull.js, line 4
Parameters:
-
value
*
Returns:
Boolean
Defined in: adventure/static/isIdInMixedArray.js, line 5
Parameters:
-
array
Array
- convert ' ' to '_'
- convert ' and ' to '&'
- convert '.' to '$'
Returns:
Array
Defined in: adventure/game/listRoomContents.js, line 9
Parameters:
-
room
Object
Returns:
String
Defined in: adventure/game/log.js, line 8
Parameters:
-
method
String | Number
0=log, 1=warn, 2=error -
level
String | Number
0=critical, 1=high, 2=medium, 3=low -
msg
*
Message for console.
Defined in: adventure/Game.js, line 747
Parameters:
-
patch_verb
object
plug.phrase1
and in your game file, call patchVerb with
line visible: true
removed.
MyGame.patchVerb({
name: "plug",
phrase1:{
accepts_noun: true,
requires_noun: true,
accepts_preposition: true,
noun_must_be:
{
known: true,
tangible: true,
present: true,
visible: true,
reachable: true,
},
},
});
( game.patchVerb() actually forwards to game.dictionary.patchVerb() ).
Returns:
object
Defined in: adventure/game/play.js, line 9
Returns:
adventurejs.GameDefined in: adventure/Game.js, line 620
Parameters:
-
msg
String
Returns:
boolean
Defined in: adventure/game/print.js, line 8
Parameters:
-
msg
String -
classes
String
Defined in: adventure/game/printCurrentRoom.js, line 9
Todos: Formalize handling for description/brief/verbose descriptions
Parameters:
-
params
Object
For example:
printCurrentRoom( { verbose: true } )
Defined in: adventure/game/printCurrentRoomContents.js, line 9
Defined in: adventure/game/printCurrentRoomExits.js, line 9
Defined in: adventure/game/printPlayerInventory.js, line 9
Defined in: adventure/game/printRoomZoneEvents.js, line 9
Defined in: adventure/game/printWithInput.js, line 9
Parameters:
-
msg
String -
classes
String
Defined in: adventure/static/propercase.js, line 5
Parameters:
-
string
String
Returns:
String
Defined in: adventure/Game.js, line 787
Todos: add number of turns as a param and only call per x turn
Parameters:
-
id
string
id of Asset which owns the function. -
callback
string
The function to call on interval.
Defined in: adventure/Game.js, line 703
Parameters:
-
oldVerb
object -
newVerb
object
Returns:
object
Defined in: adventure/Game.js, line 252
Defined in: adventure/game/set.js, line 9
Parameters:
-
props
Object
A generic object containing properties to copy to the Game instance.
Defined in: adventure/game/setGlobalDescriptions.js, line 9
Parameters:
-
globals
Object
A list of globle Scenery Assets.
Defined in: adventure/game/setPlayerRoom.js, line 9
Parameters:
-
newRoom
Object
A room object to which to move player. -
params
Object
Defined in: adventure/static/stringToArray.js, line 4
Parameters:
-
string
String
Returns:
Array
Defined in: adventure/game/substituteCustomTemplates.js, line 8
Todos: update classdesc
Parameters:
-
msg
String
A string on which to perform substitutions.
For example:
descriptions: { look: "The drawer is $( drawer is open or closed )." }
This method is similar to Javascript ES6 template literals but with important distinctions.
There are several types of valid substitutions:
$( author_variables )
refers to author-created custom variables
that are stored within the game scope so that they
can be written out to saved game files. (See
How to Use Custom Vars
for more info.)
$( asset is state or unstate )
allows authors to refer to a game asset by name or id
and print certain verb states. Names are serialized
during the substitution process, meaning that, for example:
$( brown jar is open or closed )
will be interpreted to check for
MyGame.world.brown_jar.is.closed
.$(tag|text)
is a
shortcut to <span class="tag">text</span>,
to make it easier to add custom CSS styles to text.
Adventurejs custom templates can be mixed & matched with template literals. Custom templates can be used in any string that outputs to the game display. However, because template literals in strings are evaluated when the properties containing them are created, they will cause errors on game startup. In order to use native Javascript template literals, they must be returned by functions. MyGame.createAsset({ class: "Room", name: "Standing Room", descriptions: { brief: "The north door is $(north is open or closed). ", through: "Through the door you see a $(northcolor) light. ", verbose: return function(){ `The north door ${MyGame.world.aurora_door.is.closed ? "is closed, hiding the aurora. " : "is open, revealing the $(northcolor) aurora light" }` } } })
Returns:
String
Defined in: adventure/game/substituteHTMLTags.js, line 8
Parameters:
-
msg
String
A string on which to perform substitutions.
For example:
MyGame.createAsset({ class: "Room", name: "Standing Room", descriptions: { brief: "The north door is
Returns:
String
Defined in: adventure/game/tryTravel.js, line 9
Parameters:
-
direction
String -
params
Object
Defined in: adventure/Game.js, line 808
Parameters:
-
id
string
id of Asset which owns the function. -
callback
string
The function to call on interval.
Defined in: adventure/Game.js, line 836
Defined in: adventure/Game.js, line 886
Defined in: adventure/Game.js, line 364
Defined in: adventure/Game.js, line 392
Defined in: adventure/Game.js, line 330
Defined in: adventure/Game.js, line 381
Todos: this has no logic yet
Defined in: adventure/static/validateClassList.js, line 4
Todos: Is this unused?
Parameters:
-
property
String | Array
Returns:
Array
Defined in: adventure/game/worldClone.js, line 9
Parameters:
-
source_world
Object
Returns:
Object
Returns a copy of the source world.
Defined in: adventure/game/worldConstructAsset.js, line 9
Parameters:
-
source
Object
A generic object containing properties to copy to a game object.
Returns:
Object
Returns an instance of whatever class was defined.
Defined in: adventure/game/worldCreateDeferredAssets.js, line 9
Objects created here also need to be validated, since they were added to the deferred list during the initial validation pass and therefore missed validation.
Defined in: adventure/game/worldDiff.js, line 9
Parameters:
-
source_world
Object
Returns:
String
Returns a stringified diff.
Defined in: adventure/game/worldInitializeAssets.js, line 9
Defined in: adventure/game/worldMerge.js, line 8
Parameters:
-
source
Object -
dest
Object
Returns:
Object
dest
Defined in: adventure/game/worldRestore.js, line 9
Parameters:
-
saved_world
Object
Defined in: adventure/game/worldSave.js, line 8
Parameters:
-
world
Object
Defined in: adventure/game/worldSortLookupValues.js, line 9
Defined in: adventure/game/worldValidateAssets.js, line 9
Defined in: adventure/game/worldValidatePrecursor.js, line 9
Parameters:
-
child
Object
A generic object.
Returns:
Object
A classed object.
Properties Collapse all |
Defined in: adventure/Game.js, line 310
Default value: ""
Defined in: adventure/Game.js, line 162
class_lookup
stores a lookup table
containing the ids of all assets by class.
Defined in: adventure/Game.js, line 534
- Use game.getCurrentRoom() instead.
Defined in: adventure/Game.js, line 79
Default value: {}
Defined in: adventure/Game.js, line 89
Defined in: adventure/Game.js, line 176
image_lookup
stores a lookup table
for optional images supplied by author.
Defined in: adventure/Game.js, line 547
- Use game.getInput() instead.
Defined in: adventure/Game.js, line 235
inventorydocks
is an array that
stores references to any custom inventorydocks.
Defined in: adventure/Game.js, line 560
- Use game.getLastTurn() instead.
Defined in: adventure/Game.js, line 521
- Use game.getPlayer() instead.
Defined in: adventure/Game.js, line 169
room_lookup
stores an array
containing the ids of all rooms.
Defined in: adventure/Game.js, line 425
Default value: ""
Defined in: adventure/Game.js, line 213
scorecard
is a singleton class
that stores all the game's score events
and functions for managing score.
Defined in: adventure/Game.js, line 110
settings
is a singleton class
that stores all the game's global settings.
Defined in: adventure/Game.js, line 289
Default value: ""
Defined in: adventure/Game.js, line 442
Default value: ""
Defined in: adventure/Game.js, line 121
world
stores all the game asset
objects. It also stores a handful of variables
such as _player and _currentRoom which are used
for convenience. It also stores any global vars
created by the game's author. This last is done
to give authors a place to store vars that can
be written out with saved games.
Defined in: adventure/Game.js, line 133
world._vars
is a namespace for
authors to store any custom vars that they
would like to be written out with saved games,
so that they can be restored along with other
saved game data in order to retain game state.
Defined in: adventure/Game.js, line 155
world_lookup
stores a lookup table
containing indexed keywords for all game assets.