Peoplemon  0.1.0
Peoplemon 3 game source documentation
Tile.hpp
Go to the documentation of this file.
1 #ifndef CORE_MAPS_TILE_HPP
2 #define CORE_MAPS_TILE_HPP
3 
4 #include <BLIB/Graphics.hpp>
5 #include <BLIB/Serialization.hpp>
6 #include <SFML/Graphics.hpp>
7 #include <functional>
8 #include <variant>
9 
10 namespace editor
11 {
12 namespace component
13 {
14 class EditMap;
15 }
16 } // namespace editor
17 
18 namespace core
19 {
20 namespace map
21 {
22 class Tileset;
23 
30 class Tile {
31 public:
32  // The type of tile ids in Tileset
33  using IdType = std::uint16_t;
34 
36  static constexpr IdType Blank = 0;
37 
42  Tile();
43 
49  Tile(const Tile& copy) = default;
50 
57  Tile& operator=(const Tile& copy) = default;
58 
65  Tile(IdType id, bool isAnim = false);
66 
72  bool isAnimation() const;
73 
79  IdType id() const;
80 
88  void set(IdType id, bool anim);
89 
94  void step();
95 
96 private:
97  bool isAnim;
98  IdType tid;
99 
100  std::variant<std::monostate, bl::gfx::BatchSpriteSimple, bl::gfx::BatchSlideshowSimple,
101  std::shared_ptr<bl::gfx::Animation2D>>
102  renderObject;
103 
104  friend class Map;
106  friend struct bl::serial::SerializableObject<Tile>;
107 };
108 
109 } // namespace map
110 } // namespace core
111 
112 namespace bl
113 {
114 namespace serial
115 {
116 namespace binary
117 {
118 template<>
119 struct Serializer<core::map::Tile, false> {
120  static bool serialize(OutputStream& output, const core::map::Tile& tile) {
121  if (!Serializer<core::map::Tile::IdType>::serialize(output, tile.id())) return false;
122  return Serializer<bool>::serialize(output, tile.isAnimation());
123  }
124 
125  static bool deserialize(InputStream& input, core::map::Tile& result) {
127  bool isAnim;
128  if (!Serializer<core::map::Tile::IdType>::deserialize(input, id)) return false;
129  if (!Serializer<bool>::deserialize(input, isAnim)) return false;
130  result.set(id, isAnim);
131  return true;
132  }
133 
134  static std::uint32_t size(const core::map::Tile&) {
135  return Serializer<core::map::Tile::IdType>::size(core::map::Tile::Blank) +
136  Serializer<bool>::size(false);
137  }
138 };
139 } // namespace binary
140 
141 template<>
142 struct SerializableObject<core::map::Tile> : public SerializableObjectBase {
143  SerializableField<1, core::map::Tile, bool> anim;
144  SerializableField<2, core::map::Tile, core::map::Tile::IdType> id;
145 
147  : SerializableObjectBase("Tile")
148  , anim("anim", *this, &core::map::Tile::isAnim, SerializableFieldBase::Required{})
149  , id("id", *this, &core::map::Tile::tid, SerializableFieldBase::Required{}) {}
150 };
151 
152 } // namespace serial
153 } // namespace bl
154 
155 #endif
Core classes and functionality for both the editor and game.
bl::serial::json::Serializer< Player > Serializer
Definition: Player.cpp:27
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
Data representation of a tile in a Map TileLayer.
Definition: Tile.hpp:30
void step()
Triggers the animation when the tile is stepped on.
Definition: Tile.cpp:29
Tile(const Tile &copy)=default
Copy constructs from the given tile.
IdType id() const
Returns the id of the image or animation of this tile.
Definition: Tile.cpp:22
static constexpr IdType Blank
Special id for blank tiles.
Definition: Tile.hpp:36
std::uint16_t IdType
Definition: Tile.hpp:33
Tile & operator=(const Tile &copy)=default
Copy constructs from the given tile.
Tile()
Makes a blank tile.
Definition: Tile.cpp:10
bool isAnimation() const
Returns whether this tile is an animation or not.
Definition: Tile.cpp:20
void set(IdType id, bool anim)
Sets the information of the tile.
Definition: Tile.cpp:24
static bool serialize(OutputStream &output, const core::map::Tile &tile)
Definition: Tile.hpp:120
static std::uint32_t size(const core::map::Tile &)
Definition: Tile.hpp:134
static bool deserialize(InputStream &input, core::map::Tile &result)
Definition: Tile.hpp:125
SerializableField< 1, core::map::Tile, bool > anim
Definition: Tile.hpp:143
SerializableField< 2, core::map::Tile, core::map::Tile::IdType > id
Definition: Tile.hpp:144
Wrapper over the core::Map class that is directly usable in a bl::gui::GUI.
Definition: EditMap.hpp:28