A lightweight (~50kb minified) JavaScript library to create text adventures / interactive fiction with flexible player interactions.
https://github.com/jddunn/text-rpg-engine
npm install text-rpg-engine
npm run build
to produce a new production version of the library.
main.js
file, you will have to transpile the code again with Browserify. For example, if you make any changes for the example game, you will have to run this command inside the directory:
browserify -t brfs main.js -o bundle.js
const game = require('text-rpg-engine');
// Below code uses library API to programmatically build games
// Add a room (by default will be beginning room since it was first added)
const startRoom = game.addRoom('Beginning', 'This is the beginning room');
// Add a second room (by default will be winning room since it was added last)
const endRoom = game.addRoom('SecondRoom', 'You did it! You won!');
// Or we could do this, to manually set which rooms to start / end
// game.startRoom = 'Beginning'; // Set beginning room programatically
// game.endRoom = 'SecondRoom'; // Set end room programatically
// Add required item to room
endRoom.requirements.push('accessKey');
startRoom.addPrompt(
// name of prompt (required)
'go right',
// keywords that will activate prompt (required)
['go right', 'move right', 'open right', 'enter right', 'door right', 'right door'],
// results of prompt
{
// successful prompt result text (required)
'successText': 'You enter in the access code "14052" and successfully open the door.',
// failed prompt result text (optional; a default fail text is displayed when a prompt fails)
'failText': 'The door is locked with an access code!',
// room to enter as result of prompt (optional)
'roomToEnter': 'SecondRoom',
// items added to inventory after successful prompt result (optional)
'itemsGiven': 'trophy'
},
// required items to successfully do prompt (optional)
['accessKey']
);
startRoom.addPrompt(
'look room',
['look room', 'look at room', 'search room', 'examine room', 'look in'],
{
'successText': 'You see a room with a door to the right and a statue in the middle.'
}
);
startRoom.addPrompt(
'get statue',
['get statue', 'pick up statue', 'take statue', 'pick statue'],
{
'successText': `You pick up the statue. It feels heavy in your hands, and there's something hanging off
the bottom.`,
'itemsGiven': ['statue']
}
);
startRoom.addPrompt(
'rotate statue',
['rotate statue', 'rotate the statue'],
{
'successText': 'You take the note from the bottom of the statue.',
'failText': 'You have no statue to look at!',
'itemsGiven': ['note']
},
['statue']
);
startRoom.addPrompt(
'look note',
['look at note', 'examine note', 'take note', 'get note', 'check note', 'read note', 'look note'],
{
'successText': 'You look at the note and find an access code: "14052."',
'failText': 'You have no note to look at!',
'itemsGiven': ['accessKey']
},
['statue', 'note']
);
game.init();
const game = require('text-rpg-engine');
const path = require('path');
const fs = require('fs');
// Below code loads game data from static JSON file
let data = JSON.parse(fs.readFileSync(path.join(__dirname, './example.json')));
game.loadData(data);
game.init();
// By default, the library looks for an input form element with the class `input`, and a div element with the class `display`
// for the game's input and display (these classnames can be remapped in the game's input and display properties).
// Send user input to our game (on pressing 'Enter' in the form)
document.getElementById('input').addEventListener('keypress', function (event) {
if (event.keyCode === 13) {
event.preventDefault();
game.userSend(document.getElementById('input').value);
document.getElementById('input').value = '';
}
});