Skip to content

Commit a94880c

Browse files
committed
POTIONS!
----------------------- Implemented a new item type, with single effects when consumed. (MISSING: Item will not disappear from the Inventory)
1 parent 35d106e commit a94880c

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

ZORK/Creature.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,17 @@ void Creature::Go(const vector<string>& tokens) {
257257
ChangeParent(exit->GetDestination((Room*)parent)); // change the parent of the player (position)
258258
GetRoom()->creaturesIn.push_back(this);
259259
}
260+
}
261+
262+
// ---- GET MAX HP ----
263+
/* Returns the maximum number of Health Points this creature has.
264+
265+
Parameters:
266+
- NONE
267+
Return:
268+
- NONE
269+
*/
270+
const int Creature::GetMaxHP() const
271+
{
272+
return maxHP;
260273
}

ZORK/Creature.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Creature : public Entity
2020
void PrintStats() const;
2121
virtual void Inventory() const;
2222
virtual void Go(const vector<string>& tokens);
23+
const int GetMaxHP() const;
2324

2425
// Object Attr
2526
int HP = 10; // !!! Is it correct to initialise here?

ZORK/Item.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ enum ItemType
1010
COMMON,
1111
WEAPON,
1212
ARMOUR,
13-
SHIELD
13+
SHIELD,
14+
HP_POTION
1415
};
1516

1617
class Item : public Entity

ZORK/Player.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ void Player::Loot(const vector<string>& tokens)
469469
cout << "That is not something you can loot." << endl;
470470
}
471471

472-
// ---- LOOT ----
472+
// ---- VICTORY ----
473473
/* Check the conditions for winning the game. The conditions are:
474474
- The player has the troll's amulet
475475
- The player escaped to the Rooftop
@@ -488,4 +488,38 @@ void Player::Victory() const
488488
gameOver = true;
489489
}
490490
}
491+
}
492+
493+
// ---- DRINK ----
494+
/* Player consumes a potion from its inventory
495+
496+
Parameters:
497+
- Vector of strings
498+
Return:
499+
- NONE
500+
*/
501+
void Player::Drink(const vector<string>& tokens)
502+
{
503+
Entity* thing = Find(tokens[1]);
504+
505+
if (thing != NULL) {
506+
if (thing->type == ITEM) {
507+
Item* potion = (Item*)thing;
508+
if (potion->item_type == HP_POTION) {
509+
HP += potion->combatVal;
510+
511+
if (HP > GetMaxHP()) {
512+
HP = GetMaxHP();
513+
}
514+
515+
cout << "You drink " << potion->name << " and recover " << potion->combatVal << " HP." << endl;
516+
}
517+
else
518+
cout << "You cannot drink this!" << endl;
519+
}
520+
else
521+
cout << "You cannot drink this!" << endl;
522+
}
523+
else
524+
cout << "Cannot find '" << tokens[1] << "', it is not in your inventory." << endl;
491525
}

ZORK/Player.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Player : public Creature
2323
void Stats(const vector<string>& tokens) const;
2424
void Loot(const vector<string>& tokens);
2525
void Victory() const;
26+
void Drink(const vector<string>& tokens);
2627

2728
};
2829

ZORK/World.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ World::World() {
2323
Room* mazeE = new Room("Maze Entrance", "You enter a dark and narrow corridor excavated in the underground that splits into three. \n"
2424
"As you get to the crossroad, the doorway from where you came collapses. You are trapped!");
2525
rooms.push_back(mazeE);
26-
Room* bones = new Room("Bone Room", "You follow the corridor to a room with an extinguished firecamp in a corner,\nand human bones scattered all around the floor.");
26+
Room* bones = new Room("Bone Room", "You follow the corridor to a room with an extinguished firecamp in a corner,\nand human bones scattered all around the floor.\n"
27+
"There is a rotting corpse in a corner");
2728
rooms.push_back(bones);
2829
Room* maze1 = new Room("Maze1", "You are in a small room that splits the corridor in two directions.");
2930
rooms.push_back(maze1);
@@ -68,17 +69,21 @@ World::World() {
6869
exits.push_back(exME);
6970

7071
// PLAYER
71-
player = new Player("Hero", "You are an awesome adventurer!", maze1);
72+
player = new Player("Hero", "You are an awesome adventurer!", forest);
7273

7374
// CREATURES
7475
Creature* butler = new Creature("Butler", "It's James, the house Butler.", house);
7576
butler->SetStats(15, 0, 3, 0);
7677
creatures.push_back(butler);
7778

7879
Creature* troll = new Creature("Troll", "It's an enormous Troll! You see he is wearing a shiny amulet on his neck.", maze1);
79-
butler->SetStats(5, 0, 3, 0);
80+
troll->SetStats(40, 0, 7, 3);
8081
creatures.push_back(troll);
8182

83+
Creature* corpse = new Creature("Soldier", "It's the rotting body of someone, possibly a soldier or an adventurer like you.\nOne leg has been... bitten? by something big.", bones);
84+
corpse->SetStats(0, 0, 3, 0);
85+
creatures.push_back(corpse);
86+
8287
// ITEMS
8388
Item* mailbox = new Item("Mailbox", "Looks like it might contain something.", house);
8489
mailbox->isContainer = true;
@@ -102,6 +107,10 @@ World::World() {
102107
//exME->key = amulet;
103108
items.push_back(amulet);
104109

110+
Item* potion = new Item("Potion", "A magical beverage to recover vitality.", corpse, HP_POTION);
111+
potion->combatVal = 15;
112+
items.push_back(potion);
113+
105114
// EQUIP CREATURES
106115
butler->Equip(shield);
107116
butler->Equip(sword2);
@@ -126,6 +135,7 @@ const bool World::Parser(vector<string>& tokens)
126135
{
127136
if (Same(tokens[0], "look") || Same(tokens[0], "l"))
128137
{
138+
trollAutoMove(trollPath);
129139
player->Look(tokens);
130140
}
131141
else if (Same(tokens[0], "north") || Same(tokens[0], "n"))
@@ -231,6 +241,10 @@ const bool World::Parser(vector<string>& tokens)
231241
{
232242
player->Stats(tokens);
233243
}
244+
else if (Same(tokens[0], "drink"))
245+
{
246+
player->Drink(tokens);
247+
}
234248
else
235249
ret = false;
236250
break;

0 commit comments

Comments
 (0)