Tileson  1.3.0
A helpful json parser for Tiled maps
Chunk.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 22.03.2020.
3 //
4 
5 #ifndef TILESON_CHUNK_HPP
6 #define TILESON_CHUNK_HPP
7 
8 //#include "../external/json.hpp"
9 #include "../objects/Vector2.hpp"
10 
11 namespace tson
12 {
13  class Chunk
14  {
15  public:
16  inline Chunk() = default;
17  inline explicit Chunk(IJson &json);
18  inline bool parse(IJson &json);
19 
20  [[nodiscard]] inline const std::vector<int> &getData() const;
21  [[nodiscard]] inline const std::string &getBase64Data() const;
22  [[nodiscard]] inline const Vector2i &getSize() const;
23  [[nodiscard]] inline const Vector2i &getPosition() const;
24 
25  private:
26  std::vector<int> m_data;
27  std::string m_base64Data;
28  tson::Vector2i m_size;
29  tson::Vector2i m_position;
30  };
31 }
32 
33 #endif //TILESON_CHUNK_HPP
34 
40 {
41  parse(json);
42 }
43 
50 {
51  bool allFound = true;
52 
53  if(json.count("width") > 0 && json.count("height") > 0)
54  m_size = {json["width"].get<int>(), json["height"].get<int>()}; else allFound = false;
55  if(json.count("x") > 0 && json.count("y") > 0)
56  m_position = {json["x"].get<int>(), json["y"].get<int>()}; else allFound = false;
57 
58  //Handle DATA (Optional)
59  if(json.count("data") > 0)
60  {
61  if(json["data"].isArray())
62  {
63  auto &data = json.array("data");
64  std::for_each(data.begin(), data.end(), [&](std::unique_ptr<IJson> &item) { m_data.push_back(item->get<int>()); });
65  }
66  else
67  m_base64Data = json["data"].get<std::string>();
68  }
69 
70  return allFound;
71 }
72 
77 const std::vector<int> &tson::Chunk::getData() const
78 {
79  return m_data;
80 }
81 
86 const std::string &tson::Chunk::getBase64Data() const
87 {
88  return m_base64Data;
89 }
90 
96 {
97  return m_size;
98 }
99 
105 {
106  return m_position;
107 }
Definition: Chunk.hpp:14
Chunk()=default
const Vector2i & getSize() const
Definition: Chunk.hpp:95
const std::string & getBase64Data() const
Definition: Chunk.hpp:86
const std::vector< int > & getData() const
Definition: Chunk.hpp:77
bool parse(IJson &json)
Definition: Chunk.hpp:49
const Vector2i & getPosition() const
Definition: Chunk.hpp:104
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: Base64.hpp:12