tile_map.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /**************************************************************************/
  2. /* tile_map.cpp */
  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. #include "tile_map.h"
  31. #include "tile_map.compat.inc"
  32. #include "core/io/marshalls.h"
  33. #include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"
  34. #include "servers/navigation_server_2d.h"
  35. #define TILEMAP_CALL_FOR_LAYER(layer, function, ...) \
  36. if (layer < 0) { \
  37. layer = layers.size() + layer; \
  38. }; \
  39. ERR_FAIL_INDEX(layer, (int)layers.size()); \
  40. layers[layer]->function(__VA_ARGS__);
  41. #define TILEMAP_CALL_FOR_LAYER_V(layer, err_value, function, ...) \
  42. if (layer < 0) { \
  43. layer = layers.size() + layer; \
  44. }; \
  45. ERR_FAIL_INDEX_V(layer, (int)layers.size(), err_value); \
  46. return layers[layer]->function(__VA_ARGS__);
  47. Callable TileMap::_navmesh_source_geometry_parsing_callback;
  48. RID TileMap::_navmesh_source_geometry_parser;
  49. void TileMap::_tile_set_changed() {
  50. update_configuration_warnings();
  51. }
  52. void TileMap::_emit_changed() {
  53. emit_signal(CoreStringName(changed));
  54. }
  55. void TileMap::_set_tile_map_data_using_compatibility_format(int p_layer, TileMapDataFormat p_format, const Vector<int> &p_data) {
  56. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  57. ERR_FAIL_COND(p_format >= TileMapDataFormat::TILE_MAP_DATA_FORMAT_MAX);
  58. #ifndef DISABLE_DEPRECATED
  59. ERR_FAIL_COND_MSG(p_format != (TileMapDataFormat)(TILE_MAP_DATA_FORMAT_MAX - 1), "Old TileMap data format detected despite DISABLE_DEPRECATED being set compilation time.");
  60. #endif // DISABLE_DEPRECATED
  61. // Set data for a given tile from raw data.
  62. int c = p_data.size();
  63. const int *r = p_data.ptr();
  64. int offset = (p_format >= TileMapDataFormat::TILE_MAP_DATA_FORMAT_2) ? 3 : 2;
  65. ERR_FAIL_COND_MSG(c % offset != 0, vformat("Corrupted tile data. Got size: %d. Expected modulo: %d", c, offset));
  66. layers[p_layer]->clear();
  67. for (int i = 0; i < c; i += offset) {
  68. const uint8_t *ptr = (const uint8_t *)&r[i];
  69. uint8_t local[12];
  70. const int buffer_size = (p_format >= TILE_MAP_DATA_FORMAT_2) ? 12 : 8;
  71. for (int j = 0; j < buffer_size; j++) {
  72. local[j] = ptr[j];
  73. }
  74. #ifdef BIG_ENDIAN_ENABLED
  75. SWAP(local[0], local[3]);
  76. SWAP(local[1], local[2]);
  77. SWAP(local[4], local[7]);
  78. SWAP(local[5], local[6]);
  79. //TODO: ask someone to check this...
  80. if (FORMAT >= FORMAT_2) {
  81. SWAP(local[8], local[11]);
  82. SWAP(local[9], local[10]);
  83. }
  84. #endif // BIG_ENDIAN_ENABLED
  85. // Extracts position in TileMap.
  86. int16_t x = decode_uint16(&local[0]);
  87. int16_t y = decode_uint16(&local[2]);
  88. if (p_format == TileMapDataFormat::TILE_MAP_DATA_FORMAT_3) {
  89. uint16_t source_id = decode_uint16(&local[4]);
  90. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  91. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  92. uint16_t alternative_tile = decode_uint16(&local[10]);
  93. layers[p_layer]->set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  94. } else {
  95. #ifndef DISABLE_DEPRECATED
  96. // Previous decated format.
  97. uint32_t v = decode_uint32(&local[4]);
  98. // Extract the transform flags that used to be in the tilemap.
  99. bool flip_h = v & (1UL << 29);
  100. bool flip_v = v & (1UL << 30);
  101. bool transpose = v & (1UL << 31);
  102. v &= (1UL << 29) - 1;
  103. // Extract autotile/atlas coords.
  104. int16_t coord_x = 0;
  105. int16_t coord_y = 0;
  106. if (p_format == TileMapDataFormat::TILE_MAP_DATA_FORMAT_2) {
  107. coord_x = decode_uint16(&local[8]);
  108. coord_y = decode_uint16(&local[10]);
  109. }
  110. if (tile_set.is_valid()) {
  111. Array a = tile_set->compatibility_tilemap_map(v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose);
  112. if (a.size() == 3) {
  113. layers[p_layer]->set_cell(Vector2i(x, y), a[0], a[1], a[2]);
  114. } else {
  115. ERR_PRINT(vformat("No valid tile in Tileset for: tile:%s coords:%s flip_h:%s flip_v:%s transpose:%s", v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose));
  116. }
  117. } else {
  118. int compatibility_alternative_tile = ((int)flip_h) + ((int)flip_v << 1) + ((int)transpose << 2);
  119. layers[p_layer]->set_cell(Vector2i(x, y), v, Vector2i(coord_x, coord_y), compatibility_alternative_tile);
  120. }
  121. #endif // DISABLE_DEPRECATED
  122. }
  123. }
  124. }
  125. Vector<int> TileMap::_get_tile_map_data_using_compatibility_format(int p_layer) const {
  126. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), Vector<int>());
  127. // Export tile data to raw format.
  128. const HashMap<Vector2i, CellData> tile_map_layer_data = layers[p_layer]->get_tile_map_layer_data();
  129. Vector<int> tile_data;
  130. tile_data.resize(tile_map_layer_data.size() * 3);
  131. int *w = tile_data.ptrw();
  132. // Save in highest format.
  133. int idx = 0;
  134. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  135. uint8_t *ptr = (uint8_t *)&w[idx];
  136. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  137. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  138. encode_uint16(E.value.cell.source_id, &ptr[4]);
  139. encode_uint16(E.value.cell.coord_x, &ptr[6]);
  140. encode_uint16(E.value.cell.coord_y, &ptr[8]);
  141. encode_uint16(E.value.cell.alternative_tile, &ptr[10]);
  142. idx += 3;
  143. }
  144. return tile_data;
  145. }
  146. void TileMap::_set_layer_tile_data(int p_layer, const PackedInt32Array &p_data) {
  147. _set_tile_map_data_using_compatibility_format(p_layer, format, p_data);
  148. }
  149. void TileMap::_notification(int p_what) {
  150. switch (p_what) {
  151. case TileMap::NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  152. // This is only executed when collision_animatable is enabled.
  153. bool in_editor = false;
  154. #ifdef TOOLS_ENABLED
  155. in_editor = Engine::get_singleton()->is_editor_hint();
  156. #endif // TOOLS_ENABLED
  157. if (is_inside_tree() && collision_animatable && !in_editor) {
  158. // Update transform on the physics tick when in animatable mode.
  159. last_valid_transform = new_transform;
  160. set_notify_local_transform(false);
  161. set_global_transform(new_transform);
  162. set_notify_local_transform(true);
  163. }
  164. } break;
  165. case TileMap::NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  166. // This is only executed when collision_animatable is enabled.
  167. bool in_editor = false;
  168. #ifdef TOOLS_ENABLED
  169. in_editor = Engine::get_singleton()->is_editor_hint();
  170. #endif // TOOLS_ENABLED
  171. if (is_inside_tree() && collision_animatable && !in_editor) {
  172. // Store last valid transform.
  173. new_transform = get_global_transform();
  174. // ... but then revert changes.
  175. set_notify_local_transform(false);
  176. set_global_transform(last_valid_transform);
  177. set_notify_local_transform(true);
  178. }
  179. } break;
  180. }
  181. }
  182. #ifndef DISABLE_DEPRECATED
  183. // Deprecated methods.
  184. void TileMap::force_update(int p_layer) {
  185. notify_runtime_tile_data_update(p_layer);
  186. update_internals();
  187. }
  188. #endif // DISABLE_DEPRECATED
  189. void TileMap::set_rendering_quadrant_size(int p_size) {
  190. ERR_FAIL_COND_MSG(p_size < 1, "TileMapQuadrant size cannot be smaller than 1.");
  191. rendering_quadrant_size = p_size;
  192. for (TileMapLayer *layer : layers) {
  193. layer->set_rendering_quadrant_size(p_size);
  194. }
  195. _emit_changed();
  196. }
  197. int TileMap::get_rendering_quadrant_size() const {
  198. return rendering_quadrant_size;
  199. }
  200. void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
  201. if (p_tileset == tile_set) {
  202. return;
  203. }
  204. // Set the tileset, registering to its changes.
  205. if (tile_set.is_valid()) {
  206. tile_set->disconnect_changed(callable_mp(this, &TileMap::_tile_set_changed));
  207. }
  208. tile_set = p_tileset;
  209. if (tile_set.is_valid()) {
  210. tile_set->connect_changed(callable_mp(this, &TileMap::_tile_set_changed));
  211. }
  212. for (int i = 0; i < get_child_count(); i++) {
  213. TileMapLayer *layer = Object::cast_to<TileMapLayer>(get_child(i));
  214. if (layer) {
  215. layer->set_tile_set(tile_set);
  216. }
  217. }
  218. }
  219. Ref<TileSet> TileMap::get_tileset() const {
  220. return tile_set;
  221. }
  222. int TileMap::get_layers_count() const {
  223. return layers.size();
  224. }
  225. void TileMap::add_layer(int p_to_pos) {
  226. if (p_to_pos < 0) {
  227. p_to_pos = layers.size() + p_to_pos + 1;
  228. }
  229. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  230. // Must clear before adding the layer.
  231. TileMapLayer *new_layer = memnew(TileMapLayer);
  232. layers.insert(p_to_pos, new_layer);
  233. add_child(new_layer, false, INTERNAL_MODE_FRONT);
  234. new_layer->set_name(vformat("Layer%d", p_to_pos));
  235. new_layer->set_tile_set(tile_set);
  236. move_child(new_layer, p_to_pos);
  237. for (uint32_t i = 0; i < layers.size(); i++) {
  238. layers[i]->set_as_tile_map_internal_node(i);
  239. }
  240. new_layer->connect(CoreStringName(changed), callable_mp(this, &TileMap::_emit_changed));
  241. notify_property_list_changed();
  242. _emit_changed();
  243. update_configuration_warnings();
  244. }
  245. void TileMap::move_layer(int p_layer, int p_to_pos) {
  246. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  247. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  248. // Clear before shuffling layers.
  249. TileMapLayer *layer = layers[p_layer];
  250. layers.insert(p_to_pos, layer);
  251. layers.remove_at(p_to_pos < p_layer ? p_layer + 1 : p_layer);
  252. for (uint32_t i = 0; i < layers.size(); i++) {
  253. move_child(layers[i], i);
  254. layers[i]->set_as_tile_map_internal_node(i);
  255. }
  256. notify_property_list_changed();
  257. _emit_changed();
  258. update_configuration_warnings();
  259. }
  260. void TileMap::remove_layer(int p_layer) {
  261. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  262. // Clear before removing the layer.
  263. TileMapLayer *removed = layers[p_layer];
  264. layers.remove_at(p_layer);
  265. remove_child(removed);
  266. removed->queue_free();
  267. for (uint32_t i = 0; i < layers.size(); i++) {
  268. layers[i]->set_as_tile_map_internal_node(i);
  269. }
  270. notify_property_list_changed();
  271. _emit_changed();
  272. update_configuration_warnings();
  273. }
  274. void TileMap::set_layer_name(int p_layer, String p_name) {
  275. TILEMAP_CALL_FOR_LAYER(p_layer, set_name, p_name);
  276. }
  277. String TileMap::get_layer_name(int p_layer) const {
  278. TILEMAP_CALL_FOR_LAYER_V(p_layer, "", get_name);
  279. }
  280. void TileMap::set_layer_enabled(int p_layer, bool p_enabled) {
  281. TILEMAP_CALL_FOR_LAYER(p_layer, set_enabled, p_enabled);
  282. }
  283. bool TileMap::is_layer_enabled(int p_layer) const {
  284. TILEMAP_CALL_FOR_LAYER_V(p_layer, false, is_enabled);
  285. }
  286. void TileMap::set_layer_modulate(int p_layer, Color p_modulate) {
  287. TILEMAP_CALL_FOR_LAYER(p_layer, set_modulate, p_modulate);
  288. }
  289. Color TileMap::get_layer_modulate(int p_layer) const {
  290. TILEMAP_CALL_FOR_LAYER_V(p_layer, Color(), get_modulate);
  291. }
  292. void TileMap::set_layer_y_sort_enabled(int p_layer, bool p_y_sort_enabled) {
  293. TILEMAP_CALL_FOR_LAYER(p_layer, set_y_sort_enabled, p_y_sort_enabled);
  294. update_configuration_warnings();
  295. }
  296. bool TileMap::is_layer_y_sort_enabled(int p_layer) const {
  297. TILEMAP_CALL_FOR_LAYER_V(p_layer, false, is_y_sort_enabled);
  298. }
  299. void TileMap::set_layer_y_sort_origin(int p_layer, int p_y_sort_origin) {
  300. TILEMAP_CALL_FOR_LAYER(p_layer, set_y_sort_origin, p_y_sort_origin);
  301. update_configuration_warnings();
  302. }
  303. int TileMap::get_layer_y_sort_origin(int p_layer) const {
  304. TILEMAP_CALL_FOR_LAYER_V(p_layer, 0, get_y_sort_origin);
  305. }
  306. void TileMap::set_layer_z_index(int p_layer, int p_z_index) {
  307. TILEMAP_CALL_FOR_LAYER(p_layer, set_z_index, p_z_index);
  308. }
  309. int TileMap::get_layer_z_index(int p_layer) const {
  310. TILEMAP_CALL_FOR_LAYER_V(p_layer, 0, get_z_index);
  311. }
  312. void TileMap::set_layer_navigation_enabled(int p_layer, bool p_enabled) {
  313. TILEMAP_CALL_FOR_LAYER(p_layer, set_navigation_enabled, p_enabled);
  314. }
  315. bool TileMap::is_layer_navigation_enabled(int p_layer) const {
  316. TILEMAP_CALL_FOR_LAYER_V(p_layer, false, is_navigation_enabled);
  317. }
  318. void TileMap::set_layer_navigation_map(int p_layer, RID p_map) {
  319. TILEMAP_CALL_FOR_LAYER(p_layer, set_navigation_map, p_map);
  320. }
  321. RID TileMap::get_layer_navigation_map(int p_layer) const {
  322. TILEMAP_CALL_FOR_LAYER_V(p_layer, RID(), get_navigation_map);
  323. }
  324. void TileMap::set_collision_animatable(bool p_collision_animatable) {
  325. if (collision_animatable == p_collision_animatable) {
  326. return;
  327. }
  328. collision_animatable = p_collision_animatable;
  329. set_notify_local_transform(p_collision_animatable);
  330. set_physics_process_internal(p_collision_animatable);
  331. for (TileMapLayer *layer : layers) {
  332. layer->set_use_kinematic_bodies(layer);
  333. }
  334. }
  335. bool TileMap::is_collision_animatable() const {
  336. return collision_animatable;
  337. }
  338. void TileMap::set_collision_visibility_mode(TileMap::VisibilityMode p_show_collision) {
  339. if (collision_visibility_mode == p_show_collision) {
  340. return;
  341. }
  342. collision_visibility_mode = p_show_collision;
  343. for (TileMapLayer *layer : layers) {
  344. layer->set_collision_visibility_mode(TileMapLayer::DebugVisibilityMode(p_show_collision));
  345. }
  346. _emit_changed();
  347. }
  348. TileMap::VisibilityMode TileMap::get_collision_visibility_mode() const {
  349. return collision_visibility_mode;
  350. }
  351. void TileMap::set_navigation_visibility_mode(TileMap::VisibilityMode p_show_navigation) {
  352. if (navigation_visibility_mode == p_show_navigation) {
  353. return;
  354. }
  355. navigation_visibility_mode = p_show_navigation;
  356. for (TileMapLayer *layer : layers) {
  357. layer->set_navigation_visibility_mode(TileMapLayer::DebugVisibilityMode(p_show_navigation));
  358. }
  359. _emit_changed();
  360. }
  361. TileMap::VisibilityMode TileMap::get_navigation_visibility_mode() const {
  362. return navigation_visibility_mode;
  363. }
  364. void TileMap::set_y_sort_enabled(bool p_enable) {
  365. if (is_y_sort_enabled() == p_enable) {
  366. return;
  367. }
  368. Node2D::set_y_sort_enabled(p_enable);
  369. _emit_changed();
  370. update_configuration_warnings();
  371. }
  372. void TileMap::set_cell(int p_layer, const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  373. TILEMAP_CALL_FOR_LAYER(p_layer, set_cell, p_coords, p_source_id, p_atlas_coords, p_alternative_tile);
  374. }
  375. void TileMap::erase_cell(int p_layer, const Vector2i &p_coords) {
  376. TILEMAP_CALL_FOR_LAYER(p_layer, set_cell, p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  377. }
  378. int TileMap::get_cell_source_id(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  379. if (p_use_proxies && tile_set.is_valid()) {
  380. if (p_layer < 0) {
  381. p_layer = layers.size() + p_layer;
  382. }
  383. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSet::INVALID_SOURCE);
  384. int source_id = layers[p_layer]->get_cell_source_id(p_coords);
  385. Vector2i atlas_coords = layers[p_layer]->get_cell_atlas_coords(p_coords);
  386. int alternative_id = layers[p_layer]->get_cell_alternative_tile(p_coords);
  387. Array arr = tile_set->map_tile_proxy(source_id, atlas_coords, alternative_id);
  388. ERR_FAIL_COND_V(arr.size() != 3, TileSet::INVALID_SOURCE);
  389. return arr[0];
  390. } else {
  391. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileSet::INVALID_SOURCE, get_cell_source_id, p_coords);
  392. }
  393. }
  394. Vector2i TileMap::get_cell_atlas_coords(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  395. if (p_use_proxies && tile_set.is_valid()) {
  396. if (p_layer < 0) {
  397. p_layer = layers.size() + p_layer;
  398. }
  399. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSetAtlasSource::INVALID_ATLAS_COORDS);
  400. int source_id = layers[p_layer]->get_cell_source_id(p_coords);
  401. Vector2i atlas_coords = layers[p_layer]->get_cell_atlas_coords(p_coords);
  402. int alternative_id = layers[p_layer]->get_cell_alternative_tile(p_coords);
  403. Array arr = tile_set->map_tile_proxy(source_id, atlas_coords, alternative_id);
  404. ERR_FAIL_COND_V(arr.size() != 3, TileSetSource::INVALID_ATLAS_COORDS);
  405. return arr[1];
  406. } else {
  407. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileSetSource::INVALID_ATLAS_COORDS, get_cell_atlas_coords, p_coords);
  408. }
  409. }
  410. int TileMap::get_cell_alternative_tile(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  411. if (p_use_proxies && tile_set.is_valid()) {
  412. if (p_layer < 0) {
  413. p_layer = layers.size() + p_layer;
  414. }
  415. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  416. int source_id = layers[p_layer]->get_cell_source_id(p_coords);
  417. Vector2i atlas_coords = layers[p_layer]->get_cell_atlas_coords(p_coords);
  418. int alternative_id = layers[p_layer]->get_cell_alternative_tile(p_coords);
  419. Array arr = tile_set->map_tile_proxy(source_id, atlas_coords, alternative_id);
  420. ERR_FAIL_COND_V(arr.size() != 3, TileSetSource::INVALID_TILE_ALTERNATIVE);
  421. return arr[2];
  422. } else {
  423. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileSetSource::INVALID_TILE_ALTERNATIVE, get_cell_alternative_tile, p_coords);
  424. }
  425. }
  426. TileData *TileMap::get_cell_tile_data(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  427. if (p_use_proxies && tile_set.is_valid()) {
  428. if (p_layer < 0) {
  429. p_layer = layers.size() + p_layer;
  430. }
  431. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  432. int source_id = layers[p_layer]->get_cell_source_id(p_coords);
  433. Vector2i atlas_coords = layers[p_layer]->get_cell_atlas_coords(p_coords);
  434. int alternative_id = layers[p_layer]->get_cell_alternative_tile(p_coords);
  435. Array arr = tile_set->map_tile_proxy(source_id, atlas_coords, alternative_id);
  436. ERR_FAIL_COND_V(arr.size() != 3, nullptr);
  437. Ref<TileSetAtlasSource> atlas_source = tile_set->get_source(arr[0]);
  438. if (atlas_source.is_valid()) {
  439. return atlas_source->get_tile_data(arr[1], arr[2]);
  440. } else {
  441. return nullptr;
  442. }
  443. } else {
  444. TILEMAP_CALL_FOR_LAYER_V(p_layer, nullptr, get_cell_tile_data, p_coords);
  445. }
  446. }
  447. bool TileMap::is_cell_flipped_h(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  448. return get_cell_alternative_tile(p_layer, p_coords, p_use_proxies) & TileSetAtlasSource::TRANSFORM_FLIP_H;
  449. }
  450. bool TileMap::is_cell_flipped_v(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  451. return get_cell_alternative_tile(p_layer, p_coords, p_use_proxies) & TileSetAtlasSource::TRANSFORM_FLIP_V;
  452. }
  453. bool TileMap::is_cell_transposed(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  454. return get_cell_alternative_tile(p_layer, p_coords, p_use_proxies) & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
  455. }
  456. Ref<TileMapPattern> TileMap::get_pattern(int p_layer, TypedArray<Vector2i> p_coords_array) {
  457. TILEMAP_CALL_FOR_LAYER_V(p_layer, Ref<TileMapPattern>(), get_pattern, p_coords_array);
  458. }
  459. Vector2i TileMap::map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern) {
  460. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  461. return tile_set->map_pattern(p_position_in_tilemap, p_coords_in_pattern, p_pattern);
  462. }
  463. void TileMap::set_pattern(int p_layer, const Vector2i &p_position, const Ref<TileMapPattern> p_pattern) {
  464. TILEMAP_CALL_FOR_LAYER(p_layer, set_pattern, p_position, p_pattern);
  465. }
  466. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_constraints(int p_layer, const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints) {
  467. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  468. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_constraints, p_to_replace, p_terrain_set, p_constraints);
  469. }
  470. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_connect(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  471. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  472. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_connect, p_coords_array, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  473. }
  474. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_path(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  475. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  476. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_path, p_coords_array, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  477. }
  478. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::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) {
  479. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  480. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_pattern, p_coords_array, p_terrain_set, p_terrains_pattern, p_ignore_empty_terrains);
  481. }
  482. void TileMap::set_cells_terrain_connect(int p_layer, TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  483. TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_connect, p_cells, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  484. }
  485. void TileMap::set_cells_terrain_path(int p_layer, TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  486. TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_path, p_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  487. }
  488. TileMapCell TileMap::get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  489. if (p_use_proxies) {
  490. WARN_DEPRECATED_MSG("use_proxies is deprecated.");
  491. }
  492. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileMapCell(), get_cell, p_coords);
  493. }
  494. Vector2i TileMap::get_coords_for_body_rid(RID p_physics_body) {
  495. for (const TileMapLayer *layer : layers) {
  496. if (layer->has_body_rid(p_physics_body)) {
  497. return layer->get_coords_for_body_rid(p_physics_body);
  498. }
  499. }
  500. ERR_FAIL_V_MSG(Vector2i(), vformat("No tiles for the given body RID %d.", p_physics_body.get_id()));
  501. }
  502. int TileMap::get_layer_for_body_rid(RID p_physics_body) {
  503. for (uint32_t i = 0; i < layers.size(); i++) {
  504. if (layers[i]->has_body_rid(p_physics_body)) {
  505. return i;
  506. }
  507. }
  508. ERR_FAIL_V_MSG(-1, vformat("No tiles for the given body RID %d.", p_physics_body.get_id()));
  509. }
  510. void TileMap::fix_invalid_tiles() {
  511. for (TileMapLayer *layer : layers) {
  512. layer->fix_invalid_tiles();
  513. }
  514. }
  515. #ifdef TOOLS_ENABLED
  516. TileMapLayer *TileMap::duplicate_layer_from_internal(int p_layer) {
  517. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  518. return Object::cast_to<TileMapLayer>(layers[p_layer]->duplicate(DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR));
  519. }
  520. #endif // TOOLS_ENABLED
  521. void TileMap::clear_layer(int p_layer) {
  522. TILEMAP_CALL_FOR_LAYER(p_layer, clear)
  523. }
  524. void TileMap::clear() {
  525. for (TileMapLayer *layer : layers) {
  526. layer->clear();
  527. }
  528. }
  529. void TileMap::update_internals() {
  530. for (TileMapLayer *layer : layers) {
  531. layer->update_internals();
  532. }
  533. }
  534. void TileMap::notify_runtime_tile_data_update(int p_layer) {
  535. if (p_layer >= 0) {
  536. TILEMAP_CALL_FOR_LAYER(p_layer, notify_runtime_tile_data_update);
  537. } else {
  538. for (TileMapLayer *layer : layers) {
  539. layer->notify_runtime_tile_data_update();
  540. }
  541. }
  542. }
  543. #ifdef DEBUG_ENABLED
  544. Rect2 TileMap::_edit_get_rect() const {
  545. // Return the visible rect of the tilemap.
  546. if (layers.is_empty()) {
  547. return Rect2();
  548. }
  549. bool any_changed = false;
  550. bool changed = false;
  551. Rect2 rect = layers[0]->get_rect(changed);
  552. any_changed |= changed;
  553. for (uint32_t i = 1; i < layers.size(); i++) {
  554. rect = rect.merge(layers[i]->get_rect(changed));
  555. any_changed |= changed;
  556. }
  557. const_cast<TileMap *>(this)->item_rect_changed(any_changed);
  558. return rect;
  559. }
  560. #endif // DEBUG_ENABLED
  561. bool TileMap::_set(const StringName &p_name, const Variant &p_value) {
  562. int index;
  563. const String sname = p_name;
  564. Vector<String> components = String(p_name).split("/", true, 2);
  565. if (sname == "format") {
  566. if (p_value.get_type() == Variant::INT) {
  567. format = (TileMapDataFormat)(p_value.operator int64_t()); // Set format used for loading.
  568. return true;
  569. }
  570. }
  571. #ifndef DISABLE_DEPRECATED
  572. else if (sname == "cell_quadrant_size") {
  573. set_rendering_quadrant_size(p_value);
  574. return true;
  575. }
  576. #endif // DISABLE_DEPRECATED
  577. else if (property_helper.is_property_valid(sname, &index)) {
  578. if (index >= (int)layers.size()) {
  579. while (index >= (int)layers.size()) {
  580. TileMapLayer *new_layer = memnew(TileMapLayer);
  581. add_child(new_layer, false, INTERNAL_MODE_FRONT);
  582. new_layer->set_as_tile_map_internal_node(index);
  583. new_layer->set_name(vformat("Layer%d", index));
  584. new_layer->set_tile_set(tile_set);
  585. new_layer->connect(CoreStringName(changed), callable_mp(this, &TileMap::_emit_changed));
  586. layers.push_back(new_layer);
  587. }
  588. notify_property_list_changed();
  589. _emit_changed();
  590. update_configuration_warnings();
  591. }
  592. if (property_helper.property_set_value(sname, p_value)) {
  593. if (components[1] == "tile_data") {
  594. _emit_changed();
  595. }
  596. return true;
  597. }
  598. }
  599. return false;
  600. }
  601. bool TileMap::_get(const StringName &p_name, Variant &r_ret) const {
  602. const String sname = p_name;
  603. Vector<String> components = String(p_name).split("/", true, 2);
  604. if (p_name == "format") {
  605. r_ret = TileMapDataFormat::TILE_MAP_DATA_FORMAT_MAX - 1; // When saving, always save highest format.
  606. return true;
  607. }
  608. #ifndef DISABLE_DEPRECATED
  609. else if (sname == "cell_quadrant_size") { // Kept for compatibility reasons.
  610. r_ret = get_rendering_quadrant_size();
  611. return true;
  612. }
  613. #endif // DISABLE_DEPRECATED
  614. else {
  615. return property_helper.property_get_value(sname, r_ret);
  616. }
  617. }
  618. void TileMap::_get_property_list(List<PropertyInfo> *p_list) const {
  619. p_list->push_back(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  620. property_helper.get_property_list(p_list);
  621. }
  622. Vector2 TileMap::map_to_local(const Vector2i &p_pos) const {
  623. ERR_FAIL_COND_V(tile_set.is_null(), Vector2());
  624. return tile_set->map_to_local(p_pos);
  625. }
  626. Vector2i TileMap::local_to_map(const Vector2 &p_pos) const {
  627. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  628. return tile_set->local_to_map(p_pos);
  629. }
  630. bool TileMap::is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const {
  631. ERR_FAIL_COND_V(tile_set.is_null(), false);
  632. return tile_set->is_existing_neighbor(p_cell_neighbor);
  633. }
  634. Vector2i TileMap::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  635. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  636. return tile_set->get_neighbor_cell(p_coords, p_cell_neighbor);
  637. }
  638. TypedArray<Vector2i> TileMap::get_used_cells(int p_layer) const {
  639. TILEMAP_CALL_FOR_LAYER_V(p_layer, TypedArray<Vector2i>(), get_used_cells);
  640. }
  641. TypedArray<Vector2i> TileMap::get_used_cells_by_id(int p_layer, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) const {
  642. TILEMAP_CALL_FOR_LAYER_V(p_layer, TypedArray<Vector2i>(), get_used_cells_by_id, p_source_id, p_atlas_coords, p_alternative_tile);
  643. }
  644. Rect2i TileMap::get_used_rect() const {
  645. // Return the visible rect of the tilemap.
  646. bool first = true;
  647. Rect2i rect = Rect2i();
  648. for (const TileMapLayer *layer : layers) {
  649. Rect2i layer_rect = layer->get_used_rect();
  650. if (layer_rect == Rect2i()) {
  651. continue;
  652. }
  653. if (first) {
  654. rect = layer_rect;
  655. first = false;
  656. } else {
  657. rect = rect.merge(layer_rect);
  658. }
  659. }
  660. return rect;
  661. }
  662. // --- Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems ---
  663. void TileMap::set_light_mask(int p_light_mask) {
  664. // Set light mask for occlusion and applies it to all layers too.
  665. CanvasItem::set_light_mask(p_light_mask);
  666. for (TileMapLayer *layer : layers) {
  667. layer->set_light_mask(p_light_mask);
  668. }
  669. }
  670. void TileMap::set_self_modulate(const Color &p_self_modulate) {
  671. // Set self_modulation and applies it to all layers too.
  672. CanvasItem::set_self_modulate(p_self_modulate);
  673. for (TileMapLayer *layer : layers) {
  674. layer->set_self_modulate(p_self_modulate);
  675. }
  676. }
  677. void TileMap::set_texture_filter(TextureFilter p_texture_filter) {
  678. // Set a default texture filter and applies it to all layers too.
  679. CanvasItem::set_texture_filter(p_texture_filter);
  680. for (TileMapLayer *layer : layers) {
  681. layer->set_texture_filter(p_texture_filter);
  682. }
  683. }
  684. void TileMap::set_texture_repeat(CanvasItem::TextureRepeat p_texture_repeat) {
  685. // Set a default texture repeat and applies it to all layers too.
  686. CanvasItem::set_texture_repeat(p_texture_repeat);
  687. for (TileMapLayer *layer : layers) {
  688. layer->set_texture_repeat(p_texture_repeat);
  689. }
  690. }
  691. TypedArray<Vector2i> TileMap::get_surrounding_cells(const Vector2i &p_coords) {
  692. if (tile_set.is_null()) {
  693. return TypedArray<Vector2i>();
  694. }
  695. return tile_set->get_surrounding_cells(p_coords);
  696. }
  697. PackedStringArray TileMap::get_configuration_warnings() const {
  698. PackedStringArray warnings = Node2D::get_configuration_warnings();
  699. warnings.push_back(RTR("The TileMap node is deprecated as it is superseded by the use of multiple TileMapLayer nodes.\nTo convert a TileMap to a set of TileMapLayer nodes, open the TileMap bottom panel with this node selected, click the toolbox icon in the top-right corner and choose \"Extract TileMap layers as individual TileMapLayer nodes\"."));
  700. // Retrieve the set of Z index values with a Y-sorted layer.
  701. RBSet<int> y_sorted_z_index;
  702. for (const TileMapLayer *layer : layers) {
  703. if (layer->is_y_sort_enabled()) {
  704. y_sorted_z_index.insert(layer->get_z_index());
  705. }
  706. }
  707. // Check if we have a non-sorted layer in a Z-index with a Y-sorted layer.
  708. for (const TileMapLayer *layer : layers) {
  709. if (!layer->is_y_sort_enabled() && y_sorted_z_index.has(layer->get_z_index())) {
  710. warnings.push_back(RTR("A Y-sorted layer has the same Z-index value as a not Y-sorted layer.\nThis may lead to unwanted behaviors, as a layer that is not Y-sorted will be Y-sorted as a whole with tiles from Y-sorted layers."));
  711. break;
  712. }
  713. }
  714. if (!is_y_sort_enabled()) {
  715. // Check if Y-sort is enabled on a layer but not on the node.
  716. for (const TileMapLayer *layer : layers) {
  717. if (layer->is_y_sort_enabled()) {
  718. warnings.push_back(RTR("A TileMap layer is set as Y-sorted, but Y-sort is not enabled on the TileMap node itself."));
  719. break;
  720. }
  721. }
  722. } else {
  723. // Check if Y-sort is enabled on the node, but not on any of the layers.
  724. bool need_warning = true;
  725. for (const TileMapLayer *layer : layers) {
  726. if (layer->is_y_sort_enabled()) {
  727. need_warning = false;
  728. break;
  729. }
  730. }
  731. if (need_warning) {
  732. warnings.push_back(RTR("The TileMap node is set as Y-sorted, but Y-sort is not enabled on any of the TileMap's layers.\nThis may lead to unwanted behaviors, as a layer that is not Y-sorted will be Y-sorted as a whole."));
  733. }
  734. }
  735. // Check if we are in isometric mode without Y-sort enabled.
  736. if (tile_set.is_valid() && tile_set->get_tile_shape() == TileSet::TILE_SHAPE_ISOMETRIC) {
  737. bool warn = !is_y_sort_enabled();
  738. if (!warn) {
  739. for (const TileMapLayer *layer : layers) {
  740. if (!layer->is_y_sort_enabled()) {
  741. warn = true;
  742. break;
  743. }
  744. }
  745. }
  746. if (warn) {
  747. warnings.push_back(RTR("Isometric TileSet will likely not look as intended without Y-sort enabled for the TileMap and all of its layers."));
  748. }
  749. }
  750. return warnings;
  751. }
  752. void TileMap::_bind_methods() {
  753. #ifndef DISABLE_DEPRECATED
  754. ClassDB::bind_method(D_METHOD("set_navigation_map", "layer", "map"), &TileMap::set_layer_navigation_map);
  755. ClassDB::bind_method(D_METHOD("get_navigation_map", "layer"), &TileMap::get_layer_navigation_map);
  756. ClassDB::bind_method(D_METHOD("force_update", "layer"), &TileMap::force_update, DEFVAL(-1));
  757. #endif // DISABLE_DEPRECATED
  758. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  759. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  760. ClassDB::bind_method(D_METHOD("set_rendering_quadrant_size", "size"), &TileMap::set_rendering_quadrant_size);
  761. ClassDB::bind_method(D_METHOD("get_rendering_quadrant_size"), &TileMap::get_rendering_quadrant_size);
  762. ClassDB::bind_method(D_METHOD("get_layers_count"), &TileMap::get_layers_count);
  763. ClassDB::bind_method(D_METHOD("add_layer", "to_position"), &TileMap::add_layer);
  764. ClassDB::bind_method(D_METHOD("move_layer", "layer", "to_position"), &TileMap::move_layer);
  765. ClassDB::bind_method(D_METHOD("remove_layer", "layer"), &TileMap::remove_layer);
  766. ClassDB::bind_method(D_METHOD("set_layer_name", "layer", "name"), &TileMap::set_layer_name);
  767. ClassDB::bind_method(D_METHOD("get_layer_name", "layer"), &TileMap::get_layer_name);
  768. ClassDB::bind_method(D_METHOD("set_layer_enabled", "layer", "enabled"), &TileMap::set_layer_enabled);
  769. ClassDB::bind_method(D_METHOD("is_layer_enabled", "layer"), &TileMap::is_layer_enabled);
  770. ClassDB::bind_method(D_METHOD("set_layer_modulate", "layer", "modulate"), &TileMap::set_layer_modulate);
  771. ClassDB::bind_method(D_METHOD("get_layer_modulate", "layer"), &TileMap::get_layer_modulate);
  772. ClassDB::bind_method(D_METHOD("set_layer_y_sort_enabled", "layer", "y_sort_enabled"), &TileMap::set_layer_y_sort_enabled);
  773. ClassDB::bind_method(D_METHOD("is_layer_y_sort_enabled", "layer"), &TileMap::is_layer_y_sort_enabled);
  774. ClassDB::bind_method(D_METHOD("set_layer_y_sort_origin", "layer", "y_sort_origin"), &TileMap::set_layer_y_sort_origin);
  775. ClassDB::bind_method(D_METHOD("get_layer_y_sort_origin", "layer"), &TileMap::get_layer_y_sort_origin);
  776. ClassDB::bind_method(D_METHOD("set_layer_z_index", "layer", "z_index"), &TileMap::set_layer_z_index);
  777. ClassDB::bind_method(D_METHOD("get_layer_z_index", "layer"), &TileMap::get_layer_z_index);
  778. ClassDB::bind_method(D_METHOD("set_layer_navigation_enabled", "layer", "enabled"), &TileMap::set_layer_navigation_enabled);
  779. ClassDB::bind_method(D_METHOD("is_layer_navigation_enabled", "layer"), &TileMap::is_layer_navigation_enabled);
  780. ClassDB::bind_method(D_METHOD("set_layer_navigation_map", "layer", "map"), &TileMap::set_layer_navigation_map);
  781. ClassDB::bind_method(D_METHOD("get_layer_navigation_map", "layer"), &TileMap::get_layer_navigation_map);
  782. ClassDB::bind_method(D_METHOD("set_collision_animatable", "enabled"), &TileMap::set_collision_animatable);
  783. ClassDB::bind_method(D_METHOD("is_collision_animatable"), &TileMap::is_collision_animatable);
  784. ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "collision_visibility_mode"), &TileMap::set_collision_visibility_mode);
  785. ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMap::get_collision_visibility_mode);
  786. ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "navigation_visibility_mode"), &TileMap::set_navigation_visibility_mode);
  787. ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode);
  788. ClassDB::bind_method(D_METHOD("set_cell", "layer", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMap::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(0));
  789. ClassDB::bind_method(D_METHOD("erase_cell", "layer", "coords"), &TileMap::erase_cell);
  790. ClassDB::bind_method(D_METHOD("get_cell_source_id", "layer", "coords", "use_proxies"), &TileMap::get_cell_source_id, DEFVAL(false));
  791. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "layer", "coords", "use_proxies"), &TileMap::get_cell_atlas_coords, DEFVAL(false));
  792. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "layer", "coords", "use_proxies"), &TileMap::get_cell_alternative_tile, DEFVAL(false));
  793. ClassDB::bind_method(D_METHOD("get_cell_tile_data", "layer", "coords", "use_proxies"), &TileMap::get_cell_tile_data, DEFVAL(false));
  794. ClassDB::bind_method(D_METHOD("is_cell_flipped_h", "layer", "coords", "use_proxies"), &TileMap::is_cell_flipped_h, DEFVAL(false));
  795. ClassDB::bind_method(D_METHOD("is_cell_flipped_v", "layer", "coords", "use_proxies"), &TileMap::is_cell_flipped_v, DEFVAL(false));
  796. ClassDB::bind_method(D_METHOD("is_cell_transposed", "layer", "coords", "use_proxies"), &TileMap::is_cell_transposed, DEFVAL(false));
  797. ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMap::get_coords_for_body_rid);
  798. ClassDB::bind_method(D_METHOD("get_layer_for_body_rid", "body"), &TileMap::get_layer_for_body_rid);
  799. ClassDB::bind_method(D_METHOD("get_pattern", "layer", "coords_array"), &TileMap::get_pattern);
  800. ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMap::map_pattern);
  801. ClassDB::bind_method(D_METHOD("set_pattern", "layer", "position", "pattern"), &TileMap::set_pattern);
  802. ClassDB::bind_method(D_METHOD("set_cells_terrain_connect", "layer", "cells", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_connect, DEFVAL(true));
  803. ClassDB::bind_method(D_METHOD("set_cells_terrain_path", "layer", "path", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_path, DEFVAL(true));
  804. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
  805. ClassDB::bind_method(D_METHOD("clear_layer", "layer"), &TileMap::clear_layer);
  806. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  807. ClassDB::bind_method(D_METHOD("update_internals"), &TileMap::update_internals);
  808. ClassDB::bind_method(D_METHOD("notify_runtime_tile_data_update", "layer"), &TileMap::notify_runtime_tile_data_update, DEFVAL(-1));
  809. ClassDB::bind_method(D_METHOD("get_surrounding_cells", "coords"), &TileMap::get_surrounding_cells);
  810. ClassDB::bind_method(D_METHOD("get_used_cells", "layer"), &TileMap::get_used_cells);
  811. ClassDB::bind_method(D_METHOD("get_used_cells_by_id", "layer", "source_id", "atlas_coords", "alternative_tile"), &TileMap::get_used_cells_by_id, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE));
  812. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  813. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &TileMap::map_to_local);
  814. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &TileMap::local_to_map);
  815. ClassDB::bind_method(D_METHOD("get_neighbor_cell", "coords", "neighbor"), &TileMap::get_neighbor_cell);
  816. GDVIRTUAL_BIND(_use_tile_data_runtime_update, "layer", "coords");
  817. GDVIRTUAL_BIND(_tile_data_runtime_update, "layer", "coords", "tile_data");
  818. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  819. ADD_PROPERTY(PropertyInfo(Variant::INT, "rendering_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_rendering_quadrant_size", "get_rendering_quadrant_size");
  820. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_animatable"), "set_collision_animatable", "is_collision_animatable");
  821. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
  822. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
  823. ADD_ARRAY("layers", "layer_");
  824. ADD_PROPERTY_DEFAULT("format", TileMapDataFormat::TILE_MAP_DATA_FORMAT_1);
  825. ADD_SIGNAL(MethodInfo(CoreStringName(changed)));
  826. BIND_ENUM_CONSTANT(VISIBILITY_MODE_DEFAULT);
  827. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_HIDE);
  828. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_SHOW);
  829. }
  830. TileMap::TileMap() {
  831. TileMapLayer *new_layer = memnew(TileMapLayer);
  832. add_child(new_layer, false, INTERNAL_MODE_FRONT);
  833. new_layer->set_as_tile_map_internal_node(0);
  834. new_layer->set_name("Layer0");
  835. new_layer->set_tile_set(tile_set);
  836. new_layer->connect(CoreStringName(changed), callable_mp(this, &TileMap::_emit_changed));
  837. layers.push_back(new_layer);
  838. if (!base_property_helper.is_initialized()) {
  839. // Initialize static PropertyListHelper if it wasn't yet. This has to be done here,
  840. // because creating TileMapLayer in a static context is not always safe.
  841. TileMapLayer *defaults = memnew(TileMapLayer);
  842. base_property_helper.set_prefix("layer_");
  843. base_property_helper.set_array_length_getter(&TileMap::get_layers_count);
  844. base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults->get_name(), &TileMap::set_layer_name, &TileMap::get_layer_name);
  845. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "enabled"), defaults->is_enabled(), &TileMap::set_layer_enabled, &TileMap::is_layer_enabled);
  846. base_property_helper.register_property(PropertyInfo(Variant::COLOR, "modulate"), defaults->get_modulate(), &TileMap::set_layer_modulate, &TileMap::get_layer_modulate);
  847. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "y_sort_enabled"), defaults->is_y_sort_enabled(), &TileMap::set_layer_y_sort_enabled, &TileMap::is_layer_y_sort_enabled);
  848. base_property_helper.register_property(PropertyInfo(Variant::INT, "y_sort_origin", PROPERTY_HINT_NONE, "suffix:px"), defaults->get_y_sort_origin(), &TileMap::set_layer_y_sort_origin, &TileMap::get_layer_y_sort_origin);
  849. base_property_helper.register_property(PropertyInfo(Variant::INT, "z_index"), defaults->get_z_index(), &TileMap::set_layer_z_index, &TileMap::get_layer_z_index);
  850. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "navigation_enabled"), defaults->is_navigation_enabled(), &TileMap::set_layer_navigation_enabled, &TileMap::is_layer_navigation_enabled);
  851. base_property_helper.register_property(PropertyInfo(Variant::PACKED_INT32_ARRAY, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), Vector<int>(), &TileMap::_set_layer_tile_data, &TileMap::_get_tile_map_data_using_compatibility_format);
  852. PropertyListHelper::register_base_helper(&base_property_helper);
  853. memdelete(defaults);
  854. }
  855. property_helper.setup_for_instance(base_property_helper, this);
  856. }
  857. void TileMap::navmesh_parse_init() {
  858. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  859. if (!_navmesh_source_geometry_parser.is_valid()) {
  860. _navmesh_source_geometry_parsing_callback = callable_mp_static(&TileMap::navmesh_parse_source_geometry);
  861. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  862. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  863. }
  864. }
  865. void TileMap::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  866. TileMap *nb_tilemap = Object::cast_to<TileMap>(p_node);
  867. if (nb_tilemap == nullptr) {
  868. return;
  869. }
  870. // Special case for TileMap, so that internal layer get parsed even if p_recurse_children is false.
  871. bool recurse_children = p_navigation_mesh->get_source_geometry_mode() != NavigationPolygon::SOURCE_GEOMETRY_GROUPS_EXPLICIT;
  872. if (!recurse_children) {
  873. for (int i = 0; i < p_node->get_child_count(); i++) {
  874. TileMapLayer *tile_map_layer = Object::cast_to<TileMapLayer>(p_node->get_child(i));
  875. if (tile_map_layer && tile_map_layer->get_index_in_tile_map() >= 0) {
  876. tile_map_layer->navmesh_parse_source_geometry(p_navigation_mesh, p_source_geometry_data, tile_map_layer);
  877. }
  878. }
  879. }
  880. }
  881. #undef TILEMAP_CALL_FOR_LAYER
  882. #undef TILEMAP_CALL_FOR_LAYER_V