Skip to content

Commit 0c56ee2

Browse files
committed
Level: Fix issue where getNeighbours would result in out of bound error if no neighbours exists in the direction (#56)
1 parent c3ddf6e commit 0c56ee2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Level.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ Level::Level(const nlohmann::json& j, World* w)
2727
m_layers.emplace_back(level, w, this);
2828
}
2929

30+
m_neighbours_by_dir.insert({Dir::None, {}});
31+
m_neighbours_by_dir.insert({Dir::North, {}});
32+
m_neighbours_by_dir.insert({Dir::NorthEast, {}});
33+
m_neighbours_by_dir.insert({Dir::East, {}});
34+
m_neighbours_by_dir.insert({Dir::SouthEast, {}});
35+
m_neighbours_by_dir.insert({Dir::South, {}});
36+
m_neighbours_by_dir.insert({Dir::SouthWest, {}});
37+
m_neighbours_by_dir.insert({Dir::West, {}});
38+
m_neighbours_by_dir.insert({Dir::NorthWest, {}});
39+
m_neighbours_by_dir.insert({Dir::Over, {}});
40+
m_neighbours_by_dir.insert({Dir::Under, {}});
41+
m_neighbours_by_dir.insert({Dir::Overlap, {}});
42+
3043
m_neighbours_iid_by_dir.emplace(Dir::None, 0);
3144
m_neighbours_iid_by_dir.emplace(Dir::North, 0);
3245
m_neighbours_iid_by_dir.emplace(Dir::NorthEast, 0);
@@ -105,6 +118,8 @@ auto Level::allNeighbours() const -> const std::vector<ref_wrapper<const Level>>
105118

106119
auto Level::getNeighbours(const Dir& direction) const -> const std::vector<ref_wrapper<const Level>>&
107120
{
121+
if (direction == Dir::None)
122+
return {};
108123
return m_neighbours_by_dir.at(direction);
109124
}
110125

src/World.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ World::World(const nlohmann::json& j, Project* p, const FileLoader& file_loader,
4747

4848
// fill levels neighbours
4949
for (auto& level : m_levels) {
50-
level.m_neighbours_by_dir.insert({Dir::None, {}});
5150
for (const auto& iid : level.m_neighbours_iid) {
5251
level.m_neighbours.emplace_back(getLevel(iid));
5352
}

0 commit comments

Comments
 (0)