-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-005.js
208 lines (196 loc) · 6.34 KB
/
generation-005.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* CHANGELOG:
* Generation 0: implement base script
* Generation 1: added basic game loop
* Generation 2: added player object and inventory system
* Generation 3: added items and ability to pick up and drop items
* Generation 4: added rooms and ability to move between rooms
* Generation 5: added enemies and combat system
*/
import { generations, evolve } from "./base.js";
import readline from "readline";
const generation = 5;
console.log(`Generation ${generation} spawned`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const player = {
name: "",
inventory: [],
location: "start",
health: 100,
attack: 10,
defense: 5,
};
const items = {
key: {
name: "key",
location: "start",
description: "A small, rusty key",
},
sword: {
name: "sword",
location: "cave",
description: "A sharp, shiny sword",
},
};
const enemies = {
dragon: {
name: "dragon",
location: "cave",
description: "A fierce dragon",
health: 100,
attack: 20,
defense: 10,
},
};
const rooms = {
start: {
name: "Start",
description: "You are in a small room with a door to the north.",
exits: {
north: "cave",
},
},
cave: {
name: "Cave",
description: "You are in a dark cave with a door to the south.",
exits: {
south: "start",
},
enemies: ["dragon"],
},
};
async function output() {
console.log("Welcome to the Text Based RPG!");
console.log("Type 'help' for a list of commands.");
}
async function gameLoop() {
while (true) {
const input = await askQuestion("What would you like to do? ");
if (input.toLowerCase() === "help") {
console.log("Available commands:");
console.log("- help: display this help message");
console.log("- quit: exit the game");
console.log("- name: set your player name");
console.log("- inventory: show your inventory");
console.log("- look: look around the room");
console.log("- pick up [item]: pick up an item");
console.log("- drop [item]: drop an item");
console.log("- go [direction]: move to another room");
console.log("- attack [enemy]: attack an enemy in the room");
} else if (input.toLowerCase() === "quit") {
console.log("Thanks for playing!");
break;
} else if (input.toLowerCase() === "name") {
const name = await askQuestion("What is your name? ");
player.name = name;
console.log(`Your name is now ${player.name}.`);
} else if (input.toLowerCase() === "inventory") {
if (player.inventory.length === 0) {
console.log("Your inventory is empty.");
} else {
console.log("Your inventory:");
player.inventory.forEach((item) => console.log(`- ${item}`));
}
} else if (input.toLowerCase() === "look") {
console.log(rooms[player.location].description);
console.log(`You see: ${Object.values(items)
.filter((item) => item.location === player.location)
.map((item) => item.description)
.join(", ")}`);
if (rooms[player.location].enemies) {
console.log(`You also see: ${rooms[player.location].enemies
.map((enemy) => enemies[enemy].description)
.join(", ")}`);
}
} else if (input.toLowerCase().startsWith("pick up ")) {
const itemName = input.slice(8);
const item = items[itemName];
if (!item) {
console.log(`There is no ${itemName} here.`);
} else if (item.location !== player.location) {
console.log(`You cannot pick up the ${itemName} from here.`);
} else {
player.inventory.push(itemName);
item.location = "player";
console.log(`You picked up the ${itemName}.`);
}
} else if (input.toLowerCase().startsWith("drop ")) {
const itemName = input.slice(5);
const itemIndex = player.inventory.indexOf(itemName);
if (itemIndex === -1) {
console.log(`You do not have a ${itemName} in your inventory.`);
} else {
player.inventory.splice(itemIndex, 1);
items[itemName].location = player.location;
console.log(`You dropped the ${itemName}.`);
}
} else if (input.toLowerCase().startsWith("go ")) {
const direction = input.slice(3);
const room = rooms[player.location].exits[direction];
if (!room) {
console.log(`You cannot go ${direction}.`);
} else {
player.location = room;
console.log(`You are now in the ${rooms[player.location].name}.`);
if (rooms[player.location].enemies) {
console.log(`You encounter: ${rooms[player.location].enemies
.map((enemy) => enemies[enemy].description)
.join(", ")}`);
}
}
} else if (input.toLowerCase().startsWith("attack ")) {
const enemyName = input.slice(7);
const enemy = enemies[enemyName];
if (!enemy) {
console.log(`There is no ${enemyName} here.`);
} else if (enemy.location !== player.location) {
console.log(`You cannot attack the ${enemyName} from here.`);
} else {
console.log(`You attack the ${enemyName}!`);
const damage = Math.max(player.attack - enemy.defense, 0);
enemy.health -= damage;
console.log(`You deal ${damage} damage to the ${enemyName}.`);
if (enemy.health <= 0) {
console.log(`You defeated the ${enemyName}!`);
delete enemies[enemyName];
rooms[player.location].enemies = rooms[player.location].enemies.filter((e) => e !== enemyName);
} else {
console.log(`The ${enemyName} has ${enemy.health} health left.`);
const enemyDamage = Math.max(enemy.attack - player.defense, 0);
player.health -= enemyDamage;
console.log(`The ${enemyName} deals ${enemyDamage} damage to you.`);
if (player.health <= 0) {
console.log("You died!");
break;
}
}
}
} else {
console.log("Invalid command. Type 'help' for a list of commands.");
}
}
}
function askQuestion(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
async function startGame() {
await output();
await gameLoop();
}
startGame();
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
console.log("GOAL achieved!");
}