Game Development Reference
In-Depth Information
The
OnLoad
method reads the
m_name
variable out of the file, then the number of items. There is
then a
for
loop that reads out the IDs of each item and retrieves a pointer to the
Item
from the
SerializationManager
. Each
Serializable
pointer is converted into an
Item
pointer using a
dynamic_cast
.
The
Room
pointer is a little more challenging. The
Player
class does not store a raw pointer to the
Room
object; instead, a
shared_ptr
was used. Listing 24-14 shows how the
Room
class has been
updated to store a
shared_ptr
to itself, which can be used to retrieve a valid
shared_ptr
when
retrieving the object from the
SerializationManager
.
Listing 24-14. The
Room
Class
class Room
: public Entity
, public Serializable
{
public:
using Pointer = std::shared_ptr<Room>;
enum class JoiningDirections
{
North = 0,
East,
South,
West,
Max
};
private:
using JoiningRooms = std::array<Pointer, static_cast<size_t>(JoiningDirections::Max)>;
JoiningRooms m_pJoiningRooms;
using StaticOptions = std::map<unsigned int, Option::Pointer>;
StaticOptions m_staticOptions;
unsigned int m_staticOptionStartKey{ 1 };
using DynamicOptions = std::vector<Option::Pointer>;
DynamicOptions m_dynamicOptions;
Pointer m_pointer{ this };
public:
explicit Room(unsigned int serializableId);
void AddRoom(JoiningDirections direction, Pointer room);
Pointer GetRoom(JoiningDirections direction) const;
Option::Pointer EvaluateInput(unsigned int playerInput);
void AddStaticOption(Option::Pointer option);
void AddDynamicOption(Option::Pointer option);
void PrintOptions() const;