-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
127 lines (111 loc) · 4.16 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
// Get NodeJS items
const util = require('util');
const color = require('cli-color');
// Get game items
const GAMEDATA = require('./game.json');
const { Engine } = require('./engine');
// Setup text-interface for user input
const input = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// Create the Game object
const game = new Engine(GAMEDATA);
// Clear console
process.stdout.write(color.erase.screen);
// Print out the title and author
process.title = game.title;
console.log("\n " + color.bold.whiteBright(game.title));
console.log(color.italic(" By: " + game.author) + " \n");
let dashes = "";
for (let d = 0; d < game.title.length - 2; d++) {
dashes += "-";
}
console.log(" " + dashes + " \n");
// Give the context of the room.
if (typeof game.currentRoom.context === "string") {
console.log(color.cyanBright(' ? ') + game.currentRoom.context);
} else {
for (let i = 0; i < game.currentRoom.context.length; i++) {
console.log(color.cyanBright(' ? ') + game.currentRoom.context[i]);
}
}
// The game loop
let update = () => {
// Ask what the player wants to do.
input.question("\n" + color.whiteBright(" > "), (res) => {
// receive message and chop it up
let message = res.split(" ");
let output = "\n ";
// decide what the player wants to do
if (message[0] !== '') {
switch (message[0]) {
case "search": // look for something
case "where":
case "look":
for (let i = 1; i < message.length; i++) {
switch (message[i]) {
case "exit":
output += game.player.look(game.currentRoom, message[i]);
break;
case "items":
output += game.player.look(game.currentRoom, 'items');
break;
case "around":
output += game.player.look(game.currentRoom, 'around');
break;
}
}
break;
case "pickup": // pickup an item
if (message.length >= 2) {
let item = game.player.pickup(game.currentRoom, message[1]);
output += "You picked up a " + item.name;
}
break;
case "quit":
process.exit();
break;
case "goto":
if (message.length >= 2) {
game.goto(message[1]);
} else {
output += "Please enter a direction.";
}
break;
case "debug":
switch (message[1]) {
case "items":
if (game.player.inventory.length > 0) {
output += "You have: ";
for (let i = 0; i < game.player.inventory.length; i++) {
if (i === game.player.inventory.length - 1) {
output += game.player.inventory[i].name;
} else {
output += game.player.inventory[i].name + ", ";
}
}
} else {
output += "Nothing";
}
break;
default:
output += "That is not a command.";
}
break;
}
// print the output
if (output) {
console.log(output.padStart(4));
}
update();
} else {
// print a default output
console.log("You stare at " + game.currentRoom.location);
update();
}
});
};
// Start up the game
update();