autotile.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef HEADER_SUPERTUX_SUPERTUX_AUTOTILE_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_AUTOTILE_HPP
  18. #include <algorithm>
  19. #include <memory>
  20. #include <stdint.h>
  21. #include <string>
  22. #include <vector>
  23. class AutotileMask final
  24. {
  25. public:
  26. AutotileMask(uint8_t mask, bool center);
  27. bool matches(uint8_t mask, bool center) const;
  28. uint8_t get_mask() const { return m_mask; }
  29. private:
  30. uint8_t m_mask;
  31. bool m_center; // m_center should *always* be the same as the m_solid of the corresponding Autotile
  32. };
  33. class Autotile final
  34. {
  35. public:
  36. Autotile(uint32_t tile_id,
  37. const std::vector<std::pair<uint32_t, float>>& alt_tiles,
  38. const std::vector<AutotileMask>& masks,
  39. bool solid);
  40. bool matches(uint8_t mask, bool center) const;
  41. /** @deprecated Returns the base tile ID. */
  42. uint32_t get_tile_id() const { return m_tile_id; }
  43. /** Picks a tile randomly amongst the possible ones for this autotile. */
  44. uint32_t pick_tile(int x, int y) const;
  45. /** @returns true if the given tile has this tile id */
  46. bool is_amongst(uint32_t tile) const;
  47. /** @returns the first accessible mask for that autotile */
  48. uint8_t get_first_mask() const;
  49. /** Returns all possible tiles for this autotile */
  50. const std::vector<std::pair<uint32_t, float>>& get_all_tile_ids() const { return m_alt_tiles; }
  51. /** Returns true if the "center" bool of masks are true. All masks of given Autotile must have the same value for their "center" property.*/
  52. bool is_solid() const { return m_solid; }
  53. private:
  54. uint32_t m_tile_id;
  55. std::vector<std::pair<uint32_t, float>> m_alt_tiles;
  56. std::vector<AutotileMask> m_masks;
  57. bool m_solid;
  58. private:
  59. Autotile(const Autotile&) = delete;
  60. Autotile& operator=(const Autotile&) = delete;
  61. };
  62. class AutotileSet final
  63. {
  64. public:
  65. // Moved to tile_set.hpp
  66. //static AutotileSet* get_tileset_from_tile(uint32_t tile_id);
  67. public:
  68. AutotileSet(const std::vector<Autotile*>& autotiles, uint32_t default_tile, const std::string& name, bool corner);
  69. ~AutotileSet();
  70. /** Returns the ID of the tile to use, based on the surrounding tiles.
  71. * If the autotileset is corner-based, the top, left, right, bottom and
  72. * center attributes are ignored.
  73. */
  74. uint32_t get_autotile(uint32_t tile_id,
  75. bool top_left, bool top, bool top_right,
  76. bool left, bool center, bool right,
  77. bool bottom_left, bool bottom, bool bottom_right,
  78. int x, int y
  79. ) const;
  80. /** Returns the id of the first block in the autotileset. Used for erronous configs. */
  81. uint32_t get_default_tile() const { return m_default; }
  82. /** true if the given tile is present in the autotileset */
  83. bool is_member(uint32_t tile_id) const;
  84. /** true if is_member() is true AND the "center" bool is true */
  85. bool is_solid(uint32_t tile_id) const;
  86. /** true if this is a corner-based autotileset */
  87. bool is_corner() const { return m_corner; }
  88. /** Returns the first mask corresponding to the current tile
  89. * (useful for corners-based autotilesets)
  90. */
  91. uint8_t get_mask_from_tile(uint32_t tile) const;
  92. // TODO : Validate autotile config files by checking if each mask has
  93. // one and only one corresponding tile.
  94. void validate() const;
  95. public:
  96. static std::vector<std::unique_ptr<AutotileSet>> m_autotilesets;
  97. private:
  98. std::vector<Autotile*> m_autotiles;
  99. uint32_t m_default;
  100. std::string m_name;
  101. bool m_corner;
  102. private:
  103. AutotileSet(const AutotileSet&) = delete;
  104. AutotileSet& operator=(const AutotileSet&) = delete;
  105. };
  106. #endif
  107. /* EOF */