Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
Terrain.hpp
Go to the documentation of this file.
1//
2// Created by robin on 22.03.2020.
3//
4
5#ifndef TILESON_TERRAIN_HPP
6#define TILESON_TERRAIN_HPP
7
8//#include "../external/json.hpp"
9#include "../objects/PropertyCollection.hpp"
10
11namespace tson
12{
13 class Terrain
14 {
15 public:
16 inline Terrain() = default;
17 inline Terrain(std::string name, int tile);
18 inline explicit Terrain(IJson &json);
19
20 inline bool parse(IJson &json);
21
22 [[nodiscard]] inline const std::string &getName() const;
23 [[nodiscard]] inline int getTile() const;
24 [[nodiscard]] inline PropertyCollection &getProperties();
25
26 template <typename T>
27 inline T get(const std::string &name);
28 inline tson::Property * getProp(const std::string &name);
29
30 private:
31 std::string m_name;
32 int m_tile {};
33 tson::PropertyCollection m_properties;
34 };
35
42 template<typename T>
43 T tson::Terrain::get(const std::string &name)
44 {
45 return m_properties.getValue<T>(name);
46 }
47}
48
49tson::Terrain::Terrain(std::string name, int tile) : m_name {std::move(name)}, m_tile {tile}
50{
51
52}
53
55{
56 parse(json);
57}
58
60{
61 bool allFound = true;
62
63 if(json.count("name") > 0) m_name = json["name"].get<std::string>(); else allFound = false;
64 if(json.count("tile") > 0) m_tile = json["tile"].get<int>(); else allFound = false;
65
66 if(json.count("properties") > 0 && json["properties"].isArray())
67 {
68 auto &properties = json.array("properties");
69 std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item); });
70 }
71
72 return allFound;
73}
74
79const std::string &tson::Terrain::getName() const
80{
81 return m_name;
82}
83
89{
90 return m_tile;
91}
92
98{
99 return m_properties;
100}
101
107tson::Property *tson::Terrain::getProp(const std::string &name)
108{
109 if(m_properties.hasProperty(name))
110 return m_properties.getProperty(name);
111 return nullptr;
112}
113
114#endif //TILESON_TERRAIN_HPP
Definition IJson.hpp:11
T get(std::string_view key)
Definition IJson.hpp:82
virtual bool isArray() const =0
virtual size_t count(std::string_view key) const =0
virtual std::vector< std::unique_ptr< IJson > > array()=0
Definition PropertyCollection.hpp:15
T getValue(const std::string &name)
Definition PropertyCollection.hpp:47
Definition Property.hpp:23
Definition Terrain.hpp:14
PropertyCollection & getProperties()
Definition Terrain.hpp:97
bool parse(IJson &json)
Definition Terrain.hpp:59
tson::Property * getProp(const std::string &name)
Definition Terrain.hpp:107
T get(const std::string &name)
Definition Terrain.hpp:43
Terrain()=default
const std::string & getName() const
Definition Terrain.hpp:79
int getTile() const
Definition Terrain.hpp:88
Definition Base64.hpp:12