tile_map.cpp 42 KB

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