Skip to content

Commit

Permalink
Fixed problems with deleting Potion when deleting as an Item*
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff591 committed Nov 27, 2021
1 parent 5a0bc48 commit 29c128a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
8 changes: 6 additions & 2 deletions game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "enemy.h"
#include "skillset.hpp"
#include "town.hpp"
#include "Potion.hpp"
#include "Armor.hpp"

using namespace std;

Expand Down Expand Up @@ -48,7 +50,7 @@ class Game
cout << "Speed: " << playerChar->get_speed() <<endl;
cout << "Test Worked" << endl;
cin.ignore();


clearScreen();
intermission();
Expand Down Expand Up @@ -427,7 +429,9 @@ class Game
else
{
battle_health += 15;

Item* item = playerChar->get_inventory()->at(2).at(playerChar->get_inventory()->at(2).size()-1);
Potion* dele = dynamic_cast<Potion*> (item);
delete dele;
playerChar->get_inventory()->at(2).pop_back();
}
battle_health -= opponent->attack(playerChar);
Expand Down
32 changes: 29 additions & 3 deletions player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ class Player : public Character
{
for(unsigned int j = 0; j < inventory.at(i).size(); j++)
{
delete inventory.at(i).at(j);
Item* item = inventory.at(i).at(j);
if (item->get_item_type() == "Potion")
{
Potion* dele = dynamic_cast<Potion*> (item);
delete dele;
}
else
{
delete item;
}
}
}
}
Expand Down Expand Up @@ -111,7 +120,14 @@ class Player : public Character
{
cout << "You don't have enough money to purchase this item." << endl
<< endl;
cout << "Item: " << item->get_name() << " Type: " << item->get_item_type() << endl;
if (item->get_item_type() == "Potion")
{
Potion* dele = dynamic_cast<Potion*> (item);
delete dele;
dele = nullptr;
item = nullptr;
return;
}
delete item;
item = nullptr;
return;
Expand Down Expand Up @@ -167,7 +183,17 @@ class Player : public Character
}

cout << item->get_name() << " sold successfully" << endl;
delete item;
if (item->get_item_type() == "Potion")
{
Potion* dele = dynamic_cast<Potion*> (item);
delete dele;
dele = nullptr;
}
else
{
delete item;
item = nullptr;
}
cout << "Your new balance is " << this->money << endl
<< endl;
}
Expand Down
10 changes: 9 additions & 1 deletion town.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,15 @@ void Town::buyConfirmation(Item *item, int price) {
break;
case 2:
cout << "Item purchase cancelled." << endl;
delete item;
if (item->get_item_type() == "Potion")
{
Potion* dele = dynamic_cast<Potion*> (item);
delete dele;
}
else
{
delete item;
}
break;
default:
cout << "Invalid option." << endl << endl;
Expand Down

0 comments on commit 29c128a

Please sign in to comment.