tile_map.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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_2d/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. TILEMAP_CALL_FOR_LAYER_V(p_layer, (HashMap<Vector2i, TileSet::TerrainsPattern>()), terrain_fill_constraints, p_to_replace, p_terrain_set, p_constraints);
  476. }
  477. 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) {
  478. TILEMAP_CALL_FOR_LAYER_V(p_layer, (HashMap<Vector2i, TileSet::TerrainsPattern>()), terrain_fill_connect, p_coords_array, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  479. }
  480. 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) {
  481. TILEMAP_CALL_FOR_LAYER_V(p_layer, (HashMap<Vector2i, TileSet::TerrainsPattern>()), terrain_fill_path, p_coords_array, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  482. }
  483. 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) {
  484. TILEMAP_CALL_FOR_LAYER_V(p_layer, (HashMap<Vector2i, TileSet::TerrainsPattern>()), terrain_fill_pattern, p_coords_array, p_terrain_set, p_terrains_pattern, p_ignore_empty_terrains);
  485. }
  486. 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) {
  487. TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_connect, p_cells, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  488. }
  489. 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) {
  490. TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_path, p_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  491. }
  492. TileMapCell TileMap::get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  493. if (p_use_proxies) {
  494. WARN_DEPRECATED_MSG("use_proxies is deprecated.");
  495. }
  496. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileMapCell(), get_cell, p_coords);
  497. }
  498. #ifndef PHYSICS_2D_DISABLED
  499. Vector2i TileMap::get_coords_for_body_rid(RID p_physics_body) {
  500. for (const TileMapLayer *layer : layers) {
  501. if (layer->has_body_rid(p_physics_body)) {
  502. return layer->get_coords_for_body_rid(p_physics_body);
  503. }
  504. }
  505. ERR_FAIL_V_MSG(Vector2i(), vformat("No tiles for the given body RID %d.", p_physics_body.get_id()));
  506. }
  507. int TileMap::get_layer_for_body_rid(RID p_physics_body) {
  508. for (uint32_t i = 0; i < layers.size(); i++) {
  509. if (layers[i]->has_body_rid(p_physics_body)) {
  510. return i;
  511. }
  512. }
  513. ERR_FAIL_V_MSG(-1, vformat("No tiles for the given body RID %d.", p_physics_body.get_id()));
  514. }
  515. #endif // PHYSICS_2D_DISABLED
  516. void TileMap::fix_invalid_tiles() {
  517. for (TileMapLayer *layer : layers) {
  518. layer->fix_invalid_tiles();
  519. }
  520. }
  521. #ifdef TOOLS_ENABLED
  522. TileMapLayer *TileMap::duplicate_layer_from_internal(int p_layer) {
  523. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  524. return Object::cast_to<TileMapLayer>(layers[p_layer]->duplicate(DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR));
  525. }
  526. #endif // TOOLS_ENABLED
  527. void TileMap::clear_layer(int p_layer) {
  528. TILEMAP_CALL_FOR_LAYER(p_layer, clear)
  529. }
  530. void TileMap::clear() {
  531. for (TileMapLayer *layer : layers) {
  532. layer->clear();
  533. }
  534. }
  535. void TileMap::update_internals() {
  536. for (TileMapLayer *layer : layers) {
  537. layer->update_internals();
  538. }
  539. }
  540. void TileMap::notify_runtime_tile_data_update(int p_layer) {
  541. if (p_layer >= 0) {
  542. TILEMAP_CALL_FOR_LAYER(p_layer, notify_runtime_tile_data_update);
  543. } else {
  544. for (TileMapLayer *layer : layers) {
  545. layer->notify_runtime_tile_data_update();
  546. }
  547. }
  548. }
  549. #ifdef DEBUG_ENABLED
  550. Rect2 TileMap::_edit_get_rect() const {
  551. // Return the visible rect of the tilemap.
  552. if (layers.is_empty()) {
  553. return Rect2();
  554. }
  555. bool any_changed = false;
  556. bool changed = false;
  557. Rect2 rect = layers[0]->get_rect(changed);
  558. any_changed |= changed;
  559. for (uint32_t i = 1; i < layers.size(); i++) {
  560. rect = rect.merge(layers[i]->get_rect(changed));
  561. any_changed |= changed;
  562. }
  563. const_cast<TileMap *>(this)->item_rect_changed(any_changed);
  564. return rect;
  565. }
  566. #endif // DEBUG_ENABLED
  567. bool TileMap::_set(const StringName &p_name, const Variant &p_value) {
  568. int index;
  569. const String sname = p_name;
  570. Vector<String> components = String(p_name).split("/", true, 2);
  571. if (sname == "format") {
  572. if (p_value.get_type() == Variant::INT) {
  573. format = (TileMapDataFormat)(p_value.operator int64_t()); // Set format used for loading.
  574. return true;
  575. }
  576. }
  577. #ifndef DISABLE_DEPRECATED
  578. else if (sname == "cell_quadrant_size") {
  579. set_rendering_quadrant_size(p_value);
  580. return true;
  581. }
  582. #endif // DISABLE_DEPRECATED
  583. else if (property_helper.is_property_valid(sname, &index)) {
  584. if (index >= (int)layers.size()) {
  585. while (index >= (int)layers.size()) {
  586. TileMapLayer *new_layer = memnew(TileMapLayer);
  587. add_child(new_layer, false, INTERNAL_MODE_FRONT);
  588. new_layer->set_as_tile_map_internal_node(index);
  589. new_layer->set_name(vformat("Layer%d", index));
  590. new_layer->set_tile_set(tile_set);
  591. new_layer->connect(CoreStringName(changed), callable_mp(this, &TileMap::_emit_changed));
  592. layers.push_back(new_layer);
  593. }
  594. notify_property_list_changed();
  595. _emit_changed();
  596. update_configuration_warnings();
  597. }
  598. if (property_helper.property_set_value(sname, p_value)) {
  599. if (components[1] == "tile_data") {
  600. _emit_changed();
  601. }
  602. return true;
  603. }
  604. }
  605. return false;
  606. }
  607. bool TileMap::_get(const StringName &p_name, Variant &r_ret) const {
  608. const String sname = p_name;
  609. Vector<String> components = String(p_name).split("/", true, 2);
  610. if (p_name == "format") {
  611. r_ret = TileMapDataFormat::TILE_MAP_DATA_FORMAT_MAX - 1; // When saving, always save highest format.
  612. return true;
  613. }
  614. #ifndef DISABLE_DEPRECATED
  615. else if (sname == "cell_quadrant_size") { // Kept for compatibility reasons.
  616. r_ret = get_rendering_quadrant_size();
  617. return true;
  618. }
  619. #endif // DISABLE_DEPRECATED
  620. else {
  621. return property_helper.property_get_value(sname, r_ret);
  622. }
  623. }
  624. void TileMap::_get_property_list(List<PropertyInfo> *p_list) const {
  625. p_list->push_back(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  626. property_helper.get_property_list(p_list);
  627. }
  628. Vector2 TileMap::map_to_local(const Vector2i &p_pos) const {
  629. ERR_FAIL_COND_V(tile_set.is_null(), Vector2());
  630. return tile_set->map_to_local(p_pos);
  631. }
  632. Vector2i TileMap::local_to_map(const Vector2 &p_pos) const {
  633. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  634. return tile_set->local_to_map(p_pos);
  635. }
  636. bool TileMap::is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const {
  637. ERR_FAIL_COND_V(tile_set.is_null(), false);
  638. return tile_set->is_existing_neighbor(p_cell_neighbor);
  639. }
  640. Vector2i TileMap::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  641. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  642. return tile_set->get_neighbor_cell(p_coords, p_cell_neighbor);
  643. }
  644. TypedArray<Vector2i> TileMap::get_used_cells(int p_layer) const {
  645. TILEMAP_CALL_FOR_LAYER_V(p_layer, TypedArray<Vector2i>(), get_used_cells);
  646. }
  647. 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 {
  648. TILEMAP_CALL_FOR_LAYER_V(p_layer, TypedArray<Vector2i>(), get_used_cells_by_id, p_source_id, p_atlas_coords, p_alternative_tile);
  649. }
  650. Rect2i TileMap::get_used_rect() const {
  651. // Return the visible rect of the tilemap.
  652. bool first = true;
  653. Rect2i rect = Rect2i();
  654. for (const TileMapLayer *layer : layers) {
  655. Rect2i layer_rect = layer->get_used_rect();
  656. if (layer_rect == Rect2i()) {
  657. continue;
  658. }
  659. if (first) {
  660. rect = layer_rect;
  661. first = false;
  662. } else {
  663. rect = rect.merge(layer_rect);
  664. }
  665. }
  666. return rect;
  667. }
  668. // --- Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems ---
  669. void TileMap::set_light_mask(int p_light_mask) {
  670. // Set light mask for occlusion and applies it to all layers too.
  671. CanvasItem::set_light_mask(p_light_mask);
  672. for (TileMapLayer *layer : layers) {
  673. layer->set_light_mask(p_light_mask);
  674. }
  675. }
  676. void TileMap::set_self_modulate(const Color &p_self_modulate) {
  677. // Set self_modulation and applies it to all layers too.
  678. CanvasItem::set_self_modulate(p_self_modulate);
  679. for (TileMapLayer *layer : layers) {
  680. layer->set_self_modulate(p_self_modulate);
  681. }
  682. }
  683. void TileMap::set_texture_filter(TextureFilter p_texture_filter) {
  684. // Set a default texture filter and applies it to all layers too.
  685. CanvasItem::set_texture_filter(p_texture_filter);
  686. for (TileMapLayer *layer : layers) {
  687. layer->set_texture_filter(p_texture_filter);
  688. }
  689. }
  690. void TileMap::set_texture_repeat(CanvasItem::TextureRepeat p_texture_repeat) {
  691. // Set a default texture repeat and applies it to all layers too.
  692. CanvasItem::set_texture_repeat(p_texture_repeat);
  693. for (TileMapLayer *layer : layers) {
  694. layer->set_texture_repeat(p_texture_repeat);
  695. }
  696. }
  697. TypedArray<Vector2i> TileMap::get_surrounding_cells(const Vector2i &p_coords) {
  698. if (tile_set.is_null()) {
  699. return TypedArray<Vector2i>();
  700. }
  701. return tile_set->get_surrounding_cells(p_coords);
  702. }
  703. PackedStringArray TileMap::get_configuration_warnings() const {
  704. PackedStringArray warnings = Node2D::get_configuration_warnings();
  705. 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\"."));
  706. // Retrieve the set of Z index values with a Y-sorted layer.
  707. RBSet<int> y_sorted_z_index;
  708. for (const TileMapLayer *layer : layers) {
  709. if (layer->is_y_sort_enabled()) {
  710. y_sorted_z_index.insert(layer->get_z_index());
  711. }
  712. }
  713. // Check if we have a non-sorted layer in a Z-index with a Y-sorted layer.
  714. for (const TileMapLayer *layer : layers) {
  715. if (!layer->is_y_sort_enabled() && y_sorted_z_index.has(layer->get_z_index())) {
  716. 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."));
  717. break;
  718. }
  719. }
  720. if (!is_y_sort_enabled()) {
  721. // Check if Y-sort is enabled on a layer but not on the node.
  722. for (const TileMapLayer *layer : layers) {
  723. if (layer->is_y_sort_enabled()) {
  724. warnings.push_back(RTR("A TileMap layer is set as Y-sorted, but Y-sort is not enabled on the TileMap node itself."));
  725. break;
  726. }
  727. }
  728. } else {
  729. // Check if Y-sort is enabled on the node, but not on any of the layers.
  730. bool need_warning = true;
  731. for (const TileMapLayer *layer : layers) {
  732. if (layer->is_y_sort_enabled()) {
  733. need_warning = false;
  734. break;
  735. }
  736. }
  737. if (need_warning) {
  738. 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."));
  739. }
  740. }
  741. // Check if we are in isometric mode without Y-sort enabled.
  742. if (tile_set.is_valid() && tile_set->get_tile_shape() == TileSet::TILE_SHAPE_ISOMETRIC) {
  743. bool warn = !is_y_sort_enabled();
  744. if (!warn) {
  745. for (const TileMapLayer *layer : layers) {
  746. if (!layer->is_y_sort_enabled()) {
  747. warn = true;
  748. break;
  749. }
  750. }
  751. }
  752. if (warn) {
  753. warnings.push_back(RTR("Isometric TileSet will likely not look as intended without Y-sort enabled for the TileMap and all of its layers."));
  754. }
  755. }
  756. return warnings;
  757. }
  758. void TileMap::_bind_methods() {
  759. #ifndef DISABLE_DEPRECATED
  760. #ifndef NAVIGATION_2D_DISABLED
  761. ClassDB::bind_method(D_METHOD("set_navigation_map", "layer", "map"), &TileMap::set_layer_navigation_map);
  762. ClassDB::bind_method(D_METHOD("get_navigation_map", "layer"), &TileMap::get_layer_navigation_map);
  763. #endif // NAVIGATION_2D_DISABLED
  764. ClassDB::bind_method(D_METHOD("force_update", "layer"), &TileMap::force_update, DEFVAL(-1));
  765. #endif // DISABLE_DEPRECATED
  766. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  767. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  768. ClassDB::bind_method(D_METHOD("set_rendering_quadrant_size", "size"), &TileMap::set_rendering_quadrant_size);
  769. ClassDB::bind_method(D_METHOD("get_rendering_quadrant_size"), &TileMap::get_rendering_quadrant_size);
  770. ClassDB::bind_method(D_METHOD("get_layers_count"), &TileMap::get_layers_count);
  771. ClassDB::bind_method(D_METHOD("add_layer", "to_position"), &TileMap::add_layer);
  772. ClassDB::bind_method(D_METHOD("move_layer", "layer", "to_position"), &TileMap::move_layer);
  773. ClassDB::bind_method(D_METHOD("remove_layer", "layer"), &TileMap::remove_layer);
  774. ClassDB::bind_method(D_METHOD("set_layer_name", "layer", "name"), &TileMap::set_layer_name);
  775. ClassDB::bind_method(D_METHOD("get_layer_name", "layer"), &TileMap::get_layer_name);
  776. ClassDB::bind_method(D_METHOD("set_layer_enabled", "layer", "enabled"), &TileMap::set_layer_enabled);
  777. ClassDB::bind_method(D_METHOD("is_layer_enabled", "layer"), &TileMap::is_layer_enabled);
  778. ClassDB::bind_method(D_METHOD("set_layer_modulate", "layer", "modulate"), &TileMap::set_layer_modulate);
  779. ClassDB::bind_method(D_METHOD("get_layer_modulate", "layer"), &TileMap::get_layer_modulate);
  780. ClassDB::bind_method(D_METHOD("set_layer_y_sort_enabled", "layer", "y_sort_enabled"), &TileMap::set_layer_y_sort_enabled);
  781. ClassDB::bind_method(D_METHOD("is_layer_y_sort_enabled", "layer"), &TileMap::is_layer_y_sort_enabled);
  782. ClassDB::bind_method(D_METHOD("set_layer_y_sort_origin", "layer", "y_sort_origin"), &TileMap::set_layer_y_sort_origin);
  783. ClassDB::bind_method(D_METHOD("get_layer_y_sort_origin", "layer"), &TileMap::get_layer_y_sort_origin);
  784. ClassDB::bind_method(D_METHOD("set_layer_z_index", "layer", "z_index"), &TileMap::set_layer_z_index);
  785. ClassDB::bind_method(D_METHOD("get_layer_z_index", "layer"), &TileMap::get_layer_z_index);
  786. #ifndef NAVIGATION_2D_DISABLED
  787. ClassDB::bind_method(D_METHOD("set_layer_navigation_enabled", "layer", "enabled"), &TileMap::set_layer_navigation_enabled);
  788. ClassDB::bind_method(D_METHOD("is_layer_navigation_enabled", "layer"), &TileMap::is_layer_navigation_enabled);
  789. ClassDB::bind_method(D_METHOD("set_layer_navigation_map", "layer", "map"), &TileMap::set_layer_navigation_map);
  790. ClassDB::bind_method(D_METHOD("get_layer_navigation_map", "layer"), &TileMap::get_layer_navigation_map);
  791. #endif // NAVIGATION_2D_DISABLED
  792. ClassDB::bind_method(D_METHOD("set_collision_animatable", "enabled"), &TileMap::set_collision_animatable);
  793. ClassDB::bind_method(D_METHOD("is_collision_animatable"), &TileMap::is_collision_animatable);
  794. ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "collision_visibility_mode"), &TileMap::set_collision_visibility_mode);
  795. ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMap::get_collision_visibility_mode);
  796. #ifndef NAVIGATION_2D_DISABLED
  797. ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "navigation_visibility_mode"), &TileMap::set_navigation_visibility_mode);
  798. ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode);
  799. #endif // NAVIGATION_2D_DISABLED
  800. 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));
  801. ClassDB::bind_method(D_METHOD("erase_cell", "layer", "coords"), &TileMap::erase_cell);
  802. ClassDB::bind_method(D_METHOD("get_cell_source_id", "layer", "coords", "use_proxies"), &TileMap::get_cell_source_id, DEFVAL(false));
  803. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "layer", "coords", "use_proxies"), &TileMap::get_cell_atlas_coords, DEFVAL(false));
  804. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "layer", "coords", "use_proxies"), &TileMap::get_cell_alternative_tile, DEFVAL(false));
  805. ClassDB::bind_method(D_METHOD("get_cell_tile_data", "layer", "coords", "use_proxies"), &TileMap::get_cell_tile_data, DEFVAL(false));
  806. ClassDB::bind_method(D_METHOD("is_cell_flipped_h", "layer", "coords", "use_proxies"), &TileMap::is_cell_flipped_h, DEFVAL(false));
  807. ClassDB::bind_method(D_METHOD("is_cell_flipped_v", "layer", "coords", "use_proxies"), &TileMap::is_cell_flipped_v, DEFVAL(false));
  808. ClassDB::bind_method(D_METHOD("is_cell_transposed", "layer", "coords", "use_proxies"), &TileMap::is_cell_transposed, DEFVAL(false));
  809. #ifndef PHYSICS_2D_DISABLED
  810. ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMap::get_coords_for_body_rid);
  811. ClassDB::bind_method(D_METHOD("get_layer_for_body_rid", "body"), &TileMap::get_layer_for_body_rid);
  812. #endif // PHYSICS_2D_DISABLED
  813. ClassDB::bind_method(D_METHOD("get_pattern", "layer", "coords_array"), &TileMap::get_pattern);
  814. ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMap::map_pattern);
  815. ClassDB::bind_method(D_METHOD("set_pattern", "layer", "position", "pattern"), &TileMap::set_pattern);
  816. ClassDB::bind_method(D_METHOD("set_cells_terrain_connect", "layer", "cells", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_connect, DEFVAL(true));
  817. ClassDB::bind_method(D_METHOD("set_cells_terrain_path", "layer", "path", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_path, DEFVAL(true));
  818. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
  819. ClassDB::bind_method(D_METHOD("clear_layer", "layer"), &TileMap::clear_layer);
  820. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  821. ClassDB::bind_method(D_METHOD("update_internals"), &TileMap::update_internals);
  822. ClassDB::bind_method(D_METHOD("notify_runtime_tile_data_update", "layer"), &TileMap::notify_runtime_tile_data_update, DEFVAL(-1));
  823. ClassDB::bind_method(D_METHOD("get_surrounding_cells", "coords"), &TileMap::get_surrounding_cells);
  824. ClassDB::bind_method(D_METHOD("get_used_cells", "layer"), &TileMap::get_used_cells);
  825. 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));
  826. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  827. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &TileMap::map_to_local);
  828. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &TileMap::local_to_map);
  829. ClassDB::bind_method(D_METHOD("get_neighbor_cell", "coords", "neighbor"), &TileMap::get_neighbor_cell);
  830. GDVIRTUAL_BIND(_use_tile_data_runtime_update, "layer", "coords");
  831. GDVIRTUAL_BIND(_tile_data_runtime_update, "layer", "coords", "tile_data");
  832. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  833. ADD_PROPERTY(PropertyInfo(Variant::INT, "rendering_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_rendering_quadrant_size", "get_rendering_quadrant_size");
  834. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_animatable"), "set_collision_animatable", "is_collision_animatable");
  835. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
  836. #ifndef NAVIGATION_2D_DISABLED
  837. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
  838. #endif // NAVIGATION_2D_DISABLED
  839. ADD_ARRAY("layers", "layer_");
  840. ADD_PROPERTY_DEFAULT("format", TileMapDataFormat::TILE_MAP_DATA_FORMAT_1);
  841. ADD_SIGNAL(MethodInfo(CoreStringName(changed)));
  842. BIND_ENUM_CONSTANT(VISIBILITY_MODE_DEFAULT);
  843. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_HIDE);
  844. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_SHOW);
  845. }
  846. TileMap::TileMap() {
  847. TileMapLayer *new_layer = memnew(TileMapLayer);
  848. add_child(new_layer, false, INTERNAL_MODE_FRONT);
  849. new_layer->set_as_tile_map_internal_node(0);
  850. new_layer->set_name("Layer0");
  851. new_layer->set_tile_set(tile_set);
  852. new_layer->connect(CoreStringName(changed), callable_mp(this, &TileMap::_emit_changed));
  853. layers.push_back(new_layer);
  854. if (!base_property_helper.is_initialized()) {
  855. // Initialize static PropertyListHelper if it wasn't yet. This has to be done here,
  856. // because creating TileMapLayer in a static context is not always safe.
  857. TileMapLayer *defaults = memnew(TileMapLayer);
  858. base_property_helper.set_prefix("layer_");
  859. base_property_helper.set_array_length_getter(&TileMap::get_layers_count);
  860. base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults->get_name(), &TileMap::set_layer_name, &TileMap::get_layer_name);
  861. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "enabled"), defaults->is_enabled(), &TileMap::set_layer_enabled, &TileMap::is_layer_enabled);
  862. base_property_helper.register_property(PropertyInfo(Variant::COLOR, "modulate"), defaults->get_modulate(), &TileMap::set_layer_modulate, &TileMap::get_layer_modulate);
  863. 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);
  864. 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);
  865. base_property_helper.register_property(PropertyInfo(Variant::INT, "z_index"), defaults->get_z_index(), &TileMap::set_layer_z_index, &TileMap::get_layer_z_index);
  866. #ifndef NAVIGATION_2D_DISABLED
  867. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "navigation_enabled"), defaults->is_navigation_enabled(), &TileMap::set_layer_navigation_enabled, &TileMap::is_layer_navigation_enabled);
  868. #endif // NAVIGATION_2D_DISABLED
  869. 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);
  870. PropertyListHelper::register_base_helper(&base_property_helper);
  871. memdelete(defaults);
  872. }
  873. property_helper.setup_for_instance(base_property_helper, this);
  874. }
  875. #ifndef NAVIGATION_2D_DISABLED
  876. void TileMap::navmesh_parse_init() {
  877. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  878. if (!_navmesh_source_geometry_parser.is_valid()) {
  879. _navmesh_source_geometry_parsing_callback = callable_mp_static(&TileMap::navmesh_parse_source_geometry);
  880. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  881. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  882. }
  883. }
  884. void TileMap::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  885. TileMap *nb_tilemap = Object::cast_to<TileMap>(p_node);
  886. if (nb_tilemap == nullptr) {
  887. return;
  888. }
  889. // Special case for TileMap, so that internal layer get parsed even if p_recurse_children is false.
  890. bool recurse_children = p_navigation_mesh->get_source_geometry_mode() != NavigationPolygon::SOURCE_GEOMETRY_GROUPS_EXPLICIT;
  891. if (!recurse_children) {
  892. for (int i = 0; i < p_node->get_child_count(); i++) {
  893. TileMapLayer *tile_map_layer = Object::cast_to<TileMapLayer>(p_node->get_child(i));
  894. if (tile_map_layer && tile_map_layer->get_index_in_tile_map() >= 0) {
  895. tile_map_layer->navmesh_parse_source_geometry(p_navigation_mesh, p_source_geometry_data, tile_map_layer);
  896. }
  897. }
  898. }
  899. }
  900. #endif // NAVIGATION_2D_DISABLED
  901. #undef TILEMAP_CALL_FOR_LAYER
  902. #undef TILEMAP_CALL_FOR_LAYER_V