tile_set.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/shape_2d.h"
  37. #include "scene/resources/texture.h"
  38. class TileSet : public Resource {
  39. GDCLASS(TileSet, Resource);
  40. public:
  41. struct ShapeData {
  42. Ref<Shape2D> shape;
  43. Transform2D shape_transform;
  44. Vector2 autotile_coord;
  45. bool one_way_collision;
  46. ShapeData() {
  47. one_way_collision = false;
  48. }
  49. };
  50. enum BitmaskMode {
  51. BITMASK_2X2,
  52. BITMASK_3X3_MINIMAL,
  53. BITMASK_3X3
  54. };
  55. enum AutotileBindings {
  56. BIND_TOPLEFT = 1,
  57. BIND_TOP = 2,
  58. BIND_TOPRIGHT = 4,
  59. BIND_LEFT = 8,
  60. BIND_CENTER = 16,
  61. BIND_RIGHT = 32,
  62. BIND_BOTTOMLEFT = 64,
  63. BIND_BOTTOM = 128,
  64. BIND_BOTTOMRIGHT = 256
  65. };
  66. enum TileMode {
  67. SINGLE_TILE,
  68. AUTO_TILE,
  69. ATLAS_TILE
  70. };
  71. struct AutotileData {
  72. BitmaskMode bitmask_mode;
  73. Size2 size;
  74. int spacing;
  75. Vector2 icon_coord;
  76. Map<Vector2, uint16_t> flags;
  77. Map<Vector2, Ref<OccluderPolygon2D> > occluder_map;
  78. Map<Vector2, Ref<NavigationPolygon> > navpoly_map;
  79. Map<Vector2, int> priority_map;
  80. Map<Vector2, int> z_index_map;
  81. // Default size to prevent invalid value
  82. explicit AutotileData() :
  83. bitmask_mode(BITMASK_2X2),
  84. size(64, 64),
  85. spacing(0),
  86. icon_coord(0, 0) {}
  87. };
  88. private:
  89. struct TileData {
  90. String name;
  91. Ref<Texture> texture;
  92. Ref<Texture> normal_map;
  93. Vector2 offset;
  94. Rect2i region;
  95. Vector<ShapeData> shapes_data;
  96. Vector2 occluder_offset;
  97. Ref<OccluderPolygon2D> occluder;
  98. Vector2 navigation_polygon_offset;
  99. Ref<NavigationPolygon> navigation_polygon;
  100. Ref<ShaderMaterial> material;
  101. TileMode tile_mode;
  102. Color modulate;
  103. AutotileData autotile_data;
  104. int z_index;
  105. // Default modulate for back-compat
  106. explicit TileData() :
  107. tile_mode(SINGLE_TILE),
  108. modulate(1, 1, 1),
  109. z_index(0) {}
  110. };
  111. Map<int, TileData> tile_map;
  112. protected:
  113. bool _set(const StringName &p_name, const Variant &p_value);
  114. bool _get(const StringName &p_name, Variant &r_ret) const;
  115. void _get_property_list(List<PropertyInfo> *p_list) const;
  116. void _tile_set_shapes(int p_id, const Array &p_shapes);
  117. Array _tile_get_shapes(int p_id) const;
  118. Array _get_tiles_ids() const;
  119. static void _bind_methods();
  120. public:
  121. void create_tile(int p_id);
  122. void autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode);
  123. BitmaskMode autotile_get_bitmask_mode(int p_id) const;
  124. void tile_set_name(int p_id, const String &p_name);
  125. String tile_get_name(int p_id) const;
  126. void tile_set_texture(int p_id, const Ref<Texture> &p_texture);
  127. Ref<Texture> tile_get_texture(int p_id) const;
  128. void tile_set_normal_map(int p_id, const Ref<Texture> &p_normal_map);
  129. Ref<Texture> tile_get_normal_map(int p_id) const;
  130. void tile_set_texture_offset(int p_id, const Vector2 &p_offset);
  131. Vector2 tile_get_texture_offset(int p_id) const;
  132. void tile_set_region(int p_id, const Rect2 &p_region);
  133. Rect2 tile_get_region(int p_id) const;
  134. void tile_set_tile_mode(int p_id, TileMode p_tile_mode);
  135. TileMode tile_get_tile_mode(int p_id) const;
  136. void autotile_set_icon_coordinate(int p_id, Vector2 coord);
  137. Vector2 autotile_get_icon_coordinate(int p_id) const;
  138. void autotile_set_spacing(int p_id, int p_spacing);
  139. int autotile_get_spacing(int p_id) const;
  140. void autotile_set_size(int p_id, Size2 p_size);
  141. Size2 autotile_get_size(int p_id) const;
  142. void autotile_clear_bitmask_map(int p_id);
  143. void autotile_set_subtile_priority(int p_id, const Vector2 &p_coord, int p_priority);
  144. int autotile_get_subtile_priority(int p_id, const Vector2 &p_coord);
  145. const Map<Vector2, int> &autotile_get_priority_map(int p_id) const;
  146. void autotile_set_z_index(int p_id, const Vector2 &p_coord, int p_z_index);
  147. int autotile_get_z_index(int p_id, const Vector2 &p_coord);
  148. const Map<Vector2, int> &autotile_get_z_index_map(int p_id) const;
  149. void autotile_set_bitmask(int p_id, Vector2 p_coord, uint16_t p_flag);
  150. uint16_t autotile_get_bitmask(int p_id, Vector2 p_coord);
  151. const Map<Vector2, uint16_t> &autotile_get_bitmask_map(int p_id);
  152. 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());
  153. void tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape);
  154. Ref<Shape2D> tile_get_shape(int p_id, int p_shape_id) const;
  155. void tile_set_shape_transform(int p_id, int p_shape_id, const Transform2D &p_offset);
  156. Transform2D tile_get_shape_transform(int p_id, int p_shape_id) const;
  157. void tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset);
  158. Vector2 tile_get_shape_offset(int p_id, int p_shape_id) const;
  159. void tile_set_shape_one_way(int p_id, int p_shape_id, bool p_one_way);
  160. bool tile_get_shape_one_way(int p_id, int p_shape_id) const;
  161. void tile_clear_shapes(int p_id);
  162. 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());
  163. int tile_get_shape_count(int p_id) const;
  164. void tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes);
  165. Vector<ShapeData> tile_get_shapes(int p_id) const;
  166. void tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material);
  167. Ref<ShaderMaterial> tile_get_material(int p_id) const;
  168. void tile_set_modulate(int p_id, const Color &p_modulate);
  169. Color tile_get_modulate(int p_id) const;
  170. void tile_set_occluder_offset(int p_id, const Vector2 &p_offset);
  171. Vector2 tile_get_occluder_offset(int p_id) const;
  172. void tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder);
  173. Ref<OccluderPolygon2D> tile_get_light_occluder(int p_id) const;
  174. void autotile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder, const Vector2 &p_coord);
  175. Ref<OccluderPolygon2D> autotile_get_light_occluder(int p_id, const Vector2 &p_coord) const;
  176. const Map<Vector2, Ref<OccluderPolygon2D> > &autotile_get_light_oclusion_map(int p_id) const;
  177. void tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset);
  178. Vector2 tile_get_navigation_polygon_offset(int p_id) const;
  179. void tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon);
  180. Ref<NavigationPolygon> tile_get_navigation_polygon(int p_id) const;
  181. void autotile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon, const Vector2 &p_coord);
  182. Ref<NavigationPolygon> autotile_get_navigation_polygon(int p_id, const Vector2 &p_coord) const;
  183. const Map<Vector2, Ref<NavigationPolygon> > &autotile_get_navigation_map(int p_id) const;
  184. void tile_set_z_index(int p_id, int p_z_index);
  185. int tile_get_z_index(int p_id) const;
  186. void remove_tile(int p_id);
  187. bool has_tile(int p_id) const;
  188. bool is_tile_bound(int p_drawn_id, int p_neighbor_id);
  189. int find_tile_by_name(const String &p_name) const;
  190. void get_tile_list(List<int> *p_tiles) const;
  191. void clear();
  192. int get_last_unused_tile_id() const;
  193. TileSet();
  194. };
  195. VARIANT_ENUM_CAST(TileSet::AutotileBindings);
  196. VARIANT_ENUM_CAST(TileSet::BitmaskMode);
  197. VARIANT_ENUM_CAST(TileSet::TileMode);
  198. #endif // TILE_SET_H