Game Development Reference
In-Depth Information
void SetCurrentRoom(const Room* pCurrentRoom)
{
m_pCurrentRoom = pCurrentRoom;
}
const Room* GetCurrentRoom() const
{
return m_pCurrentRoom;
}
};
The Player class contains a pointer to a Room object and accessor methods to set and retrieve the
current Room .
Note We did not have to include Room.h in the Player.h file to add a pointer to the Player class. What
you see in Listing 10-17 is known as a forward declaration. A forward declaration can be used when only
a pointer or reference to an object is used in a header file and not the object itself. You are only required to
include the full class definition if you were going to call a method on an object or instantiate a class in any
given file.
I've added another line to InitializeRooms to set the Player's current Room pointer in Listing 10-18.
Listing 10-18. Setting m_ player's Current Room
void Game::InitializeRooms()
{
// Room 0 heads North to Room 1
m_rooms[0].AddRoom(Room::JoiningDirections::North, &(m_rooms[1]));
// Room 1 heads East to Room 2, South to Room 0 and West to Room 3
m_rooms[1].AddRoom(Room::JoiningDirections::East, &(m_rooms[2]));
m_rooms[1].AddRoom(Room::JoiningDirections::South, &(m_rooms[0]));
m_rooms[1].AddRoom(Room::JoiningDirections::West, &(m_rooms[3]));
// Room 2 heads West to Room 1
m_rooms[2].AddRoom(Room::JoiningDirections::West, &(m_rooms[1]));
// Room 3 heads East to Room 1
m_rooms[3].AddRoom(Room::JoiningDirections::East, &(m_rooms[1]));
m_player.SetCurrentRoom(&(m_rooms[0]));
}
Now that you have rooms and a way to store which room the player is in, you should add some
options to allow the player to move through those rooms.
 
Search WWH ::




Custom Search