Tileson  1.3.0
A helpful json parser for Tiled maps
World.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 01.08.2020.
3 //
4 
5 #ifndef TILESON_WORLD_HPP
6 #define TILESON_WORLD_HPP
7 
8 #include "../objects/WorldMapData.hpp"
9 #include <memory>
10 namespace tson
11 {
12  class Tileson;
13  class World
14  {
15  public:
16  #ifdef JSON11_IS_DEFINED
17  inline explicit World(std::unique_ptr<tson::IJson> jsonParser = std::make_unique<tson::Json11>()) : m_json {std::move(jsonParser)}
18  {
19  }
20 
21  inline explicit World(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser = std::make_unique<tson::Json11>());
22  #else
23  inline explicit World(std::unique_ptr<tson::IJson> jsonParser) : m_json {std::move(jsonParser)}
24  {
25  }
26 
27  inline explicit World(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser);
28  #endif
29  inline bool parse(const fs::path &path);
30  inline int loadMaps(tson::Tileson *parser); //tileson_forward.hpp
31  inline bool contains(std::string_view filename);
32  inline const WorldMapData *get(std::string_view filename) const;
33 
34  [[nodiscard]] inline const fs::path &getPath() const;
35  [[nodiscard]] inline const fs::path &getFolder() const;
36  [[nodiscard]] inline const std::vector<WorldMapData> &getMapData() const;
37  [[nodiscard]] inline bool onlyShowAdjacentMaps() const;
38  [[nodiscard]] inline const std::string &getType() const;
39  [[nodiscard]] inline const std::vector<std::unique_ptr<tson::Map>> &getMaps() const;
40 
41  private:
42  inline void parseJson(IJson &json);
43 
44  std::unique_ptr<IJson> m_json = nullptr;
45  fs::path m_path;
46  fs::path m_folder;
47  std::vector<WorldMapData> m_mapData;
48  std::vector<std::unique_ptr<tson::Map>> m_maps;
49  bool m_onlyShowAdjacentMaps;
50  std::string m_type;
51  };
52 
53  World::World(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser) : m_json {std::move(jsonParser)}
54  {
55  parse(path);
56  }
57 
58  bool World::parse(const fs::path &path)
59  {
60  m_path = path;
61  m_folder = m_path.parent_path();
62 
63  if(!m_json->parse(path))
64  return false;
65 
66  parseJson(*m_json);
67  return true;
68  }
69 
70  const fs::path &World::getPath() const
71  {
72  return m_path;
73  }
74 
75  const std::vector<WorldMapData> &World::getMapData() const
76  {
77  return m_mapData;
78  }
79 
81  {
82  return m_onlyShowAdjacentMaps;
83  }
84 
85  const std::string &World::getType() const
86  {
87  return m_type;
88  }
89 
90  void World::parseJson(IJson &json)
91  {
92  if(json.count("onlyShowAdjacentMaps") > 0) m_onlyShowAdjacentMaps = json["onlyShowAdjacentMaps"].get<bool>();
93  if(json.count("type") > 0) m_type = json["type"].get<std::string>();
94 
95  if(json["maps"].isArray())
96  {
97  auto &maps = json.array("maps");
98  std::for_each(maps.begin(), maps.end(), [&](std::unique_ptr<IJson> &item) { m_mapData.emplace_back(m_folder, *item); });
99  }
100  }
101 
102  const fs::path &World::getFolder() const
103  {
104  return m_folder;
105  }
106 
113  bool World::contains(std::string_view filename)
114  {
115  //Note: might be moved to std::ranges from C++20.
116  return std::any_of(m_mapData.begin(), m_mapData.end(), [&](const auto &item) { return item.fileName == filename; });
117  }
118 
124  const WorldMapData * World::get(std::string_view filename) const
125  {
126  auto iter = std::find_if(m_mapData.begin(), m_mapData.end(), [&](const auto &item) { return item.fileName == filename; });
127  return (iter == m_mapData.end()) ? nullptr : iter.operator->();
128  }
129 
136  const std::vector<std::unique_ptr<tson::Map>> &World::getMaps() const
137  {
138  return m_maps;
139  }
140 
141 }
142 
143 #endif //TILESON_WORLD_HPP
Definition: IJson.hpp:11
T get(std::string_view key)
Definition: IJson.hpp:72
virtual size_t count(std::string_view key) const =0
virtual std::vector< std::unique_ptr< IJson > > array()=0
Definition: tileson_parser.hpp:54
Definition: WorldMapData.hpp:11
Definition: World.hpp:14
int loadMaps(tson::Tileson *parser)
Definition: tileson_forward.hpp:153
const WorldMapData * get(std::string_view filename) const
Definition: World.hpp:124
bool onlyShowAdjacentMaps() const
Definition: World.hpp:80
const fs::path & getPath() const
Definition: World.hpp:70
World(std::unique_ptr< tson::IJson > jsonParser)
Definition: World.hpp:23
const std::string & getType() const
Definition: World.hpp:85
bool parse(const fs::path &path)
Definition: World.hpp:58
const fs::path & getFolder() const
Definition: World.hpp:102
const std::vector< std::unique_ptr< tson::Map > > & getMaps() const
Definition: World.hpp:136
const std::vector< WorldMapData > & getMapData() const
Definition: World.hpp:75
bool contains(std::string_view filename)
Definition: World.hpp:113
Definition: Base64.hpp:12