-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
tile_map.cpp
4734 lines (4164 loc) · 188 KB
/
tile_map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* tile_map.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "tile_map.h"
#include "tile_map.compat.inc"
#include "core/core_string_names.h"
#include "core/io/marshalls.h"
#include "scene/resources/world_2d.h"
#include "servers/navigation_server_2d.h"
#ifdef DEBUG_ENABLED
#include "servers/navigation_server_3d.h"
#endif // DEBUG_ENABLED
#ifdef DEBUG_ENABLED
/////////////////////////////// Debug //////////////////////////////////////////
constexpr int TILE_MAP_DEBUG_QUADRANT_SIZE = 16;
Vector2i TileMapLayer::_coords_to_debug_quadrant_coords(const Vector2i &p_coords) const {
return Vector2i(
p_coords.x > 0 ? p_coords.x / TILE_MAP_DEBUG_QUADRANT_SIZE : (p_coords.x - (TILE_MAP_DEBUG_QUADRANT_SIZE - 1)) / TILE_MAP_DEBUG_QUADRANT_SIZE,
p_coords.y > 0 ? p_coords.y / TILE_MAP_DEBUG_QUADRANT_SIZE : (p_coords.y - (TILE_MAP_DEBUG_QUADRANT_SIZE - 1)) / TILE_MAP_DEBUG_QUADRANT_SIZE);
}
void TileMapLayer::_debug_update() {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
RenderingServer *rs = RenderingServer::get_singleton();
// Check if we should cleanup everything.
bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
if (forced_cleanup) {
for (KeyValue<Vector2i, Ref<DebugQuadrant>> &kv : debug_quadrant_map) {
// Free the quadrant.
Ref<DebugQuadrant> &debug_quadrant = kv.value;
if (debug_quadrant->canvas_item.is_valid()) {
rs->free(debug_quadrant->canvas_item);
}
}
debug_quadrant_map.clear();
_debug_was_cleaned_up = true;
return;
}
// Check if anything is dirty, in such a case, redraw debug.
bool anything_changed = false;
for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
if (dirty.flags[i]) {
anything_changed = true;
break;
}
}
// List all debug quadrants to update, creating new ones if needed.
SelfList<DebugQuadrant>::List dirty_debug_quadrant_list;
if (_debug_was_cleaned_up || anything_changed) {
// Update all cells.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
CellData &cell_data = kv.value;
_debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
}
} else {
// Update dirty cells.
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
_debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
}
}
// Update those quadrants.
for (SelfList<DebugQuadrant> *quadrant_list_element = dirty_debug_quadrant_list.first(); quadrant_list_element;) {
SelfList<DebugQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
DebugQuadrant &debug_quadrant = *quadrant_list_element->self();
// Check if the quadrant has a tile.
bool has_a_tile = false;
RID &ci = debug_quadrant.canvas_item;
for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
has_a_tile = true;
break;
}
}
if (has_a_tile) {
// Update the quadrant.
if (ci.is_valid()) {
rs->canvas_item_clear(ci);
} else {
ci = rs->canvas_item_create();
rs->canvas_item_set_z_index(ci, RS::CANVAS_ITEM_Z_MAX - 1);
rs->canvas_item_set_parent(ci, tile_map_node->get_canvas_item());
}
const Vector2 quadrant_pos = tile_map_node->map_to_local(debug_quadrant.quadrant_coords * TILE_MAP_DEBUG_QUADRANT_SIZE);
Transform2D xform(0, quadrant_pos);
rs->canvas_item_set_transform(ci, xform);
for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
_rendering_draw_cell_debug(ci, quadrant_pos, cell_data);
_physics_draw_cell_debug(ci, quadrant_pos, cell_data);
_navigation_draw_cell_debug(ci, quadrant_pos, cell_data);
_scenes_draw_cell_debug(ci, quadrant_pos, cell_data);
}
}
} else {
// Free the quadrant.
if (ci.is_valid()) {
rs->free(ci);
}
quadrant_list_element->remove_from_list();
debug_quadrant_map.erase(debug_quadrant.quadrant_coords);
}
quadrant_list_element = next_quadrant_list_element;
}
dirty_debug_quadrant_list.clear();
_debug_was_cleaned_up = false;
}
void TileMapLayer::_debug_quadrants_update_cell(CellData &r_cell_data, SelfList<DebugQuadrant>::List &r_dirty_debug_quadrant_list) {
Vector2i quadrant_coords = _coords_to_debug_quadrant_coords(r_cell_data.coords);
if (!debug_quadrant_map.has(quadrant_coords)) {
// Create a new quadrant and add it to the quadrant map.
Ref<DebugQuadrant> new_quadrant;
new_quadrant.instantiate();
new_quadrant->quadrant_coords = quadrant_coords;
debug_quadrant_map[quadrant_coords] = new_quadrant;
}
// Add the cell to its quadrant, if it is not already in there.
Ref<DebugQuadrant> &debug_quadrant = debug_quadrant_map[quadrant_coords];
if (!r_cell_data.debug_quadrant_list_element.in_list()) {
debug_quadrant->cells.add(&r_cell_data.debug_quadrant_list_element);
}
// Mark the quadrant as dirty.
if (!debug_quadrant->dirty_quadrant_list_element.in_list()) {
r_dirty_debug_quadrant_list.add(&debug_quadrant->dirty_quadrant_list_element);
}
}
#endif // DEBUG_ENABLED
/////////////////////////////// Rendering //////////////////////////////////////
void TileMapLayer::_rendering_update() {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
RenderingServer *rs = RenderingServer::get_singleton();
// Check if we should cleanup everything.
bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
// ----------- Layer level processing -----------
if (forced_cleanup) {
// Cleanup.
if (canvas_item.is_valid()) {
rs->free(canvas_item);
canvas_item = RID();
}
} else {
// Create/Update the layer's CanvasItem.
if (!canvas_item.is_valid()) {
RID ci = rs->canvas_item_create();
rs->canvas_item_set_parent(ci, tile_map_node->get_canvas_item());
canvas_item = ci;
}
RID &ci = canvas_item;
rs->canvas_item_set_draw_index(ci, layer_index_in_tile_map_node - (int64_t)0x80000000);
rs->canvas_item_set_sort_children_by_y(ci, y_sort_enabled);
rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
rs->canvas_item_set_z_index(ci, z_index);
rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
// Modulate the layer.
Color layer_modulate = modulate;
int selected_layer = tile_map_node->get_selected_layer();
if (selected_layer >= 0 && layer_index_in_tile_map_node != selected_layer) {
int z_selected = tile_map_node->get_layer_z_index(selected_layer);
if (z_index < z_selected || (z_index == z_selected && layer_index_in_tile_map_node < selected_layer)) {
layer_modulate = layer_modulate.darkened(0.5);
} else if (z_index > z_selected || (z_index == z_selected && layer_index_in_tile_map_node > selected_layer)) {
layer_modulate = layer_modulate.darkened(0.5);
layer_modulate.a *= 0.3;
}
}
rs->canvas_item_set_modulate(ci, layer_modulate);
}
// ----------- Quadrants processing -----------
// List all rendering quadrants to update, creating new ones if needed.
SelfList<RenderingQuadrant>::List dirty_rendering_quadrant_list;
// Check if anything changed that might change the quadrant shape.
// If so, recreate everything.
bool quandrant_shape_changed = dirty.flags[DIRTY_FLAGS_TILE_MAP_QUADRANT_SIZE] ||
(tile_map_node->is_y_sort_enabled() && y_sort_enabled && (dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] || dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] || dirty.flags[DIRTY_FLAGS_TILE_MAP_Y_SORT_ENABLED] || dirty.flags[DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM] || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]));
// Free all quadrants.
if (forced_cleanup || quandrant_shape_changed) {
for (const KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
for (int i = 0; i < kv.value->canvas_items.size(); i++) {
const RID &ci = kv.value->canvas_items[i];
if (ci.is_valid()) {
rs->free(ci);
}
}
kv.value->cells.clear();
}
rendering_quadrant_map.clear();
_rendering_was_cleaned_up = true;
}
if (!forced_cleanup) {
// List all quadrants to update, recreating them if needed.
if (dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET] || _rendering_was_cleaned_up) {
// Update all cells.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
CellData &cell_data = kv.value;
_rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
}
} else {
// Update dirty cells.
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
_rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
}
}
// Update all dirty quadrants.
for (SelfList<RenderingQuadrant> *quadrant_list_element = dirty_rendering_quadrant_list.first(); quadrant_list_element;) {
SelfList<RenderingQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
const Ref<RenderingQuadrant> &rendering_quadrant = quadrant_list_element->self();
// Check if the quadrant has a tile.
bool has_a_tile = false;
for (SelfList<CellData> *cell_data_list_element = rendering_quadrant->cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
has_a_tile = true;
break;
}
}
if (has_a_tile) {
// Process the quadrant.
// First, clear the quadrant's canvas items.
for (RID &ci : rendering_quadrant->canvas_items) {
rs->free(ci);
}
rendering_quadrant->canvas_items.clear();
// Sort the quadrant cells.
if (tile_map_node->is_y_sort_enabled() && is_y_sort_enabled()) {
// For compatibility reasons, we use another comparator for Y-sorted layers.
rendering_quadrant->cells.sort_custom<CellDataYSortedComparator>();
} else {
rendering_quadrant->cells.sort();
}
// Those allow to group cell per material or z-index.
Ref<Material> prev_material;
int prev_z_index = 0;
RID prev_ci;
for (SelfList<CellData> *cell_data_quadrant_list_element = rendering_quadrant->cells.first(); cell_data_quadrant_list_element; cell_data_quadrant_list_element = cell_data_quadrant_list_element->next()) {
CellData &cell_data = *cell_data_quadrant_list_element->self();
TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(cell_data.cell.source_id));
// Get the tile data.
const TileData *tile_data;
if (cell_data.runtime_tile_data_cache) {
tile_data = cell_data.runtime_tile_data_cache;
} else {
tile_data = atlas_source->get_tile_data(cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile);
}
Ref<Material> mat = tile_data->get_material();
int tile_z_index = tile_data->get_z_index();
// Quandrant pos.
// --- CanvasItems ---
RID ci;
// Check if the material or the z_index changed.
if (prev_ci == RID() || prev_material != mat || prev_z_index != tile_z_index) {
// If so, create a new CanvasItem.
ci = rs->canvas_item_create();
if (mat.is_valid()) {
rs->canvas_item_set_material(ci, mat->get_rid());
}
rs->canvas_item_set_parent(ci, canvas_item);
rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
Transform2D xform(0, rendering_quadrant->canvas_items_position);
rs->canvas_item_set_transform(ci, xform);
rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
rs->canvas_item_set_z_as_relative_to_parent(ci, true);
rs->canvas_item_set_z_index(ci, tile_z_index);
rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
rendering_quadrant->canvas_items.push_back(ci);
prev_ci = ci;
prev_material = mat;
prev_z_index = tile_z_index;
} else {
// Keep the same canvas_item to draw on.
ci = prev_ci;
}
const Vector2 local_tile_pos = tile_map_node->map_to_local(cell_data.coords);
// Random animation offset.
real_t random_animation_offset = 0.0;
if (atlas_source->get_tile_animation_mode(cell_data.cell.get_atlas_coords()) != TileSetAtlasSource::TILE_ANIMATION_MODE_DEFAULT) {
Array to_hash;
to_hash.push_back(local_tile_pos);
to_hash.push_back(get_instance_id()); // Use instance id as a random hash
random_animation_offset = RandomPCG(to_hash.hash()).randf();
}
// Drawing the tile in the canvas item.
tile_map_node->draw_tile(ci, local_tile_pos - rendering_quadrant->canvas_items_position, tile_set, cell_data.cell.source_id, cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile, -1, tile_map_node->get_self_modulate(), tile_data, random_animation_offset);
}
} else {
// Free the quadrant.
for (int i = 0; i < rendering_quadrant->canvas_items.size(); i++) {
const RID &ci = rendering_quadrant->canvas_items[i];
if (ci.is_valid()) {
rs->free(ci);
}
}
rendering_quadrant->cells.clear();
rendering_quadrant_map.erase(rendering_quadrant->quadrant_coords);
}
quadrant_list_element = next_quadrant_list_element;
}
dirty_rendering_quadrant_list.clear();
// Reset the drawing indices.
{
int index = -(int64_t)0x80000000; // Always must be drawn below children.
// Sort the quadrants coords per local coordinates.
RBMap<Vector2, Ref<RenderingQuadrant>, RenderingQuadrant::CoordsWorldComparator> local_to_map;
for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
local_to_map[tile_map_node->map_to_local(rendering_quadrant->quadrant_coords)] = rendering_quadrant;
}
// Sort the quadrants.
for (const KeyValue<Vector2, Ref<RenderingQuadrant>> &E : local_to_map) {
for (const RID &ci : E.value->canvas_items) {
RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
}
}
}
// Updates on TileMap changes.
if (dirty.flags[DIRTY_FLAGS_TILE_MAP_LIGHT_MASK] ||
dirty.flags[DIRTY_FLAGS_TILE_MAP_USE_PARENT_MATERIAL] ||
dirty.flags[DIRTY_FLAGS_TILE_MAP_MATERIAL] ||
dirty.flags[DIRTY_FLAGS_TILE_MAP_TEXTURE_FILTER] ||
dirty.flags[DIRTY_FLAGS_TILE_MAP_TEXTURE_REPEAT]) {
for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
for (const RID &ci : rendering_quadrant->canvas_items) {
rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
}
}
}
}
// ----------- Occluders processing -----------
if (forced_cleanup) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
_rendering_occluders_clear_cell(kv.value);
}
} else {
if (_rendering_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
// Update all cells.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
_rendering_occluders_update_cell(kv.value);
}
} else {
// Update dirty cells.
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
_rendering_occluders_update_cell(cell_data);
}
}
// Updates on TileMap changes.
if (dirty.flags[DIRTY_FLAGS_TILE_MAP_IN_CANVAS] || dirty.flags[DIRTY_FLAGS_TILE_MAP_VISIBILITY]) {
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
CellData &cell_data = kv.value;
for (const RID &occluder : cell_data.occluders) {
Transform2D xform(0, tile_map_node->map_to_local(kv.key));
rs->canvas_light_occluder_attach_to_canvas(occluder, tile_map_node->get_canvas());
rs->canvas_light_occluder_set_transform(occluder, tile_map_node->get_global_transform() * xform);
}
}
}
}
// -----------
// Mark the rendering state as up to date.
_rendering_was_cleaned_up = forced_cleanup;
}
void TileMapLayer::_rendering_quadrants_update_cell(CellData &r_cell_data, SelfList<RenderingQuadrant>::List &r_dirty_rendering_quadrant_list) {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
// Check if the cell is valid and retrieve its y_sort_origin.
bool is_valid = false;
int tile_y_sort_origin = 0;
TileSetSource *source;
if (tile_set->has_source(r_cell_data.cell.source_id)) {
source = *tile_set->get_source(r_cell_data.cell.source_id);
TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
if (atlas_source && atlas_source->has_tile(r_cell_data.cell.get_atlas_coords()) && atlas_source->has_alternative_tile(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile)) {
is_valid = true;
tile_y_sort_origin = atlas_source->get_tile_data(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile)->get_y_sort_origin();
}
}
if (is_valid) {
// Get the quadrant coords.
Vector2 canvas_items_position;
Vector2i quadrant_coords;
if (tile_map_node->is_y_sort_enabled() && is_y_sort_enabled()) {
canvas_items_position = Vector2(0, tile_map_node->map_to_local(r_cell_data.coords).y + tile_y_sort_origin + y_sort_origin);
quadrant_coords = canvas_items_position * 100;
} else {
int quad_size = tile_map_node->get_rendering_quadrant_size();
const Vector2i &coords = r_cell_data.coords;
// Rounding down, instead of simply rounding towards zero (truncating).
quadrant_coords = Vector2i(
coords.x > 0 ? coords.x / quad_size : (coords.x - (quad_size - 1)) / quad_size,
coords.y > 0 ? coords.y / quad_size : (coords.y - (quad_size - 1)) / quad_size);
canvas_items_position = quad_size * quadrant_coords;
}
Ref<RenderingQuadrant> rendering_quadrant;
if (rendering_quadrant_map.has(quadrant_coords)) {
// Reuse existing rendering quadrant.
rendering_quadrant = rendering_quadrant_map[quadrant_coords];
} else {
// Create a new rendering quadrant.
rendering_quadrant.instantiate();
rendering_quadrant->quadrant_coords = quadrant_coords;
rendering_quadrant->canvas_items_position = canvas_items_position;
rendering_quadrant_map[quadrant_coords] = rendering_quadrant;
}
// Mark the old quadrant as dirty (if it exists).
if (r_cell_data.rendering_quadrant.is_valid()) {
if (!r_cell_data.rendering_quadrant->dirty_quadrant_list_element.in_list()) {
r_dirty_rendering_quadrant_list.add(&r_cell_data.rendering_quadrant->dirty_quadrant_list_element);
}
}
// Remove the cell from that quadrant.
if (r_cell_data.rendering_quadrant_list_element.in_list()) {
r_cell_data.rendering_quadrant_list_element.remove_from_list();
}
// Add the cell to its new quadrant.
r_cell_data.rendering_quadrant = rendering_quadrant;
r_cell_data.rendering_quadrant->cells.add(&r_cell_data.rendering_quadrant_list_element);
// Add the new quadrant to the dirty quadrant list.
if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
}
} else {
Ref<RenderingQuadrant> rendering_quadrant = r_cell_data.rendering_quadrant;
// Remove the cell from its quadrant.
r_cell_data.rendering_quadrant = Ref<RenderingQuadrant>();
if (r_cell_data.rendering_quadrant_list_element.in_list()) {
rendering_quadrant->cells.remove(&r_cell_data.rendering_quadrant_list_element);
}
if (rendering_quadrant.is_valid()) {
// Add the quadrant to the dirty quadrant list.
if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
}
}
}
}
void TileMapLayer::_rendering_occluders_clear_cell(CellData &r_cell_data) {
RenderingServer *rs = RenderingServer::get_singleton();
// Free the occluders.
for (const RID &rid : r_cell_data.occluders) {
rs->free(rid);
}
r_cell_data.occluders.clear();
}
void TileMapLayer::_rendering_occluders_update_cell(CellData &r_cell_data) {
bool node_visible = tile_map_node->is_visible_in_tree();
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
RenderingServer *rs = RenderingServer::get_singleton();
TileSetSource *source;
if (tile_set->has_source(r_cell_data.cell.source_id)) {
source = *tile_set->get_source(r_cell_data.cell.source_id);
if (source->has_tile(r_cell_data.cell.get_atlas_coords()) && source->has_alternative_tile(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile)) {
TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
if (atlas_source) {
// Get the tile data.
const TileData *tile_data;
if (r_cell_data.runtime_tile_data_cache) {
tile_data = r_cell_data.runtime_tile_data_cache;
} else {
tile_data = atlas_source->get_tile_data(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile);
}
// Update/create occluders.
for (int i = 0; i < tile_set->get_occlusion_layers_count(); i++) {
Transform2D xform;
xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
if (tile_data->get_occluder(i).is_valid()) {
RID occluder_id = rs->canvas_light_occluder_create();
rs->canvas_light_occluder_set_enabled(occluder_id, node_visible);
rs->canvas_light_occluder_set_transform(occluder_id, tile_map_node->get_global_transform() * xform);
rs->canvas_light_occluder_set_polygon(occluder_id, tile_map_node->get_transformed_polygon(Ref<Resource>(tile_data->get_occluder(i)), r_cell_data.cell.alternative_tile)->get_rid());
rs->canvas_light_occluder_attach_to_canvas(occluder_id, tile_map_node->get_canvas());
rs->canvas_light_occluder_set_light_mask(occluder_id, tile_set->get_occlusion_layer_light_mask(i));
r_cell_data.occluders.push_back(occluder_id);
}
}
return;
}
}
}
// If we did not return earlier, clear the cell.
_rendering_occluders_clear_cell(r_cell_data);
}
#ifdef DEBUG_ENABLED
void TileMapLayer::_rendering_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
ERR_FAIL_COND(!tile_set.is_valid());
if (!Engine::get_singleton()->is_editor_hint()) {
return;
}
// Draw a placeholder for tiles needing one.
RenderingServer *rs = RenderingServer::get_singleton();
const TileMapCell &c = r_cell_data.cell;
TileSetSource *source;
if (tile_set->has_source(c.source_id)) {
source = *tile_set->get_source(c.source_id);
if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
if (atlas_source) {
Vector2i grid_size = atlas_source->get_atlas_grid_size();
if (!atlas_source->get_runtime_texture().is_valid() || c.get_atlas_coords().x >= grid_size.x || c.get_atlas_coords().y >= grid_size.y) {
// Generate a random color from the hashed values of the tiles.
Array to_hash;
to_hash.push_back(c.source_id);
to_hash.push_back(c.get_atlas_coords());
to_hash.push_back(c.alternative_tile);
uint32_t hash = RandomPCG(to_hash.hash()).rand();
Color color;
color = color.from_hsv(
(float)((hash >> 24) & 0xFF) / 256.0,
Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
0.8);
// Draw a placeholder tile.
Transform2D cell_to_quadrant;
cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
rs->canvas_item_add_circle(p_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
}
}
}
}
}
#endif // DEBUG_ENABLED
/////////////////////////////// Physics //////////////////////////////////////
void TileMapLayer::_physics_update() {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
// Check if we should cleanup everything.
bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
if (forced_cleanup) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
_physics_clear_cell(kv.value);
}
} else {
if (_physics_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET] || dirty.flags[DIRTY_FLAGS_TILE_MAP_COLLISION_ANIMATABLE]) {
// Update all cells.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
_physics_update_cell(kv.value);
}
} else {
// Update dirty cells.
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
_physics_update_cell(cell_data);
}
}
}
// -----------
// Mark the physics state as up to date.
_physics_was_cleaned_up = forced_cleanup;
}
void TileMapLayer::_physics_notify_tilemap_change(TileMapLayer::DirtyFlags p_what) {
Transform2D gl_transform = tile_map_node->get_global_transform();
bool in_editor = false;
#ifdef TOOLS_ENABLED
in_editor = Engine::get_singleton()->is_editor_hint();
#endif
if (p_what == DIRTY_FLAGS_TILE_MAP_XFORM) {
if (tile_map_node->is_inside_tree() && (!tile_map_node->is_collision_animatable() || in_editor)) {
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
const CellData &cell_data = kv.value;
for (RID body : cell_data.bodies) {
if (body.is_valid()) {
Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
xform = gl_transform * xform;
PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
}
}
}
}
} else if (p_what == DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM) {
if (tile_map_node->is_inside_tree() && tile_map_node->is_collision_animatable() && !in_editor) {
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
const CellData &cell_data = kv.value;
for (RID body : cell_data.bodies) {
if (body.is_valid()) {
Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
xform = gl_transform * xform;
PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
}
}
}
}
}
}
void TileMapLayer::_physics_clear_cell(CellData &r_cell_data) {
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
// Clear bodies.
for (RID body : r_cell_data.bodies) {
if (body.is_valid()) {
bodies_coords.erase(body);
ps->free(body);
}
}
r_cell_data.bodies.clear();
}
void TileMapLayer::_physics_update_cell(CellData &r_cell_data) {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
Transform2D gl_transform = tile_map_node->get_global_transform();
RID space = tile_map_node->get_world_2d()->get_space();
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
// Recreate bodies and shapes.
TileMapCell &c = r_cell_data.cell;
TileSetSource *source;
if (tile_set->has_source(c.source_id)) {
source = *tile_set->get_source(c.source_id);
if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
if (atlas_source) {
const TileData *tile_data;
if (r_cell_data.runtime_tile_data_cache) {
tile_data = r_cell_data.runtime_tile_data_cache;
} else {
tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
}
// Free unused bodies then resize the bodies array.
for (unsigned int i = tile_set->get_physics_layers_count(); i < r_cell_data.bodies.size(); i++) {
RID body = r_cell_data.bodies[i];
if (body.is_valid()) {
bodies_coords.erase(body);
ps->free(body);
}
}
r_cell_data.bodies.resize(tile_set->get_physics_layers_count());
for (int tile_set_physics_layer = 0; tile_set_physics_layer < tile_set->get_physics_layers_count(); tile_set_physics_layer++) {
Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
RID body = r_cell_data.bodies[tile_set_physics_layer];
if (tile_data->get_collision_polygons_count(tile_set_physics_layer) == 0) {
// No body needed, free it if it exists.
if (body.is_valid()) {
bodies_coords.erase(body);
ps->free(body);
}
body = RID();
} else {
// Create or update the body.
if (!body.is_valid()) {
body = ps->body_create();
}
bodies_coords[body] = r_cell_data.coords;
ps->body_set_mode(body, tile_map_node->is_collision_animatable() ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
ps->body_set_space(body, space);
Transform2D xform;
xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
xform = gl_transform * xform;
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
ps->body_attach_object_instance_id(body, tile_map_node->get_instance_id());
ps->body_set_collision_layer(body, physics_layer);
ps->body_set_collision_mask(body, physics_mask);
ps->body_set_pickable(body, false);
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, tile_data->get_constant_linear_velocity(tile_set_physics_layer));
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, tile_data->get_constant_angular_velocity(tile_set_physics_layer));
if (!physics_material.is_valid()) {
ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, 1);
} else {
ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, physics_material->computed_friction());
}
// Clear body's shape if needed.
ps->body_clear_shapes(body);
// Add the shapes to the body.
int body_shape_index = 0;
for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(tile_set_physics_layer); polygon_index++) {
// Iterate over the polygons.
bool one_way_collision = tile_data->is_collision_polygon_one_way(tile_set_physics_layer, polygon_index);
float one_way_collision_margin = tile_data->get_collision_polygon_one_way_margin(tile_set_physics_layer, polygon_index);
int shapes_count = tile_data->get_collision_polygon_shapes_count(tile_set_physics_layer, polygon_index);
for (int shape_index = 0; shape_index < shapes_count; shape_index++) {
// Add decomposed convex shapes.
Ref<ConvexPolygonShape2D> shape = tile_data->get_collision_polygon_shape(tile_set_physics_layer, polygon_index, shape_index);
shape = tile_map_node->get_transformed_polygon(Ref<Resource>(shape), c.alternative_tile);
ps->body_add_shape(body, shape->get_rid());
ps->body_set_shape_as_one_way_collision(body, body_shape_index, one_way_collision, one_way_collision_margin);
body_shape_index++;
}
}
}
// Set the body again.
r_cell_data.bodies[tile_set_physics_layer] = body;
}
return;
}
}
}
// If we did not return earlier, clear the cell.
_physics_clear_cell(r_cell_data);
}
#ifdef DEBUG_ENABLED
void TileMapLayer::_physics_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
// Draw the debug collision shapes.
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
ERR_FAIL_COND(!tile_set.is_valid());
if (!tile_map_node->get_tree()) {
return;
}
bool show_collision = false;
switch (tile_map_node->get_collision_visibility_mode()) {
case TileMap::VISIBILITY_MODE_DEFAULT:
show_collision = !Engine::get_singleton()->is_editor_hint() && tile_map_node->get_tree()->is_debugging_collisions_hint();
break;
case TileMap::VISIBILITY_MODE_FORCE_HIDE:
show_collision = false;
break;
case TileMap::VISIBILITY_MODE_FORCE_SHOW:
show_collision = true;
break;
}
if (!show_collision) {
return;
}
RenderingServer *rs = RenderingServer::get_singleton();
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
Color debug_collision_color = tile_map_node->get_tree()->get_debug_collisions_color();
Vector<Color> color;
color.push_back(debug_collision_color);
Transform2D quadrant_to_local(0, p_quadrant_pos);
Transform2D global_to_quadrant = (tile_map_node->get_global_transform() * quadrant_to_local).affine_inverse();
for (RID body : r_cell_data.bodies) {
if (body.is_valid()) {
Transform2D body_to_quadrant = global_to_quadrant * Transform2D(ps->body_get_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM));
rs->canvas_item_add_set_transform(p_canvas_item, body_to_quadrant);
for (int shape_index = 0; shape_index < ps->body_get_shape_count(body); shape_index++) {
const RID &shape = ps->body_get_shape(body, shape_index);
const PhysicsServer2D::ShapeType &type = ps->shape_get_type(shape);
if (type == PhysicsServer2D::SHAPE_CONVEX_POLYGON) {
rs->canvas_item_add_polygon(p_canvas_item, ps->shape_get_data(shape), color);
} else {
WARN_PRINT("Wrong shape type for a tile, should be SHAPE_CONVEX_POLYGON.");
}
}
rs->canvas_item_add_set_transform(p_canvas_item, Transform2D());
}
}
};
#endif // DEBUG_ENABLED
/////////////////////////////// Navigation //////////////////////////////////////
void TileMapLayer::_navigation_update() {
ERR_FAIL_NULL(NavigationServer2D::get_singleton());
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
NavigationServer2D *ns = NavigationServer2D::get_singleton();
// Check if we should cleanup everything.
bool forced_cleanup = in_destructor || !enabled || !navigation_enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
// ----------- Layer level processing -----------
if (forced_cleanup) {
if (navigation_map.is_valid() && !uses_world_navigation_map) {
ns->free(navigation_map);
navigation_map = RID();
}
} else {
// Update navigation maps.
if (!navigation_map.is_valid()) {
if (layer_index_in_tile_map_node == 0) {
// Use the default World2D navigation map for the first layer when empty.
navigation_map = tile_map_node->get_world_2d()->get_navigation_map();
uses_world_navigation_map = true;
} else {
RID new_layer_map = ns->map_create();
// Set the default NavigationPolygon cell_size on the new map as a mismatch causes an error.
ns->map_set_cell_size(new_layer_map, 1.0);
ns->map_set_active(new_layer_map, true);
navigation_map = new_layer_map;
uses_world_navigation_map = false;
}
}
}
// ----------- Navigation regions processing -----------
if (forced_cleanup) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
_navigation_clear_cell(kv.value);
}
} else {
if (_navigation_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
// Update all cells.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
_navigation_update_cell(kv.value);
}
} else {
// Update dirty cells.
for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
CellData &cell_data = *cell_data_list_element->self();
_navigation_update_cell(cell_data);
}
}
if (dirty.flags[DIRTY_FLAGS_TILE_MAP_XFORM]) {
Transform2D tilemap_xform = tile_map_node->get_global_transform();
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
const CellData &cell_data = kv.value;
// Update navigation regions transform.
for (const RID ®ion : cell_data.navigation_regions) {
if (!region.is_valid()) {
continue;
}
Transform2D tile_transform;
tile_transform.set_origin(tile_map_node->map_to_local(kv.key));
NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
}
}
}
}
// -----------
// Mark the navigation state as up to date.
_navigation_was_cleaned_up = forced_cleanup;
}
void TileMapLayer::_navigation_clear_cell(CellData &r_cell_data) {
NavigationServer2D *ns = NavigationServer2D::get_singleton();
// Clear navigation shapes.
for (unsigned int i = 0; i < r_cell_data.navigation_regions.size(); i++) {
const RID ®ion = r_cell_data.navigation_regions[i];
if (region.is_valid()) {
ns->region_set_map(region, RID());
ns->free(region);
}
}
r_cell_data.navigation_regions.clear();
}
void TileMapLayer::_navigation_update_cell(CellData &r_cell_data) {
const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
NavigationServer2D *ns = NavigationServer2D::get_singleton();
Transform2D tilemap_xform = tile_map_node->get_global_transform();
// Get the navigation polygons and create regions.
TileMapCell &c = r_cell_data.cell;
TileSetSource *source;
if (tile_set->has_source(c.source_id)) {
source = *tile_set->get_source(c.source_id);
if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
if (atlas_source) {