tile_map.cpp 42 KB

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