tile_set.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* tile_set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. };
  69. enum TileMode {
  70. SINGLE_TILE,
  71. AUTO_TILE,
  72. ATLAS_TILE
  73. };
  74. struct AutotileData {
  75. BitmaskMode bitmask_mode;
  76. Size2 size;
  77. int spacing;
  78. Vector2 icon_coord;
  79. Map<Vector2, uint16_t> flags;
  80. Map<Vector2, Ref<OccluderPolygon2D> > occluder_map;
  81. Map<Vector2, Ref<NavigationPolygon> > navpoly_map;
  82. Map<Vector2, int> priority_map;
  83. Map<Vector2, int> z_index_map;
  84. // Default size to prevent invalid value
  85. explicit AutotileData() :
  86. bitmask_mode(BITMASK_2X2),
  87. size(64, 64),
  88. spacing(0),
  89. icon_coord(0, 0) {}
  90. };
  91. private:
  92. struct TileData {
  93. String name;
  94. Ref<Texture> texture;
  95. Ref<Texture> normal_map;
  96. Vector2 offset;
  97. Rect2i region;
  98. Vector<ShapeData> shapes_data;
  99. Vector2 occluder_offset;
  100. Ref<OccluderPolygon2D> occluder;
  101. Vector2 navigation_polygon_offset;
  102. Ref<NavigationPolygon> navigation_polygon;
  103. Ref<ShaderMaterial> material;
  104. TileMode tile_mode;
  105. Color modulate;
  106. AutotileData autotile_data;
  107. int z_index;
  108. // Default modulate for back-compat
  109. explicit TileData() :
  110. tile_mode(SINGLE_TILE),
  111. modulate(1, 1, 1),
  112. z_index(0) {}
  113. };
  114. Map<int, TileData> tile_map;
  115. protected:
  116. bool _set(const StringName &p_name, const Variant &p_value);
  117. bool _get(const StringName &p_name, Variant &r_ret) const;
  118. void _get_property_list(List<PropertyInfo> *p_list) const;
  119. void _tile_set_shapes(int p_id, const Array &p_shapes);
  120. Array _tile_get_shapes(int p_id) const;
  121. Array _get_tiles_ids() const;
  122. void _decompose_convex_shape(Ref<Shape2D> p_shape);
  123. static void _bind_methods();
  124. public:
  125. void create_tile(int p_id);
  126. void autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode);
  127. BitmaskMode autotile_get_bitmask_mode(int p_id) const;
  128. void tile_set_name(int p_id, const String &p_name);
  129. String tile_get_name(int p_id) const;
  130. void tile_set_texture(int p_id, const Ref<Texture> &p_texture);
  131. Ref<Texture> tile_get_texture(int p_id) const;
  132. void tile_set_normal_map(int p_id, const Ref<Texture> &p_normal_map);
  133. Ref<Texture> tile_get_normal_map(int p_id) const;
  134. void tile_set_texture_offset(int p_id, const Vector2 &p_offset);
  135. Vector2 tile_get_texture_offset(int p_id) const;
  136. void tile_set_region(int p_id, const Rect2 &p_region);
  137. Rect2 tile_get_region(int p_id) const;
  138. void tile_set_tile_mode(int p_id, TileMode p_tile_mode);
  139. TileMode tile_get_tile_mode(int p_id) const;
  140. void autotile_set_icon_coordinate(int p_id, Vector2 coord);
  141. Vector2 autotile_get_icon_coordinate(int p_id) const;
  142. void autotile_set_spacing(int p_id, int p_spacing);
  143. int autotile_get_spacing(int p_id) const;
  144. void autotile_set_size(int p_id, Size2 p_size);
  145. Size2 autotile_get_size(int p_id) const;
  146. void autotile_clear_bitmask_map(int p_id);
  147. void autotile_set_subtile_priority(int p_id, const Vector2 &p_coord, int p_priority);
  148. int autotile_get_subtile_priority(int p_id, const Vector2 &p_coord);
  149. const Map<Vector2, int> &autotile_get_priority_map(int p_id) const;
  150. void autotile_set_z_index(int p_id, const Vector2 &p_coord, int p_z_index);
  151. int autotile_get_z_index(int p_id, const Vector2 &p_coord);
  152. const Map<Vector2, int> &autotile_get_z_index_map(int p_id) const;
  153. void autotile_set_bitmask(int p_id, Vector2 p_coord, uint16_t p_flag);
  154. uint16_t autotile_get_bitmask(int p_id, Vector2 p_coord);
  155. const Map<Vector2, uint16_t> &autotile_get_bitmask_map(int p_id);
  156. 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());
  157. void tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape);
  158. Ref<Shape2D> tile_get_shape(int p_id, int p_shape_id) const;
  159. void tile_set_shape_transform(int p_id, int p_shape_id, const Transform2D &p_offset);
  160. Transform2D tile_get_shape_transform(int p_id, int p_shape_id) const;
  161. void tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset);
  162. Vector2 tile_get_shape_offset(int p_id, int p_shape_id) const;
  163. void tile_set_shape_one_way(int p_id, int p_shape_id, bool p_one_way);
  164. bool tile_get_shape_one_way(int p_id, int p_shape_id) const;
  165. void tile_set_shape_one_way_margin(int p_id, int p_shape_id, float p_margin);
  166. float tile_get_shape_one_way_margin(int p_id, int p_shape_id) const;
  167. void tile_clear_shapes(int p_id);
  168. 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());
  169. int tile_get_shape_count(int p_id) const;
  170. void tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes);
  171. Vector<ShapeData> tile_get_shapes(int p_id) const;
  172. void tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material);
  173. Ref<ShaderMaterial> tile_get_material(int p_id) const;
  174. void tile_set_modulate(int p_id, const Color &p_modulate);
  175. Color tile_get_modulate(int p_id) const;
  176. void tile_set_occluder_offset(int p_id, const Vector2 &p_offset);
  177. Vector2 tile_get_occluder_offset(int p_id) const;
  178. void tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder);
  179. Ref<OccluderPolygon2D> tile_get_light_occluder(int p_id) const;
  180. void autotile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder, const Vector2 &p_coord);
  181. Ref<OccluderPolygon2D> autotile_get_light_occluder(int p_id, const Vector2 &p_coord) const;
  182. const Map<Vector2, Ref<OccluderPolygon2D> > &autotile_get_light_oclusion_map(int p_id) const;
  183. void tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset);
  184. Vector2 tile_get_navigation_polygon_offset(int p_id) const;
  185. void tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon);
  186. Ref<NavigationPolygon> tile_get_navigation_polygon(int p_id) const;
  187. void autotile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon, const Vector2 &p_coord);
  188. Ref<NavigationPolygon> autotile_get_navigation_polygon(int p_id, const Vector2 &p_coord) const;
  189. const Map<Vector2, Ref<NavigationPolygon> > &autotile_get_navigation_map(int p_id) const;
  190. void tile_set_z_index(int p_id, int p_z_index);
  191. int tile_get_z_index(int p_id) const;
  192. void remove_tile(int p_id);
  193. bool has_tile(int p_id) const;
  194. bool is_tile_bound(int p_drawn_id, int p_neighbor_id);
  195. int find_tile_by_name(const String &p_name) const;
  196. void get_tile_list(List<int> *p_tiles) const;
  197. void clear();
  198. int get_last_unused_tile_id() const;
  199. TileSet();
  200. };
  201. VARIANT_ENUM_CAST(TileSet::AutotileBindings);
  202. VARIANT_ENUM_CAST(TileSet::BitmaskMode);
  203. VARIANT_ENUM_CAST(TileSet::TileMode);
  204. #endif // TILE_SET_H