tile_map.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**************************************************************************/
  2. /* tile_map.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_MAP_H
  31. #define TILE_MAP_H
  32. #include "scene/2d/tile_map_layer.h"
  33. #include "scene/property_list_helper.h"
  34. #include "scene/resources/2d/tile_set.h"
  35. class Control;
  36. class TileMapLayer;
  37. class TerrainConstraint;
  38. enum TileMapDataFormat {
  39. TILE_MAP_DATA_FORMAT_1 = 0,
  40. TILE_MAP_DATA_FORMAT_2,
  41. TILE_MAP_DATA_FORMAT_3,
  42. TILE_MAP_DATA_FORMAT_MAX,
  43. };
  44. class TileMap : public Node2D {
  45. GDCLASS(TileMap, Node2D)
  46. public:
  47. // Kept for compatibility, but should use TileMapLayer::VisibilityMode instead.
  48. enum VisibilityMode {
  49. VISIBILITY_MODE_DEFAULT,
  50. VISIBILITY_MODE_FORCE_SHOW,
  51. VISIBILITY_MODE_FORCE_HIDE,
  52. };
  53. private:
  54. friend class TileSetPlugin;
  55. // A compatibility enum to specify how is the data if formatted.
  56. mutable TileMapDataFormat format = TileMapDataFormat::TILE_MAP_DATA_FORMAT_3;
  57. // Properties.
  58. Ref<TileSet> tile_set;
  59. int rendering_quadrant_size = 16;
  60. bool collision_animatable = false;
  61. VisibilityMode collision_visibility_mode = VISIBILITY_MODE_DEFAULT;
  62. VisibilityMode navigation_visibility_mode = VISIBILITY_MODE_DEFAULT;
  63. // Layers.
  64. LocalVector<TileMapLayer *> layers;
  65. static inline PropertyListHelper base_property_helper;
  66. PropertyListHelper property_helper;
  67. // Transforms for collision_animatable.
  68. Transform2D last_valid_transform;
  69. Transform2D new_transform;
  70. void _tile_set_changed();
  71. void _emit_changed();
  72. // Kept for compatibility with TileMap. With TileMapLayers as individual nodes, the format is stored directly in the array.
  73. void _set_tile_map_data_using_compatibility_format(int p_layer, TileMapDataFormat p_format, const Vector<int> &p_data);
  74. Vector<int> _get_tile_map_data_using_compatibility_format(int p_layer) const;
  75. void _set_layer_tile_data(int p_layer, const PackedInt32Array &p_data);
  76. protected:
  77. bool _set(const StringName &p_name, const Variant &p_value);
  78. bool _get(const StringName &p_name, Variant &r_ret) const;
  79. void _get_property_list(List<PropertyInfo> *p_list) const;
  80. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  81. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  82. void _notification(int p_what);
  83. static void _bind_methods();
  84. #ifndef DISABLE_DEPRECATED
  85. Rect2i _get_used_rect_bind_compat_78328();
  86. void _set_quadrant_size_compat_81070(int p_quadrant_size);
  87. int _get_quadrant_size_compat_81070() const;
  88. VisibilityMode _get_collision_visibility_mode_bind_compat_87115();
  89. VisibilityMode _get_navigation_visibility_mode_bind_compat_87115();
  90. static void _bind_compatibility_methods();
  91. #endif // DISABLE_DEPRECATED
  92. public:
  93. #ifdef DEBUG_ENABLED
  94. virtual Rect2 _edit_get_rect() const override;
  95. #endif // DEBUG_ENABLED
  96. #ifndef DISABLE_DEPRECATED
  97. void force_update(int p_layer);
  98. #endif // DISABLE_DEPRECATED
  99. void set_rendering_quadrant_size(int p_size);
  100. int get_rendering_quadrant_size() const;
  101. // Accessors.
  102. void set_tileset(const Ref<TileSet> &p_tileset);
  103. Ref<TileSet> get_tileset() const;
  104. // Layers management.
  105. int get_layers_count() const;
  106. void add_layer(int p_to_pos);
  107. void move_layer(int p_layer, int p_to_pos);
  108. void remove_layer(int p_layer);
  109. void set_layer_name(int p_layer, String p_name);
  110. String get_layer_name(int p_layer) const;
  111. void set_layer_enabled(int p_layer, bool p_visible);
  112. bool is_layer_enabled(int p_layer) const;
  113. void set_layer_modulate(int p_layer, Color p_modulate);
  114. Color get_layer_modulate(int p_layer) const;
  115. void set_layer_y_sort_enabled(int p_layer, bool p_enabled);
  116. bool is_layer_y_sort_enabled(int p_layer) const;
  117. void set_layer_y_sort_origin(int p_layer, int p_y_sort_origin);
  118. int get_layer_y_sort_origin(int p_layer) const;
  119. void set_layer_z_index(int p_layer, int p_z_index);
  120. int get_layer_z_index(int p_layer) const;
  121. void set_layer_navigation_enabled(int p_layer, bool p_enabled);
  122. bool is_layer_navigation_enabled(int p_layer) const;
  123. void set_layer_navigation_map(int p_layer, RID p_map);
  124. RID get_layer_navigation_map(int p_layer) const;
  125. void set_collision_animatable(bool p_collision_animatable);
  126. bool is_collision_animatable() const;
  127. // Debug visibility modes.
  128. void set_collision_visibility_mode(VisibilityMode p_show_collision);
  129. VisibilityMode get_collision_visibility_mode() const;
  130. void set_navigation_visibility_mode(VisibilityMode p_show_navigation);
  131. VisibilityMode get_navigation_visibility_mode() const;
  132. // Cells accessors.
  133. void set_cell(int p_layer, const Vector2i &p_coords, int p_source_id = TileSet::INVALID_SOURCE, const Vector2i p_atlas_coords = TileSetSource::INVALID_ATLAS_COORDS, int p_alternative_tile = 0);
  134. void erase_cell(int p_layer, const Vector2i &p_coords);
  135. int get_cell_source_id(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  136. Vector2i get_cell_atlas_coords(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  137. int get_cell_alternative_tile(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  138. // Helper method to make accessing the data easier.
  139. TileData *get_cell_tile_data(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  140. bool is_cell_flipped_h(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  141. bool is_cell_flipped_v(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  142. bool is_cell_transposed(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  143. // Patterns.
  144. Ref<TileMapPattern> get_pattern(int p_layer, TypedArray<Vector2i> p_coords_array);
  145. Vector2i map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern);
  146. void set_pattern(int p_layer, const Vector2i &p_position, const Ref<TileMapPattern> p_pattern);
  147. // Terrains (Not exposed).
  148. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_constraints(int p_layer, const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints);
  149. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_connect(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
  150. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_path(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
  151. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_pattern(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern, bool p_ignore_empty_terrains = true);
  152. // Terrains (exposed).
  153. void set_cells_terrain_connect(int p_layer, TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
  154. void set_cells_terrain_path(int p_layer, TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
  155. // Not exposed to users.
  156. TileMapCell get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies = false) const;
  157. int get_effective_quadrant_size(int p_layer) const;
  158. virtual void set_y_sort_enabled(bool p_enable) override;
  159. Vector2 map_to_local(const Vector2i &p_pos) const;
  160. Vector2i local_to_map(const Vector2 &p_pos) const;
  161. bool is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const;
  162. Vector2i get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const;
  163. TypedArray<Vector2i> get_used_cells(int p_layer) const;
  164. TypedArray<Vector2i> get_used_cells_by_id(int p_layer, int p_source_id = TileSet::INVALID_SOURCE, const Vector2i p_atlas_coords = TileSetSource::INVALID_ATLAS_COORDS, int p_alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE) const;
  165. Rect2i get_used_rect() const;
  166. // Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems.
  167. virtual void set_light_mask(int p_light_mask) override;
  168. virtual void set_self_modulate(const Color &p_self_modulate) override;
  169. virtual void set_texture_filter(CanvasItem::TextureFilter p_texture_filter) override;
  170. virtual void set_texture_repeat(CanvasItem::TextureRepeat p_texture_repeat) override;
  171. // For finding tiles from collision.
  172. Vector2i get_coords_for_body_rid(RID p_physics_body);
  173. // For getting their layers as well.
  174. int get_layer_for_body_rid(RID p_physics_body);
  175. // Fixing and clearing methods.
  176. void fix_invalid_tiles();
  177. #ifdef TOOLS_ENABLED
  178. // Moving layers outside of TileMap.
  179. TileMapLayer *duplicate_layer_from_internal(int p_layer);
  180. #endif // TOOLS_ENABLED
  181. // Clears tiles from a given layer.
  182. void clear_layer(int p_layer);
  183. void clear();
  184. // Force a TileMap update.
  185. void update_internals();
  186. void notify_runtime_tile_data_update(int p_layer = -1);
  187. // Helpers?
  188. TypedArray<Vector2i> get_surrounding_cells(const Vector2i &p_coords);
  189. // Virtual function to modify the TileData at runtime.
  190. GDVIRTUAL2R(bool, _use_tile_data_runtime_update, int, Vector2i);
  191. GDVIRTUAL3(_tile_data_runtime_update, int, Vector2i, TileData *);
  192. // Configuration warnings.
  193. PackedStringArray get_configuration_warnings() const override;
  194. TileMap();
  195. };
  196. VARIANT_ENUM_CAST(TileMap::VisibilityMode);
  197. #endif // TILE_MAP_H