supertux
autotile.hpp
1 // SuperTux
2 // Copyright (C) 2020 A. Semphris <semphris@protonmail.com>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_SUPERTUX_AUTOTILE_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_AUTOTILE_HPP
19 
20 #include <algorithm>
21 #include <memory>
22 #include <stdint.h>
23 #include <string>
24 #include <vector>
25 
26 class AutotileMask final
27 {
28 public:
29  AutotileMask(uint8_t mask, bool center);
30 
31  bool matches(uint8_t mask, bool center) const;
32 
33  uint8_t get_mask() const { return m_mask; }
34 
35 private:
36  uint8_t m_mask;
37  bool m_center; // m_center should *always* be the same as the m_solid of the corresponding Autotile
38 };
39 
40 class Autotile final
41 {
42 public:
43  Autotile(uint32_t tile_id,
44  const std::vector<std::pair<uint32_t, float>>& alt_tiles,
45  const std::vector<AutotileMask>& masks,
46  bool solid);
47 
48  bool matches(uint8_t mask, bool center) const;
49 
51  uint32_t get_tile_id() const { return m_tile_id; }
52 
54  uint32_t pick_tile(int x, int y) const;
55 
57  bool is_amongst(uint32_t tile) const;
58 
60  uint8_t get_first_mask() const;
61 
63  const std::vector<std::pair<uint32_t, float>>& get_all_tile_ids() const { return m_alt_tiles; }
64 
66  bool is_solid() const { return m_solid; }
67 
68 private:
69  uint32_t m_tile_id;
70  std::vector<std::pair<uint32_t, float>> m_alt_tiles;
71  std::vector<AutotileMask> m_masks;
72  bool m_solid;
73 
74 private:
75  Autotile(const Autotile&) = delete;
76  Autotile& operator=(const Autotile&) = delete;
77 };
78 
79 class AutotileSet final
80 {
81 public:
82  // Moved to tile_set.hpp
83  //static AutotileSet* get_tileset_from_tile(uint32_t tile_id);
84 
85 public:
86  AutotileSet(const std::vector<Autotile*>& autotiles, uint32_t default_tile, const std::string& name, bool corner);
87  ~AutotileSet();
88 
93  uint32_t get_autotile(uint32_t tile_id,
94  bool top_left, bool top, bool top_right,
95  bool left, bool center, bool right,
96  bool bottom_left, bool bottom, bool bottom_right,
97  int x, int y
98  ) const;
99 
101  uint32_t get_default_tile() const { return m_default; }
102 
104  bool is_member(uint32_t tile_id) const;
105 
107  bool is_solid(uint32_t tile_id) const;
108 
110  bool is_corner() const { return m_corner; }
111 
115  uint8_t get_mask_from_tile(uint32_t tile) const;
116 
117  // TODO : Validate autotile config files by checking if each mask has
118  // one and only one corresponding tile.
119  void validate() const;
120 
121 public:
122  static std::vector<std::unique_ptr<AutotileSet>> m_autotilesets;
123 
124 private:
125  std::vector<Autotile*> m_autotiles;
126  uint32_t m_default;
127  std::string m_name;
128  bool m_corner;
129 
130 private:
131  AutotileSet(const AutotileSet&) = delete;
132  AutotileSet& operator=(const AutotileSet&) = delete;
133 };
134 
135 #endif
136 
137 /* EOF */
uint32_t get_default_tile() const
Returns the id of the first block in the autotileset.
Definition: autotile.hpp:101
Definition: autotile.hpp:40
bool is_solid() const
Returns true if the "center" bool of masks are true.
Definition: autotile.hpp:66
bool is_corner() const
true if this is a corner-based autotileset
Definition: autotile.hpp:110
uint32_t get_tile_id() const
Definition: autotile.hpp:51
Definition: autotile.hpp:26
const std::vector< std::pair< uint32_t, float > > & get_all_tile_ids() const
Returns all possible tiles for this autotile.
Definition: autotile.hpp:63
Definition: autotile.hpp:79