tile_map_layer.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /**************************************************************************/
  2. /* tile_map_layer.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. #pragma once
  31. #include "scene/resources/2d/tile_set.h"
  32. #ifndef NAVIGATION_2D_DISABLED
  33. class NavigationMeshSourceGeometryData2D;
  34. #endif // NAVIGATION_2D_DISABLED
  35. class TileSetAtlasSource;
  36. class TileMap;
  37. enum TileMapLayerDataFormat {
  38. TILE_MAP_LAYER_DATA_FORMAT_0 = 0,
  39. TILE_MAP_LAYER_DATA_FORMAT_MAX,
  40. };
  41. class TerrainConstraint {
  42. private:
  43. Ref<TileSet> tile_set;
  44. Vector2i base_cell_coords;
  45. int bit = -1;
  46. int terrain = -1;
  47. int priority = 1;
  48. public:
  49. bool operator<(const TerrainConstraint &p_other) const {
  50. if (base_cell_coords == p_other.base_cell_coords) {
  51. return bit < p_other.bit;
  52. }
  53. return base_cell_coords < p_other.base_cell_coords;
  54. }
  55. String to_string() const {
  56. return vformat("Constraint {pos:%s, bit:%d, terrain:%d, priority:%d}", base_cell_coords, bit, terrain, priority);
  57. }
  58. Vector2i get_base_cell_coords() const {
  59. return base_cell_coords;
  60. }
  61. bool is_center_bit() const {
  62. return bit == 0;
  63. }
  64. HashMap<Vector2i, TileSet::CellNeighbor> get_overlapping_coords_and_peering_bits() const;
  65. void set_terrain(int p_terrain) {
  66. terrain = p_terrain;
  67. }
  68. int get_terrain() const {
  69. return terrain;
  70. }
  71. void set_priority(int p_priority) {
  72. priority = p_priority;
  73. }
  74. int get_priority() const {
  75. return priority;
  76. }
  77. TerrainConstraint(Ref<TileSet> p_tile_set, const Vector2i &p_position, int p_terrain); // For the center terrain bit
  78. TerrainConstraint(Ref<TileSet> p_tile_set, const Vector2i &p_position, const TileSet::CellNeighbor &p_bit, int p_terrain); // For peering bits
  79. TerrainConstraint() {}
  80. };
  81. #ifdef DEBUG_ENABLED
  82. class DebugQuadrant;
  83. #endif // DEBUG_ENABLED
  84. class RenderingQuadrant;
  85. class PhysicsQuadrant;
  86. struct CellData {
  87. Vector2i coords;
  88. TileMapCell cell;
  89. // Debug
  90. SelfList<CellData> debug_quadrant_list_element;
  91. // Rendering.
  92. Ref<RenderingQuadrant> rendering_quadrant;
  93. SelfList<CellData> rendering_quadrant_list_element;
  94. LocalVector<LocalVector<RID>> occluders;
  95. #ifndef PHYSICS_2D_DISABLED
  96. // Physics.
  97. Ref<PhysicsQuadrant> physics_quadrant;
  98. SelfList<CellData> physics_quadrant_list_element;
  99. #endif // PHYSICS_2D_DISABLED
  100. // Navigation.
  101. LocalVector<RID> navigation_regions;
  102. // Scenes.
  103. String scene;
  104. // Runtime TileData cache.
  105. TileData *runtime_tile_data_cache = nullptr;
  106. // List elements.
  107. SelfList<CellData> dirty_list_element;
  108. bool operator<(const CellData &p_other) const {
  109. return coords < p_other.coords;
  110. }
  111. // For those, copy everything but SelfList elements.
  112. void operator=(const CellData &p_other) {
  113. coords = p_other.coords;
  114. cell = p_other.cell;
  115. occluders = p_other.occluders;
  116. navigation_regions = p_other.navigation_regions;
  117. scene = p_other.scene;
  118. runtime_tile_data_cache = p_other.runtime_tile_data_cache;
  119. }
  120. CellData(const CellData &p_other) :
  121. debug_quadrant_list_element(this),
  122. rendering_quadrant_list_element(this),
  123. #ifndef PHYSICS_2D_DISABLED
  124. physics_quadrant_list_element(this),
  125. #endif // PHYSICS_2D_DISABLED
  126. dirty_list_element(this) {
  127. coords = p_other.coords;
  128. cell = p_other.cell;
  129. occluders = p_other.occluders;
  130. navigation_regions = p_other.navigation_regions;
  131. scene = p_other.scene;
  132. runtime_tile_data_cache = p_other.runtime_tile_data_cache;
  133. }
  134. CellData() :
  135. debug_quadrant_list_element(this),
  136. rendering_quadrant_list_element(this),
  137. #ifndef PHYSICS_2D_DISABLED
  138. physics_quadrant_list_element(this),
  139. #endif // PHYSICS_2D_DISABLED
  140. dirty_list_element(this) {
  141. }
  142. };
  143. // We use another comparator for Y-sorted layers with reversed X drawing order.
  144. struct CellDataYSortedXReversedComparator {
  145. _FORCE_INLINE_ bool operator()(const CellData &p_a, const CellData &p_b) const {
  146. return p_a.coords.x == p_b.coords.x ? (p_a.coords.y < p_b.coords.y) : (p_a.coords.x > p_b.coords.x);
  147. }
  148. };
  149. #ifdef DEBUG_ENABLED
  150. class DebugQuadrant : public RefCounted {
  151. GDCLASS(DebugQuadrant, RefCounted);
  152. public:
  153. Vector2i quadrant_coords;
  154. SelfList<CellData>::List cells;
  155. RID canvas_item;
  156. RID physics_mesh;
  157. // Used to deleted unused quadrants.
  158. bool drawn_to = false;
  159. SelfList<DebugQuadrant> dirty_quadrant_list_element;
  160. DebugQuadrant() :
  161. dirty_quadrant_list_element(this) {
  162. }
  163. ~DebugQuadrant() {
  164. cells.clear();
  165. }
  166. };
  167. #endif // DEBUG_ENABLED
  168. class RenderingQuadrant : public RefCounted {
  169. GDCLASS(RenderingQuadrant, RefCounted);
  170. public:
  171. struct CoordsWorldComparator {
  172. _ALWAYS_INLINE_ bool operator()(const Vector2 &p_a, const Vector2 &p_b) const {
  173. // We sort the cells by their local coords, as it is needed by rendering.
  174. if (p_a.y == p_b.y) {
  175. return p_a.x > p_b.x;
  176. } else {
  177. return p_a.y < p_b.y;
  178. }
  179. }
  180. };
  181. Vector2i quadrant_coords;
  182. SelfList<CellData>::List cells;
  183. List<RID> canvas_items;
  184. Vector2 canvas_items_position;
  185. SelfList<RenderingQuadrant> dirty_quadrant_list_element;
  186. RenderingQuadrant() :
  187. dirty_quadrant_list_element(this) {
  188. }
  189. ~RenderingQuadrant() {
  190. cells.clear();
  191. }
  192. };
  193. #ifndef PHYSICS_2D_DISABLED
  194. class PhysicsQuadrant : public RefCounted {
  195. GDCLASS(PhysicsQuadrant, RefCounted);
  196. public:
  197. struct PhysicsBodyKey {
  198. int physics_layer = 0;
  199. Vector2 linear_velocity;
  200. real_t angular_velocity = 0.0;
  201. bool one_way_collision = false;
  202. real_t one_way_collision_margin = 0.0;
  203. bool operator<(const PhysicsBodyKey &p_other) const {
  204. if (physics_layer == p_other.physics_layer) {
  205. if (linear_velocity == p_other.linear_velocity) {
  206. if (angular_velocity == p_other.angular_velocity) {
  207. if (one_way_collision == p_other.one_way_collision) {
  208. return one_way_collision_margin < p_other.one_way_collision_margin;
  209. }
  210. return one_way_collision < p_other.one_way_collision;
  211. }
  212. return angular_velocity < p_other.angular_velocity;
  213. }
  214. return linear_velocity < p_other.linear_velocity;
  215. }
  216. return physics_layer < p_other.physics_layer;
  217. }
  218. bool operator!=(const PhysicsBodyKey &p_other) const {
  219. return !this->operator==(p_other);
  220. }
  221. bool operator==(const PhysicsBodyKey &p_other) const {
  222. return physics_layer == p_other.physics_layer &&
  223. linear_velocity == p_other.linear_velocity &&
  224. angular_velocity == p_other.angular_velocity &&
  225. one_way_collision == p_other.one_way_collision &&
  226. one_way_collision_margin == p_other.one_way_collision_margin;
  227. }
  228. };
  229. struct PhysicsBodyKeyHasher {
  230. static uint32_t hash(const PhysicsBodyKey &p_hash) {
  231. uint32_t h = hash_murmur3_one_32(p_hash.physics_layer);
  232. h = hash_murmur3_one_real(p_hash.linear_velocity.x);
  233. h = hash_murmur3_one_real(p_hash.linear_velocity.y, h);
  234. h = hash_murmur3_one_real(p_hash.angular_velocity, h);
  235. return h;
  236. }
  237. };
  238. struct PhysicsBodyValue {
  239. RID body;
  240. Vector<Vector<Vector2>> polygons;
  241. };
  242. struct CoordsWorldComparator {
  243. _ALWAYS_INLINE_ bool operator()(const Vector2 &p_a, const Vector2 &p_b) const {
  244. // We sort the cells by their local coords, as it is needed by rendering.
  245. if (p_a.y == p_b.y) {
  246. return p_a.x > p_b.x;
  247. } else {
  248. return p_a.y < p_b.y;
  249. }
  250. }
  251. };
  252. Vector2i quadrant_coords;
  253. SelfList<CellData>::List cells;
  254. HashMap<PhysicsBodyKey, PhysicsBodyValue, PhysicsBodyKeyHasher> bodies;
  255. LocalVector<Ref<ConvexPolygonShape2D>> shapes;
  256. SelfList<PhysicsQuadrant> dirty_quadrant_list_element;
  257. PhysicsQuadrant() :
  258. dirty_quadrant_list_element(this) {
  259. }
  260. ~PhysicsQuadrant() {
  261. cells.clear();
  262. }
  263. };
  264. #endif // PHYSICS_2D_DISABLED
  265. class TileMapLayer : public Node2D {
  266. GDCLASS(TileMapLayer, Node2D);
  267. public:
  268. enum HighlightMode {
  269. HIGHLIGHT_MODE_DEFAULT,
  270. HIGHLIGHT_MODE_ABOVE,
  271. HIGHLIGHT_MODE_BELOW,
  272. };
  273. enum DebugVisibilityMode {
  274. DEBUG_VISIBILITY_MODE_DEFAULT,
  275. DEBUG_VISIBILITY_MODE_FORCE_SHOW,
  276. DEBUG_VISIBILITY_MODE_FORCE_HIDE,
  277. };
  278. enum DirtyFlags {
  279. DIRTY_FLAGS_LAYER_ENABLED = 0,
  280. DIRTY_FLAGS_LAYER_IN_TREE,
  281. DIRTY_FLAGS_LAYER_IN_CANVAS,
  282. DIRTY_FLAGS_LAYER_LOCAL_TRANSFORM,
  283. DIRTY_FLAGS_LAYER_VISIBILITY,
  284. DIRTY_FLAGS_LAYER_SELF_MODULATE,
  285. DIRTY_FLAGS_LAYER_Y_SORT_ENABLED,
  286. DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN,
  287. DIRTY_FLAGS_LAYER_X_DRAW_ORDER_REVERSED,
  288. DIRTY_FLAGS_LAYER_Z_INDEX,
  289. DIRTY_FLAGS_LAYER_LIGHT_MASK,
  290. DIRTY_FLAGS_LAYER_TEXTURE_FILTER,
  291. DIRTY_FLAGS_LAYER_TEXTURE_REPEAT,
  292. DIRTY_FLAGS_LAYER_RENDERING_QUADRANT_SIZE,
  293. DIRTY_FLAGS_LAYER_COLLISION_ENABLED,
  294. DIRTY_FLAGS_LAYER_USE_KINEMATIC_BODIES,
  295. DIRTY_FLAGS_LAYER_PHYSICS_QUADRANT_SIZE,
  296. DIRTY_FLAGS_LAYER_COLLISION_VISIBILITY_MODE,
  297. DIRTY_FLAGS_LAYER_OCCLUSION_ENABLED,
  298. DIRTY_FLAGS_LAYER_NAVIGATION_ENABLED,
  299. DIRTY_FLAGS_LAYER_NAVIGATION_MAP,
  300. DIRTY_FLAGS_LAYER_NAVIGATION_VISIBILITY_MODE,
  301. DIRTY_FLAGS_LAYER_RUNTIME_UPDATE,
  302. DIRTY_FLAGS_LAYER_INDEX_IN_TILE_MAP_NODE, // For compatibility.
  303. DIRTY_FLAGS_LAYER_GROUP_SELECTED_LAYERS,
  304. DIRTY_FLAGS_LAYER_GROUP_HIGHLIGHT_SELECTED,
  305. DIRTY_FLAGS_TILE_SET,
  306. DIRTY_FLAGS_MAX,
  307. };
  308. private:
  309. static constexpr float FP_ADJUST = 0.00001;
  310. // Properties.
  311. HashMap<Vector2i, CellData> tile_map_layer_data;
  312. bool enabled = true;
  313. Ref<TileSet> tile_set;
  314. HighlightMode highlight_mode = HIGHLIGHT_MODE_DEFAULT;
  315. int y_sort_origin = 0;
  316. bool x_draw_order_reversed = false;
  317. int rendering_quadrant_size = 16;
  318. bool collision_enabled = true;
  319. bool use_kinematic_bodies = false;
  320. int physics_quadrant_size = 16;
  321. DebugVisibilityMode collision_visibility_mode = DEBUG_VISIBILITY_MODE_DEFAULT;
  322. bool occlusion_enabled = true;
  323. bool navigation_enabled = true;
  324. RID navigation_map_override;
  325. DebugVisibilityMode navigation_visibility_mode = DEBUG_VISIBILITY_MODE_DEFAULT;
  326. // Internal.
  327. bool pending_update = false;
  328. // For keeping compatibility with TileMap.
  329. TileMap *tile_map_node = nullptr;
  330. int layer_index_in_tile_map_node = -1;
  331. // Dirty flag. Allows knowing what was modified since the last update.
  332. struct {
  333. bool flags[DIRTY_FLAGS_MAX] = { false };
  334. SelfList<CellData>::List cell_list;
  335. } dirty;
  336. // Rect cache.
  337. mutable Rect2 rect_cache;
  338. mutable bool rect_cache_dirty = true;
  339. mutable Rect2i used_rect_cache;
  340. mutable bool used_rect_cache_dirty = true;
  341. // Runtime tile data.
  342. bool _runtime_update_tile_data_was_cleaned_up = false;
  343. void _build_runtime_update_tile_data(bool p_force_cleanup);
  344. void _build_runtime_update_tile_data_for_cell(CellData &r_cell_data, bool p_use_tilemap_for_runtime, bool p_auto_add_to_dirty_list = false);
  345. bool _runtime_update_needs_all_cells_cleaned_up = false;
  346. void _clear_runtime_update_tile_data();
  347. void _clear_runtime_update_tile_data_for_cell(CellData &r_cell_data);
  348. void _update_cells_callback(bool p_force_cleanup);
  349. // Coords to quadrant coords
  350. Vector2i _coords_to_quadrant_coords(const Vector2i &p_coords, const int p_quadrant_size) const;
  351. // Per-system methods.
  352. #ifdef DEBUG_ENABLED
  353. HashMap<Vector2i, Ref<DebugQuadrant>> debug_quadrant_map;
  354. bool _debug_was_cleaned_up = false;
  355. void _debug_update(bool p_force_cleanup);
  356. void _debug_quadrants_update_cell(CellData &r_cell_data);
  357. void _get_debug_quadrant_for_cell(const Vector2i &p_coords);
  358. #endif // DEBUG_ENABLED
  359. HashMap<Vector2i, Ref<RenderingQuadrant>> rendering_quadrant_map;
  360. bool _rendering_was_cleaned_up = false;
  361. void _rendering_update(bool p_force_cleanup);
  362. void _rendering_notification(int p_what);
  363. void _rendering_quadrants_update_cell(CellData &r_cell_data, SelfList<RenderingQuadrant>::List &r_dirty_rendering_quadrant_list);
  364. void _rendering_occluders_clear_cell(CellData &r_cell_data);
  365. void _rendering_occluders_update_cell(CellData &r_cell_data);
  366. #ifdef DEBUG_ENABLED
  367. void _rendering_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data);
  368. #endif // DEBUG_ENABLED
  369. #ifndef PHYSICS_2D_DISABLED
  370. HashMap<Vector2i, Ref<PhysicsQuadrant>> physics_quadrant_map;
  371. HashMap<RID, Vector2i> bodies_coords; // Mapping for RID to coords.
  372. bool _physics_was_cleaned_up = false;
  373. void _physics_update(bool p_force_cleanup);
  374. void _physics_notification(int p_what);
  375. void _physics_quadrants_update_cell(CellData &r_cell_data, SelfList<PhysicsQuadrant>::List &r_dirty_physics_quadrant_list);
  376. void _physics_clear_cell(CellData &r_cell_data);
  377. void _physics_update_cell(CellData &r_cell_data);
  378. #ifdef DEBUG_ENABLED
  379. void _physics_draw_quadrant_debug(const RID &p_canvas_item, DebugQuadrant &r_debug_quadrant);
  380. #endif // DEBUG_ENABLED
  381. #endif // PHYSICS_2D_DISABLED
  382. #ifndef NAVIGATION_2D_DISABLED
  383. bool _navigation_was_cleaned_up = false;
  384. void _navigation_update(bool p_force_cleanup);
  385. void _navigation_notification(int p_what);
  386. void _navigation_clear_cell(CellData &r_cell_data);
  387. void _navigation_update_cell(CellData &r_cell_data);
  388. #ifdef DEBUG_ENABLED
  389. void _navigation_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data);
  390. #endif // DEBUG_ENABLED
  391. #endif // NAVIGATION_2D_DISABLED
  392. bool _scenes_was_cleaned_up = false;
  393. void _scenes_update(bool p_force_cleanup);
  394. void _scenes_clear_cell(CellData &r_cell_data);
  395. void _scenes_update_cell(CellData &r_cell_data);
  396. #ifdef DEBUG_ENABLED
  397. void _scenes_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data);
  398. #endif // DEBUG_ENABLED
  399. // Terrains.
  400. TileSet::TerrainsPattern _get_best_terrain_pattern_for_constraints(int p_terrain_set, const Vector2i &p_position, const RBSet<TerrainConstraint> &p_constraints, TileSet::TerrainsPattern p_current_pattern) const;
  401. RBSet<TerrainConstraint> _get_terrain_constraints_from_added_pattern(const Vector2i &p_position, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const;
  402. RBSet<TerrainConstraint> _get_terrain_constraints_from_painted_cells_list(const RBSet<Vector2i> &p_painted, int p_terrain_set, bool p_ignore_empty_terrains) const;
  403. void _tile_set_changed();
  404. void _renamed();
  405. void _update_notify_local_transform();
  406. // Internal updates.
  407. void _queue_internal_update();
  408. void _deferred_internal_update();
  409. void _internal_update(bool p_force_cleanup);
  410. virtual void _physics_interpolated_changed() override;
  411. protected:
  412. void _notification(int p_what);
  413. static void _bind_methods();
  414. void _validate_property(PropertyInfo &p_property) const;
  415. virtual void _update_self_texture_filter(RS::CanvasItemTextureFilter p_texture_filter) override;
  416. virtual void _update_self_texture_repeat(RS::CanvasItemTextureRepeat p_texture_repeat) override;
  417. public:
  418. #ifdef TOOLS_ENABLED
  419. virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const override;
  420. #endif
  421. // TileMap node.
  422. void set_as_tile_map_internal_node(int p_index);
  423. int get_index_in_tile_map() const {
  424. return layer_index_in_tile_map_node;
  425. }
  426. const HashMap<Vector2i, CellData> &get_tile_map_layer_data() const {
  427. return tile_map_layer_data;
  428. }
  429. // Rect caching.
  430. Rect2 get_rect(bool &r_changed) const;
  431. // Terrains.
  432. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_constraints(const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints) const; // Not exposed.
  433. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_connect(const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true) const; // Not exposed.
  434. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_path(const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true) const; // Not exposed.
  435. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_pattern(const Vector<Vector2i> &p_coords_array, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern, bool p_ignore_empty_terrains = true) const; // Not exposed.
  436. // Not exposed to users.
  437. TileMapCell get_cell(const Vector2i &p_coords) const;
  438. static void compute_transformed_tile_dest_rect(Rect2 &r_dest_rect, bool &r_transpose, const Vector2 &p_position, const Vector2 &p_dest_rect_size, const TileData *p_tile_data, int p_alternative_tile);
  439. static void draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame = -1, const TileData *p_tile_data_override = nullptr, real_t p_normalized_animation_offset = 0.0);
  440. ////////////// Exposed functions //////////////
  441. // --- Cells manipulation ---
  442. // Generic cells manipulations and data access.
  443. void set_cell(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);
  444. void erase_cell(const Vector2i &p_coords);
  445. void fix_invalid_tiles();
  446. void clear();
  447. int get_cell_source_id(const Vector2i &p_coords) const;
  448. Vector2i get_cell_atlas_coords(const Vector2i &p_coords) const;
  449. int get_cell_alternative_tile(const Vector2i &p_coords) const;
  450. TileData *get_cell_tile_data(const Vector2i &p_coords) const; // Helper method to make accessing the data easier.
  451. TypedArray<Vector2i> get_used_cells() const;
  452. TypedArray<Vector2i> get_used_cells_by_id(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;
  453. Rect2i get_used_rect() const;
  454. bool is_cell_flipped_h(const Vector2i &p_coords) const;
  455. bool is_cell_flipped_v(const Vector2i &p_coords) const;
  456. bool is_cell_transposed(const Vector2i &p_coords) const;
  457. // Patterns.
  458. Ref<TileMapPattern> get_pattern(TypedArray<Vector2i> p_coords_array);
  459. void set_pattern(const Vector2i &p_position, const Ref<TileMapPattern> p_pattern);
  460. // Terrains.
  461. void set_cells_terrain_connect(TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
  462. void set_cells_terrain_path(TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
  463. #ifndef PHYSICS_2D_DISABLED
  464. // --- Physics helpers ---
  465. bool has_body_rid(RID p_physics_body) const;
  466. Vector2i get_coords_for_body_rid(RID p_physics_body) const; // For finding tiles from collision.
  467. #endif // PHYSICS_2D_DISABLED
  468. // --- Runtime ---
  469. void update_internals();
  470. void notify_runtime_tile_data_update();
  471. GDVIRTUAL1R(bool, _use_tile_data_runtime_update, Vector2i);
  472. GDVIRTUAL2(_tile_data_runtime_update, Vector2i, TileData *);
  473. GDVIRTUAL2(_update_cells, TypedArray<Vector2i>, bool);
  474. // --- Shortcuts to methods defined in TileSet ---
  475. Vector2i map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern);
  476. TypedArray<Vector2i> get_surrounding_cells(const Vector2i &p_coords);
  477. Vector2i get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const;
  478. Vector2 map_to_local(const Vector2i &p_pos) const;
  479. Vector2i local_to_map(const Vector2 &p_pos) const;
  480. // --- Accessors ---
  481. void set_tile_map_data_from_array(const Vector<uint8_t> &p_data);
  482. Vector<uint8_t> get_tile_map_data_as_array() const;
  483. void set_enabled(bool p_enabled);
  484. bool is_enabled() const;
  485. void set_tile_set(const Ref<TileSet> &p_tile_set);
  486. Ref<TileSet> get_tile_set() const;
  487. void set_highlight_mode(HighlightMode p_highlight_mode);
  488. HighlightMode get_highlight_mode() const;
  489. virtual void set_self_modulate(const Color &p_self_modulate) override;
  490. virtual void set_y_sort_enabled(bool p_y_sort_enabled) override;
  491. void set_y_sort_origin(int p_y_sort_origin);
  492. int get_y_sort_origin() const;
  493. void set_x_draw_order_reversed(bool p_x_draw_order_reversed);
  494. bool is_x_draw_order_reversed() const;
  495. virtual void set_z_index(int p_z_index) override;
  496. virtual void set_light_mask(int p_light_mask) override;
  497. void set_rendering_quadrant_size(int p_size);
  498. int get_rendering_quadrant_size() const;
  499. void set_collision_enabled(bool p_enabled);
  500. bool is_collision_enabled() const;
  501. void set_use_kinematic_bodies(bool p_use_kinematic_bodies);
  502. bool is_using_kinematic_bodies() const;
  503. void set_collision_visibility_mode(DebugVisibilityMode p_show_collision);
  504. DebugVisibilityMode get_collision_visibility_mode() const;
  505. void set_physics_quadrant_size(int p_size);
  506. int get_physics_quadrant_size() const;
  507. void set_occlusion_enabled(bool p_enabled);
  508. bool is_occlusion_enabled() const;
  509. void set_navigation_enabled(bool p_enabled);
  510. bool is_navigation_enabled() const;
  511. void set_navigation_map(RID p_map);
  512. RID get_navigation_map() const;
  513. void set_navigation_visibility_mode(DebugVisibilityMode p_show_navigation);
  514. DebugVisibilityMode get_navigation_visibility_mode() const;
  515. private:
  516. #ifndef NAVIGATION_2D_DISABLED
  517. static Callable _navmesh_source_geometry_parsing_callback;
  518. static RID _navmesh_source_geometry_parser;
  519. #endif // NAVIGATION_2D_DISABLED
  520. public:
  521. #ifndef NAVIGATION_2D_DISABLED
  522. static void navmesh_parse_init();
  523. static void navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
  524. #endif // NAVIGATION_2D_DISABLED
  525. TileMapLayer();
  526. ~TileMapLayer();
  527. };
  528. VARIANT_ENUM_CAST(TileMapLayer::DebugVisibilityMode);