tile_map.cpp 44 KB

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