-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.cpp
More file actions
72 lines (64 loc) · 1.64 KB
/
room.cpp
File metadata and controls
72 lines (64 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "room.h"
#include <format>
Room::Room(const std::string& id, int row, int col): m_id(id), m_row(row), m_col(col), m_slots(row * col, nullptr)
{
}
std::string Room::getId()
{
return m_id;
}
void Room::addLink(const std::string& id, Room *entity)
{
m_links[id] = entity;
}
Room *Room::getLink(const std::string& link_id)
{
auto it = m_links.find(link_id);
if (it != m_links.end()){
return it->second;
}
return nullptr;
}
bool Room::putEntity(RoomEntity *entity)
{
// find first empty slot
auto it = std::find(m_slots.begin(), m_slots.end(), nullptr);
if (it != m_slots.end()){
*it = entity;
entity->setRoom(this);
return true;
}
return false;
}
bool Room::removeEntity(RoomEntity *entity)
{
auto it = std::find(m_slots.begin(), m_slots.end(), entity);
if (it != m_slots.end()){
*it = nullptr;
entity->setRoom(nullptr);
return true;
}
return false;
}
RoomEntity *Room::getEntity(const std::string& id)
{
for (auto entity : m_slots){
if (entity && entity->getId() == id) return entity;
}
return nullptr;
}
std::string Room::toString()
{
std::string result = std::format("Room ID:{}, Row:{}, Col:{}\n", m_id, m_row, m_col);
result += "Entities:\n";
for (auto entity : m_slots){
if (entity){
result += entity->toString() + "\n";
}
}
result += "Links:\n";
for (auto link : m_links){
result += std::format("Link ID:{} -> Room ID:{}\n", link.first, link.second->getId());
}
return result;
}