tile_map.cpp 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890
  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/core_string_names.h"
  33. #include "core/io/marshalls.h"
  34. #include "scene/resources/world_2d.h"
  35. #include "servers/navigation_server_2d.h"
  36. #ifdef DEBUG_ENABLED
  37. #include "servers/navigation_server_3d.h"
  38. #endif // DEBUG_ENABLED
  39. #ifdef DEBUG_ENABLED
  40. /////////////////////////////// Debug //////////////////////////////////////////
  41. constexpr int TILE_MAP_DEBUG_QUADRANT_SIZE = 16;
  42. Vector2i TileMapLayer::_coords_to_debug_quadrant_coords(const Vector2i &p_coords) const {
  43. return Vector2i(
  44. 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,
  45. 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);
  46. }
  47. void TileMapLayer::_debug_update() {
  48. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  49. RenderingServer *rs = RenderingServer::get_singleton();
  50. // Check if we should cleanup everything.
  51. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
  52. if (forced_cleanup) {
  53. for (KeyValue<Vector2i, Ref<DebugQuadrant>> &kv : debug_quadrant_map) {
  54. // Free the quadrant.
  55. Ref<DebugQuadrant> &debug_quadrant = kv.value;
  56. if (debug_quadrant->canvas_item.is_valid()) {
  57. rs->free(debug_quadrant->canvas_item);
  58. }
  59. }
  60. debug_quadrant_map.clear();
  61. _debug_was_cleaned_up = true;
  62. return;
  63. }
  64. // Check if anything is dirty, in such a case, redraw debug.
  65. bool anything_changed = false;
  66. for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
  67. if (dirty.flags[i]) {
  68. anything_changed = true;
  69. break;
  70. }
  71. }
  72. // List all debug quadrants to update, creating new ones if needed.
  73. SelfList<DebugQuadrant>::List dirty_debug_quadrant_list;
  74. if (_debug_was_cleaned_up || anything_changed) {
  75. // Update all cells.
  76. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  77. CellData &cell_data = kv.value;
  78. _debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
  79. }
  80. } else {
  81. // Update dirty cells.
  82. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  83. CellData &cell_data = *cell_data_list_element->self();
  84. _debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
  85. }
  86. }
  87. // Update those quadrants.
  88. for (SelfList<DebugQuadrant> *quadrant_list_element = dirty_debug_quadrant_list.first(); quadrant_list_element;) {
  89. SelfList<DebugQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
  90. DebugQuadrant &debug_quadrant = *quadrant_list_element->self();
  91. // Check if the quadrant has a tile.
  92. bool has_a_tile = false;
  93. RID &ci = debug_quadrant.canvas_item;
  94. for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  95. CellData &cell_data = *cell_data_list_element->self();
  96. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  97. has_a_tile = true;
  98. break;
  99. }
  100. }
  101. if (has_a_tile) {
  102. // Update the quadrant.
  103. if (ci.is_valid()) {
  104. rs->canvas_item_clear(ci);
  105. } else {
  106. ci = rs->canvas_item_create();
  107. rs->canvas_item_set_z_index(ci, RS::CANVAS_ITEM_Z_MAX - 1);
  108. rs->canvas_item_set_parent(ci, tile_map_node->get_canvas_item());
  109. }
  110. const Vector2 quadrant_pos = tile_map_node->map_to_local(debug_quadrant.quadrant_coords * TILE_MAP_DEBUG_QUADRANT_SIZE);
  111. Transform2D xform(0, quadrant_pos);
  112. rs->canvas_item_set_transform(ci, xform);
  113. for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  114. CellData &cell_data = *cell_data_list_element->self();
  115. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  116. _rendering_draw_cell_debug(ci, quadrant_pos, cell_data);
  117. _physics_draw_cell_debug(ci, quadrant_pos, cell_data);
  118. _navigation_draw_cell_debug(ci, quadrant_pos, cell_data);
  119. _scenes_draw_cell_debug(ci, quadrant_pos, cell_data);
  120. }
  121. }
  122. } else {
  123. // Free the quadrant.
  124. if (ci.is_valid()) {
  125. rs->free(ci);
  126. }
  127. quadrant_list_element->remove_from_list();
  128. debug_quadrant_map.erase(debug_quadrant.quadrant_coords);
  129. }
  130. quadrant_list_element = next_quadrant_list_element;
  131. }
  132. dirty_debug_quadrant_list.clear();
  133. _debug_was_cleaned_up = false;
  134. }
  135. void TileMapLayer::_debug_quadrants_update_cell(CellData &r_cell_data, SelfList<DebugQuadrant>::List &r_dirty_debug_quadrant_list) {
  136. Vector2i quadrant_coords = _coords_to_debug_quadrant_coords(r_cell_data.coords);
  137. if (!debug_quadrant_map.has(quadrant_coords)) {
  138. // Create a new quadrant and add it to the quadrant map.
  139. Ref<DebugQuadrant> new_quadrant;
  140. new_quadrant.instantiate();
  141. new_quadrant->quadrant_coords = quadrant_coords;
  142. debug_quadrant_map[quadrant_coords] = new_quadrant;
  143. }
  144. // Add the cell to its quadrant, if it is not already in there.
  145. Ref<DebugQuadrant> &debug_quadrant = debug_quadrant_map[quadrant_coords];
  146. if (!r_cell_data.debug_quadrant_list_element.in_list()) {
  147. debug_quadrant->cells.add(&r_cell_data.debug_quadrant_list_element);
  148. }
  149. // Mark the quadrant as dirty.
  150. if (!debug_quadrant->dirty_quadrant_list_element.in_list()) {
  151. r_dirty_debug_quadrant_list.add(&debug_quadrant->dirty_quadrant_list_element);
  152. }
  153. }
  154. #endif // DEBUG_ENABLED
  155. /////////////////////////////// Rendering //////////////////////////////////////
  156. void TileMapLayer::_rendering_update() {
  157. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  158. RenderingServer *rs = RenderingServer::get_singleton();
  159. // Check if we should cleanup everything.
  160. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
  161. // ----------- Layer level processing -----------
  162. if (forced_cleanup) {
  163. // Cleanup.
  164. if (canvas_item.is_valid()) {
  165. rs->free(canvas_item);
  166. canvas_item = RID();
  167. }
  168. } else {
  169. // Create/Update the layer's CanvasItem.
  170. if (!canvas_item.is_valid()) {
  171. RID ci = rs->canvas_item_create();
  172. rs->canvas_item_set_parent(ci, tile_map_node->get_canvas_item());
  173. canvas_item = ci;
  174. }
  175. RID &ci = canvas_item;
  176. rs->canvas_item_set_draw_index(ci, layer_index_in_tile_map_node - (int64_t)0x80000000);
  177. rs->canvas_item_set_sort_children_by_y(ci, y_sort_enabled);
  178. rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
  179. rs->canvas_item_set_z_index(ci, z_index);
  180. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
  181. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
  182. rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
  183. // Modulate the layer.
  184. Color layer_modulate = modulate;
  185. int selected_layer = tile_map_node->get_selected_layer();
  186. if (selected_layer >= 0 && layer_index_in_tile_map_node != selected_layer) {
  187. int z_selected = tile_map_node->get_layer_z_index(selected_layer);
  188. if (z_index < z_selected || (z_index == z_selected && layer_index_in_tile_map_node < selected_layer)) {
  189. layer_modulate = layer_modulate.darkened(0.5);
  190. } else if (z_index > z_selected || (z_index == z_selected && layer_index_in_tile_map_node > selected_layer)) {
  191. layer_modulate = layer_modulate.darkened(0.5);
  192. layer_modulate.a *= 0.3;
  193. }
  194. }
  195. rs->canvas_item_set_modulate(ci, layer_modulate);
  196. }
  197. // ----------- Quadrants processing -----------
  198. // List all rendering quadrants to update, creating new ones if needed.
  199. SelfList<RenderingQuadrant>::List dirty_rendering_quadrant_list;
  200. // Check if anything changed that might change the quadrant shape.
  201. // If so, recreate everything.
  202. bool quandrant_shape_changed = dirty.flags[DIRTY_FLAGS_TILE_MAP_QUADRANT_SIZE] ||
  203. (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]));
  204. // Free all quadrants.
  205. if (forced_cleanup || quandrant_shape_changed) {
  206. for (const KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  207. for (int i = 0; i < kv.value->canvas_items.size(); i++) {
  208. const RID &ci = kv.value->canvas_items[i];
  209. if (ci.is_valid()) {
  210. rs->free(ci);
  211. }
  212. }
  213. kv.value->cells.clear();
  214. }
  215. rendering_quadrant_map.clear();
  216. _rendering_was_cleaned_up = true;
  217. }
  218. if (!forced_cleanup) {
  219. // List all quadrants to update, recreating them if needed.
  220. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET] || _rendering_was_cleaned_up) {
  221. // Update all cells.
  222. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  223. CellData &cell_data = kv.value;
  224. _rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
  225. }
  226. } else {
  227. // Update dirty cells.
  228. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  229. CellData &cell_data = *cell_data_list_element->self();
  230. _rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
  231. }
  232. }
  233. // Update all dirty quadrants.
  234. for (SelfList<RenderingQuadrant> *quadrant_list_element = dirty_rendering_quadrant_list.first(); quadrant_list_element;) {
  235. SelfList<RenderingQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
  236. const Ref<RenderingQuadrant> &rendering_quadrant = quadrant_list_element->self();
  237. // Check if the quadrant has a tile.
  238. bool has_a_tile = false;
  239. for (SelfList<CellData> *cell_data_list_element = rendering_quadrant->cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  240. CellData &cell_data = *cell_data_list_element->self();
  241. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  242. has_a_tile = true;
  243. break;
  244. }
  245. }
  246. if (has_a_tile) {
  247. // Process the quadrant.
  248. // First, clear the quadrant's canvas items.
  249. for (RID &ci : rendering_quadrant->canvas_items) {
  250. rs->free(ci);
  251. }
  252. rendering_quadrant->canvas_items.clear();
  253. // Sort the quadrant cells.
  254. if (tile_map_node->is_y_sort_enabled() && is_y_sort_enabled()) {
  255. // For compatibility reasons, we use another comparator for Y-sorted layers.
  256. rendering_quadrant->cells.sort_custom<CellDataYSortedComparator>();
  257. } else {
  258. rendering_quadrant->cells.sort();
  259. }
  260. // Those allow to group cell per material or z-index.
  261. Ref<Material> prev_material;
  262. int prev_z_index = 0;
  263. RID prev_ci;
  264. 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()) {
  265. CellData &cell_data = *cell_data_quadrant_list_element->self();
  266. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(cell_data.cell.source_id));
  267. // Get the tile data.
  268. const TileData *tile_data;
  269. if (cell_data.runtime_tile_data_cache) {
  270. tile_data = cell_data.runtime_tile_data_cache;
  271. } else {
  272. tile_data = atlas_source->get_tile_data(cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile);
  273. }
  274. Ref<Material> mat = tile_data->get_material();
  275. int tile_z_index = tile_data->get_z_index();
  276. // Quandrant pos.
  277. // --- CanvasItems ---
  278. RID ci;
  279. // Check if the material or the z_index changed.
  280. if (prev_ci == RID() || prev_material != mat || prev_z_index != tile_z_index) {
  281. // If so, create a new CanvasItem.
  282. ci = rs->canvas_item_create();
  283. if (mat.is_valid()) {
  284. rs->canvas_item_set_material(ci, mat->get_rid());
  285. }
  286. rs->canvas_item_set_parent(ci, canvas_item);
  287. rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
  288. Transform2D xform(0, rendering_quadrant->canvas_items_position);
  289. rs->canvas_item_set_transform(ci, xform);
  290. rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
  291. rs->canvas_item_set_z_as_relative_to_parent(ci, true);
  292. rs->canvas_item_set_z_index(ci, tile_z_index);
  293. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
  294. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
  295. rendering_quadrant->canvas_items.push_back(ci);
  296. prev_ci = ci;
  297. prev_material = mat;
  298. prev_z_index = tile_z_index;
  299. } else {
  300. // Keep the same canvas_item to draw on.
  301. ci = prev_ci;
  302. }
  303. const Vector2 local_tile_pos = tile_map_node->map_to_local(cell_data.coords);
  304. // Random animation offset.
  305. real_t random_animation_offset = 0.0;
  306. if (atlas_source->get_tile_animation_mode(cell_data.cell.get_atlas_coords()) != TileSetAtlasSource::TILE_ANIMATION_MODE_DEFAULT) {
  307. Array to_hash;
  308. to_hash.push_back(local_tile_pos);
  309. to_hash.push_back(get_instance_id()); // Use instance id as a random hash
  310. random_animation_offset = RandomPCG(to_hash.hash()).randf();
  311. }
  312. // Drawing the tile in the canvas item.
  313. 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);
  314. }
  315. } else {
  316. // Free the quadrant.
  317. for (int i = 0; i < rendering_quadrant->canvas_items.size(); i++) {
  318. const RID &ci = rendering_quadrant->canvas_items[i];
  319. if (ci.is_valid()) {
  320. rs->free(ci);
  321. }
  322. }
  323. rendering_quadrant->cells.clear();
  324. rendering_quadrant_map.erase(rendering_quadrant->quadrant_coords);
  325. }
  326. quadrant_list_element = next_quadrant_list_element;
  327. }
  328. dirty_rendering_quadrant_list.clear();
  329. // Reset the drawing indices.
  330. {
  331. int index = -(int64_t)0x80000000; // Always must be drawn below children.
  332. // Sort the quadrants coords per local coordinates.
  333. RBMap<Vector2, Ref<RenderingQuadrant>, RenderingQuadrant::CoordsWorldComparator> local_to_map;
  334. for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  335. Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
  336. local_to_map[tile_map_node->map_to_local(rendering_quadrant->quadrant_coords)] = rendering_quadrant;
  337. }
  338. // Sort the quadrants.
  339. for (const KeyValue<Vector2, Ref<RenderingQuadrant>> &E : local_to_map) {
  340. for (const RID &ci : E.value->canvas_items) {
  341. RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
  342. }
  343. }
  344. }
  345. // Updates on TileMap changes.
  346. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_LIGHT_MASK] ||
  347. dirty.flags[DIRTY_FLAGS_TILE_MAP_USE_PARENT_MATERIAL] ||
  348. dirty.flags[DIRTY_FLAGS_TILE_MAP_MATERIAL] ||
  349. dirty.flags[DIRTY_FLAGS_TILE_MAP_TEXTURE_FILTER] ||
  350. dirty.flags[DIRTY_FLAGS_TILE_MAP_TEXTURE_REPEAT]) {
  351. for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  352. Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
  353. for (const RID &ci : rendering_quadrant->canvas_items) {
  354. rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
  355. rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
  356. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
  357. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
  358. }
  359. }
  360. }
  361. }
  362. // ----------- Occluders processing -----------
  363. if (forced_cleanup) {
  364. // Clean everything.
  365. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  366. _rendering_occluders_clear_cell(kv.value);
  367. }
  368. } else {
  369. if (_rendering_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  370. // Update all cells.
  371. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  372. _rendering_occluders_update_cell(kv.value);
  373. }
  374. } else {
  375. // Update dirty cells.
  376. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  377. CellData &cell_data = *cell_data_list_element->self();
  378. _rendering_occluders_update_cell(cell_data);
  379. }
  380. }
  381. // Updates on TileMap changes.
  382. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_IN_CANVAS] || dirty.flags[DIRTY_FLAGS_TILE_MAP_VISIBILITY]) {
  383. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  384. CellData &cell_data = kv.value;
  385. for (const RID &occluder : cell_data.occluders) {
  386. if (occluder.is_valid()) {
  387. Transform2D xform(0, tile_map_node->map_to_local(kv.key));
  388. rs->canvas_light_occluder_attach_to_canvas(occluder, tile_map_node->get_canvas());
  389. rs->canvas_light_occluder_set_transform(occluder, tile_map_node->get_global_transform() * xform);
  390. }
  391. }
  392. }
  393. }
  394. }
  395. // -----------
  396. // Mark the rendering state as up to date.
  397. _rendering_was_cleaned_up = forced_cleanup;
  398. }
  399. void TileMapLayer::_rendering_quadrants_update_cell(CellData &r_cell_data, SelfList<RenderingQuadrant>::List &r_dirty_rendering_quadrant_list) {
  400. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  401. // Check if the cell is valid and retrieve its y_sort_origin.
  402. bool is_valid = false;
  403. int tile_y_sort_origin = 0;
  404. TileSetSource *source;
  405. if (tile_set->has_source(r_cell_data.cell.source_id)) {
  406. source = *tile_set->get_source(r_cell_data.cell.source_id);
  407. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  408. 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)) {
  409. is_valid = true;
  410. const TileData *tile_data;
  411. if (r_cell_data.runtime_tile_data_cache) {
  412. tile_data = r_cell_data.runtime_tile_data_cache;
  413. } else {
  414. tile_data = atlas_source->get_tile_data(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile);
  415. }
  416. tile_y_sort_origin = tile_data->get_y_sort_origin();
  417. }
  418. }
  419. if (is_valid) {
  420. // Get the quadrant coords.
  421. Vector2 canvas_items_position;
  422. Vector2i quadrant_coords;
  423. if (tile_map_node->is_y_sort_enabled() && is_y_sort_enabled()) {
  424. canvas_items_position = Vector2(0, tile_map_node->map_to_local(r_cell_data.coords).y + tile_y_sort_origin + y_sort_origin);
  425. quadrant_coords = canvas_items_position * 100;
  426. } else {
  427. int quad_size = tile_map_node->get_rendering_quadrant_size();
  428. const Vector2i &coords = r_cell_data.coords;
  429. // Rounding down, instead of simply rounding towards zero (truncating).
  430. quadrant_coords = Vector2i(
  431. coords.x > 0 ? coords.x / quad_size : (coords.x - (quad_size - 1)) / quad_size,
  432. coords.y > 0 ? coords.y / quad_size : (coords.y - (quad_size - 1)) / quad_size);
  433. canvas_items_position = quad_size * quadrant_coords;
  434. }
  435. Ref<RenderingQuadrant> rendering_quadrant;
  436. if (rendering_quadrant_map.has(quadrant_coords)) {
  437. // Reuse existing rendering quadrant.
  438. rendering_quadrant = rendering_quadrant_map[quadrant_coords];
  439. } else {
  440. // Create a new rendering quadrant.
  441. rendering_quadrant.instantiate();
  442. rendering_quadrant->quadrant_coords = quadrant_coords;
  443. rendering_quadrant->canvas_items_position = canvas_items_position;
  444. rendering_quadrant_map[quadrant_coords] = rendering_quadrant;
  445. }
  446. // Mark the old quadrant as dirty (if it exists).
  447. if (r_cell_data.rendering_quadrant.is_valid()) {
  448. if (!r_cell_data.rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  449. r_dirty_rendering_quadrant_list.add(&r_cell_data.rendering_quadrant->dirty_quadrant_list_element);
  450. }
  451. }
  452. // Remove the cell from that quadrant.
  453. if (r_cell_data.rendering_quadrant_list_element.in_list()) {
  454. r_cell_data.rendering_quadrant_list_element.remove_from_list();
  455. }
  456. // Add the cell to its new quadrant.
  457. r_cell_data.rendering_quadrant = rendering_quadrant;
  458. r_cell_data.rendering_quadrant->cells.add(&r_cell_data.rendering_quadrant_list_element);
  459. // Add the new quadrant to the dirty quadrant list.
  460. if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  461. r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
  462. }
  463. } else {
  464. Ref<RenderingQuadrant> rendering_quadrant = r_cell_data.rendering_quadrant;
  465. // Remove the cell from its quadrant.
  466. r_cell_data.rendering_quadrant = Ref<RenderingQuadrant>();
  467. if (r_cell_data.rendering_quadrant_list_element.in_list()) {
  468. rendering_quadrant->cells.remove(&r_cell_data.rendering_quadrant_list_element);
  469. }
  470. if (rendering_quadrant.is_valid()) {
  471. // Add the quadrant to the dirty quadrant list.
  472. if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  473. r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
  474. }
  475. }
  476. }
  477. }
  478. void TileMapLayer::_rendering_occluders_clear_cell(CellData &r_cell_data) {
  479. RenderingServer *rs = RenderingServer::get_singleton();
  480. // Free the occluders.
  481. for (const RID &rid : r_cell_data.occluders) {
  482. rs->free(rid);
  483. }
  484. r_cell_data.occluders.clear();
  485. }
  486. void TileMapLayer::_rendering_occluders_update_cell(CellData &r_cell_data) {
  487. bool node_visible = tile_map_node->is_visible_in_tree();
  488. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  489. RenderingServer *rs = RenderingServer::get_singleton();
  490. // Free unused occluders then resize the occluders array.
  491. for (uint32_t i = tile_set->get_occlusion_layers_count(); i < r_cell_data.occluders.size(); i++) {
  492. RID occluder_id = r_cell_data.occluders[i];
  493. if (occluder_id.is_valid()) {
  494. rs->free(occluder_id);
  495. }
  496. }
  497. r_cell_data.occluders.resize(tile_set->get_occlusion_layers_count());
  498. TileSetSource *source;
  499. if (tile_set->has_source(r_cell_data.cell.source_id)) {
  500. source = *tile_set->get_source(r_cell_data.cell.source_id);
  501. 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)) {
  502. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  503. if (atlas_source) {
  504. // Get the tile data.
  505. const TileData *tile_data;
  506. if (r_cell_data.runtime_tile_data_cache) {
  507. tile_data = r_cell_data.runtime_tile_data_cache;
  508. } else {
  509. tile_data = atlas_source->get_tile_data(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile);
  510. }
  511. // Create, update or clear occluders.
  512. for (uint32_t occlusion_layer_index = 0; occlusion_layer_index < r_cell_data.occluders.size(); occlusion_layer_index++) {
  513. Ref<OccluderPolygon2D> occluder_polygon = tile_data->get_occluder(occlusion_layer_index);
  514. RID &occluder = r_cell_data.occluders[occlusion_layer_index];
  515. if (occluder_polygon.is_valid()) {
  516. // Create or update occluder.
  517. Transform2D xform;
  518. xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  519. if (!occluder.is_valid()) {
  520. occluder = rs->canvas_light_occluder_create();
  521. }
  522. rs->canvas_light_occluder_set_enabled(occluder, node_visible);
  523. rs->canvas_light_occluder_set_transform(occluder, tile_map_node->get_global_transform() * xform);
  524. rs->canvas_light_occluder_set_polygon(occluder, tile_map_node->get_transformed_polygon(Ref<Resource>(tile_data->get_occluder(occlusion_layer_index)), r_cell_data.cell.alternative_tile)->get_rid());
  525. rs->canvas_light_occluder_attach_to_canvas(occluder, tile_map_node->get_canvas());
  526. rs->canvas_light_occluder_set_light_mask(occluder, tile_set->get_occlusion_layer_light_mask(occlusion_layer_index));
  527. } else {
  528. // Clear occluder.
  529. if (occluder.is_valid()) {
  530. rs->free(occluder);
  531. occluder = RID();
  532. }
  533. }
  534. }
  535. return;
  536. }
  537. }
  538. }
  539. // If we did not return earlier, clear the cell.
  540. _rendering_occluders_clear_cell(r_cell_data);
  541. }
  542. #ifdef DEBUG_ENABLED
  543. void TileMapLayer::_rendering_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  544. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  545. ERR_FAIL_COND(!tile_set.is_valid());
  546. if (!Engine::get_singleton()->is_editor_hint()) {
  547. return;
  548. }
  549. // Draw a placeholder for tiles needing one.
  550. RenderingServer *rs = RenderingServer::get_singleton();
  551. const TileMapCell &c = r_cell_data.cell;
  552. TileSetSource *source;
  553. if (tile_set->has_source(c.source_id)) {
  554. source = *tile_set->get_source(c.source_id);
  555. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  556. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  557. if (atlas_source) {
  558. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  559. if (!atlas_source->get_runtime_texture().is_valid() || c.get_atlas_coords().x >= grid_size.x || c.get_atlas_coords().y >= grid_size.y) {
  560. // Generate a random color from the hashed values of the tiles.
  561. Array to_hash;
  562. to_hash.push_back(c.source_id);
  563. to_hash.push_back(c.get_atlas_coords());
  564. to_hash.push_back(c.alternative_tile);
  565. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  566. Color color;
  567. color = color.from_hsv(
  568. (float)((hash >> 24) & 0xFF) / 256.0,
  569. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  570. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  571. 0.8);
  572. // Draw a placeholder tile.
  573. Transform2D cell_to_quadrant;
  574. cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  575. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  576. 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);
  577. }
  578. }
  579. }
  580. }
  581. }
  582. #endif // DEBUG_ENABLED
  583. /////////////////////////////// Physics //////////////////////////////////////
  584. void TileMapLayer::_physics_update() {
  585. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  586. // Check if we should cleanup everything.
  587. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid();
  588. if (forced_cleanup) {
  589. // Clean everything.
  590. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  591. _physics_clear_cell(kv.value);
  592. }
  593. } else {
  594. if (_physics_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET] || dirty.flags[DIRTY_FLAGS_TILE_MAP_COLLISION_ANIMATABLE]) {
  595. // Update all cells.
  596. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  597. _physics_update_cell(kv.value);
  598. }
  599. } else {
  600. // Update dirty cells.
  601. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  602. CellData &cell_data = *cell_data_list_element->self();
  603. _physics_update_cell(cell_data);
  604. }
  605. }
  606. }
  607. // -----------
  608. // Mark the physics state as up to date.
  609. _physics_was_cleaned_up = forced_cleanup;
  610. }
  611. void TileMapLayer::_physics_notify_tilemap_change(TileMapLayer::DirtyFlags p_what) {
  612. Transform2D gl_transform = tile_map_node->get_global_transform();
  613. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  614. bool in_editor = false;
  615. #ifdef TOOLS_ENABLED
  616. in_editor = Engine::get_singleton()->is_editor_hint();
  617. #endif
  618. if (p_what == DIRTY_FLAGS_TILE_MAP_XFORM) {
  619. if (tile_map_node->is_inside_tree() && (!tile_map_node->is_collision_animatable() || in_editor)) {
  620. // Move the collisison shapes along with the TileMap.
  621. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  622. const CellData &cell_data = kv.value;
  623. for (RID body : cell_data.bodies) {
  624. if (body.is_valid()) {
  625. Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
  626. xform = gl_transform * xform;
  627. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  628. }
  629. }
  630. }
  631. }
  632. } else if (p_what == DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM) {
  633. // With collisions animatable, move the collisison shapes along with the TileMap only on local xform change (they are synchornized on physics tick instead).
  634. if (tile_map_node->is_inside_tree() && tile_map_node->is_collision_animatable() && !in_editor) {
  635. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  636. const CellData &cell_data = kv.value;
  637. for (RID body : cell_data.bodies) {
  638. if (body.is_valid()) {
  639. Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
  640. xform = gl_transform * xform;
  641. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  642. }
  643. }
  644. }
  645. }
  646. } else if (p_what == DIRTY_FLAGS_TILE_MAP_IN_TREE) {
  647. // Changes in the tree may cause the space to change (e.g. when reparenting to a SubViewport).
  648. if (tile_map_node->is_inside_tree()) {
  649. RID space = tile_map_node->get_world_2d()->get_space();
  650. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  651. const CellData &cell_data = kv.value;
  652. for (RID body : cell_data.bodies) {
  653. if (body.is_valid()) {
  654. ps->body_set_space(body, space);
  655. }
  656. }
  657. }
  658. }
  659. }
  660. }
  661. void TileMapLayer::_physics_clear_cell(CellData &r_cell_data) {
  662. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  663. // Clear bodies.
  664. for (RID body : r_cell_data.bodies) {
  665. if (body.is_valid()) {
  666. bodies_coords.erase(body);
  667. ps->free(body);
  668. }
  669. }
  670. r_cell_data.bodies.clear();
  671. }
  672. void TileMapLayer::_physics_update_cell(CellData &r_cell_data) {
  673. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  674. Transform2D gl_transform = tile_map_node->get_global_transform();
  675. RID space = tile_map_node->get_world_2d()->get_space();
  676. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  677. // Recreate bodies and shapes.
  678. TileMapCell &c = r_cell_data.cell;
  679. TileSetSource *source;
  680. if (tile_set->has_source(c.source_id)) {
  681. source = *tile_set->get_source(c.source_id);
  682. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  683. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  684. if (atlas_source) {
  685. const TileData *tile_data;
  686. if (r_cell_data.runtime_tile_data_cache) {
  687. tile_data = r_cell_data.runtime_tile_data_cache;
  688. } else {
  689. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  690. }
  691. // Free unused bodies then resize the bodies array.
  692. for (uint32_t i = tile_set->get_physics_layers_count(); i < r_cell_data.bodies.size(); i++) {
  693. RID body = r_cell_data.bodies[i];
  694. if (body.is_valid()) {
  695. bodies_coords.erase(body);
  696. ps->free(body);
  697. }
  698. }
  699. r_cell_data.bodies.resize(tile_set->get_physics_layers_count());
  700. for (uint32_t tile_set_physics_layer = 0; tile_set_physics_layer < (uint32_t)tile_set->get_physics_layers_count(); tile_set_physics_layer++) {
  701. Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
  702. uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
  703. uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
  704. RID body = r_cell_data.bodies[tile_set_physics_layer];
  705. if (tile_data->get_collision_polygons_count(tile_set_physics_layer) == 0) {
  706. // No body needed, free it if it exists.
  707. if (body.is_valid()) {
  708. bodies_coords.erase(body);
  709. ps->free(body);
  710. }
  711. body = RID();
  712. } else {
  713. // Create or update the body.
  714. if (!body.is_valid()) {
  715. body = ps->body_create();
  716. }
  717. bodies_coords[body] = r_cell_data.coords;
  718. ps->body_set_mode(body, tile_map_node->is_collision_animatable() ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
  719. ps->body_set_space(body, space);
  720. Transform2D xform;
  721. xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  722. xform = gl_transform * xform;
  723. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  724. ps->body_attach_object_instance_id(body, tile_map_node->get_instance_id());
  725. ps->body_set_collision_layer(body, physics_layer);
  726. ps->body_set_collision_mask(body, physics_mask);
  727. ps->body_set_pickable(body, false);
  728. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, tile_data->get_constant_linear_velocity(tile_set_physics_layer));
  729. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, tile_data->get_constant_angular_velocity(tile_set_physics_layer));
  730. if (!physics_material.is_valid()) {
  731. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  732. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  733. } else {
  734. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
  735. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, physics_material->computed_friction());
  736. }
  737. // Clear body's shape if needed.
  738. ps->body_clear_shapes(body);
  739. // Add the shapes to the body.
  740. int body_shape_index = 0;
  741. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(tile_set_physics_layer); polygon_index++) {
  742. // Iterate over the polygons.
  743. bool one_way_collision = tile_data->is_collision_polygon_one_way(tile_set_physics_layer, polygon_index);
  744. float one_way_collision_margin = tile_data->get_collision_polygon_one_way_margin(tile_set_physics_layer, polygon_index);
  745. int shapes_count = tile_data->get_collision_polygon_shapes_count(tile_set_physics_layer, polygon_index);
  746. for (int shape_index = 0; shape_index < shapes_count; shape_index++) {
  747. // Add decomposed convex shapes.
  748. Ref<ConvexPolygonShape2D> shape = tile_data->get_collision_polygon_shape(tile_set_physics_layer, polygon_index, shape_index);
  749. shape = tile_map_node->get_transformed_polygon(Ref<Resource>(shape), c.alternative_tile);
  750. ps->body_add_shape(body, shape->get_rid());
  751. ps->body_set_shape_as_one_way_collision(body, body_shape_index, one_way_collision, one_way_collision_margin);
  752. body_shape_index++;
  753. }
  754. }
  755. }
  756. // Set the body again.
  757. r_cell_data.bodies[tile_set_physics_layer] = body;
  758. }
  759. return;
  760. }
  761. }
  762. }
  763. // If we did not return earlier, clear the cell.
  764. _physics_clear_cell(r_cell_data);
  765. }
  766. #ifdef DEBUG_ENABLED
  767. void TileMapLayer::_physics_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  768. // Draw the debug collision shapes.
  769. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  770. ERR_FAIL_COND(!tile_set.is_valid());
  771. if (!tile_map_node->get_tree()) {
  772. return;
  773. }
  774. bool show_collision = false;
  775. switch (tile_map_node->get_collision_visibility_mode()) {
  776. case TileMap::VISIBILITY_MODE_DEFAULT:
  777. show_collision = !Engine::get_singleton()->is_editor_hint() && tile_map_node->get_tree()->is_debugging_collisions_hint();
  778. break;
  779. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  780. show_collision = false;
  781. break;
  782. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  783. show_collision = true;
  784. break;
  785. }
  786. if (!show_collision) {
  787. return;
  788. }
  789. RenderingServer *rs = RenderingServer::get_singleton();
  790. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  791. Color debug_collision_color = tile_map_node->get_tree()->get_debug_collisions_color();
  792. Vector<Color> color;
  793. color.push_back(debug_collision_color);
  794. Transform2D quadrant_to_local(0, p_quadrant_pos);
  795. Transform2D global_to_quadrant = (tile_map_node->get_global_transform() * quadrant_to_local).affine_inverse();
  796. for (RID body : r_cell_data.bodies) {
  797. if (body.is_valid()) {
  798. Transform2D body_to_quadrant = global_to_quadrant * Transform2D(ps->body_get_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM));
  799. rs->canvas_item_add_set_transform(p_canvas_item, body_to_quadrant);
  800. for (int shape_index = 0; shape_index < ps->body_get_shape_count(body); shape_index++) {
  801. const RID &shape = ps->body_get_shape(body, shape_index);
  802. const PhysicsServer2D::ShapeType &type = ps->shape_get_type(shape);
  803. if (type == PhysicsServer2D::SHAPE_CONVEX_POLYGON) {
  804. rs->canvas_item_add_polygon(p_canvas_item, ps->shape_get_data(shape), color);
  805. } else {
  806. WARN_PRINT("Wrong shape type for a tile, should be SHAPE_CONVEX_POLYGON.");
  807. }
  808. }
  809. rs->canvas_item_add_set_transform(p_canvas_item, Transform2D());
  810. }
  811. }
  812. };
  813. #endif // DEBUG_ENABLED
  814. /////////////////////////////// Navigation //////////////////////////////////////
  815. void TileMapLayer::_navigation_update() {
  816. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  817. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  818. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  819. // Check if we should cleanup everything.
  820. bool forced_cleanup = in_destructor || !enabled || !navigation_enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid();
  821. // ----------- Layer level processing -----------
  822. if (forced_cleanup) {
  823. if (navigation_map.is_valid() && !uses_world_navigation_map) {
  824. ns->free(navigation_map);
  825. navigation_map = RID();
  826. }
  827. } else {
  828. // Update navigation maps.
  829. if (!navigation_map.is_valid()) {
  830. if (layer_index_in_tile_map_node == 0) {
  831. // Use the default World2D navigation map for the first layer when empty.
  832. navigation_map = tile_map_node->get_world_2d()->get_navigation_map();
  833. uses_world_navigation_map = true;
  834. } else {
  835. RID new_layer_map = ns->map_create();
  836. // Set the default NavigationPolygon cell_size on the new map as a mismatch causes an error.
  837. ns->map_set_cell_size(new_layer_map, 1.0);
  838. ns->map_set_active(new_layer_map, true);
  839. navigation_map = new_layer_map;
  840. uses_world_navigation_map = false;
  841. }
  842. }
  843. }
  844. // ----------- Navigation regions processing -----------
  845. if (forced_cleanup) {
  846. // Clean everything.
  847. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  848. _navigation_clear_cell(kv.value);
  849. }
  850. } else {
  851. if (_navigation_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  852. // Update all cells.
  853. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  854. _navigation_update_cell(kv.value);
  855. }
  856. } else {
  857. // Update dirty cells.
  858. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  859. CellData &cell_data = *cell_data_list_element->self();
  860. _navigation_update_cell(cell_data);
  861. }
  862. }
  863. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_XFORM]) {
  864. Transform2D tilemap_xform = tile_map_node->get_global_transform();
  865. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  866. const CellData &cell_data = kv.value;
  867. // Update navigation regions transform.
  868. for (const RID &region : cell_data.navigation_regions) {
  869. if (!region.is_valid()) {
  870. continue;
  871. }
  872. Transform2D tile_transform;
  873. tile_transform.set_origin(tile_map_node->map_to_local(kv.key));
  874. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  875. }
  876. }
  877. }
  878. }
  879. // -----------
  880. // Mark the navigation state as up to date.
  881. _navigation_was_cleaned_up = forced_cleanup;
  882. }
  883. void TileMapLayer::_navigation_clear_cell(CellData &r_cell_data) {
  884. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  885. // Clear navigation shapes.
  886. for (uint32_t i = 0; i < r_cell_data.navigation_regions.size(); i++) {
  887. const RID &region = r_cell_data.navigation_regions[i];
  888. if (region.is_valid()) {
  889. ns->region_set_map(region, RID());
  890. ns->free(region);
  891. }
  892. }
  893. r_cell_data.navigation_regions.clear();
  894. }
  895. void TileMapLayer::_navigation_update_cell(CellData &r_cell_data) {
  896. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  897. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  898. Transform2D tilemap_xform = tile_map_node->get_global_transform();
  899. // Get the navigation polygons and create regions.
  900. TileMapCell &c = r_cell_data.cell;
  901. TileSetSource *source;
  902. if (tile_set->has_source(c.source_id)) {
  903. source = *tile_set->get_source(c.source_id);
  904. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  905. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  906. if (atlas_source) {
  907. const TileData *tile_data;
  908. if (r_cell_data.runtime_tile_data_cache) {
  909. tile_data = r_cell_data.runtime_tile_data_cache;
  910. } else {
  911. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  912. }
  913. // Free unused regions then resize the regions array.
  914. for (uint32_t i = tile_set->get_navigation_layers_count(); i < r_cell_data.navigation_regions.size(); i++) {
  915. RID &region = r_cell_data.navigation_regions[i];
  916. if (region.is_valid()) {
  917. ns->region_set_map(region, RID());
  918. ns->free(region);
  919. region = RID();
  920. }
  921. }
  922. r_cell_data.navigation_regions.resize(tile_set->get_navigation_layers_count());
  923. // Create, update or clear regions.
  924. for (uint32_t navigation_layer_index = 0; navigation_layer_index < r_cell_data.navigation_regions.size(); navigation_layer_index++) {
  925. Ref<NavigationPolygon> navigation_polygon;
  926. navigation_polygon = tile_data->get_navigation_polygon(navigation_layer_index);
  927. navigation_polygon = tile_map_node->get_transformed_polygon(Ref<Resource>(navigation_polygon), c.alternative_tile);
  928. RID &region = r_cell_data.navigation_regions[navigation_layer_index];
  929. if (navigation_polygon.is_valid() && (navigation_polygon->get_polygon_count() > 0 || navigation_polygon->get_outline_count() > 0)) {
  930. // Create or update regions.
  931. Transform2D tile_transform;
  932. tile_transform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  933. if (!region.is_valid()) {
  934. region = ns->region_create();
  935. }
  936. ns->region_set_owner_id(region, tile_map_node->get_instance_id());
  937. ns->region_set_map(region, navigation_map);
  938. ns->region_set_transform(region, tilemap_xform * tile_transform);
  939. ns->region_set_navigation_layers(region, tile_set->get_navigation_layer_layers(navigation_layer_index));
  940. ns->region_set_navigation_polygon(region, navigation_polygon);
  941. } else {
  942. // Clear region.
  943. if (region.is_valid()) {
  944. ns->region_set_map(region, RID());
  945. ns->free(region);
  946. region = RID();
  947. }
  948. }
  949. }
  950. return;
  951. }
  952. }
  953. }
  954. // If we did not return earlier, clear the cell.
  955. _navigation_clear_cell(r_cell_data);
  956. }
  957. #ifdef DEBUG_ENABLED
  958. void TileMapLayer::_navigation_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  959. // Draw the debug collision shapes.
  960. bool show_navigation = false;
  961. switch (tile_map_node->get_navigation_visibility_mode()) {
  962. case TileMap::VISIBILITY_MODE_DEFAULT:
  963. show_navigation = !Engine::get_singleton()->is_editor_hint() && tile_map_node->get_tree()->is_debugging_navigation_hint();
  964. break;
  965. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  966. show_navigation = false;
  967. break;
  968. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  969. show_navigation = true;
  970. break;
  971. }
  972. if (!show_navigation) {
  973. return;
  974. }
  975. // Check if the navigation is used.
  976. if (r_cell_data.navigation_regions.is_empty()) {
  977. return;
  978. }
  979. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  980. RenderingServer *rs = RenderingServer::get_singleton();
  981. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  982. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  983. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  984. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  985. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  986. RandomPCG rand;
  987. const TileMapCell &c = r_cell_data.cell;
  988. TileSetSource *source;
  989. if (tile_set->has_source(c.source_id)) {
  990. source = *tile_set->get_source(c.source_id);
  991. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  992. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  993. if (atlas_source) {
  994. const TileData *tile_data;
  995. if (r_cell_data.runtime_tile_data_cache) {
  996. tile_data = r_cell_data.runtime_tile_data_cache;
  997. } else {
  998. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  999. }
  1000. Transform2D cell_to_quadrant;
  1001. cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  1002. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  1003. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1004. Ref<NavigationPolygon> navigation_polygon = tile_data->get_navigation_polygon(layer_index);
  1005. if (navigation_polygon.is_valid()) {
  1006. navigation_polygon = tile_map_node->get_transformed_polygon(Ref<Resource>(navigation_polygon), c.alternative_tile);
  1007. Vector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices();
  1008. if (navigation_polygon_vertices.size() < 3) {
  1009. continue;
  1010. }
  1011. for (int i = 0; i < navigation_polygon->get_polygon_count(); i++) {
  1012. // An array of vertices for this polygon.
  1013. Vector<int> polygon = navigation_polygon->get_polygon(i);
  1014. Vector<Vector2> debug_polygon_vertices;
  1015. debug_polygon_vertices.resize(polygon.size());
  1016. for (int j = 0; j < polygon.size(); j++) {
  1017. ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
  1018. debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]];
  1019. }
  1020. // Generate the polygon color, slightly randomly modified from the settings one.
  1021. Color random_variation_color = debug_face_color;
  1022. if (enabled_geometry_face_random_color) {
  1023. random_variation_color.set_hsv(
  1024. debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1,
  1025. debug_face_color.get_s(),
  1026. debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  1027. }
  1028. random_variation_color.a = debug_face_color.a;
  1029. Vector<Color> debug_face_colors;
  1030. debug_face_colors.push_back(random_variation_color);
  1031. rs->canvas_item_add_polygon(p_canvas_item, debug_polygon_vertices, debug_face_colors);
  1032. if (enabled_edge_lines) {
  1033. Vector<Color> debug_edge_colors;
  1034. debug_edge_colors.push_back(debug_edge_color);
  1035. debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline.
  1036. rs->canvas_item_add_polyline(p_canvas_item, debug_polygon_vertices, debug_edge_colors);
  1037. }
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. #endif // DEBUG_ENABLED
  1046. /////////////////////////////// Scenes //////////////////////////////////////
  1047. void TileMapLayer::_scenes_update() {
  1048. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1049. // Check if we should cleanup everything.
  1050. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid();
  1051. if (forced_cleanup) {
  1052. // Clean everything.
  1053. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  1054. _scenes_clear_cell(kv.value);
  1055. }
  1056. } else {
  1057. if (_scenes_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  1058. // Update all cells.
  1059. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  1060. _scenes_update_cell(kv.value);
  1061. }
  1062. } else {
  1063. // Update dirty cells.
  1064. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1065. CellData &cell_data = *cell_data_list_element->self();
  1066. _scenes_update_cell(cell_data);
  1067. }
  1068. }
  1069. }
  1070. // -----------
  1071. // Mark the scenes state as up to date.
  1072. _scenes_was_cleaned_up = forced_cleanup;
  1073. }
  1074. void TileMapLayer::_scenes_clear_cell(CellData &r_cell_data) {
  1075. // Cleanup existing scene.
  1076. Node *node = tile_map_node->get_node_or_null(r_cell_data.scene);
  1077. if (node) {
  1078. node->queue_free();
  1079. }
  1080. r_cell_data.scene = "";
  1081. }
  1082. void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
  1083. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1084. // Clear the scene in any case.
  1085. _scenes_clear_cell(r_cell_data);
  1086. // Create the scene.
  1087. const TileMapCell &c = r_cell_data.cell;
  1088. TileSetSource *source;
  1089. if (tile_set->has_source(c.source_id)) {
  1090. source = *tile_set->get_source(c.source_id);
  1091. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1092. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1093. if (scenes_collection_source) {
  1094. Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
  1095. if (packed_scene.is_valid()) {
  1096. Node *scene = packed_scene->instantiate();
  1097. Control *scene_as_control = Object::cast_to<Control>(scene);
  1098. Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
  1099. if (scene_as_control) {
  1100. scene_as_control->set_position(tile_map_node->map_to_local(r_cell_data.coords) + scene_as_control->get_position());
  1101. } else if (scene_as_node2d) {
  1102. Transform2D xform;
  1103. xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  1104. scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
  1105. }
  1106. tile_map_node->add_child(scene);
  1107. r_cell_data.scene = scene->get_name();
  1108. }
  1109. }
  1110. }
  1111. }
  1112. }
  1113. #ifdef DEBUG_ENABLED
  1114. void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  1115. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1116. ERR_FAIL_COND(!tile_set.is_valid());
  1117. if (!Engine::get_singleton()->is_editor_hint()) {
  1118. return;
  1119. }
  1120. // Draw a placeholder for scenes needing one.
  1121. RenderingServer *rs = RenderingServer::get_singleton();
  1122. const TileMapCell &c = r_cell_data.cell;
  1123. TileSetSource *source;
  1124. if (tile_set->has_source(c.source_id)) {
  1125. source = *tile_set->get_source(c.source_id);
  1126. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1127. return;
  1128. }
  1129. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1130. if (scenes_collection_source) {
  1131. if (!scenes_collection_source->get_scene_tile_scene(c.alternative_tile).is_valid() || scenes_collection_source->get_scene_tile_display_placeholder(c.alternative_tile)) {
  1132. // Generate a random color from the hashed values of the tiles.
  1133. Array to_hash;
  1134. to_hash.push_back(c.source_id);
  1135. to_hash.push_back(c.alternative_tile);
  1136. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1137. Color color;
  1138. color = color.from_hsv(
  1139. (float)((hash >> 24) & 0xFF) / 256.0,
  1140. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1141. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1142. 0.8);
  1143. // Draw a placeholder tile.
  1144. Transform2D cell_to_quadrant;
  1145. cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  1146. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  1147. 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);
  1148. }
  1149. }
  1150. }
  1151. }
  1152. #endif // DEBUG_ENABLED
  1153. /////////////////////////////////////////////////////////////////////
  1154. void TileMapLayer::_build_runtime_update_tile_data() {
  1155. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1156. // Check if we should cleanup everything.
  1157. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
  1158. if (!forced_cleanup) {
  1159. if (tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update)) {
  1160. if (_runtime_update_tile_data_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  1161. for (KeyValue<Vector2i, CellData> &E : tile_map) {
  1162. _build_runtime_update_tile_data_for_cell(E.value);
  1163. }
  1164. } else if (dirty.flags[DIRTY_FLAGS_TILE_MAP_RUNTIME_UPDATE]) {
  1165. for (KeyValue<Vector2i, CellData> &E : tile_map) {
  1166. _build_runtime_update_tile_data_for_cell(E.value, true);
  1167. }
  1168. } else {
  1169. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1170. CellData &cell_data = *cell_data_list_element->self();
  1171. _build_runtime_update_tile_data_for_cell(cell_data);
  1172. }
  1173. }
  1174. }
  1175. }
  1176. // -----------
  1177. // Mark the navigation state as up to date.
  1178. _runtime_update_tile_data_was_cleaned_up = forced_cleanup;
  1179. }
  1180. void TileMapLayer::_build_runtime_update_tile_data_for_cell(CellData &r_cell_data, bool p_auto_add_to_dirty_list) {
  1181. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1182. TileMapCell &c = r_cell_data.cell;
  1183. TileSetSource *source;
  1184. if (tile_set->has_source(c.source_id)) {
  1185. source = *tile_set->get_source(c.source_id);
  1186. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1187. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1188. if (atlas_source) {
  1189. bool ret = false;
  1190. if (tile_map_node->GDVIRTUAL_CALL(_use_tile_data_runtime_update, layer_index_in_tile_map_node, r_cell_data.coords, ret) && ret) {
  1191. TileData *tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1192. // Create the runtime TileData.
  1193. TileData *tile_data_runtime_use = tile_data->duplicate();
  1194. tile_data_runtime_use->set_allow_transform(true);
  1195. r_cell_data.runtime_tile_data_cache = tile_data_runtime_use;
  1196. tile_map_node->GDVIRTUAL_CALL(_tile_data_runtime_update, layer_index_in_tile_map_node, r_cell_data.coords, tile_data_runtime_use);
  1197. if (p_auto_add_to_dirty_list) {
  1198. dirty.cell_list.add(&r_cell_data.dirty_list_element);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. void TileMapLayer::_clear_runtime_update_tile_data() {
  1206. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1207. CellData &cell_data = *cell_data_list_element->self();
  1208. // Clear the runtime tile data.
  1209. if (cell_data.runtime_tile_data_cache) {
  1210. memdelete(cell_data.runtime_tile_data_cache);
  1211. cell_data.runtime_tile_data_cache = nullptr;
  1212. }
  1213. }
  1214. }
  1215. TileSet::TerrainsPattern TileMapLayer::_get_best_terrain_pattern_for_constraints(int p_terrain_set, const Vector2i &p_position, const RBSet<TerrainConstraint> &p_constraints, TileSet::TerrainsPattern p_current_pattern) {
  1216. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1217. if (!tile_set.is_valid()) {
  1218. return TileSet::TerrainsPattern();
  1219. }
  1220. // Returns all tiles compatible with the given constraints.
  1221. RBMap<TileSet::TerrainsPattern, int> terrain_pattern_score;
  1222. RBSet<TileSet::TerrainsPattern> pattern_set = tile_set->get_terrains_pattern_set(p_terrain_set);
  1223. ERR_FAIL_COND_V(pattern_set.is_empty(), TileSet::TerrainsPattern());
  1224. for (TileSet::TerrainsPattern &terrain_pattern : pattern_set) {
  1225. int score = 0;
  1226. // Check the center bit constraint.
  1227. TerrainConstraint terrain_constraint = TerrainConstraint(tile_map_node, p_position, terrain_pattern.get_terrain());
  1228. const RBSet<TerrainConstraint>::Element *in_set_constraint_element = p_constraints.find(terrain_constraint);
  1229. if (in_set_constraint_element) {
  1230. if (in_set_constraint_element->get().get_terrain() != terrain_constraint.get_terrain()) {
  1231. score += in_set_constraint_element->get().get_priority();
  1232. }
  1233. } else if (p_current_pattern.get_terrain() != terrain_pattern.get_terrain()) {
  1234. continue; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1235. }
  1236. // Check the surrounding bits
  1237. bool invalid_pattern = false;
  1238. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1239. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1240. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1241. // Check if the bit is compatible with the constraints.
  1242. TerrainConstraint terrain_bit_constraint = TerrainConstraint(tile_map_node, p_position, bit, terrain_pattern.get_terrain_peering_bit(bit));
  1243. in_set_constraint_element = p_constraints.find(terrain_bit_constraint);
  1244. if (in_set_constraint_element) {
  1245. if (in_set_constraint_element->get().get_terrain() != terrain_bit_constraint.get_terrain()) {
  1246. score += in_set_constraint_element->get().get_priority();
  1247. }
  1248. } else if (p_current_pattern.get_terrain_peering_bit(bit) != terrain_pattern.get_terrain_peering_bit(bit)) {
  1249. invalid_pattern = true; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1250. break;
  1251. }
  1252. }
  1253. }
  1254. if (invalid_pattern) {
  1255. continue;
  1256. }
  1257. terrain_pattern_score[terrain_pattern] = score;
  1258. }
  1259. // Compute the minimum score.
  1260. TileSet::TerrainsPattern min_score_pattern = p_current_pattern;
  1261. int min_score = INT32_MAX;
  1262. for (KeyValue<TileSet::TerrainsPattern, int> E : terrain_pattern_score) {
  1263. if (E.value < min_score) {
  1264. min_score_pattern = E.key;
  1265. min_score = E.value;
  1266. }
  1267. }
  1268. return min_score_pattern;
  1269. }
  1270. RBSet<TerrainConstraint> TileMapLayer::_get_terrain_constraints_from_added_pattern(const Vector2i &p_position, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  1271. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1272. if (!tile_set.is_valid()) {
  1273. return RBSet<TerrainConstraint>();
  1274. }
  1275. // Compute the constraints needed from the surrounding tiles.
  1276. RBSet<TerrainConstraint> output;
  1277. output.insert(TerrainConstraint(tile_map_node, p_position, p_terrains_pattern.get_terrain()));
  1278. for (uint32_t i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1279. TileSet::CellNeighbor side = TileSet::CellNeighbor(i);
  1280. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, side)) {
  1281. TerrainConstraint c = TerrainConstraint(tile_map_node, p_position, side, p_terrains_pattern.get_terrain_peering_bit(side));
  1282. output.insert(c);
  1283. }
  1284. }
  1285. return output;
  1286. }
  1287. RBSet<TerrainConstraint> TileMapLayer::_get_terrain_constraints_from_painted_cells_list(const RBSet<Vector2i> &p_painted, int p_terrain_set, bool p_ignore_empty_terrains) const {
  1288. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1289. if (!tile_set.is_valid()) {
  1290. return RBSet<TerrainConstraint>();
  1291. }
  1292. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), RBSet<TerrainConstraint>());
  1293. // Build a set of dummy constraints to get the constrained points.
  1294. RBSet<TerrainConstraint> dummy_constraints;
  1295. for (const Vector2i &E : p_painted) {
  1296. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { // Iterates over neighbor bits.
  1297. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1298. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1299. dummy_constraints.insert(TerrainConstraint(tile_map_node, E, bit, -1));
  1300. }
  1301. }
  1302. }
  1303. // For each constrained point, we get all overlapping tiles, and select the most adequate terrain for it.
  1304. RBSet<TerrainConstraint> constraints;
  1305. for (const TerrainConstraint &E_constraint : dummy_constraints) {
  1306. HashMap<int, int> terrain_count;
  1307. // Count the number of occurrences per terrain.
  1308. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = E_constraint.get_overlapping_coords_and_peering_bits();
  1309. for (const KeyValue<Vector2i, TileSet::CellNeighbor> &E_overlapping : overlapping_terrain_bits) {
  1310. TileData *neighbor_tile_data = nullptr;
  1311. TileMapCell neighbor_cell = get_cell(E_overlapping.key);
  1312. if (neighbor_cell.source_id != TileSet::INVALID_SOURCE) {
  1313. Ref<TileSetSource> source = tile_set->get_source(neighbor_cell.source_id);
  1314. Ref<TileSetAtlasSource> atlas_source = source;
  1315. if (atlas_source.is_valid()) {
  1316. TileData *tile_data = atlas_source->get_tile_data(neighbor_cell.get_atlas_coords(), neighbor_cell.alternative_tile);
  1317. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1318. neighbor_tile_data = tile_data;
  1319. }
  1320. }
  1321. }
  1322. int terrain = neighbor_tile_data ? neighbor_tile_data->get_terrain_peering_bit(TileSet::CellNeighbor(E_overlapping.value)) : -1;
  1323. if (!p_ignore_empty_terrains || terrain >= 0) {
  1324. if (!terrain_count.has(terrain)) {
  1325. terrain_count[terrain] = 0;
  1326. }
  1327. terrain_count[terrain] += 1;
  1328. }
  1329. }
  1330. // Get the terrain with the max number of occurrences.
  1331. int max = 0;
  1332. int max_terrain = -1;
  1333. for (const KeyValue<int, int> &E_terrain_count : terrain_count) {
  1334. if (E_terrain_count.value > max) {
  1335. max = E_terrain_count.value;
  1336. max_terrain = E_terrain_count.key;
  1337. }
  1338. }
  1339. // Set the adequate terrain.
  1340. if (max > 0) {
  1341. TerrainConstraint c = E_constraint;
  1342. c.set_terrain(max_terrain);
  1343. constraints.insert(c);
  1344. }
  1345. }
  1346. // Add the centers as constraints.
  1347. for (Vector2i E_coords : p_painted) {
  1348. TileData *tile_data = nullptr;
  1349. TileMapCell cell = get_cell(E_coords);
  1350. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1351. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1352. Ref<TileSetAtlasSource> atlas_source = source;
  1353. if (atlas_source.is_valid()) {
  1354. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1355. }
  1356. }
  1357. int terrain = (tile_data && tile_data->get_terrain_set() == p_terrain_set) ? tile_data->get_terrain() : -1;
  1358. if (!p_ignore_empty_terrains || terrain >= 0) {
  1359. constraints.insert(TerrainConstraint(tile_map_node, E_coords, terrain));
  1360. }
  1361. }
  1362. return constraints;
  1363. }
  1364. void TileMapLayer::set_tile_map(TileMap *p_tile_map) {
  1365. tile_map_node = p_tile_map;
  1366. }
  1367. void TileMapLayer::set_layer_index_in_tile_map_node(int p_index) {
  1368. if (p_index == layer_index_in_tile_map_node) {
  1369. return;
  1370. }
  1371. layer_index_in_tile_map_node = p_index;
  1372. dirty.flags[DIRTY_FLAGS_LAYER_INDEX_IN_TILE_MAP_NODE] = true;
  1373. tile_map_node->queue_internal_update();
  1374. }
  1375. Rect2 TileMapLayer::get_rect(bool &r_changed) const {
  1376. // Compute the displayed area of the tilemap.
  1377. r_changed = false;
  1378. #ifdef DEBUG_ENABLED
  1379. if (rect_cache_dirty) {
  1380. Rect2 r_total;
  1381. bool first = true;
  1382. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  1383. Rect2 r;
  1384. r.position = tile_map_node->map_to_local(E.key);
  1385. r.size = Size2();
  1386. if (first) {
  1387. r_total = r;
  1388. first = false;
  1389. } else {
  1390. r_total = r_total.merge(r);
  1391. }
  1392. }
  1393. r_changed = rect_cache != r_total;
  1394. rect_cache = r_total;
  1395. rect_cache_dirty = false;
  1396. }
  1397. #endif
  1398. return rect_cache;
  1399. }
  1400. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_constraints(const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints) {
  1401. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1402. if (!tile_set.is_valid()) {
  1403. return HashMap<Vector2i, TileSet::TerrainsPattern>();
  1404. }
  1405. // Copy the constraints set.
  1406. RBSet<TerrainConstraint> constraints = p_constraints;
  1407. // Output map.
  1408. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1409. // Add all positions to a set.
  1410. for (int i = 0; i < p_to_replace.size(); i++) {
  1411. const Vector2i &coords = p_to_replace[i];
  1412. // Select the best pattern for the given constraints.
  1413. TileSet::TerrainsPattern current_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  1414. TileMapCell cell = get_cell(coords);
  1415. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1416. TileSetSource *source = *tile_set->get_source(cell.source_id);
  1417. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1418. if (atlas_source) {
  1419. // Get tile data.
  1420. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1421. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1422. current_pattern = tile_data->get_terrains_pattern();
  1423. }
  1424. }
  1425. }
  1426. TileSet::TerrainsPattern pattern = _get_best_terrain_pattern_for_constraints(p_terrain_set, coords, constraints, current_pattern);
  1427. // Update the constraint set with the new ones.
  1428. RBSet<TerrainConstraint> new_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, pattern);
  1429. for (const TerrainConstraint &E_constraint : new_constraints) {
  1430. if (constraints.has(E_constraint)) {
  1431. constraints.erase(E_constraint);
  1432. }
  1433. TerrainConstraint c = E_constraint;
  1434. c.set_priority(5);
  1435. constraints.insert(c);
  1436. }
  1437. output[coords] = pattern;
  1438. }
  1439. return output;
  1440. }
  1441. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_connect(const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1442. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1443. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1444. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  1445. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1446. // Build list and set of tiles that can be modified (painted and their surroundings).
  1447. Vector<Vector2i> can_modify_list;
  1448. RBSet<Vector2i> can_modify_set;
  1449. RBSet<Vector2i> painted_set;
  1450. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1451. const Vector2i &coords = p_coords_array[i];
  1452. can_modify_list.push_back(coords);
  1453. can_modify_set.insert(coords);
  1454. painted_set.insert(coords);
  1455. }
  1456. for (Vector2i coords : p_coords_array) {
  1457. // Find the adequate neighbor.
  1458. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1459. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1460. if (tile_map_node->is_existing_neighbor(bit)) {
  1461. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1462. if (!can_modify_set.has(neighbor)) {
  1463. can_modify_list.push_back(neighbor);
  1464. can_modify_set.insert(neighbor);
  1465. }
  1466. }
  1467. }
  1468. }
  1469. // Build a set, out of the possibly modified tiles, of the one with a center bit that is set (or will be) to the painted terrain.
  1470. RBSet<Vector2i> cells_with_terrain_center_bit;
  1471. for (Vector2i coords : can_modify_set) {
  1472. bool connect = false;
  1473. if (painted_set.has(coords)) {
  1474. connect = true;
  1475. } else {
  1476. // Get the center bit of the cell.
  1477. TileData *tile_data = nullptr;
  1478. TileMapCell cell = get_cell(coords);
  1479. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1480. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1481. Ref<TileSetAtlasSource> atlas_source = source;
  1482. if (atlas_source.is_valid()) {
  1483. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1484. }
  1485. }
  1486. if (tile_data && tile_data->get_terrain_set() == p_terrain_set && tile_data->get_terrain() == p_terrain) {
  1487. connect = true;
  1488. }
  1489. }
  1490. if (connect) {
  1491. cells_with_terrain_center_bit.insert(coords);
  1492. }
  1493. }
  1494. RBSet<TerrainConstraint> constraints;
  1495. // Add new constraints from the path drawn.
  1496. for (Vector2i coords : p_coords_array) {
  1497. // Constraints on the center bit.
  1498. TerrainConstraint c = TerrainConstraint(tile_map_node, coords, p_terrain);
  1499. c.set_priority(10);
  1500. constraints.insert(c);
  1501. // Constraints on the connecting bits.
  1502. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1503. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1504. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1505. c = TerrainConstraint(tile_map_node, coords, bit, p_terrain);
  1506. c.set_priority(10);
  1507. if ((int(bit) % 2) == 0) {
  1508. // Side peering bits: add the constraint if the center is of the same terrain.
  1509. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1510. if (cells_with_terrain_center_bit.has(neighbor)) {
  1511. constraints.insert(c);
  1512. }
  1513. } else {
  1514. // Corner peering bits: add the constraint if all tiles on the constraint has the same center bit.
  1515. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = c.get_overlapping_coords_and_peering_bits();
  1516. bool valid = true;
  1517. for (KeyValue<Vector2i, TileSet::CellNeighbor> kv : overlapping_terrain_bits) {
  1518. if (!cells_with_terrain_center_bit.has(kv.key)) {
  1519. valid = false;
  1520. break;
  1521. }
  1522. }
  1523. if (valid) {
  1524. constraints.insert(c);
  1525. }
  1526. }
  1527. }
  1528. }
  1529. }
  1530. // Fills in the constraint list from existing tiles.
  1531. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1532. constraints.insert(c);
  1533. }
  1534. // Fill the terrains.
  1535. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1536. return output;
  1537. }
  1538. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_path(const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1539. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1540. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1541. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  1542. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1543. // Make sure the path is correct and build the peering bit list while doing it.
  1544. Vector<TileSet::CellNeighbor> neighbor_list;
  1545. for (int i = 0; i < p_coords_array.size() - 1; i++) {
  1546. // Find the adequate neighbor.
  1547. TileSet::CellNeighbor found_bit = TileSet::CELL_NEIGHBOR_MAX;
  1548. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1549. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1550. if (tile_map_node->is_existing_neighbor(bit)) {
  1551. if (tile_map_node->get_neighbor_cell(p_coords_array[i], bit) == p_coords_array[i + 1]) {
  1552. found_bit = bit;
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. ERR_FAIL_COND_V_MSG(found_bit == TileSet::CELL_NEIGHBOR_MAX, output, vformat("Invalid terrain path, %s is not a neighboring tile of %s", p_coords_array[i + 1], p_coords_array[i]));
  1558. neighbor_list.push_back(found_bit);
  1559. }
  1560. // Build list and set of tiles that can be modified (painted and their surroundings).
  1561. Vector<Vector2i> can_modify_list;
  1562. RBSet<Vector2i> can_modify_set;
  1563. RBSet<Vector2i> painted_set;
  1564. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1565. const Vector2i &coords = p_coords_array[i];
  1566. can_modify_list.push_back(coords);
  1567. can_modify_set.insert(coords);
  1568. painted_set.insert(coords);
  1569. }
  1570. for (Vector2i coords : p_coords_array) {
  1571. // Find the adequate neighbor.
  1572. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1573. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1574. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1575. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1576. if (!can_modify_set.has(neighbor)) {
  1577. can_modify_list.push_back(neighbor);
  1578. can_modify_set.insert(neighbor);
  1579. }
  1580. }
  1581. }
  1582. }
  1583. RBSet<TerrainConstraint> constraints;
  1584. // Add new constraints from the path drawn.
  1585. for (Vector2i coords : p_coords_array) {
  1586. // Constraints on the center bit.
  1587. TerrainConstraint c = TerrainConstraint(tile_map_node, coords, p_terrain);
  1588. c.set_priority(10);
  1589. constraints.insert(c);
  1590. }
  1591. for (int i = 0; i < p_coords_array.size() - 1; i++) {
  1592. // Constraints on the peering bits.
  1593. TerrainConstraint c = TerrainConstraint(tile_map_node, p_coords_array[i], neighbor_list[i], p_terrain);
  1594. c.set_priority(10);
  1595. constraints.insert(c);
  1596. }
  1597. // Fills in the constraint list from existing tiles.
  1598. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1599. constraints.insert(c);
  1600. }
  1601. // Fill the terrains.
  1602. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1603. return output;
  1604. }
  1605. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_pattern(const Vector<Vector2i> &p_coords_array, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern, bool p_ignore_empty_terrains) {
  1606. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1607. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1608. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  1609. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1610. // Build list and set of tiles that can be modified (painted and their surroundings).
  1611. Vector<Vector2i> can_modify_list;
  1612. RBSet<Vector2i> can_modify_set;
  1613. RBSet<Vector2i> painted_set;
  1614. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1615. const Vector2i &coords = p_coords_array[i];
  1616. can_modify_list.push_back(coords);
  1617. can_modify_set.insert(coords);
  1618. painted_set.insert(coords);
  1619. }
  1620. for (Vector2i coords : p_coords_array) {
  1621. // Find the adequate neighbor.
  1622. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1623. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1624. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1625. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1626. if (!can_modify_set.has(neighbor)) {
  1627. can_modify_list.push_back(neighbor);
  1628. can_modify_set.insert(neighbor);
  1629. }
  1630. }
  1631. }
  1632. }
  1633. // Add constraint by the new ones.
  1634. RBSet<TerrainConstraint> constraints;
  1635. // Add new constraints from the path drawn.
  1636. for (Vector2i coords : p_coords_array) {
  1637. // Constraints on the center bit.
  1638. RBSet<TerrainConstraint> added_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, p_terrains_pattern);
  1639. for (TerrainConstraint c : added_constraints) {
  1640. c.set_priority(10);
  1641. constraints.insert(c);
  1642. }
  1643. }
  1644. // Fills in the constraint list from modified tiles border.
  1645. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1646. constraints.insert(c);
  1647. }
  1648. // Fill the terrains.
  1649. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1650. return output;
  1651. }
  1652. TileMapCell TileMapLayer::get_cell(const Vector2i &p_coords, bool p_use_proxies) const {
  1653. if (!tile_map.has(p_coords)) {
  1654. return TileMapCell();
  1655. } else {
  1656. TileMapCell c = tile_map.find(p_coords)->value.cell;
  1657. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1658. if (p_use_proxies && tile_set.is_valid()) {
  1659. Array proxyed = tile_set->map_tile_proxy(c.source_id, c.get_atlas_coords(), c.alternative_tile);
  1660. c.source_id = proxyed[0];
  1661. c.set_atlas_coords(proxyed[1]);
  1662. c.alternative_tile = proxyed[2];
  1663. }
  1664. return c;
  1665. }
  1666. }
  1667. void TileMapLayer::set_tile_data(TileMapLayer::DataFormat p_format, const Vector<int> &p_data) {
  1668. ERR_FAIL_COND(p_format > TileMapLayer::FORMAT_3);
  1669. // Set data for a given tile from raw data.
  1670. int c = p_data.size();
  1671. const int *r = p_data.ptr();
  1672. int offset = (p_format >= TileMapLayer::FORMAT_2) ? 3 : 2;
  1673. ERR_FAIL_COND_MSG(c % offset != 0, vformat("Corrupted tile data. Got size: %s. Expected modulo: %s", offset));
  1674. clear();
  1675. #ifdef DISABLE_DEPRECATED
  1676. ERR_FAIL_COND_MSG(p_format != TileMapLayer::FORMAT_3, vformat("Cannot handle deprecated TileMap data format version %d. This Godot version was compiled with no support for deprecated data.", p_format));
  1677. #endif
  1678. for (int i = 0; i < c; i += offset) {
  1679. const uint8_t *ptr = (const uint8_t *)&r[i];
  1680. uint8_t local[12];
  1681. for (int j = 0; j < ((p_format >= TileMapLayer::FORMAT_2) ? 12 : 8); j++) {
  1682. local[j] = ptr[j];
  1683. }
  1684. #ifdef BIG_ENDIAN_ENABLED
  1685. SWAP(local[0], local[3]);
  1686. SWAP(local[1], local[2]);
  1687. SWAP(local[4], local[7]);
  1688. SWAP(local[5], local[6]);
  1689. //TODO: ask someone to check this...
  1690. if (FORMAT >= FORMAT_2) {
  1691. SWAP(local[8], local[11]);
  1692. SWAP(local[9], local[10]);
  1693. }
  1694. #endif
  1695. // Extracts position in TileMap.
  1696. int16_t x = decode_uint16(&local[0]);
  1697. int16_t y = decode_uint16(&local[2]);
  1698. if (p_format == TileMapLayer::FORMAT_3) {
  1699. uint16_t source_id = decode_uint16(&local[4]);
  1700. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  1701. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  1702. uint16_t alternative_tile = decode_uint16(&local[10]);
  1703. set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  1704. } else {
  1705. #ifndef DISABLE_DEPRECATED
  1706. // Previous decated format.
  1707. uint32_t v = decode_uint32(&local[4]);
  1708. // Extract the transform flags that used to be in the tilemap.
  1709. bool flip_h = v & (1UL << 29);
  1710. bool flip_v = v & (1UL << 30);
  1711. bool transpose = v & (1UL << 31);
  1712. v &= (1UL << 29) - 1;
  1713. // Extract autotile/atlas coords.
  1714. int16_t coord_x = 0;
  1715. int16_t coord_y = 0;
  1716. if (p_format == TileMapLayer::FORMAT_2) {
  1717. coord_x = decode_uint16(&local[8]);
  1718. coord_y = decode_uint16(&local[10]);
  1719. }
  1720. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1721. if (tile_set.is_valid()) {
  1722. Array a = tile_set->compatibility_tilemap_map(v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose);
  1723. if (a.size() == 3) {
  1724. set_cell(Vector2i(x, y), a[0], a[1], a[2]);
  1725. } else {
  1726. 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));
  1727. }
  1728. } else {
  1729. int compatibility_alternative_tile = ((int)flip_h) + ((int)flip_v << 1) + ((int)transpose << 2);
  1730. set_cell(Vector2i(x, y), v, Vector2i(coord_x, coord_y), compatibility_alternative_tile);
  1731. }
  1732. #endif
  1733. }
  1734. }
  1735. }
  1736. Vector<int> TileMapLayer::get_tile_data() const {
  1737. // Export tile data to raw format.
  1738. Vector<int> tile_data;
  1739. tile_data.resize(tile_map.size() * 3);
  1740. int *w = tile_data.ptrw();
  1741. // Save in highest format.
  1742. int idx = 0;
  1743. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  1744. uint8_t *ptr = (uint8_t *)&w[idx];
  1745. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  1746. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  1747. encode_uint16(E.value.cell.source_id, &ptr[4]);
  1748. encode_uint16(E.value.cell.coord_x, &ptr[6]);
  1749. encode_uint16(E.value.cell.coord_y, &ptr[8]);
  1750. encode_uint16(E.value.cell.alternative_tile, &ptr[10]);
  1751. idx += 3;
  1752. }
  1753. return tile_data;
  1754. }
  1755. void TileMapLayer::notify_tile_map_change(DirtyFlags p_what) {
  1756. dirty.flags[p_what] = true;
  1757. tile_map_node->queue_internal_update();
  1758. _physics_notify_tilemap_change(p_what);
  1759. }
  1760. void TileMapLayer::internal_update() {
  1761. // Find TileData that need a runtime modification.
  1762. // This may add cells to the dirty list is a runtime modification has been notified.
  1763. _build_runtime_update_tile_data();
  1764. // Update all subsystems.
  1765. _rendering_update();
  1766. _physics_update();
  1767. _navigation_update();
  1768. _scenes_update();
  1769. #ifdef DEBUG_ENABLED
  1770. _debug_update();
  1771. #endif // DEBUG_ENABLED
  1772. _clear_runtime_update_tile_data();
  1773. // Clear the "what is dirty" flags.
  1774. for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
  1775. dirty.flags[i] = false;
  1776. }
  1777. // List the cells to delete definitely.
  1778. Vector<Vector2i> to_delete;
  1779. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1780. CellData &cell_data = *cell_data_list_element->self();
  1781. // Select the the cell from tile_map if it is invalid.
  1782. if (cell_data.cell.source_id == TileSet::INVALID_SOURCE) {
  1783. to_delete.push_back(cell_data.coords);
  1784. }
  1785. }
  1786. // Remove cells that are empty after the cleanup.
  1787. for (const Vector2i &coords : to_delete) {
  1788. tile_map.erase(coords);
  1789. }
  1790. // Clear the dirty cells list.
  1791. dirty.cell_list.clear();
  1792. }
  1793. void TileMapLayer::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  1794. // Set the current cell tile (using integer position).
  1795. Vector2i pk(p_coords);
  1796. HashMap<Vector2i, CellData>::Iterator E = tile_map.find(pk);
  1797. int source_id = p_source_id;
  1798. Vector2i atlas_coords = p_atlas_coords;
  1799. int alternative_tile = p_alternative_tile;
  1800. if ((source_id == TileSet::INVALID_SOURCE || atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE) &&
  1801. (source_id != TileSet::INVALID_SOURCE || atlas_coords != TileSetSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetSource::INVALID_TILE_ALTERNATIVE)) {
  1802. source_id = TileSet::INVALID_SOURCE;
  1803. atlas_coords = TileSetSource::INVALID_ATLAS_COORDS;
  1804. alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1805. }
  1806. if (!E) {
  1807. if (source_id == TileSet::INVALID_SOURCE) {
  1808. return; // Nothing to do, the tile is already empty.
  1809. }
  1810. // Insert a new cell in the tile map.
  1811. CellData new_cell_data;
  1812. new_cell_data.coords = pk;
  1813. E = tile_map.insert(pk, new_cell_data);
  1814. } else {
  1815. if (E->value.cell.source_id == source_id && E->value.cell.get_atlas_coords() == atlas_coords && E->value.cell.alternative_tile == alternative_tile) {
  1816. return; // Nothing changed.
  1817. }
  1818. }
  1819. TileMapCell &c = E->value.cell;
  1820. c.source_id = source_id;
  1821. c.set_atlas_coords(atlas_coords);
  1822. c.alternative_tile = alternative_tile;
  1823. // Make the given cell dirty.
  1824. if (!E->value.dirty_list_element.in_list()) {
  1825. dirty.cell_list.add(&(E->value.dirty_list_element));
  1826. }
  1827. tile_map_node->queue_internal_update();
  1828. used_rect_cache_dirty = true;
  1829. }
  1830. void TileMapLayer::erase_cell(const Vector2i &p_coords) {
  1831. set_cell(p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1832. }
  1833. int TileMapLayer::get_cell_source_id(const Vector2i &p_coords, bool p_use_proxies) const {
  1834. // Get a cell source id from position.
  1835. HashMap<Vector2i, CellData>::ConstIterator E = tile_map.find(p_coords);
  1836. if (!E) {
  1837. return TileSet::INVALID_SOURCE;
  1838. }
  1839. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1840. if (p_use_proxies && tile_set.is_valid()) {
  1841. Array proxyed = tile_set->map_tile_proxy(E->value.cell.source_id, E->value.cell.get_atlas_coords(), E->value.cell.alternative_tile);
  1842. return proxyed[0];
  1843. }
  1844. return E->value.cell.source_id;
  1845. }
  1846. Vector2i TileMapLayer::get_cell_atlas_coords(const Vector2i &p_coords, bool p_use_proxies) const {
  1847. // Get a cell source id from position.
  1848. HashMap<Vector2i, CellData>::ConstIterator E = tile_map.find(p_coords);
  1849. if (!E) {
  1850. return TileSetSource::INVALID_ATLAS_COORDS;
  1851. }
  1852. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1853. if (p_use_proxies && tile_set.is_valid()) {
  1854. Array proxyed = tile_set->map_tile_proxy(E->value.cell.source_id, E->value.cell.get_atlas_coords(), E->value.cell.alternative_tile);
  1855. return proxyed[1];
  1856. }
  1857. return E->value.cell.get_atlas_coords();
  1858. }
  1859. int TileMapLayer::get_cell_alternative_tile(const Vector2i &p_coords, bool p_use_proxies) const {
  1860. // Get a cell source id from position.
  1861. HashMap<Vector2i, CellData>::ConstIterator E = tile_map.find(p_coords);
  1862. if (!E) {
  1863. return TileSetSource::INVALID_TILE_ALTERNATIVE;
  1864. }
  1865. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1866. if (p_use_proxies && tile_set.is_valid()) {
  1867. Array proxyed = tile_set->map_tile_proxy(E->value.cell.source_id, E->value.cell.get_atlas_coords(), E->value.cell.alternative_tile);
  1868. return proxyed[2];
  1869. }
  1870. return E->value.cell.alternative_tile;
  1871. }
  1872. TileData *TileMapLayer::get_cell_tile_data(const Vector2i &p_coords, bool p_use_proxies) const {
  1873. int source_id = get_cell_source_id(p_coords, p_use_proxies);
  1874. if (source_id == TileSet::INVALID_SOURCE) {
  1875. return nullptr;
  1876. }
  1877. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1878. Ref<TileSetAtlasSource> source = tile_set->get_source(source_id);
  1879. if (source.is_valid()) {
  1880. return source->get_tile_data(get_cell_atlas_coords(p_coords, p_use_proxies), get_cell_alternative_tile(p_coords, p_use_proxies));
  1881. }
  1882. return nullptr;
  1883. }
  1884. void TileMapLayer::clear() {
  1885. // Remove all tiles.
  1886. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  1887. erase_cell(kv.key);
  1888. }
  1889. used_rect_cache_dirty = true;
  1890. }
  1891. Ref<TileMapPattern> TileMapLayer::get_pattern(TypedArray<Vector2i> p_coords_array) {
  1892. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1893. ERR_FAIL_COND_V(!tile_set.is_valid(), nullptr);
  1894. Ref<TileMapPattern> output;
  1895. output.instantiate();
  1896. if (p_coords_array.is_empty()) {
  1897. return output;
  1898. }
  1899. Vector2i min = Vector2i(p_coords_array[0]);
  1900. for (int i = 1; i < p_coords_array.size(); i++) {
  1901. min = min.min(p_coords_array[i]);
  1902. }
  1903. Vector<Vector2i> coords_in_pattern_array;
  1904. coords_in_pattern_array.resize(p_coords_array.size());
  1905. Vector2i ensure_positive_offset;
  1906. for (int i = 0; i < p_coords_array.size(); i++) {
  1907. Vector2i coords = p_coords_array[i];
  1908. Vector2i coords_in_pattern = coords - min;
  1909. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  1910. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  1911. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1912. coords_in_pattern.x -= 1;
  1913. if (coords_in_pattern.x < 0) {
  1914. ensure_positive_offset.x = 1;
  1915. }
  1916. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1917. coords_in_pattern.y -= 1;
  1918. if (coords_in_pattern.y < 0) {
  1919. ensure_positive_offset.y = 1;
  1920. }
  1921. }
  1922. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1923. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1924. coords_in_pattern.x += 1;
  1925. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1926. coords_in_pattern.y += 1;
  1927. }
  1928. }
  1929. }
  1930. coords_in_pattern_array.write[i] = coords_in_pattern;
  1931. }
  1932. for (int i = 0; i < coords_in_pattern_array.size(); i++) {
  1933. Vector2i coords = p_coords_array[i];
  1934. Vector2i coords_in_pattern = coords_in_pattern_array[i];
  1935. output->set_cell(coords_in_pattern + ensure_positive_offset, get_cell_source_id(coords), get_cell_atlas_coords(coords), get_cell_alternative_tile(coords));
  1936. }
  1937. return output;
  1938. }
  1939. void TileMapLayer::set_pattern(const Vector2i &p_position, const Ref<TileMapPattern> p_pattern) {
  1940. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1941. ERR_FAIL_COND(tile_set.is_null());
  1942. ERR_FAIL_COND(p_pattern.is_null());
  1943. TypedArray<Vector2i> used_cells = p_pattern->get_used_cells();
  1944. for (int i = 0; i < used_cells.size(); i++) {
  1945. Vector2i coords = tile_map_node->map_pattern(p_position, used_cells[i], p_pattern);
  1946. set_cell(coords, p_pattern->get_cell_source_id(used_cells[i]), p_pattern->get_cell_atlas_coords(used_cells[i]), p_pattern->get_cell_alternative_tile(used_cells[i]));
  1947. }
  1948. }
  1949. void TileMapLayer::set_cells_terrain_connect(TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1950. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1951. ERR_FAIL_COND(!tile_set.is_valid());
  1952. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  1953. Vector<Vector2i> cells_vector;
  1954. HashSet<Vector2i> painted_set;
  1955. for (int i = 0; i < p_cells.size(); i++) {
  1956. cells_vector.push_back(p_cells[i]);
  1957. painted_set.insert(p_cells[i]);
  1958. }
  1959. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_connect(cells_vector, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  1960. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  1961. if (painted_set.has(kv.key)) {
  1962. // Paint a random tile with the correct terrain for the painted path.
  1963. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  1964. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  1965. } else {
  1966. // Avoids updating the painted path from the output if the new pattern is the same as before.
  1967. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  1968. TileMapCell cell = get_cell(kv.key);
  1969. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1970. TileSetSource *source = *tile_set->get_source(cell.source_id);
  1971. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1972. if (atlas_source) {
  1973. // Get tile data.
  1974. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1975. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1976. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  1977. }
  1978. }
  1979. }
  1980. if (in_map_terrain_pattern != kv.value) {
  1981. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  1982. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  1983. }
  1984. }
  1985. }
  1986. }
  1987. void TileMapLayer::set_cells_terrain_path(TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1988. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1989. ERR_FAIL_COND(!tile_set.is_valid());
  1990. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  1991. Vector<Vector2i> vector_path;
  1992. HashSet<Vector2i> painted_set;
  1993. for (int i = 0; i < p_path.size(); i++) {
  1994. vector_path.push_back(p_path[i]);
  1995. painted_set.insert(p_path[i]);
  1996. }
  1997. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_path(vector_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  1998. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  1999. if (painted_set.has(kv.key)) {
  2000. // Paint a random tile with the correct terrain for the painted path.
  2001. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2002. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2003. } else {
  2004. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2005. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2006. TileMapCell cell = get_cell(kv.key);
  2007. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2008. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2009. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2010. if (atlas_source) {
  2011. // Get tile data.
  2012. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2013. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2014. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2015. }
  2016. }
  2017. }
  2018. if (in_map_terrain_pattern != kv.value) {
  2019. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2020. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2021. }
  2022. }
  2023. }
  2024. }
  2025. TypedArray<Vector2i> TileMapLayer::get_used_cells() const {
  2026. // Returns the cells used in the tilemap.
  2027. TypedArray<Vector2i> a;
  2028. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2029. const TileMapCell &c = E.value.cell;
  2030. if (c.source_id == TileSet::INVALID_SOURCE) {
  2031. continue;
  2032. }
  2033. a.push_back(E.key);
  2034. }
  2035. return a;
  2036. }
  2037. TypedArray<Vector2i> TileMapLayer::get_used_cells_by_id(int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) const {
  2038. // Returns the cells used in the tilemap.
  2039. TypedArray<Vector2i> a;
  2040. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2041. const TileMapCell &c = E.value.cell;
  2042. if (c.source_id == TileSet::INVALID_SOURCE) {
  2043. continue;
  2044. }
  2045. if ((p_source_id == TileSet::INVALID_SOURCE || p_source_id == c.source_id) &&
  2046. (p_atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || p_atlas_coords == c.get_atlas_coords()) &&
  2047. (p_alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE || p_alternative_tile == c.alternative_tile)) {
  2048. a.push_back(E.key);
  2049. }
  2050. }
  2051. return a;
  2052. }
  2053. Rect2i TileMapLayer::get_used_rect() const {
  2054. // Return the rect of the currently used area.
  2055. if (used_rect_cache_dirty) {
  2056. used_rect_cache = Rect2i();
  2057. bool first = true;
  2058. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2059. const TileMapCell &c = E.value.cell;
  2060. if (c.source_id == TileSet::INVALID_SOURCE) {
  2061. continue;
  2062. }
  2063. if (first) {
  2064. used_rect_cache = Rect2i(E.key.x, E.key.y, 0, 0);
  2065. first = false;
  2066. } else {
  2067. used_rect_cache.expand_to(E.key);
  2068. }
  2069. }
  2070. if (!first) {
  2071. // Only if we have at least one cell.
  2072. // The cache expands to top-left coordinate, so we add one full tile.
  2073. used_rect_cache.size += Vector2i(1, 1);
  2074. }
  2075. used_rect_cache_dirty = false;
  2076. }
  2077. return used_rect_cache;
  2078. }
  2079. void TileMapLayer::set_name(String p_name) {
  2080. if (name == p_name) {
  2081. return;
  2082. }
  2083. name = p_name;
  2084. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2085. }
  2086. String TileMapLayer::get_name() const {
  2087. return name;
  2088. }
  2089. void TileMapLayer::set_enabled(bool p_enabled) {
  2090. if (enabled == p_enabled) {
  2091. return;
  2092. }
  2093. enabled = p_enabled;
  2094. dirty.flags[DIRTY_FLAGS_LAYER_ENABLED] = true;
  2095. tile_map_node->queue_internal_update();
  2096. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2097. tile_map_node->update_configuration_warnings();
  2098. }
  2099. bool TileMapLayer::is_enabled() const {
  2100. return enabled;
  2101. }
  2102. void TileMapLayer::set_modulate(Color p_modulate) {
  2103. if (modulate == p_modulate) {
  2104. return;
  2105. }
  2106. modulate = p_modulate;
  2107. dirty.flags[DIRTY_FLAGS_LAYER_MODULATE] = true;
  2108. tile_map_node->queue_internal_update();
  2109. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2110. }
  2111. Color TileMapLayer::get_modulate() const {
  2112. return modulate;
  2113. }
  2114. void TileMapLayer::set_y_sort_enabled(bool p_y_sort_enabled) {
  2115. if (y_sort_enabled == p_y_sort_enabled) {
  2116. return;
  2117. }
  2118. y_sort_enabled = p_y_sort_enabled;
  2119. dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] = true;
  2120. tile_map_node->queue_internal_update();
  2121. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2122. tile_map_node->update_configuration_warnings();
  2123. }
  2124. bool TileMapLayer::is_y_sort_enabled() const {
  2125. return y_sort_enabled;
  2126. }
  2127. void TileMapLayer::set_y_sort_origin(int p_y_sort_origin) {
  2128. if (y_sort_origin == p_y_sort_origin) {
  2129. return;
  2130. }
  2131. y_sort_origin = p_y_sort_origin;
  2132. dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] = true;
  2133. tile_map_node->queue_internal_update();
  2134. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2135. }
  2136. int TileMapLayer::get_y_sort_origin() const {
  2137. return y_sort_origin;
  2138. }
  2139. void TileMapLayer::set_z_index(int p_z_index) {
  2140. if (z_index == p_z_index) {
  2141. return;
  2142. }
  2143. z_index = p_z_index;
  2144. dirty.flags[DIRTY_FLAGS_LAYER_Z_INDEX] = true;
  2145. tile_map_node->queue_internal_update();
  2146. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2147. tile_map_node->update_configuration_warnings();
  2148. }
  2149. int TileMapLayer::get_z_index() const {
  2150. return z_index;
  2151. }
  2152. void TileMapLayer::set_navigation_enabled(bool p_enabled) {
  2153. if (navigation_enabled == p_enabled) {
  2154. return;
  2155. }
  2156. navigation_enabled = p_enabled;
  2157. dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_ENABLED] = true;
  2158. tile_map_node->queue_internal_update();
  2159. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2160. }
  2161. bool TileMapLayer::is_navigation_enabled() const {
  2162. return navigation_enabled;
  2163. }
  2164. void TileMapLayer::set_navigation_map(RID p_map) {
  2165. ERR_FAIL_COND_MSG(!tile_map_node->is_inside_tree(), "A TileMap navigation map can only be changed while inside the SceneTree.");
  2166. navigation_map = p_map;
  2167. uses_world_navigation_map = p_map == tile_map_node->get_world_2d()->get_navigation_map();
  2168. }
  2169. RID TileMapLayer::get_navigation_map() const {
  2170. if (navigation_map.is_valid()) {
  2171. return navigation_map;
  2172. }
  2173. return RID();
  2174. }
  2175. void TileMapLayer::fix_invalid_tiles() {
  2176. Ref<TileSet> tileset = tile_map_node->get_tileset();
  2177. ERR_FAIL_COND_MSG(tileset.is_null(), "Cannot call fix_invalid_tiles() on a TileMap without a valid TileSet.");
  2178. RBSet<Vector2i> coords;
  2179. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2180. TileSetSource *source = *tileset->get_source(E.value.cell.source_id);
  2181. if (!source || !source->has_tile(E.value.cell.get_atlas_coords()) || !source->has_alternative_tile(E.value.cell.get_atlas_coords(), E.value.cell.alternative_tile)) {
  2182. coords.insert(E.key);
  2183. }
  2184. }
  2185. for (const Vector2i &E : coords) {
  2186. set_cell(E, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  2187. }
  2188. }
  2189. bool TileMapLayer::has_body_rid(RID p_physics_body) const {
  2190. return bodies_coords.has(p_physics_body);
  2191. }
  2192. Vector2i TileMapLayer::get_coords_for_body_rid(RID p_physics_body) const {
  2193. return bodies_coords[p_physics_body];
  2194. }
  2195. TileMapLayer::~TileMapLayer() {
  2196. if (!tile_map_node) {
  2197. // Temporary layer.
  2198. return;
  2199. }
  2200. in_destructor = true;
  2201. clear();
  2202. internal_update();
  2203. }
  2204. HashMap<Vector2i, TileSet::CellNeighbor> TerrainConstraint::get_overlapping_coords_and_peering_bits() const {
  2205. HashMap<Vector2i, TileSet::CellNeighbor> output;
  2206. ERR_FAIL_COND_V(is_center_bit(), output);
  2207. Ref<TileSet> ts = tile_map->get_tileset();
  2208. ERR_FAIL_COND_V(!ts.is_valid(), output);
  2209. TileSet::TileShape shape = ts->get_tile_shape();
  2210. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2211. switch (bit) {
  2212. case 1:
  2213. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  2214. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  2215. break;
  2216. case 2:
  2217. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2218. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2219. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2220. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2221. break;
  2222. case 3:
  2223. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  2224. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  2225. break;
  2226. default:
  2227. ERR_FAIL_V(output);
  2228. }
  2229. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2230. switch (bit) {
  2231. case 1:
  2232. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2233. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2234. break;
  2235. case 2:
  2236. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  2237. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  2238. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  2239. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  2240. break;
  2241. case 3:
  2242. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2243. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2244. break;
  2245. default:
  2246. ERR_FAIL_V(output);
  2247. }
  2248. } else {
  2249. // Half offset shapes.
  2250. TileSet::TileOffsetAxis offset_axis = ts->get_tile_offset_axis();
  2251. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2252. switch (bit) {
  2253. case 1:
  2254. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  2255. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  2256. break;
  2257. case 2:
  2258. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2259. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2260. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  2261. break;
  2262. case 3:
  2263. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2264. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2265. break;
  2266. case 4:
  2267. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  2268. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2269. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2270. break;
  2271. case 5:
  2272. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2273. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2274. break;
  2275. default:
  2276. ERR_FAIL_V(output);
  2277. }
  2278. } else {
  2279. switch (bit) {
  2280. case 1:
  2281. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  2282. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2283. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2284. break;
  2285. case 2:
  2286. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2287. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2288. break;
  2289. case 3:
  2290. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2291. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  2292. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2293. break;
  2294. case 4:
  2295. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  2296. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  2297. break;
  2298. case 5:
  2299. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2300. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2301. break;
  2302. default:
  2303. ERR_FAIL_V(output);
  2304. }
  2305. }
  2306. }
  2307. return output;
  2308. }
  2309. TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, int p_terrain) {
  2310. tile_map = p_tile_map;
  2311. Ref<TileSet> ts = tile_map->get_tileset();
  2312. ERR_FAIL_COND(!ts.is_valid());
  2313. bit = 0;
  2314. base_cell_coords = p_position;
  2315. terrain = p_terrain;
  2316. }
  2317. TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, const TileSet::CellNeighbor &p_bit, int p_terrain) {
  2318. // The way we build the constraint make it easy to detect conflicting constraints.
  2319. tile_map = p_tile_map;
  2320. Ref<TileSet> ts = tile_map->get_tileset();
  2321. ERR_FAIL_COND(!ts.is_valid());
  2322. TileSet::TileShape shape = ts->get_tile_shape();
  2323. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2324. switch (p_bit) {
  2325. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2326. bit = 1;
  2327. base_cell_coords = p_position;
  2328. break;
  2329. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2330. bit = 2;
  2331. base_cell_coords = p_position;
  2332. break;
  2333. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2334. bit = 3;
  2335. base_cell_coords = p_position;
  2336. break;
  2337. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2338. bit = 2;
  2339. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2340. break;
  2341. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2342. bit = 1;
  2343. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2344. break;
  2345. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2346. bit = 2;
  2347. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER);
  2348. break;
  2349. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2350. bit = 3;
  2351. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2352. break;
  2353. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2354. bit = 2;
  2355. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2356. break;
  2357. default:
  2358. ERR_FAIL();
  2359. break;
  2360. }
  2361. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2362. switch (p_bit) {
  2363. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2364. bit = 2;
  2365. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2366. break;
  2367. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2368. bit = 1;
  2369. base_cell_coords = p_position;
  2370. break;
  2371. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2372. bit = 2;
  2373. base_cell_coords = p_position;
  2374. break;
  2375. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2376. bit = 3;
  2377. base_cell_coords = p_position;
  2378. break;
  2379. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2380. bit = 2;
  2381. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2382. break;
  2383. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2384. bit = 1;
  2385. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2386. break;
  2387. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2388. bit = 2;
  2389. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_CORNER);
  2390. break;
  2391. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2392. bit = 3;
  2393. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2394. break;
  2395. default:
  2396. ERR_FAIL();
  2397. break;
  2398. }
  2399. } else {
  2400. // Half-offset shapes.
  2401. TileSet::TileOffsetAxis offset_axis = ts->get_tile_offset_axis();
  2402. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2403. switch (p_bit) {
  2404. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2405. bit = 1;
  2406. base_cell_coords = p_position;
  2407. break;
  2408. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2409. bit = 2;
  2410. base_cell_coords = p_position;
  2411. break;
  2412. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2413. bit = 3;
  2414. base_cell_coords = p_position;
  2415. break;
  2416. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2417. bit = 4;
  2418. base_cell_coords = p_position;
  2419. break;
  2420. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2421. bit = 5;
  2422. base_cell_coords = p_position;
  2423. break;
  2424. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2425. bit = 2;
  2426. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2427. break;
  2428. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2429. bit = 1;
  2430. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2431. break;
  2432. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2433. bit = 4;
  2434. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2435. break;
  2436. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2437. bit = 3;
  2438. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2439. break;
  2440. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2441. bit = 2;
  2442. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2443. break;
  2444. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2445. bit = 5;
  2446. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2447. break;
  2448. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2449. bit = 4;
  2450. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2451. break;
  2452. default:
  2453. ERR_FAIL();
  2454. break;
  2455. }
  2456. } else {
  2457. switch (p_bit) {
  2458. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2459. bit = 1;
  2460. base_cell_coords = p_position;
  2461. break;
  2462. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2463. bit = 2;
  2464. base_cell_coords = p_position;
  2465. break;
  2466. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2467. bit = 3;
  2468. base_cell_coords = p_position;
  2469. break;
  2470. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2471. bit = 4;
  2472. base_cell_coords = p_position;
  2473. break;
  2474. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2475. bit = 1;
  2476. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  2477. break;
  2478. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2479. bit = 5;
  2480. base_cell_coords = p_position;
  2481. break;
  2482. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2483. bit = 3;
  2484. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2485. break;
  2486. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2487. bit = 2;
  2488. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2489. break;
  2490. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2491. bit = 1;
  2492. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2493. break;
  2494. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2495. bit = 4;
  2496. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2497. break;
  2498. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2499. bit = 3;
  2500. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2501. break;
  2502. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2503. bit = 5;
  2504. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2505. break;
  2506. default:
  2507. ERR_FAIL();
  2508. break;
  2509. }
  2510. }
  2511. }
  2512. terrain = p_terrain;
  2513. }
  2514. #define TILEMAP_CALL_FOR_LAYER(layer, function, ...) \
  2515. if (layer < 0) { \
  2516. layer = layers.size() + layer; \
  2517. }; \
  2518. ERR_FAIL_INDEX(layer, (int)layers.size()); \
  2519. layers[layer]->function(__VA_ARGS__);
  2520. #define TILEMAP_CALL_FOR_LAYER_V(layer, err_value, function, ...) \
  2521. if (layer < 0) { \
  2522. layer = layers.size() + layer; \
  2523. }; \
  2524. ERR_FAIL_INDEX_V(layer, (int)layers.size(), err_value); \
  2525. return layers[layer]->function(__VA_ARGS__);
  2526. Vector2i TileMap::transform_coords_layout(const Vector2i &p_coords, TileSet::TileOffsetAxis p_offset_axis, TileSet::TileLayout p_from_layout, TileSet::TileLayout p_to_layout) {
  2527. // Transform to stacked layout.
  2528. Vector2i output = p_coords;
  2529. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  2530. SWAP(output.x, output.y);
  2531. }
  2532. switch (p_from_layout) {
  2533. case TileSet::TILE_LAYOUT_STACKED:
  2534. break;
  2535. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2536. if (output.y % 2) {
  2537. output.x -= 1;
  2538. }
  2539. break;
  2540. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2541. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2542. if ((p_from_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2543. if (output.y < 0 && bool(output.y % 2)) {
  2544. output = Vector2i(output.x + output.y / 2 - 1, output.y);
  2545. } else {
  2546. output = Vector2i(output.x + output.y / 2, output.y);
  2547. }
  2548. } else {
  2549. if (output.x < 0 && bool(output.x % 2)) {
  2550. output = Vector2i(output.x / 2 - 1, output.x + output.y * 2);
  2551. } else {
  2552. output = Vector2i(output.x / 2, output.x + output.y * 2);
  2553. }
  2554. }
  2555. break;
  2556. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2557. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2558. if ((p_from_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2559. if ((output.x + output.y) < 0 && (output.x - output.y) % 2) {
  2560. output = Vector2i((output.x + output.y) / 2 - 1, output.y - output.x);
  2561. } else {
  2562. output = Vector2i((output.x + output.y) / 2, -output.x + output.y);
  2563. }
  2564. } else {
  2565. if ((output.x - output.y) < 0 && (output.x + output.y) % 2) {
  2566. output = Vector2i((output.x - output.y) / 2 - 1, output.x + output.y);
  2567. } else {
  2568. output = Vector2i((output.x - output.y) / 2, output.x + output.y);
  2569. }
  2570. }
  2571. break;
  2572. }
  2573. switch (p_to_layout) {
  2574. case TileSet::TILE_LAYOUT_STACKED:
  2575. break;
  2576. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2577. if (output.y % 2) {
  2578. output.x += 1;
  2579. }
  2580. break;
  2581. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2582. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2583. if ((p_to_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2584. if (output.y < 0 && (output.y % 2)) {
  2585. output = Vector2i(output.x - output.y / 2 + 1, output.y);
  2586. } else {
  2587. output = Vector2i(output.x - output.y / 2, output.y);
  2588. }
  2589. } else {
  2590. if (output.y % 2) {
  2591. if (output.y < 0) {
  2592. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2 - 1);
  2593. } else {
  2594. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2);
  2595. }
  2596. } else {
  2597. output = Vector2i(2 * output.x, -output.x + output.y / 2);
  2598. }
  2599. }
  2600. break;
  2601. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2602. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2603. if ((p_to_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2604. if (output.y % 2) {
  2605. if (output.y > 0) {
  2606. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2 + 1);
  2607. } else {
  2608. output = Vector2i(output.x - output.y / 2 + 1, output.x + output.y / 2);
  2609. }
  2610. } else {
  2611. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2);
  2612. }
  2613. } else {
  2614. if (output.y % 2) {
  2615. if (output.y < 0) {
  2616. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2 - 1);
  2617. } else {
  2618. output = Vector2i(output.x + output.y / 2 + 1, -output.x + output.y / 2);
  2619. }
  2620. } else {
  2621. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2);
  2622. }
  2623. }
  2624. break;
  2625. }
  2626. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  2627. SWAP(output.x, output.y);
  2628. }
  2629. return output;
  2630. }
  2631. void TileMap::set_selected_layer(int p_layer_id) {
  2632. ERR_FAIL_COND(p_layer_id < -1 || p_layer_id >= (int)layers.size());
  2633. if (selected_layer == p_layer_id) {
  2634. return;
  2635. }
  2636. selected_layer = p_layer_id;
  2637. emit_signal(CoreStringNames::get_singleton()->changed);
  2638. // Update the layers modulation.
  2639. for (Ref<TileMapLayer> &layer : layers) {
  2640. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_SELECTED_LAYER);
  2641. }
  2642. }
  2643. int TileMap::get_selected_layer() const {
  2644. return selected_layer;
  2645. }
  2646. void TileMap::_notification(int p_what) {
  2647. switch (p_what) {
  2648. case NOTIFICATION_ENTER_TREE: {
  2649. for (Ref<TileMapLayer> &layer : layers) {
  2650. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_IN_TREE);
  2651. }
  2652. } break;
  2653. case NOTIFICATION_EXIT_TREE: {
  2654. for (Ref<TileMapLayer> &layer : layers) {
  2655. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_IN_TREE);
  2656. }
  2657. } break;
  2658. case TileMap::NOTIFICATION_ENTER_CANVAS: {
  2659. for (Ref<TileMapLayer> &layer : layers) {
  2660. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_IN_CANVAS);
  2661. }
  2662. } break;
  2663. case TileMap::NOTIFICATION_EXIT_CANVAS: {
  2664. for (Ref<TileMapLayer> &layer : layers) {
  2665. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_IN_CANVAS);
  2666. }
  2667. } break;
  2668. case NOTIFICATION_DRAW: {
  2669. // Rendering.
  2670. if (tile_set.is_valid()) {
  2671. RenderingServer::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), is_y_sort_enabled());
  2672. }
  2673. } break;
  2674. case TileMap::NOTIFICATION_VISIBILITY_CHANGED: {
  2675. for (Ref<TileMapLayer> &layer : layers) {
  2676. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_VISIBILITY);
  2677. }
  2678. } break;
  2679. case TileMap::NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  2680. // Physics.
  2681. bool in_editor = false;
  2682. #ifdef TOOLS_ENABLED
  2683. in_editor = Engine::get_singleton()->is_editor_hint();
  2684. #endif
  2685. if (is_inside_tree() && collision_animatable && !in_editor) {
  2686. // Update transform on the physics tick when in animatable mode.
  2687. last_valid_transform = new_transform;
  2688. set_notify_local_transform(false);
  2689. set_global_transform(new_transform);
  2690. _update_notify_local_transform();
  2691. }
  2692. } break;
  2693. case NOTIFICATION_TRANSFORM_CHANGED: {
  2694. // Physics.
  2695. for (Ref<TileMapLayer> &layer : layers) {
  2696. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_XFORM);
  2697. }
  2698. } break;
  2699. case TileMap::NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  2700. for (Ref<TileMapLayer> &layer : layers) {
  2701. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM);
  2702. }
  2703. // Physics.
  2704. bool in_editor = false;
  2705. #ifdef TOOLS_ENABLED
  2706. in_editor = Engine::get_singleton()->is_editor_hint();
  2707. #endif
  2708. // Only active when animatable. Send the new transform to the physics...
  2709. if (is_inside_tree() && collision_animatable && !in_editor) {
  2710. // Store last valid transform.
  2711. new_transform = get_global_transform();
  2712. // ... but then revert changes.
  2713. set_notify_local_transform(false);
  2714. set_global_transform(last_valid_transform);
  2715. _update_notify_local_transform();
  2716. }
  2717. } break;
  2718. }
  2719. }
  2720. #ifndef DISABLE_DEPRECATED
  2721. // Deprecated methods.
  2722. void TileMap::force_update(int p_layer) {
  2723. notify_runtime_tile_data_update(p_layer);
  2724. update_internals();
  2725. }
  2726. #endif
  2727. void TileMap::queue_internal_update() {
  2728. if (pending_update) {
  2729. return;
  2730. }
  2731. pending_update = true;
  2732. callable_mp(this, &TileMap::_internal_update).call_deferred();
  2733. }
  2734. void TileMap::_internal_update() {
  2735. // Other updates.
  2736. if (!pending_update) {
  2737. return;
  2738. }
  2739. // FIXME: This should only clear polygons that are no longer going to be used, but since it's difficult to determine,
  2740. // the cache is never cleared at runtime to prevent invalidating used polygons.
  2741. if (Engine::get_singleton()->is_editor_hint()) {
  2742. polygon_cache.clear();
  2743. }
  2744. // Update dirty quadrants on layers.
  2745. for (Ref<TileMapLayer> &layer : layers) {
  2746. layer->internal_update();
  2747. }
  2748. pending_update = false;
  2749. }
  2750. void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
  2751. if (p_tileset == tile_set) {
  2752. return;
  2753. }
  2754. // Set the tileset, registering to its changes.
  2755. if (tile_set.is_valid()) {
  2756. tile_set->disconnect_changed(callable_mp(this, &TileMap::_tile_set_changed));
  2757. }
  2758. tile_set = p_tileset;
  2759. if (tile_set.is_valid()) {
  2760. tile_set->connect_changed(callable_mp(this, &TileMap::_tile_set_changed));
  2761. }
  2762. for (Ref<TileMapLayer> &layer : layers) {
  2763. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_TILE_SET);
  2764. }
  2765. emit_signal(CoreStringNames::get_singleton()->changed);
  2766. }
  2767. Ref<TileSet> TileMap::get_tileset() const {
  2768. return tile_set;
  2769. }
  2770. void TileMap::set_rendering_quadrant_size(int p_size) {
  2771. ERR_FAIL_COND_MSG(p_size < 1, "TileMapQuadrant size cannot be smaller than 1.");
  2772. rendering_quadrant_size = p_size;
  2773. for (Ref<TileMapLayer> &layer : layers) {
  2774. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_QUADRANT_SIZE);
  2775. }
  2776. emit_signal(CoreStringNames::get_singleton()->changed);
  2777. }
  2778. int TileMap::get_rendering_quadrant_size() const {
  2779. return rendering_quadrant_size;
  2780. }
  2781. void TileMap::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override, real_t p_animation_offset) {
  2782. ERR_FAIL_COND(!p_tile_set.is_valid());
  2783. ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id));
  2784. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords));
  2785. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_alternative_tile(p_atlas_coords, p_alternative_tile));
  2786. TileSetSource *source = *p_tile_set->get_source(p_atlas_source_id);
  2787. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2788. if (atlas_source) {
  2789. // Check for the frame.
  2790. if (p_frame >= 0) {
  2791. ERR_FAIL_INDEX(p_frame, atlas_source->get_tile_animation_frames_count(p_atlas_coords));
  2792. }
  2793. // Get the texture.
  2794. Ref<Texture2D> tex = atlas_source->get_runtime_texture();
  2795. if (!tex.is_valid()) {
  2796. return;
  2797. }
  2798. // Check if we are in the texture, return otherwise.
  2799. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  2800. if (p_atlas_coords.x >= grid_size.x || p_atlas_coords.y >= grid_size.y) {
  2801. return;
  2802. }
  2803. // Get tile data.
  2804. const TileData *tile_data = p_tile_data_override ? p_tile_data_override : atlas_source->get_tile_data(p_atlas_coords, p_alternative_tile);
  2805. // Get the tile modulation.
  2806. Color modulate = tile_data->get_modulate() * p_modulation;
  2807. // Compute the offset.
  2808. Vector2 tile_offset = tile_data->get_texture_origin();
  2809. // Get destination rect.
  2810. Rect2 dest_rect;
  2811. dest_rect.size = atlas_source->get_runtime_tile_texture_region(p_atlas_coords).size;
  2812. dest_rect.size.x += FP_ADJUST;
  2813. dest_rect.size.y += FP_ADJUST;
  2814. bool transpose = tile_data->get_transpose() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  2815. if (transpose) {
  2816. dest_rect.position = (p_position - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset);
  2817. } else {
  2818. dest_rect.position = (p_position - dest_rect.size / 2 - tile_offset);
  2819. }
  2820. if (tile_data->get_flip_h() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H)) {
  2821. dest_rect.size.x = -dest_rect.size.x;
  2822. }
  2823. if (tile_data->get_flip_v() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V)) {
  2824. dest_rect.size.y = -dest_rect.size.y;
  2825. }
  2826. // Draw the tile.
  2827. if (p_frame >= 0) {
  2828. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, p_frame);
  2829. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  2830. } else if (atlas_source->get_tile_animation_frames_count(p_atlas_coords) == 1) {
  2831. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, 0);
  2832. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  2833. } else {
  2834. real_t speed = atlas_source->get_tile_animation_speed(p_atlas_coords);
  2835. real_t animation_duration = atlas_source->get_tile_animation_total_duration(p_atlas_coords) / speed;
  2836. // Accumulate durations unaffected by the speed to avoid accumulating floating point division errors.
  2837. // Aka do `sum(duration[i]) / speed` instead of `sum(duration[i] / speed)`.
  2838. real_t time_unscaled = 0.0;
  2839. for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(p_atlas_coords); frame++) {
  2840. real_t frame_duration_unscaled = atlas_source->get_tile_animation_frame_duration(p_atlas_coords, frame);
  2841. real_t slice_start = time_unscaled / speed;
  2842. real_t slice_end = (time_unscaled + frame_duration_unscaled) / speed;
  2843. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, slice_start, slice_end, p_animation_offset);
  2844. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, frame);
  2845. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  2846. time_unscaled += frame_duration_unscaled;
  2847. }
  2848. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, 1.0, 0.0, 1.0, 0.0);
  2849. }
  2850. }
  2851. }
  2852. int TileMap::get_layers_count() const {
  2853. return layers.size();
  2854. }
  2855. void TileMap::add_layer(int p_to_pos) {
  2856. if (p_to_pos < 0) {
  2857. p_to_pos = layers.size() + p_to_pos + 1;
  2858. }
  2859. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  2860. // Must clear before adding the layer.
  2861. Ref<TileMapLayer> new_layer;
  2862. new_layer.instantiate();
  2863. new_layer->set_tile_map(this);
  2864. layers.insert(p_to_pos, new_layer);
  2865. for (uint32_t i = 0; i < layers.size(); i++) {
  2866. layers[i]->set_layer_index_in_tile_map_node(i);
  2867. }
  2868. queue_internal_update();
  2869. notify_property_list_changed();
  2870. emit_signal(CoreStringNames::get_singleton()->changed);
  2871. update_configuration_warnings();
  2872. }
  2873. void TileMap::move_layer(int p_layer, int p_to_pos) {
  2874. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2875. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  2876. // Clear before shuffling layers.
  2877. Ref<TileMapLayer> layer = layers[p_layer];
  2878. layers.insert(p_to_pos, layer);
  2879. layers.remove_at(p_to_pos < p_layer ? p_layer + 1 : p_layer);
  2880. for (uint32_t i = 0; i < layers.size(); i++) {
  2881. layers[i]->set_layer_index_in_tile_map_node(i);
  2882. }
  2883. queue_internal_update();
  2884. notify_property_list_changed();
  2885. if (selected_layer == p_layer) {
  2886. selected_layer = p_to_pos < p_layer ? p_to_pos - 1 : p_to_pos;
  2887. }
  2888. emit_signal(CoreStringNames::get_singleton()->changed);
  2889. update_configuration_warnings();
  2890. }
  2891. void TileMap::remove_layer(int p_layer) {
  2892. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2893. // Clear before removing the layer.
  2894. layers.remove_at(p_layer);
  2895. for (uint32_t i = 0; i < layers.size(); i++) {
  2896. layers[i]->set_layer_index_in_tile_map_node(i);
  2897. }
  2898. queue_internal_update();
  2899. notify_property_list_changed();
  2900. if (selected_layer >= p_layer) {
  2901. selected_layer -= 1;
  2902. }
  2903. emit_signal(CoreStringNames::get_singleton()->changed);
  2904. update_configuration_warnings();
  2905. }
  2906. void TileMap::set_layer_name(int p_layer, String p_name) {
  2907. TILEMAP_CALL_FOR_LAYER(p_layer, set_name, p_name);
  2908. }
  2909. String TileMap::get_layer_name(int p_layer) const {
  2910. TILEMAP_CALL_FOR_LAYER_V(p_layer, "", get_name);
  2911. }
  2912. void TileMap::set_layer_enabled(int p_layer, bool p_enabled) {
  2913. TILEMAP_CALL_FOR_LAYER(p_layer, set_enabled, p_enabled);
  2914. }
  2915. bool TileMap::is_layer_enabled(int p_layer) const {
  2916. TILEMAP_CALL_FOR_LAYER_V(p_layer, false, is_enabled);
  2917. }
  2918. void TileMap::set_layer_modulate(int p_layer, Color p_modulate) {
  2919. TILEMAP_CALL_FOR_LAYER(p_layer, set_modulate, p_modulate);
  2920. }
  2921. Color TileMap::get_layer_modulate(int p_layer) const {
  2922. TILEMAP_CALL_FOR_LAYER_V(p_layer, Color(), get_modulate);
  2923. }
  2924. void TileMap::set_layer_y_sort_enabled(int p_layer, bool p_y_sort_enabled) {
  2925. TILEMAP_CALL_FOR_LAYER(p_layer, set_y_sort_enabled, p_y_sort_enabled);
  2926. _update_notify_local_transform();
  2927. }
  2928. bool TileMap::is_layer_y_sort_enabled(int p_layer) const {
  2929. TILEMAP_CALL_FOR_LAYER_V(p_layer, false, is_y_sort_enabled);
  2930. }
  2931. void TileMap::set_layer_y_sort_origin(int p_layer, int p_y_sort_origin) {
  2932. TILEMAP_CALL_FOR_LAYER(p_layer, set_y_sort_origin, p_y_sort_origin);
  2933. }
  2934. int TileMap::get_layer_y_sort_origin(int p_layer) const {
  2935. TILEMAP_CALL_FOR_LAYER_V(p_layer, 0, get_y_sort_origin);
  2936. }
  2937. void TileMap::set_layer_z_index(int p_layer, int p_z_index) {
  2938. TILEMAP_CALL_FOR_LAYER(p_layer, set_z_index, p_z_index);
  2939. }
  2940. int TileMap::get_layer_z_index(int p_layer) const {
  2941. TILEMAP_CALL_FOR_LAYER_V(p_layer, 0, get_z_index);
  2942. }
  2943. void TileMap::set_layer_navigation_enabled(int p_layer, bool p_enabled) {
  2944. TILEMAP_CALL_FOR_LAYER(p_layer, set_navigation_enabled, p_enabled);
  2945. }
  2946. bool TileMap::is_layer_navigation_enabled(int p_layer) const {
  2947. TILEMAP_CALL_FOR_LAYER_V(p_layer, false, is_navigation_enabled);
  2948. }
  2949. void TileMap::set_layer_navigation_map(int p_layer, RID p_map) {
  2950. TILEMAP_CALL_FOR_LAYER(p_layer, set_navigation_map, p_map);
  2951. }
  2952. RID TileMap::get_layer_navigation_map(int p_layer) const {
  2953. TILEMAP_CALL_FOR_LAYER_V(p_layer, RID(), get_navigation_map);
  2954. }
  2955. void TileMap::set_collision_animatable(bool p_enabled) {
  2956. if (collision_animatable == p_enabled) {
  2957. return;
  2958. }
  2959. collision_animatable = p_enabled;
  2960. _update_notify_local_transform();
  2961. set_physics_process_internal(p_enabled);
  2962. for (Ref<TileMapLayer> &layer : layers) {
  2963. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_COLLISION_ANIMATABLE);
  2964. }
  2965. emit_signal(CoreStringNames::get_singleton()->changed);
  2966. }
  2967. bool TileMap::is_collision_animatable() const {
  2968. return collision_animatable;
  2969. }
  2970. void TileMap::set_collision_visibility_mode(TileMap::VisibilityMode p_show_collision) {
  2971. if (collision_visibility_mode == p_show_collision) {
  2972. return;
  2973. }
  2974. collision_visibility_mode = p_show_collision;
  2975. for (Ref<TileMapLayer> &layer : layers) {
  2976. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_COLLISION_VISIBILITY_MODE);
  2977. }
  2978. emit_signal(CoreStringNames::get_singleton()->changed);
  2979. }
  2980. TileMap::VisibilityMode TileMap::get_collision_visibility_mode() {
  2981. return collision_visibility_mode;
  2982. }
  2983. void TileMap::set_navigation_visibility_mode(TileMap::VisibilityMode p_show_navigation) {
  2984. if (navigation_visibility_mode == p_show_navigation) {
  2985. return;
  2986. }
  2987. navigation_visibility_mode = p_show_navigation;
  2988. for (Ref<TileMapLayer> &layer : layers) {
  2989. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_NAVIGATION_VISIBILITY_MODE);
  2990. }
  2991. emit_signal(CoreStringNames::get_singleton()->changed);
  2992. }
  2993. TileMap::VisibilityMode TileMap::get_navigation_visibility_mode() {
  2994. return navigation_visibility_mode;
  2995. }
  2996. void TileMap::set_y_sort_enabled(bool p_enable) {
  2997. if (is_y_sort_enabled() == p_enable) {
  2998. return;
  2999. }
  3000. Node2D::set_y_sort_enabled(p_enable);
  3001. for (Ref<TileMapLayer> &layer : layers) {
  3002. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_Y_SORT_ENABLED);
  3003. }
  3004. emit_signal(CoreStringNames::get_singleton()->changed);
  3005. update_configuration_warnings();
  3006. }
  3007. void TileMap::set_cell(int p_layer, const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  3008. TILEMAP_CALL_FOR_LAYER(p_layer, set_cell, p_coords, p_source_id, p_atlas_coords, p_alternative_tile);
  3009. }
  3010. void TileMap::erase_cell(int p_layer, const Vector2i &p_coords) {
  3011. TILEMAP_CALL_FOR_LAYER(p_layer, set_cell, p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  3012. }
  3013. int TileMap::get_cell_source_id(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  3014. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileSet::INVALID_SOURCE, get_cell_source_id, p_coords, p_use_proxies);
  3015. }
  3016. Vector2i TileMap::get_cell_atlas_coords(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  3017. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileSetSource::INVALID_ATLAS_COORDS, get_cell_atlas_coords, p_coords, p_use_proxies);
  3018. }
  3019. int TileMap::get_cell_alternative_tile(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  3020. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileSetSource::INVALID_TILE_ALTERNATIVE, get_cell_alternative_tile, p_coords, p_use_proxies);
  3021. }
  3022. TileData *TileMap::get_cell_tile_data(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  3023. TILEMAP_CALL_FOR_LAYER_V(p_layer, nullptr, get_cell_tile_data, p_coords, p_use_proxies);
  3024. }
  3025. Ref<TileMapPattern> TileMap::get_pattern(int p_layer, TypedArray<Vector2i> p_coords_array) {
  3026. TILEMAP_CALL_FOR_LAYER_V(p_layer, Ref<TileMapPattern>(), get_pattern, p_coords_array);
  3027. }
  3028. Vector2i TileMap::map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern) {
  3029. ERR_FAIL_COND_V(p_pattern.is_null(), Vector2i());
  3030. ERR_FAIL_COND_V(!p_pattern->has_cell(p_coords_in_pattern), Vector2i());
  3031. Vector2i output = p_position_in_tilemap + p_coords_in_pattern;
  3032. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  3033. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  3034. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(p_position_in_tilemap.y % 2) && bool(p_coords_in_pattern.y % 2)) {
  3035. output.x += 1;
  3036. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(p_position_in_tilemap.x % 2) && bool(p_coords_in_pattern.x % 2)) {
  3037. output.y += 1;
  3038. }
  3039. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  3040. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(p_position_in_tilemap.y % 2) && bool(p_coords_in_pattern.y % 2)) {
  3041. output.x -= 1;
  3042. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(p_position_in_tilemap.x % 2) && bool(p_coords_in_pattern.x % 2)) {
  3043. output.y -= 1;
  3044. }
  3045. }
  3046. }
  3047. return output;
  3048. }
  3049. void TileMap::set_pattern(int p_layer, const Vector2i &p_position, const Ref<TileMapPattern> p_pattern) {
  3050. TILEMAP_CALL_FOR_LAYER(p_layer, set_pattern, p_position, p_pattern);
  3051. }
  3052. 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) {
  3053. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  3054. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_constraints, p_to_replace, p_terrain_set, p_constraints);
  3055. }
  3056. 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) {
  3057. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  3058. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_connect, p_coords_array, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  3059. }
  3060. 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) {
  3061. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  3062. TILEMAP_CALL_FOR_LAYER_V(p_layer, err_value, terrain_fill_path, p_coords_array, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  3063. }
  3064. 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) {
  3065. HashMap<Vector2i, TileSet::TerrainsPattern> err_value;
  3066. 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);
  3067. }
  3068. 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) {
  3069. TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_connect, p_cells, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  3070. }
  3071. 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) {
  3072. TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_path, p_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  3073. }
  3074. TileMapCell TileMap::get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  3075. TILEMAP_CALL_FOR_LAYER_V(p_layer, TileMapCell(), get_cell, p_coords, p_use_proxies);
  3076. }
  3077. Vector2i TileMap::get_coords_for_body_rid(RID p_physics_body) {
  3078. for (const Ref<TileMapLayer> &layer : layers) {
  3079. if (layer->has_body_rid(p_physics_body)) {
  3080. return layer->get_coords_for_body_rid(p_physics_body);
  3081. }
  3082. }
  3083. ERR_FAIL_V_MSG(Vector2i(), vformat("No tiles for the given body RID %d.", p_physics_body.get_id()));
  3084. }
  3085. int TileMap::get_layer_for_body_rid(RID p_physics_body) {
  3086. for (uint32_t i = 0; i < layers.size(); i++) {
  3087. if (layers[i]->has_body_rid(p_physics_body)) {
  3088. return i;
  3089. }
  3090. }
  3091. ERR_FAIL_V_MSG(-1, vformat("No tiles for the given body RID %d.", p_physics_body.get_id()));
  3092. }
  3093. void TileMap::fix_invalid_tiles() {
  3094. for (Ref<TileMapLayer> &layer : layers) {
  3095. layer->fix_invalid_tiles();
  3096. }
  3097. }
  3098. void TileMap::clear_layer(int p_layer) {
  3099. TILEMAP_CALL_FOR_LAYER(p_layer, clear)
  3100. }
  3101. void TileMap::clear() {
  3102. for (Ref<TileMapLayer> &layer : layers) {
  3103. layer->clear();
  3104. }
  3105. }
  3106. void TileMap::update_internals() {
  3107. pending_update = true;
  3108. _internal_update();
  3109. }
  3110. void TileMap::notify_runtime_tile_data_update(int p_layer) {
  3111. if (p_layer >= 0) {
  3112. TILEMAP_CALL_FOR_LAYER(p_layer, notify_tile_map_change, TileMapLayer::DIRTY_FLAGS_TILE_MAP_RUNTIME_UPDATE);
  3113. } else {
  3114. for (Ref<TileMapLayer> &layer : layers) {
  3115. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_RUNTIME_UPDATE);
  3116. }
  3117. }
  3118. }
  3119. #ifdef TOOLS_ENABLED
  3120. Rect2 TileMap::_edit_get_rect() const {
  3121. // Return the visible rect of the tilemap.
  3122. if (layers.is_empty()) {
  3123. return Rect2();
  3124. }
  3125. bool any_changed = false;
  3126. bool changed = false;
  3127. Rect2 rect = layers[0]->get_rect(changed);
  3128. any_changed |= changed;
  3129. for (uint32_t i = 1; i < layers.size(); i++) {
  3130. rect = rect.merge(layers[i]->get_rect(changed));
  3131. any_changed |= changed;
  3132. }
  3133. const_cast<TileMap *>(this)->item_rect_changed(any_changed);
  3134. return rect;
  3135. }
  3136. #endif
  3137. PackedVector2Array TileMap::_get_transformed_vertices(const PackedVector2Array &p_vertices, int p_alternative_id) {
  3138. const Vector2 *r = p_vertices.ptr();
  3139. int size = p_vertices.size();
  3140. PackedVector2Array new_points;
  3141. new_points.resize(size);
  3142. Vector2 *w = new_points.ptrw();
  3143. bool flip_h = (p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H);
  3144. bool flip_v = (p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V);
  3145. bool transpose = (p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  3146. for (int i = 0; i < size; i++) {
  3147. Vector2 v;
  3148. if (transpose) {
  3149. v = Vector2(r[i].y, r[i].x);
  3150. } else {
  3151. v = r[i];
  3152. }
  3153. if (flip_h) {
  3154. v.x *= -1;
  3155. }
  3156. if (flip_v) {
  3157. v.y *= -1;
  3158. }
  3159. w[i] = v;
  3160. }
  3161. return new_points;
  3162. }
  3163. bool TileMap::_set(const StringName &p_name, const Variant &p_value) {
  3164. Vector<String> components = String(p_name).split("/", true, 2);
  3165. if (p_name == "format") {
  3166. if (p_value.get_type() == Variant::INT) {
  3167. format = (TileMapLayer::DataFormat)(p_value.operator int64_t()); // Set format used for loading.
  3168. return true;
  3169. }
  3170. }
  3171. #ifndef DISABLE_DEPRECATED
  3172. else if (p_name == "tile_data") { // Kept for compatibility reasons.
  3173. if (p_value.is_array()) {
  3174. if (layers.size() == 0) {
  3175. Ref<TileMapLayer> new_layer;
  3176. new_layer.instantiate();
  3177. new_layer->set_tile_map(this);
  3178. new_layer->set_layer_index_in_tile_map_node(0);
  3179. layers.push_back(new_layer);
  3180. }
  3181. layers[0]->set_tile_data(format, p_value);
  3182. emit_signal(CoreStringNames::get_singleton()->changed);
  3183. return true;
  3184. }
  3185. return false;
  3186. } else if (p_name == "cell_quadrant_size") {
  3187. set_rendering_quadrant_size(p_value);
  3188. return true;
  3189. }
  3190. #endif // DISABLE_DEPRECATED
  3191. else if (components.size() == 2 && components[0].begins_with("layer_") && components[0].trim_prefix("layer_").is_valid_int()) {
  3192. int index = components[0].trim_prefix("layer_").to_int();
  3193. if (index < 0) {
  3194. return false;
  3195. }
  3196. if (index >= (int)layers.size()) {
  3197. while (index >= (int)layers.size()) {
  3198. Ref<TileMapLayer> new_layer;
  3199. new_layer.instantiate();
  3200. new_layer->set_tile_map(this);
  3201. new_layer->set_layer_index_in_tile_map_node(index);
  3202. layers.push_back(new_layer);
  3203. }
  3204. notify_property_list_changed();
  3205. emit_signal(CoreStringNames::get_singleton()->changed);
  3206. update_configuration_warnings();
  3207. }
  3208. if (components[1] == "name") {
  3209. set_layer_name(index, p_value);
  3210. return true;
  3211. } else if (components[1] == "enabled") {
  3212. set_layer_enabled(index, p_value);
  3213. return true;
  3214. } else if (components[1] == "modulate") {
  3215. set_layer_modulate(index, p_value);
  3216. return true;
  3217. } else if (components[1] == "y_sort_enabled") {
  3218. set_layer_y_sort_enabled(index, p_value);
  3219. return true;
  3220. } else if (components[1] == "y_sort_origin") {
  3221. set_layer_y_sort_origin(index, p_value);
  3222. return true;
  3223. } else if (components[1] == "z_index") {
  3224. set_layer_z_index(index, p_value);
  3225. return true;
  3226. } else if (components[1] == "navigation_enabled") {
  3227. set_layer_navigation_enabled(index, p_value);
  3228. return true;
  3229. } else if (components[1] == "tile_data") {
  3230. layers[index]->set_tile_data(format, p_value);
  3231. emit_signal(CoreStringNames::get_singleton()->changed);
  3232. return true;
  3233. } else {
  3234. return false;
  3235. }
  3236. }
  3237. return false;
  3238. }
  3239. bool TileMap::_get(const StringName &p_name, Variant &r_ret) const {
  3240. Vector<String> components = String(p_name).split("/", true, 2);
  3241. if (p_name == "format") {
  3242. r_ret = TileMapLayer::FORMAT_MAX - 1; // When saving, always save highest format.
  3243. return true;
  3244. }
  3245. #ifndef DISABLE_DEPRECATED
  3246. else if (p_name == "cell_quadrant_size") { // Kept for compatibility reasons.
  3247. r_ret = get_rendering_quadrant_size();
  3248. return true;
  3249. }
  3250. #endif
  3251. else if (components.size() == 2 && components[0].begins_with("layer_") && components[0].trim_prefix("layer_").is_valid_int()) {
  3252. int index = components[0].trim_prefix("layer_").to_int();
  3253. if (index < 0 || index >= (int)layers.size()) {
  3254. return false;
  3255. }
  3256. if (components[1] == "name") {
  3257. r_ret = get_layer_name(index);
  3258. return true;
  3259. } else if (components[1] == "enabled") {
  3260. r_ret = is_layer_enabled(index);
  3261. return true;
  3262. } else if (components[1] == "modulate") {
  3263. r_ret = get_layer_modulate(index);
  3264. return true;
  3265. } else if (components[1] == "y_sort_enabled") {
  3266. r_ret = is_layer_y_sort_enabled(index);
  3267. return true;
  3268. } else if (components[1] == "y_sort_origin") {
  3269. r_ret = get_layer_y_sort_origin(index);
  3270. return true;
  3271. } else if (components[1] == "z_index") {
  3272. r_ret = get_layer_z_index(index);
  3273. return true;
  3274. } else if (components[1] == "navigation_enabled") {
  3275. r_ret = is_layer_navigation_enabled(index);
  3276. return true;
  3277. } else if (components[1] == "tile_data") {
  3278. r_ret = layers[index]->get_tile_data();
  3279. return true;
  3280. } else {
  3281. return false;
  3282. }
  3283. }
  3284. return false;
  3285. }
  3286. void TileMap::_get_property_list(List<PropertyInfo> *p_list) const {
  3287. p_list->push_back(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  3288. p_list->push_back(PropertyInfo(Variant::NIL, "Layers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3289. #define MAKE_LAYER_PROPERTY(m_type, m_name, m_hint) \
  3290. { \
  3291. const String property_name = vformat("layer_%d/" m_name, i); \
  3292. p_list->push_back(PropertyInfo(m_type, property_name, PROPERTY_HINT_NONE, m_hint, (get(property_name) == property_get_revert(property_name)) ? PROPERTY_USAGE_EDITOR : PROPERTY_USAGE_DEFAULT)); \
  3293. }
  3294. for (uint32_t i = 0; i < layers.size(); i++) {
  3295. MAKE_LAYER_PROPERTY(Variant::STRING, "name", "");
  3296. MAKE_LAYER_PROPERTY(Variant::BOOL, "enabled", "");
  3297. MAKE_LAYER_PROPERTY(Variant::COLOR, "modulate", "");
  3298. MAKE_LAYER_PROPERTY(Variant::BOOL, "y_sort_enabled", "");
  3299. MAKE_LAYER_PROPERTY(Variant::INT, "y_sort_origin", "suffix:px");
  3300. MAKE_LAYER_PROPERTY(Variant::INT, "z_index", "");
  3301. MAKE_LAYER_PROPERTY(Variant::BOOL, "navigation_enabled", "");
  3302. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("layer_%d/tile_data", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3303. }
  3304. #undef MAKE_LAYER_PROPERTY
  3305. }
  3306. bool TileMap::_property_can_revert(const StringName &p_name) const {
  3307. Vector<String> components = String(p_name).split("/", true, 2);
  3308. if (components.size() == 2 && components[0].begins_with("layer_")) {
  3309. int index = components[0].trim_prefix("layer_").to_int();
  3310. if (index <= 0 || index >= (int)layers.size()) {
  3311. return false;
  3312. }
  3313. if (components[1] == "name") {
  3314. return layers[index]->get_name() != default_layer->get_name();
  3315. } else if (components[1] == "enabled") {
  3316. return layers[index]->is_enabled() != default_layer->is_enabled();
  3317. } else if (components[1] == "modulate") {
  3318. return layers[index]->get_modulate() != default_layer->get_modulate();
  3319. } else if (components[1] == "y_sort_enabled") {
  3320. return layers[index]->is_y_sort_enabled() != default_layer->is_y_sort_enabled();
  3321. } else if (components[1] == "y_sort_origin") {
  3322. return layers[index]->get_y_sort_origin() != default_layer->get_y_sort_origin();
  3323. } else if (components[1] == "z_index") {
  3324. return layers[index]->get_z_index() != default_layer->get_z_index();
  3325. } else if (components[1] == "navigation_enabled") {
  3326. return layers[index]->is_navigation_enabled() != default_layer->is_navigation_enabled();
  3327. }
  3328. }
  3329. return false;
  3330. }
  3331. bool TileMap::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  3332. Vector<String> components = String(p_name).split("/", true, 2);
  3333. if (components.size() == 2 && components[0].begins_with("layer_")) {
  3334. int index = components[0].trim_prefix("layer_").to_int();
  3335. if (index <= 0 || index >= (int)layers.size()) {
  3336. return false;
  3337. }
  3338. if (components[1] == "name") {
  3339. r_property = default_layer->get_name();
  3340. return true;
  3341. } else if (components[1] == "enabled") {
  3342. r_property = default_layer->is_enabled();
  3343. return true;
  3344. } else if (components[1] == "modulate") {
  3345. r_property = default_layer->get_modulate();
  3346. return true;
  3347. } else if (components[1] == "y_sort_enabled") {
  3348. r_property = default_layer->is_y_sort_enabled();
  3349. return true;
  3350. } else if (components[1] == "y_sort_origin") {
  3351. r_property = default_layer->get_y_sort_origin();
  3352. return true;
  3353. } else if (components[1] == "z_index") {
  3354. r_property = default_layer->get_z_index();
  3355. return true;
  3356. } else if (components[1] == "navigation_enabled") {
  3357. r_property = default_layer->is_navigation_enabled();
  3358. return true;
  3359. }
  3360. }
  3361. return false;
  3362. }
  3363. Vector2 TileMap::map_to_local(const Vector2i &p_pos) const {
  3364. // SHOULD RETURN THE CENTER OF THE CELL.
  3365. ERR_FAIL_COND_V(!tile_set.is_valid(), Vector2());
  3366. Vector2 ret = p_pos;
  3367. TileSet::TileShape tile_shape = tile_set->get_tile_shape();
  3368. TileSet::TileOffsetAxis tile_offset_axis = tile_set->get_tile_offset_axis();
  3369. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3370. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  3371. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap.
  3372. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3373. switch (tile_set->get_tile_layout()) {
  3374. case TileSet::TILE_LAYOUT_STACKED:
  3375. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 0 ? 0.0 : 0.5), ret.y);
  3376. break;
  3377. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  3378. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 1 ? 0.0 : 0.5), ret.y);
  3379. break;
  3380. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  3381. ret = Vector2(ret.x + ret.y / 2, ret.y);
  3382. break;
  3383. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  3384. ret = Vector2(ret.x / 2, ret.y * 2 + ret.x);
  3385. break;
  3386. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3387. ret = Vector2((ret.x + ret.y) / 2, ret.y - ret.x);
  3388. break;
  3389. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  3390. ret = Vector2((ret.x - ret.y) / 2, ret.y + ret.x);
  3391. break;
  3392. }
  3393. } else { // TILE_OFFSET_AXIS_VERTICAL.
  3394. switch (tile_set->get_tile_layout()) {
  3395. case TileSet::TILE_LAYOUT_STACKED:
  3396. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 0 ? 0.0 : 0.5));
  3397. break;
  3398. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  3399. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 1 ? 0.0 : 0.5));
  3400. break;
  3401. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  3402. ret = Vector2(ret.x * 2 + ret.y, ret.y / 2);
  3403. break;
  3404. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  3405. ret = Vector2(ret.x, ret.y + ret.x / 2);
  3406. break;
  3407. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3408. ret = Vector2(ret.x + ret.y, (ret.y - ret.x) / 2);
  3409. break;
  3410. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  3411. ret = Vector2(ret.x - ret.y, (ret.y + ret.x) / 2);
  3412. break;
  3413. }
  3414. }
  3415. }
  3416. // Multiply by the overlapping ratio.
  3417. double overlapping_ratio = 1.0;
  3418. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3419. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3420. overlapping_ratio = 0.5;
  3421. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  3422. overlapping_ratio = 0.75;
  3423. }
  3424. ret.y *= overlapping_ratio;
  3425. } else { // TILE_OFFSET_AXIS_VERTICAL.
  3426. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3427. overlapping_ratio = 0.5;
  3428. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  3429. overlapping_ratio = 0.75;
  3430. }
  3431. ret.x *= overlapping_ratio;
  3432. }
  3433. return (ret + Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  3434. }
  3435. Vector2i TileMap::local_to_map(const Vector2 &p_local_position) const {
  3436. ERR_FAIL_COND_V(!tile_set.is_valid(), Vector2i());
  3437. Vector2 ret = p_local_position;
  3438. ret /= tile_set->get_tile_size();
  3439. TileSet::TileShape tile_shape = tile_set->get_tile_shape();
  3440. TileSet::TileOffsetAxis tile_offset_axis = tile_set->get_tile_offset_axis();
  3441. TileSet::TileLayout tile_layout = tile_set->get_tile_layout();
  3442. // Divide by the overlapping ratio.
  3443. double overlapping_ratio = 1.0;
  3444. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3445. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3446. overlapping_ratio = 0.5;
  3447. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  3448. overlapping_ratio = 0.75;
  3449. }
  3450. ret.y /= overlapping_ratio;
  3451. } else { // TILE_OFFSET_AXIS_VERTICAL.
  3452. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3453. overlapping_ratio = 0.5;
  3454. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  3455. overlapping_ratio = 0.75;
  3456. }
  3457. ret.x /= overlapping_ratio;
  3458. }
  3459. // For each half-offset shape, we check if we are in the corner of the tile, and thus should correct the local position accordingly.
  3460. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3461. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  3462. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap.
  3463. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3464. // Smart floor of the position
  3465. Vector2 raw_pos = ret;
  3466. if (Math::posmod(Math::floor(ret.y), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  3467. ret = Vector2(Math::floor(ret.x + 0.5) - 0.5, Math::floor(ret.y));
  3468. } else {
  3469. ret = ret.floor();
  3470. }
  3471. // Compute the tile offset, and if we might the output for a neighbor top tile.
  3472. Vector2 in_tile_pos = raw_pos - ret;
  3473. bool in_top_left_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(-0.5, 1.0 / overlapping_ratio - 1)) <= 0;
  3474. bool in_top_right_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(0.5, 1.0 / overlapping_ratio - 1)) > 0;
  3475. switch (tile_layout) {
  3476. case TileSet::TILE_LAYOUT_STACKED:
  3477. ret = ret.floor();
  3478. if (in_top_left_triangle) {
  3479. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : -1, -1);
  3480. } else if (in_top_right_triangle) {
  3481. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 1 : 0, -1);
  3482. }
  3483. break;
  3484. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  3485. ret = ret.floor();
  3486. if (in_top_left_triangle) {
  3487. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? -1 : 0, -1);
  3488. } else if (in_top_right_triangle) {
  3489. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : 1, -1);
  3490. }
  3491. break;
  3492. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  3493. ret = Vector2(ret.x - ret.y / 2, ret.y).floor();
  3494. if (in_top_left_triangle) {
  3495. ret += Vector2i(0, -1);
  3496. } else if (in_top_right_triangle) {
  3497. ret += Vector2i(1, -1);
  3498. }
  3499. break;
  3500. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  3501. ret = Vector2(ret.x * 2, ret.y / 2 - ret.x).floor();
  3502. if (in_top_left_triangle) {
  3503. ret += Vector2i(-1, 0);
  3504. } else if (in_top_right_triangle) {
  3505. ret += Vector2i(1, -1);
  3506. }
  3507. break;
  3508. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3509. ret = Vector2(ret.x - ret.y / 2, ret.y / 2 + ret.x).floor();
  3510. if (in_top_left_triangle) {
  3511. ret += Vector2i(0, -1);
  3512. } else if (in_top_right_triangle) {
  3513. ret += Vector2i(1, 0);
  3514. }
  3515. break;
  3516. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  3517. ret = Vector2(ret.x + ret.y / 2, ret.y / 2 - ret.x).floor();
  3518. if (in_top_left_triangle) {
  3519. ret += Vector2i(-1, 0);
  3520. } else if (in_top_right_triangle) {
  3521. ret += Vector2i(0, -1);
  3522. }
  3523. break;
  3524. }
  3525. } else { // TILE_OFFSET_AXIS_VERTICAL.
  3526. // Smart floor of the position.
  3527. Vector2 raw_pos = ret;
  3528. if (Math::posmod(Math::floor(ret.x), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  3529. ret = Vector2(Math::floor(ret.x), Math::floor(ret.y + 0.5) - 0.5);
  3530. } else {
  3531. ret = ret.floor();
  3532. }
  3533. // Compute the tile offset, and if we might the output for a neighbor top tile.
  3534. Vector2 in_tile_pos = raw_pos - ret;
  3535. bool in_top_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, -0.5)) > 0;
  3536. bool in_bottom_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, 0.5)) <= 0;
  3537. switch (tile_layout) {
  3538. case TileSet::TILE_LAYOUT_STACKED:
  3539. ret = ret.floor();
  3540. if (in_top_left_triangle) {
  3541. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : -1);
  3542. } else if (in_bottom_left_triangle) {
  3543. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 1 : 0);
  3544. }
  3545. break;
  3546. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  3547. ret = ret.floor();
  3548. if (in_top_left_triangle) {
  3549. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? -1 : 0);
  3550. } else if (in_bottom_left_triangle) {
  3551. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : 1);
  3552. }
  3553. break;
  3554. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  3555. ret = Vector2(ret.x / 2 - ret.y, ret.y * 2).floor();
  3556. if (in_top_left_triangle) {
  3557. ret += Vector2i(0, -1);
  3558. } else if (in_bottom_left_triangle) {
  3559. ret += Vector2i(-1, 1);
  3560. }
  3561. break;
  3562. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  3563. ret = Vector2(ret.x, ret.y - ret.x / 2).floor();
  3564. if (in_top_left_triangle) {
  3565. ret += Vector2i(-1, 0);
  3566. } else if (in_bottom_left_triangle) {
  3567. ret += Vector2i(-1, 1);
  3568. }
  3569. break;
  3570. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3571. ret = Vector2(ret.x / 2 - ret.y, ret.y + ret.x / 2).floor();
  3572. if (in_top_left_triangle) {
  3573. ret += Vector2i(0, -1);
  3574. } else if (in_bottom_left_triangle) {
  3575. ret += Vector2i(-1, 0);
  3576. }
  3577. break;
  3578. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  3579. ret = Vector2(ret.x / 2 + ret.y, ret.y - ret.x / 2).floor();
  3580. if (in_top_left_triangle) {
  3581. ret += Vector2i(-1, 0);
  3582. } else if (in_bottom_left_triangle) {
  3583. ret += Vector2i(0, 1);
  3584. }
  3585. break;
  3586. }
  3587. }
  3588. } else {
  3589. ret = (ret + Vector2(0.00005, 0.00005)).floor();
  3590. }
  3591. return Vector2i(ret);
  3592. }
  3593. bool TileMap::is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const {
  3594. ERR_FAIL_COND_V(!tile_set.is_valid(), false);
  3595. TileSet::TileShape shape = tile_set->get_tile_shape();
  3596. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3597. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  3598. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  3599. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  3600. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  3601. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  3602. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  3603. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  3604. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  3605. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3606. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  3607. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  3608. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  3609. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  3610. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  3611. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  3612. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  3613. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  3614. } else {
  3615. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3616. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  3617. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  3618. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  3619. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  3620. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  3621. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  3622. } else {
  3623. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  3624. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  3625. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  3626. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  3627. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  3628. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  3629. }
  3630. }
  3631. }
  3632. Vector2i TileMap::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  3633. ERR_FAIL_COND_V(!tile_set.is_valid(), p_coords);
  3634. TileSet::TileShape shape = tile_set->get_tile_shape();
  3635. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3636. switch (p_cell_neighbor) {
  3637. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  3638. return p_coords + Vector2i(1, 0);
  3639. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  3640. return p_coords + Vector2i(1, 1);
  3641. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  3642. return p_coords + Vector2i(0, 1);
  3643. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  3644. return p_coords + Vector2i(-1, 1);
  3645. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  3646. return p_coords + Vector2i(-1, 0);
  3647. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  3648. return p_coords + Vector2i(-1, -1);
  3649. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  3650. return p_coords + Vector2i(0, -1);
  3651. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  3652. return p_coords + Vector2i(1, -1);
  3653. default:
  3654. ERR_FAIL_V(p_coords);
  3655. }
  3656. } else { // Half-offset shapes (square and hexagon).
  3657. switch (tile_set->get_tile_layout()) {
  3658. case TileSet::TILE_LAYOUT_STACKED: {
  3659. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3660. bool is_offset = p_coords.y % 2;
  3661. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3662. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3663. return p_coords + Vector2i(1, 0);
  3664. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3665. return p_coords + Vector2i(is_offset ? 1 : 0, 1);
  3666. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3667. return p_coords + Vector2i(0, 2);
  3668. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3669. return p_coords + Vector2i(is_offset ? 0 : -1, 1);
  3670. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3671. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3672. return p_coords + Vector2i(-1, 0);
  3673. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3674. return p_coords + Vector2i(is_offset ? 0 : -1, -1);
  3675. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3676. return p_coords + Vector2i(0, -2);
  3677. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3678. return p_coords + Vector2i(is_offset ? 1 : 0, -1);
  3679. } else {
  3680. ERR_FAIL_V(p_coords);
  3681. }
  3682. } else {
  3683. bool is_offset = p_coords.x % 2;
  3684. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3685. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3686. return p_coords + Vector2i(0, 1);
  3687. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3688. return p_coords + Vector2i(1, is_offset ? 1 : 0);
  3689. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3690. return p_coords + Vector2i(2, 0);
  3691. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3692. return p_coords + Vector2i(1, is_offset ? 0 : -1);
  3693. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3694. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3695. return p_coords + Vector2i(0, -1);
  3696. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3697. return p_coords + Vector2i(-1, is_offset ? 0 : -1);
  3698. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3699. return p_coords + Vector2i(-2, 0);
  3700. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3701. return p_coords + Vector2i(-1, is_offset ? 1 : 0);
  3702. } else {
  3703. ERR_FAIL_V(p_coords);
  3704. }
  3705. }
  3706. } break;
  3707. case TileSet::TILE_LAYOUT_STACKED_OFFSET: {
  3708. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3709. bool is_offset = p_coords.y % 2;
  3710. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3711. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3712. return p_coords + Vector2i(1, 0);
  3713. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3714. return p_coords + Vector2i(is_offset ? 0 : 1, 1);
  3715. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3716. return p_coords + Vector2i(0, 2);
  3717. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3718. return p_coords + Vector2i(is_offset ? -1 : 0, 1);
  3719. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3720. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3721. return p_coords + Vector2i(-1, 0);
  3722. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3723. return p_coords + Vector2i(is_offset ? -1 : 0, -1);
  3724. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3725. return p_coords + Vector2i(0, -2);
  3726. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3727. return p_coords + Vector2i(is_offset ? 0 : 1, -1);
  3728. } else {
  3729. ERR_FAIL_V(p_coords);
  3730. }
  3731. } else {
  3732. bool is_offset = p_coords.x % 2;
  3733. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3734. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3735. return p_coords + Vector2i(0, 1);
  3736. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3737. return p_coords + Vector2i(1, is_offset ? 0 : 1);
  3738. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3739. return p_coords + Vector2i(2, 0);
  3740. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3741. return p_coords + Vector2i(1, is_offset ? -1 : 0);
  3742. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3743. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3744. return p_coords + Vector2i(0, -1);
  3745. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3746. return p_coords + Vector2i(-1, is_offset ? -1 : 0);
  3747. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3748. return p_coords + Vector2i(-2, 0);
  3749. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3750. return p_coords + Vector2i(-1, is_offset ? 0 : 1);
  3751. } else {
  3752. ERR_FAIL_V(p_coords);
  3753. }
  3754. }
  3755. } break;
  3756. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  3757. case TileSet::TILE_LAYOUT_STAIRS_DOWN: {
  3758. if ((tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  3759. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3760. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3761. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3762. return p_coords + Vector2i(1, 0);
  3763. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3764. return p_coords + Vector2i(0, 1);
  3765. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3766. return p_coords + Vector2i(-1, 2);
  3767. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3768. return p_coords + Vector2i(-1, 1);
  3769. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3770. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3771. return p_coords + Vector2i(-1, 0);
  3772. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3773. return p_coords + Vector2i(0, -1);
  3774. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3775. return p_coords + Vector2i(1, -2);
  3776. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3777. return p_coords + Vector2i(1, -1);
  3778. } else {
  3779. ERR_FAIL_V(p_coords);
  3780. }
  3781. } else {
  3782. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3783. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3784. return p_coords + Vector2i(0, 1);
  3785. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3786. return p_coords + Vector2i(1, 0);
  3787. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3788. return p_coords + Vector2i(2, -1);
  3789. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3790. return p_coords + Vector2i(1, -1);
  3791. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3792. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3793. return p_coords + Vector2i(0, -1);
  3794. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3795. return p_coords + Vector2i(-1, 0);
  3796. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3797. return p_coords + Vector2i(-2, 1);
  3798. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3799. return p_coords + Vector2i(-1, 1);
  3800. } else {
  3801. ERR_FAIL_V(p_coords);
  3802. }
  3803. }
  3804. } else {
  3805. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3806. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3807. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3808. return p_coords + Vector2i(2, -1);
  3809. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3810. return p_coords + Vector2i(1, 0);
  3811. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3812. return p_coords + Vector2i(0, 1);
  3813. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3814. return p_coords + Vector2i(-1, 1);
  3815. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3816. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3817. return p_coords + Vector2i(-2, 1);
  3818. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3819. return p_coords + Vector2i(-1, 0);
  3820. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3821. return p_coords + Vector2i(0, -1);
  3822. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3823. return p_coords + Vector2i(1, -1);
  3824. } else {
  3825. ERR_FAIL_V(p_coords);
  3826. }
  3827. } else {
  3828. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3829. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3830. return p_coords + Vector2i(-1, 2);
  3831. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3832. return p_coords + Vector2i(0, 1);
  3833. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3834. return p_coords + Vector2i(1, 0);
  3835. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3836. return p_coords + Vector2i(1, -1);
  3837. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3838. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3839. return p_coords + Vector2i(1, -2);
  3840. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3841. return p_coords + Vector2i(0, -1);
  3842. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3843. return p_coords + Vector2i(-1, 0);
  3844. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3845. return p_coords + Vector2i(-1, 1);
  3846. } else {
  3847. ERR_FAIL_V(p_coords);
  3848. }
  3849. }
  3850. }
  3851. } break;
  3852. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3853. case TileSet::TILE_LAYOUT_DIAMOND_DOWN: {
  3854. if ((tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  3855. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3856. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3857. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3858. return p_coords + Vector2i(1, 1);
  3859. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3860. return p_coords + Vector2i(0, 1);
  3861. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3862. return p_coords + Vector2i(-1, 1);
  3863. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3864. return p_coords + Vector2i(-1, 0);
  3865. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3866. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3867. return p_coords + Vector2i(-1, -1);
  3868. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3869. return p_coords + Vector2i(0, -1);
  3870. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3871. return p_coords + Vector2i(1, -1);
  3872. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3873. return p_coords + Vector2i(1, 0);
  3874. } else {
  3875. ERR_FAIL_V(p_coords);
  3876. }
  3877. } else {
  3878. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3879. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3880. return p_coords + Vector2i(1, 1);
  3881. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3882. return p_coords + Vector2i(1, 0);
  3883. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3884. return p_coords + Vector2i(1, -1);
  3885. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3886. return p_coords + Vector2i(0, -1);
  3887. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3888. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3889. return p_coords + Vector2i(-1, -1);
  3890. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3891. return p_coords + Vector2i(-1, 0);
  3892. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3893. return p_coords + Vector2i(-1, 1);
  3894. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3895. return p_coords + Vector2i(0, 1);
  3896. } else {
  3897. ERR_FAIL_V(p_coords);
  3898. }
  3899. }
  3900. } else {
  3901. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3902. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3903. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3904. return p_coords + Vector2i(1, -1);
  3905. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3906. return p_coords + Vector2i(1, 0);
  3907. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3908. return p_coords + Vector2i(1, 1);
  3909. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3910. return p_coords + Vector2i(0, 1);
  3911. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3912. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3913. return p_coords + Vector2i(-1, 1);
  3914. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3915. return p_coords + Vector2i(-1, 0);
  3916. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3917. return p_coords + Vector2i(-1, -1);
  3918. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3919. return p_coords + Vector2i(0, -1);
  3920. } else {
  3921. ERR_FAIL_V(p_coords);
  3922. }
  3923. } else {
  3924. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3925. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3926. return p_coords + Vector2i(-1, 1);
  3927. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3928. return p_coords + Vector2i(0, 1);
  3929. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3930. return p_coords + Vector2i(1, 1);
  3931. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3932. return p_coords + Vector2i(1, 0);
  3933. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3934. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3935. return p_coords + Vector2i(1, -1);
  3936. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3937. return p_coords + Vector2i(0, -1);
  3938. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3939. return p_coords + Vector2i(-1, -1);
  3940. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3941. return p_coords + Vector2i(-1, 0);
  3942. } else {
  3943. ERR_FAIL_V(p_coords);
  3944. }
  3945. }
  3946. }
  3947. } break;
  3948. default:
  3949. ERR_FAIL_V(p_coords);
  3950. }
  3951. }
  3952. }
  3953. TypedArray<Vector2i> TileMap::get_used_cells(int p_layer) const {
  3954. TILEMAP_CALL_FOR_LAYER_V(p_layer, TypedArray<Vector2i>(), get_used_cells);
  3955. }
  3956. 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 {
  3957. TILEMAP_CALL_FOR_LAYER_V(p_layer, TypedArray<Vector2i>(), get_used_cells_by_id, p_source_id, p_atlas_coords, p_alternative_tile);
  3958. }
  3959. Rect2i TileMap::get_used_rect() const {
  3960. // Return the visible rect of the tilemap.
  3961. bool first = true;
  3962. Rect2i rect = Rect2i();
  3963. for (const Ref<TileMapLayer> &layer : layers) {
  3964. Rect2i layer_rect = layer->get_used_rect();
  3965. if (layer_rect == Rect2i()) {
  3966. continue;
  3967. }
  3968. if (first) {
  3969. rect = layer_rect;
  3970. first = false;
  3971. } else {
  3972. rect = rect.merge(layer_rect);
  3973. }
  3974. }
  3975. return rect;
  3976. }
  3977. // --- Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems ---
  3978. void TileMap::set_light_mask(int p_light_mask) {
  3979. // Occlusion: set light mask.
  3980. CanvasItem::set_light_mask(p_light_mask);
  3981. for (Ref<TileMapLayer> &layer : layers) {
  3982. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_LIGHT_MASK);
  3983. }
  3984. }
  3985. void TileMap::set_material(const Ref<Material> &p_material) {
  3986. // Set material for the whole tilemap.
  3987. CanvasItem::set_material(p_material);
  3988. // Update material for the whole tilemap.
  3989. for (Ref<TileMapLayer> &layer : layers) {
  3990. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_MATERIAL);
  3991. }
  3992. }
  3993. void TileMap::set_use_parent_material(bool p_use_parent_material) {
  3994. // Set use_parent_material for the whole tilemap.
  3995. CanvasItem::set_use_parent_material(p_use_parent_material);
  3996. // Update use_parent_material for the whole tilemap.
  3997. for (Ref<TileMapLayer> &layer : layers) {
  3998. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_USE_PARENT_MATERIAL);
  3999. }
  4000. }
  4001. void TileMap::set_texture_filter(TextureFilter p_texture_filter) {
  4002. // Set a default texture filter for the whole tilemap.
  4003. CanvasItem::set_texture_filter(p_texture_filter);
  4004. for (Ref<TileMapLayer> &layer : layers) {
  4005. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_TEXTURE_FILTER);
  4006. }
  4007. }
  4008. void TileMap::set_texture_repeat(CanvasItem::TextureRepeat p_texture_repeat) {
  4009. // Set a default texture repeat for the whole tilemap.
  4010. CanvasItem::set_texture_repeat(p_texture_repeat);
  4011. for (Ref<TileMapLayer> &layer : layers) {
  4012. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_TEXTURE_REPEAT);
  4013. }
  4014. }
  4015. TypedArray<Vector2i> TileMap::get_surrounding_cells(const Vector2i &coords) {
  4016. if (!tile_set.is_valid()) {
  4017. return TypedArray<Vector2i>();
  4018. }
  4019. TypedArray<Vector2i> around;
  4020. TileSet::TileShape shape = tile_set->get_tile_shape();
  4021. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  4022. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  4023. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  4024. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  4025. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  4026. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  4027. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  4028. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  4029. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  4030. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  4031. } else {
  4032. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  4033. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  4034. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  4035. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  4036. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  4037. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  4038. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  4039. } else {
  4040. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  4041. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  4042. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  4043. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  4044. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  4045. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  4046. }
  4047. }
  4048. return around;
  4049. }
  4050. void TileMap::draw_cells_outline(Control *p_control, const RBSet<Vector2i> &p_cells, Color p_color, Transform2D p_transform) {
  4051. if (!tile_set.is_valid()) {
  4052. return;
  4053. }
  4054. // Create a set.
  4055. Vector2i tile_size = tile_set->get_tile_size();
  4056. Vector<Vector2> polygon = tile_set->get_tile_shape_polygon();
  4057. TileSet::TileShape shape = tile_set->get_tile_shape();
  4058. for (const Vector2i &E : p_cells) {
  4059. Vector2 center = map_to_local(E);
  4060. #define DRAW_SIDE_IF_NEEDED(side, polygon_index_from, polygon_index_to) \
  4061. if (!p_cells.has(get_neighbor_cell(E, side))) { \
  4062. Vector2 from = p_transform.xform(center + polygon[polygon_index_from] * tile_size); \
  4063. Vector2 to = p_transform.xform(center + polygon[polygon_index_to] * tile_size); \
  4064. p_control->draw_line(from, to, p_color); \
  4065. }
  4066. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  4067. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 1, 2);
  4068. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 2, 3);
  4069. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 3, 0);
  4070. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 0, 1);
  4071. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  4072. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 2, 3);
  4073. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 1, 2);
  4074. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  4075. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 3, 0);
  4076. } else {
  4077. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  4078. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  4079. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 2, 3);
  4080. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 1, 2);
  4081. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  4082. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 5, 0);
  4083. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 4, 5);
  4084. } else {
  4085. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  4086. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 4, 5);
  4087. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 5, 0);
  4088. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  4089. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 1, 2);
  4090. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 2, 3);
  4091. }
  4092. }
  4093. }
  4094. #undef DRAW_SIDE_IF_NEEDED
  4095. }
  4096. Ref<Resource> TileMap::get_transformed_polygon(Ref<Resource> p_polygon, int p_alternative_id) {
  4097. if (!bool(p_alternative_id & (TileSetAtlasSource::TRANSFORM_FLIP_H | TileSetAtlasSource::TRANSFORM_FLIP_V | TileSetAtlasSource::TRANSFORM_TRANSPOSE))) {
  4098. return p_polygon;
  4099. }
  4100. {
  4101. HashMap<Pair<Ref<Resource>, int>, Ref<Resource>, PairHash<Ref<Resource>, int>>::Iterator E = polygon_cache.find(Pair<Ref<Resource>, int>(p_polygon, p_alternative_id));
  4102. if (E) {
  4103. return E->value;
  4104. }
  4105. }
  4106. Ref<ConvexPolygonShape2D> col = p_polygon;
  4107. if (col.is_valid()) {
  4108. Ref<ConvexPolygonShape2D> ret;
  4109. ret.instantiate();
  4110. ret->set_points(_get_transformed_vertices(col->get_points(), p_alternative_id));
  4111. polygon_cache[Pair<Ref<Resource>, int>(p_polygon, p_alternative_id)] = ret;
  4112. return ret;
  4113. }
  4114. Ref<NavigationPolygon> nav = p_polygon;
  4115. if (nav.is_valid()) {
  4116. PackedVector2Array new_points = _get_transformed_vertices(nav->get_vertices(), p_alternative_id);
  4117. Ref<NavigationPolygon> ret;
  4118. ret.instantiate();
  4119. ret->set_vertices(new_points);
  4120. PackedInt32Array indices;
  4121. indices.resize(new_points.size());
  4122. int *w = indices.ptrw();
  4123. for (int i = 0; i < new_points.size(); i++) {
  4124. w[i] = i;
  4125. }
  4126. ret->add_polygon(indices);
  4127. polygon_cache[Pair<Ref<Resource>, int>(p_polygon, p_alternative_id)] = ret;
  4128. return ret;
  4129. }
  4130. Ref<OccluderPolygon2D> ocd = p_polygon;
  4131. if (ocd.is_valid()) {
  4132. Ref<OccluderPolygon2D> ret;
  4133. ret.instantiate();
  4134. ret->set_polygon(_get_transformed_vertices(ocd->get_polygon(), p_alternative_id));
  4135. polygon_cache[Pair<Ref<Resource>, int>(p_polygon, p_alternative_id)] = ret;
  4136. return ret;
  4137. }
  4138. return p_polygon;
  4139. }
  4140. PackedStringArray TileMap::get_configuration_warnings() const {
  4141. PackedStringArray warnings = Node::get_configuration_warnings();
  4142. // Retrieve the set of Z index values with a Y-sorted layer.
  4143. RBSet<int> y_sorted_z_index;
  4144. for (const Ref<TileMapLayer> &layer : layers) {
  4145. if (layer->is_y_sort_enabled()) {
  4146. y_sorted_z_index.insert(layer->get_z_index());
  4147. }
  4148. }
  4149. // Check if we have a non-sorted layer in a Z-index with a Y-sorted layer.
  4150. for (const Ref<TileMapLayer> &layer : layers) {
  4151. if (!layer->is_y_sort_enabled() && y_sorted_z_index.has(layer->get_z_index())) {
  4152. 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."));
  4153. break;
  4154. }
  4155. }
  4156. if (!is_y_sort_enabled()) {
  4157. // Check if Y-sort is enabled on a layer but not on the node.
  4158. for (const Ref<TileMapLayer> &layer : layers) {
  4159. if (layer->is_y_sort_enabled()) {
  4160. warnings.push_back(RTR("A TileMap layer is set as Y-sorted, but Y-sort is not enabled on the TileMap node itself."));
  4161. break;
  4162. }
  4163. }
  4164. } else {
  4165. // Check if Y-sort is enabled on the node, but not on any of the layers.
  4166. bool need_warning = true;
  4167. for (const Ref<TileMapLayer> &layer : layers) {
  4168. if (layer->is_y_sort_enabled()) {
  4169. need_warning = false;
  4170. break;
  4171. }
  4172. }
  4173. if (need_warning) {
  4174. 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."));
  4175. }
  4176. }
  4177. // Check if we are in isometric mode without Y-sort enabled.
  4178. if (tile_set.is_valid() && tile_set->get_tile_shape() == TileSet::TILE_SHAPE_ISOMETRIC) {
  4179. bool warn = !is_y_sort_enabled();
  4180. if (!warn) {
  4181. for (const Ref<TileMapLayer> &layer : layers) {
  4182. if (!layer->is_y_sort_enabled()) {
  4183. warn = true;
  4184. break;
  4185. }
  4186. }
  4187. }
  4188. if (warn) {
  4189. warnings.push_back(RTR("Isometric TileSet will likely not look as intended without Y-sort enabled for the TileMap and all of its layers."));
  4190. }
  4191. }
  4192. return warnings;
  4193. }
  4194. void TileMap::_bind_methods() {
  4195. #ifndef DISABLE_DEPRECATED
  4196. ClassDB::bind_method(D_METHOD("set_navigation_map", "layer", "map"), &TileMap::set_layer_navigation_map);
  4197. ClassDB::bind_method(D_METHOD("get_navigation_map", "layer"), &TileMap::get_layer_navigation_map);
  4198. ClassDB::bind_method(D_METHOD("force_update", "layer"), &TileMap::force_update, DEFVAL(-1));
  4199. #endif // DISABLE_DEPRECATED
  4200. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  4201. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  4202. ClassDB::bind_method(D_METHOD("set_rendering_quadrant_size", "size"), &TileMap::set_rendering_quadrant_size);
  4203. ClassDB::bind_method(D_METHOD("get_rendering_quadrant_size"), &TileMap::get_rendering_quadrant_size);
  4204. ClassDB::bind_method(D_METHOD("get_layers_count"), &TileMap::get_layers_count);
  4205. ClassDB::bind_method(D_METHOD("add_layer", "to_position"), &TileMap::add_layer);
  4206. ClassDB::bind_method(D_METHOD("move_layer", "layer", "to_position"), &TileMap::move_layer);
  4207. ClassDB::bind_method(D_METHOD("remove_layer", "layer"), &TileMap::remove_layer);
  4208. ClassDB::bind_method(D_METHOD("set_layer_name", "layer", "name"), &TileMap::set_layer_name);
  4209. ClassDB::bind_method(D_METHOD("get_layer_name", "layer"), &TileMap::get_layer_name);
  4210. ClassDB::bind_method(D_METHOD("set_layer_enabled", "layer", "enabled"), &TileMap::set_layer_enabled);
  4211. ClassDB::bind_method(D_METHOD("is_layer_enabled", "layer"), &TileMap::is_layer_enabled);
  4212. ClassDB::bind_method(D_METHOD("set_layer_modulate", "layer", "modulate"), &TileMap::set_layer_modulate);
  4213. ClassDB::bind_method(D_METHOD("get_layer_modulate", "layer"), &TileMap::get_layer_modulate);
  4214. ClassDB::bind_method(D_METHOD("set_layer_y_sort_enabled", "layer", "y_sort_enabled"), &TileMap::set_layer_y_sort_enabled);
  4215. ClassDB::bind_method(D_METHOD("is_layer_y_sort_enabled", "layer"), &TileMap::is_layer_y_sort_enabled);
  4216. ClassDB::bind_method(D_METHOD("set_layer_y_sort_origin", "layer", "y_sort_origin"), &TileMap::set_layer_y_sort_origin);
  4217. ClassDB::bind_method(D_METHOD("get_layer_y_sort_origin", "layer"), &TileMap::get_layer_y_sort_origin);
  4218. ClassDB::bind_method(D_METHOD("set_layer_z_index", "layer", "z_index"), &TileMap::set_layer_z_index);
  4219. ClassDB::bind_method(D_METHOD("get_layer_z_index", "layer"), &TileMap::get_layer_z_index);
  4220. ClassDB::bind_method(D_METHOD("set_layer_navigation_enabled", "layer", "enabled"), &TileMap::set_layer_navigation_enabled);
  4221. ClassDB::bind_method(D_METHOD("is_layer_navigation_enabled", "layer"), &TileMap::is_layer_navigation_enabled);
  4222. ClassDB::bind_method(D_METHOD("set_layer_navigation_map", "layer", "map"), &TileMap::set_layer_navigation_map);
  4223. ClassDB::bind_method(D_METHOD("get_layer_navigation_map", "layer"), &TileMap::get_layer_navigation_map);
  4224. ClassDB::bind_method(D_METHOD("set_collision_animatable", "enabled"), &TileMap::set_collision_animatable);
  4225. ClassDB::bind_method(D_METHOD("is_collision_animatable"), &TileMap::is_collision_animatable);
  4226. ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "collision_visibility_mode"), &TileMap::set_collision_visibility_mode);
  4227. ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMap::get_collision_visibility_mode);
  4228. ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "navigation_visibility_mode"), &TileMap::set_navigation_visibility_mode);
  4229. ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode);
  4230. 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));
  4231. ClassDB::bind_method(D_METHOD("erase_cell", "layer", "coords"), &TileMap::erase_cell);
  4232. ClassDB::bind_method(D_METHOD("get_cell_source_id", "layer", "coords", "use_proxies"), &TileMap::get_cell_source_id, DEFVAL(false));
  4233. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "layer", "coords", "use_proxies"), &TileMap::get_cell_atlas_coords, DEFVAL(false));
  4234. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "layer", "coords", "use_proxies"), &TileMap::get_cell_alternative_tile, DEFVAL(false));
  4235. ClassDB::bind_method(D_METHOD("get_cell_tile_data", "layer", "coords", "use_proxies"), &TileMap::get_cell_tile_data, DEFVAL(false));
  4236. ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMap::get_coords_for_body_rid);
  4237. ClassDB::bind_method(D_METHOD("get_layer_for_body_rid", "body"), &TileMap::get_layer_for_body_rid);
  4238. ClassDB::bind_method(D_METHOD("get_pattern", "layer", "coords_array"), &TileMap::get_pattern);
  4239. ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMap::map_pattern);
  4240. ClassDB::bind_method(D_METHOD("set_pattern", "layer", "position", "pattern"), &TileMap::set_pattern);
  4241. ClassDB::bind_method(D_METHOD("set_cells_terrain_connect", "layer", "cells", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_connect, DEFVAL(true));
  4242. ClassDB::bind_method(D_METHOD("set_cells_terrain_path", "layer", "path", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_path, DEFVAL(true));
  4243. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
  4244. ClassDB::bind_method(D_METHOD("clear_layer", "layer"), &TileMap::clear_layer);
  4245. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  4246. ClassDB::bind_method(D_METHOD("update_internals"), &TileMap::update_internals);
  4247. ClassDB::bind_method(D_METHOD("notify_runtime_tile_data_update", "layer"), &TileMap::notify_runtime_tile_data_update, DEFVAL(-1));
  4248. ClassDB::bind_method(D_METHOD("get_surrounding_cells", "coords"), &TileMap::get_surrounding_cells);
  4249. ClassDB::bind_method(D_METHOD("get_used_cells", "layer"), &TileMap::get_used_cells);
  4250. 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));
  4251. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  4252. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &TileMap::map_to_local);
  4253. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &TileMap::local_to_map);
  4254. ClassDB::bind_method(D_METHOD("get_neighbor_cell", "coords", "neighbor"), &TileMap::get_neighbor_cell);
  4255. GDVIRTUAL_BIND(_use_tile_data_runtime_update, "layer", "coords");
  4256. GDVIRTUAL_BIND(_tile_data_runtime_update, "layer", "coords", "tile_data");
  4257. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  4258. ADD_PROPERTY(PropertyInfo(Variant::INT, "rendering_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_rendering_quadrant_size", "get_rendering_quadrant_size");
  4259. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_animatable"), "set_collision_animatable", "is_collision_animatable");
  4260. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
  4261. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
  4262. ADD_ARRAY("layers", "layer_");
  4263. ADD_PROPERTY_DEFAULT("format", TileMapLayer::FORMAT_1);
  4264. ADD_SIGNAL(MethodInfo(CoreStringNames::get_singleton()->changed));
  4265. BIND_ENUM_CONSTANT(VISIBILITY_MODE_DEFAULT);
  4266. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_HIDE);
  4267. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_SHOW);
  4268. }
  4269. void TileMap::_tile_set_changed() {
  4270. emit_signal(CoreStringNames::get_singleton()->changed);
  4271. for (Ref<TileMapLayer> layer : layers) {
  4272. layer->notify_tile_map_change(TileMapLayer::DIRTY_FLAGS_TILE_MAP_TILE_SET);
  4273. }
  4274. update_configuration_warnings();
  4275. }
  4276. void TileMap::_update_notify_local_transform() {
  4277. bool notify = collision_animatable || is_y_sort_enabled();
  4278. if (!notify) {
  4279. for (const Ref<TileMapLayer> &layer : layers) {
  4280. if (layer->is_y_sort_enabled()) {
  4281. notify = true;
  4282. break;
  4283. }
  4284. }
  4285. }
  4286. set_notify_local_transform(notify);
  4287. }
  4288. TileMap::TileMap() {
  4289. set_notify_transform(true);
  4290. _update_notify_local_transform();
  4291. Ref<TileMapLayer> new_layer;
  4292. new_layer.instantiate();
  4293. new_layer->set_tile_map(this);
  4294. new_layer->set_layer_index_in_tile_map_node(0);
  4295. layers.push_back(new_layer);
  4296. default_layer.instantiate();
  4297. }
  4298. TileMap::~TileMap() {
  4299. if (tile_set.is_valid()) {
  4300. tile_set->disconnect_changed(callable_mp(this, &TileMap::_tile_set_changed));
  4301. }
  4302. }
  4303. #undef TILEMAP_CALL_FOR_LAYER
  4304. #undef TILEMAP_CALL_FOR_LAYER_V