NasNas
An intuitive and beginner friendly 2D game framework for C++
Tile.hpp
1 // Created by Modar Nasser on 24/06/2021.
2 
3 #pragma once
4 
5 #include <cstdint>
6 #include <optional>
7 #include <string>
8 #include <vector>
9 
10 #include <SFML/System/Vector2.hpp>
11 
12 #include <NasNas/core/data/Rect.hpp>
13 #include <NasNas/tilemapping/PropertiesContainer.hpp>
14 
15 namespace ns::tm {
16  class TilesetData;
17 
18  class TileData : public PropertiesContainer {
19  friend TilesetData;
20  struct AnimFrame {
21  std::uint32_t tileid;
22  int duration;
23  };
24  void fill(const pugi::xml_node& xml_node);
25  public:
26  explicit TileData(std::uint32_t tile_id);
27  const std::uint32_t id;
28  std::string type;
29  std::vector<AnimFrame> animframes;
30  };
31 
32  struct Tile {
33  enum class Flip : std::uint8_t {
34  None = 0x0,
35  HorizontalFlip = 0x8,
36  VerticalFlip = 0x4,
37  DiagonalFlip = 0x2,
38  Rotation90 = HorizontalFlip | DiagonalFlip,
39  Rotation180 = HorizontalFlip | VerticalFlip,
40  Rotation270 = VerticalFlip | DiagonalFlip
41  };
42  static std::optional<Tile> None;
43  static std::uint32_t gidmask;
44  static inline auto getFlipFromGid(std::uint32_t gid) -> Flip {
45  return static_cast<Flip>((gid & ~Tile::gidmask) >> 28u);
46  }
47 
48  Tile(const TileData& tiledata, const TilesetData& tilesetdata, std::uint32_t tilegid, int posx, int posy, Flip tileflip=Flip::None);
49  auto getTileTexCoo() const -> std::vector<sf::Vector2f>;
50  auto getTileTextureRect() const -> ns::FloatRect;
51  const TileData& data;
52  const TilesetData& tileset;
53  const std::uint32_t gid;
54  const int x;
55  const int y;
56  const Flip flip;
57  };
58 
59  auto operator&(Tile::Flip lhs, Tile::Flip rhs) -> Tile::Flip;
60  auto operator==(const std::optional<Tile>& lhs, const std::optional<Tile>& rhs) -> bool;
61  auto operator!=(const std::optional<Tile>& lhs, const std::optional<Tile>& rhs) -> bool;
62 }