Game Development Reference
In-Depth Information
public:
Chest(const Item* item);
void Update();
bool IsOpen() const
{
return m_isOpen;
}
const Item* Open()
{
m_isOpen = true;
return m_item;
}
};
The
Chest
class inherits from
Entity
. This makes sense, as a
Chest
will be an object that exists in
the game world in its own right.
Chest
has two member variables, one to store a pointer to an
Item
and a bool to track if the
Chest
is open.
The
Open
method sets
m_isOpen
to
true
and returns the pointer to the stored
Item
to the caller.
The
Item
class is shown in Listing 19-7.
Listing 19-7. The
Item
Class
class Item
{
private:
std::string m_name;
public:
Item(const std::string& name)
: m_name{ name }
{
}
virtual ~Item() {}
const std::string& GetName() const
{
return m_name;
}
};
The only useful functionality provided by the
Item
class is to store a string that contains the
Item
's
name. This allows us to print the name of the
Item
to the player when they pick it up. This means
that we need an
Item
that the player can collect. Listing 19-8 shows the
Sword
class.