-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
332 lines (303 loc) · 8.98 KB
/
cli.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env node
import chalk from "chalk";
import inquirer from "inquirer";
import { createSpinner } from "nanospinner";
import gradient from "gradient-string";
import figlet from "figlet";
import ora from "ora"; // For additional spinner animations
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
// Game Data
const worldMap = [
{
name: "Enchanted Forest",
description: "A mystical forest filled with magical creatures.",
enemies: ["Fairy", "Troll"],
miniBoss: "Forest Guardian",
difficulty: 1,
loot: { name: "Magic Bow", attack: 10 },
},
{
name: "Haunted Castle",
description: "A dark castle haunted by restless spirits.",
enemies: ["Ghost", "Vampire"],
miniBoss: "Phantom Knight",
difficulty: 2,
loot: { name: "Silver Sword", attack: 15 },
},
{
name: "Sunken Ruins",
description: "Ancient underwater ruins teeming with sea creatures.",
enemies: ["Mermaid", "Giant Octopus"],
miniBoss: "Kraken",
difficulty: 3,
loot: {
name: "Trident of Poseidon",
attack: 20,
effect: "Deals double damage to sea creatures",
},
},
{
name: "Dwarven Mines",
description: "Deep underground mines filled with rare minerals and gems.",
enemies: ["Goblin", "Rockbiter"],
miniBoss: "Mole King",
difficulty: 2,
loot: {
name: "Adamantium Pickaxe",
attack: 12,
effect: "Doubles mining yield",
},
},
{
name: "Volcanic Lair",
description: "A fiery lair nestled within an active volcano.",
enemies: ["Lava Elemental", "Fire Drake"],
miniBoss: "Magma Lord",
difficulty: 4,
loot: {
name: "Inferno Greaves",
attack: 18,
effect: "Immune to fire damage",
},
},
];
const player = {
name: "",
level: 1,
health: 100,
attack: 10,
defense: 5,
inventory: [],
currency: 50,
xp: 0,
xpToLevelUp: 100,
magic: 0,
};
// Utility function for leveling up
const levelUp = async () => {
if (player.xp >= player.xpToLevelUp) {
player.level++;
player.xp -= player.xpToLevelUp;
player.xpToLevelUp *= 1.5;
player.attack += 5;
player.defense += 3;
player.health += 20;
console.log(
chalk.magenta(`Congratulations! You leveled up to level ${player.level}!`)
);
}
};
// Game Start
const startGame = async () => {
console.log(chalk.green("Welcome to DungeonDash!"));
// Prompt for player name
const answers = await inquirer.prompt([
{
type: "input",
name: "name",
message: "What is your character's name?",
},
]);
const spinner = createSpinner("Creating Player...").start();
await sleep();
if (answers.name) {
spinner.success({
text: chalk.cyanBright(
`Welcome ${answers.name}! Your adventure begins now.`
),
});
player.name = answers.name;
await exploreWorld();
} else {
spinner.error({
text: chalk.redBright("Error creating player, Try again!"),
});
process.exit(1);
}
};
// Explore the World Map
const exploreWorld = async () => {
const spinner = ora("Loading the world map...").start();
await sleep();
spinner.succeed(chalk.green("World map loaded!"));
const answers = await inquirer.prompt([
{
type: "list",
name: "dungeon",
message: "Choose a location to explore:",
choices: worldMap.map((d) => d.name),
},
]);
const selectedDungeon = worldMap.find((d) => d.name === answers.dungeon);
console.log(
chalk.yellow(
`You have arrived at the ${selectedDungeon.name}. ${selectedDungeon.description}`
)
);
await exploreDungeon(selectedDungeon);
};
// Explore Dungeon
const exploreDungeon = async (dungeon) => {
const spinner = ora(`Exploring the ${dungeon.name}...`).start();
await sleep(3000); // Simulate time taken for exploring
spinner.stop();
// First Encounter
console.log(chalk.red(`An enemy ${dungeon.enemies[0]} appears!`));
await combat(dungeon.enemies[0], dungeon.difficulty);
// Second Encounter or Event
const randomEvent = Math.random();
if (randomEvent > 0.5) {
console.log(chalk.red(`Another enemy ${dungeon.enemies[1]} appears!`));
await combat(dungeon.enemies[1], dungeon.difficulty);
} else {
console.log(chalk.yellow("You found a treasure chest!"));
player.inventory.push({ name: "Health Potion", effect: "Restore 20 HP" });
console.log(chalk.green("You received a Health Potion!"));
}
// Mini-Boss Encounter
console.log(chalk.red(`The Mini-Boss ${dungeon.miniBoss} challenges you!`));
await combat(dungeon.miniBoss, dungeon.difficulty + 1);
// Victory and Loot
console.log(chalk.green(`You have defeated the ${dungeon.miniBoss}!`));
player.xp += 100 * dungeon.difficulty;
player.inventory.push(dungeon.loot);
console.log(chalk.green(`You found a ${dungeon.loot.name}!`));
await levelUp();
await showVictory();
};
// Combat Function
const combat = async (enemy, difficulty) => {
let enemyHealth = 50 * difficulty;
let playerHealth = player.health;
while (enemyHealth > 0 && playerHealth > 0) {
const action = await inquirer.prompt([
{
type: "list",
name: "action",
message: `What will you do? (${enemy} - ${enemyHealth} HP) | (${player.name} - ${playerHealth} HP)`,
choices: ["Attack", "Defend", "Use Magic", "Use Item", "Run Away"],
},
]);
switch (action.action) {
case "Attack":
const damage = player.attack + Math.floor(Math.random() * 10);
enemyHealth -= damage;
console.log(chalk.green(`You dealt ${damage} damage to the ${enemy}!`));
break;
case "Defend":
const blocked = player.defense + Math.floor(Math.random() * 5);
console.log(chalk.blue(`You blocked ${blocked} damage!`));
break;
case "Use Magic":
if (player.magic > 0) {
const magicDamage = player.magic + Math.floor(Math.random() * 20);
enemyHealth -= magicDamage;
player.magic -= 10;
console.log(
chalk.magenta(`You cast a spell and dealt ${magicDamage} damage!`)
);
} else {
console.log(chalk.red("You don't have enough magic!"));
}
break;
case "Use Item":
if (player.inventory.length > 0) {
const item = await inquirer.prompt([
{
type: "list",
name: "item",
message: "Choose an item to use:",
choices: player.inventory.map((i) => i.name),
},
]);
const selectedItem = player.inventory.find(
(i) => i.name === item.item
);
if (selectedItem.effect.includes("Restore")) {
const restoreAmount = parseInt(selectedItem.effect.match(/\d+/)[0]);
playerHealth += restoreAmount;
console.log(
chalk.green(
`You used ${selectedItem.name} and restored ${restoreAmount} HP!`
)
);
}
} else {
console.log(chalk.red("You have no items!"));
}
break;
case "Run Away":
const escapeChance = Math.random();
if (escapeChance > 0.5) {
console.log(chalk.yellow("You successfully ran away!"));
return "escape";
} else {
console.log(chalk.red("You failed to escape!"));
}
break;
}
// Enemy Attack
if (enemyHealth > 0) {
const enemyDamage = difficulty * 5 + Math.floor(Math.random() * 10);
playerHealth -= enemyDamage;
console.log(
chalk.red(`The ${enemy} attacked and dealt ${enemyDamage} damage!`)
);
}
if (playerHealth <= 0) {
console.log(chalk.red("You were defeated!"));
await showDefeat();
return;
}
}
player.health = playerHealth;
};
// Show Victory Screen
const showVictory = async () => {
console.log(chalk.green("You have completed the dungeon successfully!"));
console.log(
gradient.rainbow(figlet.textSync("Victory!", { horizontalLayout: "full" }))
);
console.log(gradient.pastel(`Congratulations ${player.name}!`));
await sleep();
const playAgain = await inquirer.prompt([
{
type: "confirm",
name: "playAgain",
message: "Do you want to play again?",
default: true,
},
]);
if (playAgain.playAgain) {
await exploreWorld();
} else {
console.log(chalk.blue("Thanks for playing!"));
process.exit(0);
}
};
// Show Defeat Screen
const showDefeat = async () => {
console.log(chalk.red("Game Over! You have been defeated."));
console.log(
gradient.cristal(figlet.textSync("Defeat!", { horizontalLayout: "full" }))
);
console.log(gradient.pastel(`Better luck next time, ${player.name}.`));
await sleep();
const playAgain = await inquirer.prompt([
{
type: "confirm",
name: "playAgain",
message: "Do you want to play again?",
default: true,
},
]);
if (playAgain.playAgain) {
player.health = 100;
await exploreWorld();
} else {
console.log(chalk.blue("Thanks for playing!"));
process.exit(0);
}
};
// Start the game
startGame();