Game Development Reference
In-Depth Information
Listing 19-8. The
Sword
Class
class Sword
: public Item
{
public:
Sword()
: Item("Sword")
{
}
};
The
Sword
class does not do anything particularly exciting; its only method is a constructor that is
used to pass the string
"Sword"
through to the
Item
parent class constructor.
The interesting work is done by our new
OpenChestOptions
class, which derives from
Option
, shown
in Listing 19-9.
Listing 19-9. The
OpenChestOption
Class
class OpenChestOption
: public Option
{
private:
Chest* m_chest;
public:
OpenChestOption(Chest* chest, const std::string& outputText)
: m_chest{chest}
, Option(PlayerOptions::OpenChest, outputText)
{
}
virtual void Evaluate(Player& player);
};
The
OpenChestOption
class stores a pointer to the
Chest
object on which it will operate. It also overrides
the
Evaluate
method from the
Option
parent class. The
Evaluate
method is shown in Listing 19-10.
Listing 19-10.
OpenChestOption::Evaluate
void OpenChestOption::Evaluate(Player& player)
{
if (m_chest->IsOpen() == false)
{
const Item* item = m_chest->Open();
player.AddItem(item);
std::cout << "You picked up a " << item->GetName() << " from the chest!"
<< std::endl << std::endl;
}
}