-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.cpp
More file actions
409 lines (371 loc) · 15.7 KB
/
Copy pathWorld.cpp
File metadata and controls
409 lines (371 loc) · 15.7 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <iostream>
#include "World.h"
#include "Room.h"
#include "Exit.h"
#include "Player.h"
#include "NPC.h"
#include "Potion.h"
#include "Weapon.h"
#include "Armor.h"
#include "Enemy.h"
#include "Chest.h"
#include "Key.h"
World::World() {
isRunning = true;
setUpWorld();
}
World::~World() {
for (Entity* e : entities)
delete e;
entities.clear();
}
void World::addEntity(Entity* entity) {
entities.push_back(entity);
}
void World::removeEntity(Entity* entity) {
if (entities.size()) {
for (int i = 0; i < entities.size(); i++) {
if (entities[i] == entity) {
entities.erase(entities.begin() + i);
delete entity;
break;
}
}
}
}
void World::run() {
cout << "Welcome to the world of adventure!" << endl;
cout << "Press Enter to begin your journey, or type 'skip' to jump ahead" << endl;
cout << ">> ";
string input;
getline(cin, input);
if (input != "skip")
showIntro();
while (isRunning) {
if (!player->isAlive())
break;
// check if player is dead
else if (!necromancer->isAlive())
{
showCongratulations();
break;
// necromancer is dead -> game is successfully finished
}
if (!isRunning)
break;
cout << endl << ">> ";
getline(cin, input);
processCommand(input);
// get commands while the game is running
}
}
void World::setUpWorld() {
// ROOMS
Room* safeHouse = new Room("Safe House", "A quiet stone shelter, dimly lit by a flickering lantern. Dust hangs in the air. A lone villager watches over you.");
Room* villageCenter = new Room("Village Center", "Cracked cobblestone paths split in four directions. The village is eerily quiet, with signs of recent struggle.");
Room* forest = new Room("Forest", "Twisted trees and thick underbrush surround you. The wind howls unnaturally through the branches.");
Room* graveyard = new Room("Graveyard", "Cracked gravestones and open coffins litter the cursed ground. A chill seeps into your bones.");
Room* blacksmith = new Room("Blacksmith Workshop", "The forge glows faintly. Tools lie scattered across the floor, and something lurks in the back.");
Room* castleRoad = new Room("Castle Road", "The path here is lined with fallen banners and cracked statues. The castle looms in the distance.");
Room* castleHall = new Room("Castle Hall", "Tall stone columns line the grand hall. At its center stands the necromancer, cloaked in shadows.");
Room* gravekeeperHouse = new Room("Gravekeeper's House", "The smell of decay clings to the air. Ritual tools and bones are scattered across the floor.");
Room* alchemistLab = new Room("Alchemist Lab", "Broken shelves and smashed vials litter the floor. A few intact potions remain on a glowing table.");
// EXITS
// Safe House <-> Village Center
Exit* houseDoorN = new Exit("north", "A reinforced wooden door leads to the outside world.", safeHouse, villageCenter, Direction::NORTH, false, 0);
Exit* houseDoorS = new Exit("south", "Door to the Safe House", villageCenter, safeHouse, Direction::SOUTH, false, 0);
// Village Center <-> Forest
Exit* centerToForest = new Exit("east", "A narrow, overgrown path leads into the dark trees.", villageCenter, forest, Direction::EAST, false, 1);
Exit* forestToCenter = new Exit("west", "The overgrown trail leads back to the village square.", forest, villageCenter, Direction::WEST, false, 1);
// Village Center <-> Blacksmith Workshop
Exit* centerToBlacksmith = new Exit("west", "You hear faint metal clanging from behind thick wooden shutters.", villageCenter, blacksmith, Direction::WEST, false, 2);
Exit* blacksmithToCenter = new Exit("east", "The metal-reinforced door leads back to the village.", blacksmith, villageCenter, Direction::EAST, false, 2);
// Village Center <-> Castle Road
Exit* centerTocastleRoad = new Exit("north", "A wide dirt road leads toward the looming shape of the castle.", villageCenter, castleRoad, Direction::NORTH, false, 3);
Exit* castleRoadToCenter = new Exit("south", "The path leads back to the center of the village.", castleRoad, villageCenter, Direction::SOUTH, false, 3);
// Forest <-> Graveyard
Exit* forestToGraveyard = new Exit("east", "A rusted gate bars your way into the graveyard.", forest, graveyard, Direction::EAST, true, 4); // locked id 4
Exit* graveyardToForest = new Exit("west", "The only exit is the rusted gate you entered through.", graveyard, forest, Direction::WEST, false, 4);
// Forest <-> Gravekeeper's House
Exit* forestToGravekeeper = new Exit("south", "A worn trail descends into a shadowed glade.", forest, gravekeeperHouse, Direction::SOUTH, false, 5);
Exit* gravekeeperToForest = new Exit("north", "A steep path leads back to the forest above.", gravekeeperHouse, forest, Direction::NORTH, false, 5);
// Castle Road <-> Alchemist Lab
Exit* castleRoadToAlch = new Exit("west", "A narrow side path leads to a ruined stone building.", castleRoad, alchemistLab, Direction::WEST, false, 6);
Exit* alchToCastleRoad = new Exit("east", "A collapsed stone corridor leads back toward the castle path.", alchemistLab, castleRoad, Direction::EAST, false, 6);
// Castle Road <-> Castle Hall
Exit* castleRoadToCH = new Exit("north", "An ancient stone door marks the entrance - it can only be opened with a special key.", castleRoad, castleHall, Direction::NORTH, true, 7); // locked id 7
Exit* CHToCastleRoad = new Exit("south", "An ancient stone door marks the exit.", castleHall, castleRoad, Direction::SOUTH, false, 7);
// LINK EXITS
// Safe House <-> Village Center
safeHouse->addExit(houseDoorN);
villageCenter->addExit(houseDoorS);
// Village Center <-> Forest
villageCenter->addExit(centerToForest);
forest->addExit(forestToCenter);
// Village Center <-> Blacksmith Workshop
villageCenter->addExit(centerToBlacksmith);
blacksmith->addExit(blacksmithToCenter);
// Village Center <-> Castle Road
villageCenter->addExit(centerTocastleRoad);
castleRoad->addExit(castleRoadToCenter);
// Forest <-> Graveyard
forest->addExit(forestToGraveyard);
graveyard->addExit(graveyardToForest);
// Forest <-> Gravekeeper's House
forest->addExit(forestToGravekeeper);
gravekeeperHouse->addExit(gravekeeperToForest);
// Castle Road <-> Alchemist Lab
castleRoad->addExit(castleRoadToAlch);
alchemistLab->addExit(alchToCastleRoad);
// Castle Road <-> Castle Hall
castleRoad->addExit(castleRoadToCH);
castleHall->addExit(CHToCastleRoad);
// ADD ROOMS
addEntity(safeHouse);
addEntity(villageCenter);
addEntity(forest);
addEntity(graveyard);
addEntity(blacksmith);
addEntity(castleRoad);
addEntity(castleHall);
addEntity(gravekeeperHouse);
addEntity(alchemistLab);
// ADD EXITS
addEntity(houseDoorN);
addEntity(houseDoorS);
addEntity(centerToForest);
addEntity(forestToCenter);
addEntity(centerToBlacksmith);
addEntity(blacksmithToCenter);
addEntity(centerTocastleRoad);
addEntity(castleRoadToCenter);
addEntity(forestToGraveyard);
addEntity(graveyardToForest);
addEntity(forestToGravekeeper);
addEntity(gravekeeperToForest);
addEntity(castleRoadToAlch);
addEntity(alchToCastleRoad);
addEntity(castleRoadToCH);
addEntity(CHToCastleRoad);
// NPCS
NPC* villager = new NPC("villager", "The Villager", "You're awake. I wasn't sure you'd make it.\nThey were dragging your body to the necromancer's stronghold. Take what's in the chest\nYou'll need it if you want to survive.");
NPC* townSurvivor = new NPC("survivor", "Town Survivor", "The forest holds monsters now, but also strength.\nIf you're not ready for the castle, check the blacksmith or the lab.");
NPC* alchemist = new NPC("alchemist", "Wounded Alchemist", "You'll need potions. The lab has some left if you can get past the rubble.");
// ADD NPCS
safeHouse->add(villager);
villageCenter->add(townSurvivor);
castleRoad->add(alchemist);
// ENEMIES
Enemy* skeleton = new Enemy("skeleton", "Rusted armor rattles as this undead patrols the area.", 50, 5, 100);
Enemy* skeleton1 = new Enemy("skeleton", "A reanimated corpse of a fallen warrior, its bones held together by dark magic.", 50, 5, 100);
Enemy* skeleton2 = new Enemy("skeleton", "Rusted armor rattles as this undead patrols the area.", 50, 5, 100);
Enemy* skeleton3 = new Enemy("skeleton", "A reanimated corpse of a fallen warrior, its bones held together by dark magic.", 50, 5, 100);
Enemy* skeleton4 = new Enemy("skeleton", "Rusted armor rattles as this undead patrols the area.", 50, 5, 100);
Enemy* gravekeeper = new Enemy("gravekeeper", "Once a humble caretaker of the dead, now twisted by necromancy.\nHis eyes burn with loyalty to the cursed one he buried.", 100, 15, 200);
Enemy* kingOfOld = new Enemy("king of old", "A long-dead monarch, awakened to guard the graveyard with relentless wrath.\nHis rusted crown is fused to his skull, and a cursed sword rests in his grasp.", 150, 20, 200);
Enemy* corruptedBlacksmith = new Enemy("blacksmith", "Once a master of steel, now a puppet of dark forces.\nHis hammer drips with blood, and his forge burns with unnatural flame.", 75, 10, 150);
necromancer = new Enemy("necromancer", "The architect of the dead's return. Cloaked in shadow, he commands the grave with whispers and raises armies with a single word.", 250, 25, 500);
// Set Enemy Level and XP
skeleton->setLevel(2);
gravekeeper->setLevel(2);
kingOfOld->setLevel(3);
necromancer->setLevel(5);
// ADD ENEMIES
forest->add(skeleton);
graveyard->add(skeleton1);
blacksmith->add(skeleton2);
castleRoad->add(skeleton3);
alchemistLab->add(skeleton4);
blacksmith->add(corruptedBlacksmith);
gravekeeperHouse->add(gravekeeper);
graveyard->add(kingOfOld);
castleHall->add(necromancer);
addEntity(skeleton);
addEntity(skeleton1);
addEntity(skeleton2);
addEntity(skeleton3);
addEntity(skeleton4);
addEntity(gravekeeper);
addEntity(kingOfOld);
addEntity(corruptedBlacksmith);
addEntity(necromancer);
// ITEMS
Chest* chest = new Chest("chest", "A wooden chest.", false, 8);
Chest* blacksmithChest = new Chest("chest", "Blacksmith Chest", true, 9); // locked id 9 key dropped from corruptedBlacksmith
Weapon* woodenSword = new Weapon("wooden sword", "Wooden Sword", 5, 1);
Weapon* steelSword = new Weapon("steel sword", "Steel Sword", 10, 2);
Weapon* legendarySword = new Weapon("sword of destiny", "Sword of Destiny", 25, 5);
Armor* woodenArmor = new Armor("wooden armor", "Wooden Armor", 5, 1);
Armor* steelArmor = new Armor("steel armor", "Steel Armor", 10, 2);
Armor* mithrilArmor = new Armor("mithril armor", "Mithril Armor", 25, 5);
Potion* staminaPotion = new Potion("stamina potion", "Stamina Potion", "Stamina", 10);
Potion* healthPotion = new Potion("health potion", "Health Potion", "HP", 0);
Potion* apPotion = new Potion("strength potion", "Strength Potion", "AttackPower", 10);
Key* blacksmithKey = new Key("chest key", "Blacksmith Chest Key", 9);
Key* graveyardKey = new Key("graveyard key", "Graveyard Key", 4);
Key* castleHallKey = new Key("castle gate key", "Castle Gate Key", 7);
chest->add(woodenSword);
safeHouse->add(chest);
blacksmithChest->add(steelSword);
blacksmithChest->add(mithrilArmor);
skeleton->add(woodenArmor);
skeleton2->add(healthPotion);
skeleton3->add(steelArmor);
corruptedBlacksmith->add(blacksmithKey);
gravekeeper->add(graveyardKey);
kingOfOld->add(legendarySword);
kingOfOld->add(castleHallKey);
alchemistLab->add(apPotion);
alchemistLab->add(staminaPotion);
addEntity(chest);
addEntity(blacksmithChest);
addEntity(woodenSword);
addEntity(steelSword);
addEntity(legendarySword);
addEntity(woodenArmor);
addEntity(steelArmor);
addEntity(mithrilArmor);
addEntity(staminaPotion);
addEntity(healthPotion);
addEntity(apPotion);
addEntity(blacksmithKey);
addEntity(graveyardKey);
addEntity(castleHallKey);
blacksmith->add(blacksmithChest);
player = new Player();
player->setCurrentRoom(safeHouse);
addEntity(player);
}
void World::showIntro() {
cout << "+-------------------------------------------------------------------+\n";
cout << "| After a great battle, your body was mistaken for dead |\n";
cout << "| and taken away by skeletal hands. |\n|";
cout << " |\n";
cout << "| They carried you toward an abandoned village - a place cloaked |\n";
cout << "| in silence and rot - where a hidden necromancer is raising an |\n";
cout << "| army of the fallen. |\n|";
cout << " |\n";
cout << "| But fate intervened. |\n";
cout << "| A brave villager rescued you and brought you to safety. |\n";
cout << "| Now, weak and unequipped, you awaken to a dying world in need |\n";
cout << "| of a hero. |\n|";
cout << " |\n";
cout << "| Will you rise again and stop the darkness? |\n";
cout << "+-------------------------------------------------------------------+\n";
}
void World::showCongratulations() {
cout << "+---------------------------------------+" << endl;
cout << "| CONGRATULATIONS! |" << endl;
cout << "| You defeated the Necromancer and won! |" << endl;
cout << "+---------------------------------------+" << endl;
}
string World::getCommandArgs(istringstream& iss) {
bool bFirst = true;
string res = "";
string arg;
while (iss >> arg) {
if (bFirst) {
res += arg;
bFirst = false;
}
else {
res += ' ' + arg;
}
}
return res;
}
void World::processCommand(const string& input) {
istringstream iss(input);
string command, arg;
iss >> command;
if (command == "look") {
cout << "You are in: " << player->getRoom()->getName() << endl;
cout << player->getRoom()->getDescription() << endl;
cout << "You see:" << endl;
player->getRoom()->listEntities();
}
else if (command == "go") {
iss >> arg;
if (arg == "east") {
player->move(Direction::EAST);
}
else if (arg == "west") {
player->move(Direction::WEST);
}
else if (arg == "north") {
player->move(Direction::NORTH);
}
else if (arg == "south") {
player->move(Direction::SOUTH);
}
else {
cout << "You can't go that way!" << endl;
}
}
else if (command == "inventory") {
player->listInventory();
}
else if (command == "talk") {
string npcName = getCommandArgs(iss);
bool isValidNPC = false;
for (const auto& e : player->getRoom()->getContains()) {
if (auto npc = dynamic_cast<NPC*>(e)) {
if (npc->getName() == npcName)
{
isValidNPC = true;
cout << npc->getDescription() << ": " << npc->getDialogue() << endl;
// npc is in the current room
}
}
}
if (!isValidNPC)
cout << npcName << " is not currently here." << endl;
// npc not in the current room or entity is not an npc
}
else if (command == "take") {
string itemName = getCommandArgs(iss);
player->take(itemName);
}
else if (command == "drop") {
string itemName = getCommandArgs(iss);
player->drop(itemName);
}
else if (command == "use") {
string itemName = getCommandArgs(iss);
player->use(itemName);
}
else if (command == "equip") {
string itemName = getCommandArgs(iss);
player->equip(itemName);
}
else if (command == "unequip") {
string itemName = getCommandArgs(iss);
player->unequip(itemName);
}
else if (command == "open") {
string itemName = getCommandArgs(iss);
player->open(itemName);
}
else if (command == "unlock") {
string toUnlock = getCommandArgs(iss);
player->unlock(toUnlock);
}
else if (command == "lock") {
string toLock = getCommandArgs(iss);
player->lock(toLock);
}
else if (command == "status") {
player->showStatus();
}
else if (command == "attack") {
string enemyName = getCommandArgs(iss);
player->attack(enemyName);
}
else if (command == "quit") {
isRunning = false;
cout << "Thank you for playing! See you soon!" << endl;
}
else {
cout << "Invalid command!" << endl;
}
}