levelz-cpp 0.2.0
Loading...
Searching...
No Matches
block.hpp
1#pragma once
2
3#include <string>
4#include <unordered_map>
5
6namespace LevelZ {
7
11 struct Block {
12 public:
16 std::string name;
17
21 std::unordered_map<std::string, std::string> properties;
22
26 explicit Block(std::string name) : name(name) {}
27
33 Block(std::string name, std::unordered_map<std::string, std::string> properties) : name(name), properties(properties) {}
34
40 std::string getProperty(std::string key) const {
41 return properties.at(key);
42 }
43
50 std::string getProperty(std::string key, std::string defaultValue) const {
51 return properties.find(key) != properties.end() ? properties.at(key) : defaultValue;
52 }
53
59 bool hasProperty(std::string key) const {
60 return properties.find(key) != properties.end();
61 }
62
68 bool operator==(const Block& other) const {
69 return name == other.name && properties == other.properties;
70 }
71
77 bool operator!=(const Block& other) const {
78 return name != other.name || properties != other.properties;
79 }
80
85 std::string to_string() const {
86 std::string str;
87 if (properties.empty()) return name;
88
89 str += name + "<";
90
91 for (auto const& [k, v] : properties) {
92 str += k + "=" + v;
93 }
94
95 str += ">";
96
97 return str;
98 }
99
105 std::ostream& operator<<(std::ostream &strm) {
106 return strm << to_string();
107 }
108 };
109
113 struct LevelObject {
114 private:
115 Coordinate* _coordinate;
116 Block _block;
117 public:
124
131
136 inline Block block() {
137 return _block;
138 }
139
145 return _coordinate;
146 }
147
153 bool operator==(const LevelObject& other) const {
154 return _block == other._block && _coordinate->getMagnitude() == other._coordinate->getMagnitude();
155 }
156
162 bool operator!=(const LevelObject& other) const {
163 return _block != other._block || _coordinate->getMagnitude() != other._coordinate->getMagnitude();
164 }
165
170 std::string to_string() const {
171 return _block.to_string() + ": " + _coordinate->to_string();
172 }
173
179 std::ostream& operator<<(std::ostream &strm) {
180 return strm << to_string();
181 }
182 };
183
184}
Definition block.hpp:11
std::string to_string() const
Definition block.hpp:85
std::string getProperty(std::string key) const
Definition block.hpp:40
std::string getProperty(std::string key, std::string defaultValue) const
Definition block.hpp:50
bool operator==(const Block &other) const
Definition block.hpp:68
bool hasProperty(std::string key) const
Definition block.hpp:59
Block(std::string name)
Definition block.hpp:26
bool operator!=(const Block &other) const
Definition block.hpp:77
Block(std::string name, std::unordered_map< std::string, std::string > properties)
Definition block.hpp:33
std::ostream & operator<<(std::ostream &strm)
Definition block.hpp:105
std::unordered_map< std::string, std::string > properties
Definition block.hpp:21
std::string name
Definition block.hpp:16
Definition coordinate.hpp:28
Definition coordinate.hpp:177
Definition coordinate.hpp:11
virtual double getMagnitude() const =0
virtual std::string to_string() const =0
Definition block.hpp:113
std::string to_string() const
Definition block.hpp:170
LevelObject(Block block, Coordinate2D coordinate)
Definition block.hpp:123
Coordinate * coordinate()
Definition block.hpp:144
bool operator!=(const LevelObject &other) const
Definition block.hpp:162
Block block()
Definition block.hpp:136
std::ostream & operator<<(std::ostream &strm)
Definition block.hpp:179
bool operator==(const LevelObject &other) const
Definition block.hpp:153
LevelObject(Block block, Coordinate3D coordinate)
Definition block.hpp:130