Game Development Reference
In-Depth Information
class Enemy
: public Entity
{
public:
using Pointer = std::shared_ptr<Enemy>;
private:
EnemyType m_type;
bool m_alive{ true };
public:
Enemy(EnemyType type)
: m_type{ type }
{
}
EnemyType GetType() const
{
return m_type;
}
bool IsAlive() const
{
return m_alive;
}
void Kill()
{
m_alive = false;
}
};
The
Pointer
aliases in both classes have been defined using the
shared_ptr
template. This
means that once the instances have been created by the factories you will not need to worry about
where the objects should be deleted. The
shared_ptr
will automatically delete the instance as soon
as you no longer hold a
shared_ptr
reference.
Updating the
Game
class constructor is the next important change when using the two factory
functions. This constructor is shown in Listing 23-5.
Listing 23-5. The Updated
Game
Constructor
Game::Game()
: m_attackDragonOption{ CreateOption(PlayerOptions::AttackEnemy) }
, m_attackOrcOption{ CreateOption(PlayerOptions::AttackEnemy) }
, m_moveNorthOption{ CreateOption(PlayerOptions::GoNorth) }
, m_moveEastOption{ CreateOption(PlayerOptions::GoEast) }
, m_moveSouthOption{ CreateOption(PlayerOptions::GoSouth) }
, m_moveWestOption{ CreateOption(PlayerOptions::GoWest) }
, m_openSwordChest{ CreateOption(PlayerOptions::OpenChest) }
, m_quitOption{ CreateOption(PlayerOptions::Quit) }
, m_swordChest{ &m_sword }