Debian Codegraph @

scene/2d/tile_map.cpp

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