tile_set.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*************************************************************************/
  2. /* tile_set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TILE_SET_H
  31. #define TILE_SET_H
  32. #include "core/array.h"
  33. #include "core/resource.h"
  34. #include "scene/2d/light_occluder_2d.h"
  35. #include "scene/2d/navigation_polygon.h"
  36. #include "scene/resources/convex_polygon_shape_2d.h"
  37. #include "scene/resources/shape_2d.h"
  38. #include "scene/resources/texture.h"
  39. class TileSet : public Resource {
  40. GDCLASS(TileSet, Resource);
  41. public:
  42. struct ShapeData {
  43. Ref<Shape2D> shape;
  44. Transform2D shape_transform;
  45. Vector2 autotile_coord;
  46. bool one_way_collision;
  47. float one_way_collision_margin;
  48. ShapeData() {
  49. one_way_collision = false;
  50. one_way_collision_margin = 1.0;
  51. }
  52. };
  53. enum BitmaskMode {
  54. BITMASK_2X2,
  55. BITMASK_3X3_MINIMAL,
  56. BITMASK_3X3
  57. };
  58. enum AutotileBindings {
  59. BIND_TOPLEFT = 1,
  60. BIND_TOP = 2,
  61. BIND_TOPRIGHT = 4,
  62. BIND_LEFT = 8,
  63. BIND_CENTER = 16,
  64. BIND_RIGHT = 32,
  65. BIND_BOTTOMLEFT = 64,
  66. BIND_BOTTOM = 128,
  67. BIND_BOTTOMRIGHT = 256,
  68. BIND_IGNORE_TOPLEFT = 1 << 16,
  69. BIND_IGNORE_TOP = 1 << 17,
  70. BIND_IGNORE_TOPRIGHT = 1 << 18,
  71. BIND_IGNORE_LEFT = 1 << 19,
  72. BIND_IGNORE_CENTER = 1 << 20,
  73. BIND_IGNORE_RIGHT = 1 << 21,
  74. BIND_IGNORE_BOTTOMLEFT = 1 << 22,
  75. BIND_IGNORE_BOTTOM = 1 << 23,
  76. BIND_IGNORE_BOTTOMRIGHT = 1 << 24
  77. };
  78. enum TileMode {
  79. SINGLE_TILE,
  80. AUTO_TILE,
  81. ATLAS_TILE
  82. };
  83. struct AutotileData {
  84. BitmaskMode bitmask_mode;
  85. Size2 size;
  86. int spacing;
  87. Vector2 icon_coord;
  88. Map<Vector2, uint32_t> flags;
  89. Map<Vector2, Ref<OccluderPolygon2D> > occluder_map;
  90. Map<Vector2, Ref<NavigationPolygon> > navpoly_map;
  91. Map<Vector2, int> priority_map;
  92. Map<Vector2, int> z_index_map;
  93. // Default size to prevent invalid value
  94. explicit AutotileData() :
  95. bitmask_mode(BITMASK_2X2),
  96. size(64, 64),
  97. spacing(0),
  98. icon_coord(0, 0) {}
  99. };
  100. private:
  101. struct TileData {
  102. String name;
  103. Ref<Texture> texture;
  104. Ref<Texture> normal_map;
  105. Vector2 offset;
  106. Rect2i region;
  107. Vector<ShapeData> shapes_data;
  108. Vector2 occluder_offset;
  109. Ref<OccluderPolygon2D> occluder;
  110. Vector2 navigation_polygon_offset;
  111. Ref<NavigationPolygon> navigation_polygon;
  112. Ref<ShaderMaterial> material;
  113. TileMode tile_mode;
  114. Color modulate;
  115. AutotileData autotile_data;
  116. int z_index;
  117. // Default modulate for back-compat
  118. explicit TileData() :
  119. tile_mode(SINGLE_TILE),
  120. modulate(1, 1, 1),
  121. z_index(0) {}
  122. };
  123. Map<int, TileData> tile_map;
  124. protected:
  125. bool _set(const StringName &p_name, const Variant &p_value);
  126. bool _get(const StringName &p_name, Variant &r_ret) const;
  127. void _get_property_list(List<PropertyInfo> *p_list) const;
  128. void _tile_set_shapes(int p_id, const Array &p_shapes);
  129. Array _tile_get_shapes(int p_id) const;
  130. Array _get_tiles_ids() const;
  131. void _decompose_convex_shape(Ref<Shape2D> p_shape);
  132. static void _bind_methods();
  133. public:
  134. void create_tile(int p_id);
  135. void autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode);
  136. BitmaskMode autotile_get_bitmask_mode(int p_id) const;
  137. void tile_set_name(int p_id, const String &p_name);
  138. String tile_get_name(int p_id) const;
  139. void tile_set_texture(int p_id, const Ref<Texture> &p_texture);
  140. Ref<Texture> tile_get_texture(int p_id) const;
  141. void tile_set_normal_map(int p_id, const Ref<Texture> &p_normal_map);
  142. Ref<Texture> tile_get_normal_map(int p_id) const;
  143. void tile_set_texture_offset(int p_id, const Vector2 &p_offset);
  144. Vector2 tile_get_texture_offset(int p_id) const;
  145. void tile_set_region(int p_id, const Rect2 &p_region);
  146. Rect2 tile_get_region(int p_id) const;
  147. void tile_set_tile_mode(int p_id, TileMode p_tile_mode);
  148. TileMode tile_get_tile_mode(int p_id) const;
  149. void autotile_set_icon_coordinate(int p_id, Vector2 coord);
  150. Vector2 autotile_get_icon_coordinate(int p_id) const;
  151. void autotile_set_spacing(int p_id, int p_spacing);
  152. int autotile_get_spacing(int p_id) const;
  153. void autotile_set_size(int p_id, Size2 p_size);
  154. Size2 autotile_get_size(int p_id) const;
  155. void autotile_clear_bitmask_map(int p_id);
  156. void autotile_set_subtile_priority(int p_id, const Vector2 &p_coord, int p_priority);
  157. int autotile_get_subtile_priority(int p_id, const Vector2 &p_coord);
  158. const Map<Vector2, int> &autotile_get_priority_map(int p_id) const;
  159. void autotile_set_z_index(int p_id, const Vector2 &p_coord, int p_z_index);
  160. int autotile_get_z_index(int p_id, const Vector2 &p_coord);
  161. const Map<Vector2, int> &autotile_get_z_index_map(int p_id) const;
  162. void autotile_set_bitmask(int p_id, Vector2 p_coord, uint32_t p_flag);
  163. uint32_t autotile_get_bitmask(int p_id, Vector2 p_coord);
  164. const Map<Vector2, uint32_t> &autotile_get_bitmask_map(int p_id);
  165. Vector2 autotile_get_subtile_for_bitmask(int p_id, uint16_t p_bitmask, const Node *p_tilemap_node = NULL, const Vector2 &p_tile_location = Vector2());
  166. Vector2 atlastile_get_subtile_by_priority(int p_id, const Node *p_tilemap_node = NULL, const Vector2 &p_tile_location = Vector2());
  167. void tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape);
  168. Ref<Shape2D> tile_get_shape(int p_id, int p_shape_id) const;
  169. void tile_set_shape_transform(int p_id, int p_shape_id, const Transform2D &p_offset);
  170. Transform2D tile_get_shape_transform(int p_id, int p_shape_id) const;
  171. void tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset);
  172. Vector2 tile_get_shape_offset(int p_id, int p_shape_id) const;
  173. void tile_set_shape_one_way(int p_id, int p_shape_id, bool p_one_way);
  174. bool tile_get_shape_one_way(int p_id, int p_shape_id) const;
  175. void tile_set_shape_one_way_margin(int p_id, int p_shape_id, float p_margin);
  176. float tile_get_shape_one_way_margin(int p_id, int p_shape_id) const;
  177. void tile_clear_shapes(int p_id);
  178. void tile_add_shape(int p_id, const Ref<Shape2D> &p_shape, const Transform2D &p_transform, bool p_one_way = false, const Vector2 &p_autotile_coord = Vector2());
  179. int tile_get_shape_count(int p_id) const;
  180. void tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes);
  181. Vector<ShapeData> tile_get_shapes(int p_id) const;
  182. void tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material);
  183. Ref<ShaderMaterial> tile_get_material(int p_id) const;
  184. void tile_set_modulate(int p_id, const Color &p_modulate);
  185. Color tile_get_modulate(int p_id) const;
  186. void tile_set_occluder_offset(int p_id, const Vector2 &p_offset);
  187. Vector2 tile_get_occluder_offset(int p_id) const;
  188. void tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder);
  189. Ref<OccluderPolygon2D> tile_get_light_occluder(int p_id) const;
  190. void autotile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder, const Vector2 &p_coord);
  191. Ref<OccluderPolygon2D> autotile_get_light_occluder(int p_id, const Vector2 &p_coord) const;
  192. const Map<Vector2, Ref<OccluderPolygon2D> > &autotile_get_light_oclusion_map(int p_id) const;
  193. void tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset);
  194. Vector2 tile_get_navigation_polygon_offset(int p_id) const;
  195. void tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon);
  196. Ref<NavigationPolygon> tile_get_navigation_polygon(int p_id) const;
  197. void autotile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon, const Vector2 &p_coord);
  198. Ref<NavigationPolygon> autotile_get_navigation_polygon(int p_id, const Vector2 &p_coord) const;
  199. const Map<Vector2, Ref<NavigationPolygon> > &autotile_get_navigation_map(int p_id) const;
  200. void tile_set_z_index(int p_id, int p_z_index);
  201. int tile_get_z_index(int p_id) const;
  202. void remove_tile(int p_id);
  203. bool has_tile(int p_id) const;
  204. bool is_tile_bound(int p_drawn_id, int p_neighbor_id);
  205. int find_tile_by_name(const String &p_name) const;
  206. void get_tile_list(List<int> *p_tiles) const;
  207. void clear();
  208. int get_last_unused_tile_id() const;
  209. TileSet();
  210. };
  211. VARIANT_ENUM_CAST(TileSet::AutotileBindings);
  212. VARIANT_ENUM_CAST(TileSet::BitmaskMode);
  213. VARIANT_ENUM_CAST(TileSet::TileMode);
  214. #endif // TILE_SET_H