tile_map.cpp 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960
  1. /*************************************************************************/
  2. /* tile_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/io/marshalls.h"
  32. #include "scene/resources/world_2d.h"
  33. #include "servers/navigation_server_2d.h"
  34. HashMap<Vector2i, TileSet::CellNeighbor> TileMap::TerrainConstraint::get_overlapping_coords_and_peering_bits() const {
  35. HashMap<Vector2i, TileSet::CellNeighbor> output;
  36. ERR_FAIL_COND_V(is_center_bit(), output);
  37. Ref<TileSet> tile_set = tile_map->get_tileset();
  38. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  39. TileSet::TileShape shape = tile_set->get_tile_shape();
  40. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  41. switch (bit) {
  42. case 1:
  43. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  44. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  45. break;
  46. case 2:
  47. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  48. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  49. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  50. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  51. break;
  52. case 3:
  53. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  54. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  55. break;
  56. default:
  57. ERR_FAIL_V(output);
  58. }
  59. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  60. switch (bit) {
  61. case 1:
  62. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  63. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  64. break;
  65. case 2:
  66. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  67. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  68. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  69. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  70. break;
  71. case 3:
  72. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  73. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  74. break;
  75. default:
  76. ERR_FAIL_V(output);
  77. }
  78. } else {
  79. // Half offset shapes.
  80. TileSet::TileOffsetAxis offset_axis = tile_set->get_tile_offset_axis();
  81. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  82. switch (bit) {
  83. case 1:
  84. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  85. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  86. break;
  87. case 2:
  88. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  89. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  90. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  91. break;
  92. case 3:
  93. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  94. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  95. break;
  96. case 4:
  97. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  98. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  99. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  100. break;
  101. case 5:
  102. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  103. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  104. break;
  105. default:
  106. ERR_FAIL_V(output);
  107. }
  108. } else {
  109. switch (bit) {
  110. case 1:
  111. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  112. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  113. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  114. break;
  115. case 2:
  116. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  117. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  118. break;
  119. case 3:
  120. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  121. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  122. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  123. break;
  124. case 4:
  125. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  126. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  127. break;
  128. case 5:
  129. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  130. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  131. break;
  132. default:
  133. ERR_FAIL_V(output);
  134. }
  135. }
  136. }
  137. return output;
  138. }
  139. TileMap::TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, int p_terrain) {
  140. tile_map = p_tile_map;
  141. Ref<TileSet> tile_set = tile_map->get_tileset();
  142. ERR_FAIL_COND(!tile_set.is_valid());
  143. bit = 0;
  144. base_cell_coords = p_position;
  145. terrain = p_terrain;
  146. }
  147. TileMap::TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, const TileSet::CellNeighbor &p_bit, int p_terrain) {
  148. // The way we build the constraint make it easy to detect conflicting constraints.
  149. tile_map = p_tile_map;
  150. Ref<TileSet> tile_set = tile_map->get_tileset();
  151. ERR_FAIL_COND(!tile_set.is_valid());
  152. TileSet::TileShape shape = tile_set->get_tile_shape();
  153. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  154. switch (p_bit) {
  155. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  156. bit = 1;
  157. base_cell_coords = p_position;
  158. break;
  159. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  160. bit = 2;
  161. base_cell_coords = p_position;
  162. break;
  163. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  164. bit = 3;
  165. base_cell_coords = p_position;
  166. break;
  167. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  168. bit = 2;
  169. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  170. break;
  171. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  172. bit = 1;
  173. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  174. break;
  175. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  176. bit = 2;
  177. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER);
  178. break;
  179. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  180. bit = 3;
  181. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  182. break;
  183. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  184. bit = 2;
  185. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  186. break;
  187. default:
  188. ERR_FAIL();
  189. break;
  190. }
  191. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  192. switch (p_bit) {
  193. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  194. bit = 2;
  195. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  196. break;
  197. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  198. bit = 1;
  199. base_cell_coords = p_position;
  200. break;
  201. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  202. bit = 2;
  203. base_cell_coords = p_position;
  204. break;
  205. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  206. bit = 3;
  207. base_cell_coords = p_position;
  208. break;
  209. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  210. bit = 2;
  211. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  212. break;
  213. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  214. bit = 1;
  215. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  216. break;
  217. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  218. bit = 2;
  219. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_CORNER);
  220. break;
  221. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  222. bit = 3;
  223. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  224. break;
  225. default:
  226. ERR_FAIL();
  227. break;
  228. }
  229. } else {
  230. // Half-offset shapes
  231. TileSet::TileOffsetAxis offset_axis = tile_set->get_tile_offset_axis();
  232. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  233. switch (p_bit) {
  234. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  235. bit = 1;
  236. base_cell_coords = p_position;
  237. break;
  238. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  239. bit = 2;
  240. base_cell_coords = p_position;
  241. break;
  242. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  243. bit = 3;
  244. base_cell_coords = p_position;
  245. break;
  246. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  247. bit = 4;
  248. base_cell_coords = p_position;
  249. break;
  250. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  251. bit = 5;
  252. base_cell_coords = p_position;
  253. break;
  254. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  255. bit = 2;
  256. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  257. break;
  258. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  259. bit = 1;
  260. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  261. break;
  262. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  263. bit = 4;
  264. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  265. break;
  266. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  267. bit = 3;
  268. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  269. break;
  270. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  271. bit = 2;
  272. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  273. break;
  274. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  275. bit = 5;
  276. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  277. break;
  278. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  279. bit = 4;
  280. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  281. break;
  282. default:
  283. ERR_FAIL();
  284. break;
  285. }
  286. } else {
  287. switch (p_bit) {
  288. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  289. bit = 1;
  290. base_cell_coords = p_position;
  291. break;
  292. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  293. bit = 2;
  294. base_cell_coords = p_position;
  295. break;
  296. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  297. bit = 3;
  298. base_cell_coords = p_position;
  299. break;
  300. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  301. bit = 4;
  302. base_cell_coords = p_position;
  303. break;
  304. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  305. bit = 1;
  306. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  307. break;
  308. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  309. bit = 5;
  310. base_cell_coords = p_position;
  311. break;
  312. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  313. bit = 3;
  314. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  315. break;
  316. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  317. bit = 2;
  318. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  319. break;
  320. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  321. bit = 1;
  322. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  323. break;
  324. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  325. bit = 4;
  326. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  327. break;
  328. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  329. bit = 3;
  330. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  331. break;
  332. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  333. bit = 5;
  334. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  335. break;
  336. default:
  337. ERR_FAIL();
  338. break;
  339. }
  340. }
  341. }
  342. terrain = p_terrain;
  343. }
  344. Vector2i TileMap::transform_coords_layout(Vector2i p_coords, TileSet::TileOffsetAxis p_offset_axis, TileSet::TileLayout p_from_layout, TileSet::TileLayout p_to_layout) {
  345. // Transform to stacked layout.
  346. Vector2i output = p_coords;
  347. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  348. SWAP(output.x, output.y);
  349. }
  350. switch (p_from_layout) {
  351. case TileSet::TILE_LAYOUT_STACKED:
  352. break;
  353. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  354. if (output.y % 2) {
  355. output.x -= 1;
  356. }
  357. break;
  358. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  359. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  360. if ((p_from_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  361. if (output.y < 0 && bool(output.y % 2)) {
  362. output = Vector2i(output.x + output.y / 2 - 1, output.y);
  363. } else {
  364. output = Vector2i(output.x + output.y / 2, output.y);
  365. }
  366. } else {
  367. if (output.x < 0 && bool(output.x % 2)) {
  368. output = Vector2i(output.x / 2 - 1, output.x + output.y * 2);
  369. } else {
  370. output = Vector2i(output.x / 2, output.x + output.y * 2);
  371. }
  372. }
  373. break;
  374. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  375. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  376. if ((p_from_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  377. if ((output.x + output.y) < 0 && (output.x - output.y) % 2) {
  378. output = Vector2i((output.x + output.y) / 2 - 1, output.y - output.x);
  379. } else {
  380. output = Vector2i((output.x + output.y) / 2, -output.x + output.y);
  381. }
  382. } else {
  383. if ((output.x - output.y) < 0 && (output.x + output.y) % 2) {
  384. output = Vector2i((output.x - output.y) / 2 - 1, output.x + output.y);
  385. } else {
  386. output = Vector2i((output.x - output.y) / 2, output.x + output.y);
  387. }
  388. }
  389. break;
  390. }
  391. switch (p_to_layout) {
  392. case TileSet::TILE_LAYOUT_STACKED:
  393. break;
  394. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  395. if (output.y % 2) {
  396. output.x += 1;
  397. }
  398. break;
  399. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  400. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  401. if ((p_to_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  402. if (output.y < 0 && (output.y % 2)) {
  403. output = Vector2i(output.x - output.y / 2 + 1, output.y);
  404. } else {
  405. output = Vector2i(output.x - output.y / 2, output.y);
  406. }
  407. } else {
  408. if (output.y % 2) {
  409. if (output.y < 0) {
  410. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2 - 1);
  411. } else {
  412. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2);
  413. }
  414. } else {
  415. output = Vector2i(2 * output.x, -output.x + output.y / 2);
  416. }
  417. }
  418. break;
  419. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  420. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  421. if ((p_to_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  422. if (output.y % 2) {
  423. if (output.y > 0) {
  424. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2 + 1);
  425. } else {
  426. output = Vector2i(output.x - output.y / 2 + 1, output.x + output.y / 2);
  427. }
  428. } else {
  429. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2);
  430. }
  431. } else {
  432. if (output.y % 2) {
  433. if (output.y < 0) {
  434. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2 - 1);
  435. } else {
  436. output = Vector2i(output.x + output.y / 2 + 1, -output.x + output.y / 2);
  437. }
  438. } else {
  439. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2);
  440. }
  441. }
  442. break;
  443. }
  444. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  445. SWAP(output.x, output.y);
  446. }
  447. return output;
  448. }
  449. int TileMap::get_effective_quadrant_size(int p_layer) const {
  450. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), 1);
  451. // When using YSort, the quadrant size is reduced to 1 to have one CanvasItem per quadrant
  452. if (is_y_sort_enabled() && layers[p_layer].y_sort_enabled) {
  453. return 1;
  454. } else {
  455. return quadrant_size;
  456. }
  457. }
  458. void TileMap::set_selected_layer(int p_layer_id) {
  459. ERR_FAIL_COND(p_layer_id < -1 || p_layer_id >= (int)layers.size());
  460. selected_layer = p_layer_id;
  461. emit_signal(SNAME("changed"));
  462. _make_all_quadrants_dirty();
  463. }
  464. int TileMap::get_selected_layer() const {
  465. return selected_layer;
  466. }
  467. void TileMap::_notification(int p_what) {
  468. switch (p_what) {
  469. case NOTIFICATION_ENTER_TREE: {
  470. _clear_internals();
  471. _recreate_internals();
  472. } break;
  473. case NOTIFICATION_EXIT_TREE: {
  474. _clear_internals();
  475. } break;
  476. }
  477. // Transfers the notification to tileset plugins.
  478. if (tile_set.is_valid()) {
  479. _rendering_notification(p_what);
  480. _physics_notification(p_what);
  481. _navigation_notification(p_what);
  482. }
  483. }
  484. Ref<TileSet> TileMap::get_tileset() const {
  485. return tile_set;
  486. }
  487. void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
  488. if (p_tileset == tile_set) {
  489. return;
  490. }
  491. // Set the tileset, registering to its changes.
  492. if (tile_set.is_valid()) {
  493. tile_set->disconnect("changed", callable_mp(this, &TileMap::_tile_set_changed));
  494. }
  495. if (!p_tileset.is_valid()) {
  496. _clear_internals();
  497. }
  498. tile_set = p_tileset;
  499. if (tile_set.is_valid()) {
  500. tile_set->connect("changed", callable_mp(this, &TileMap::_tile_set_changed));
  501. _clear_internals();
  502. _recreate_internals();
  503. }
  504. emit_signal(SNAME("changed"));
  505. }
  506. void TileMap::set_quadrant_size(int p_size) {
  507. ERR_FAIL_COND_MSG(p_size < 1, "TileMapQuadrant size cannot be smaller than 1.");
  508. quadrant_size = p_size;
  509. _clear_internals();
  510. _recreate_internals();
  511. emit_signal(SNAME("changed"));
  512. }
  513. int TileMap::get_quadrant_size() const {
  514. return quadrant_size;
  515. }
  516. int TileMap::get_layers_count() const {
  517. return layers.size();
  518. }
  519. void TileMap::add_layer(int p_to_pos) {
  520. if (p_to_pos < 0) {
  521. p_to_pos = layers.size() + p_to_pos + 1;
  522. }
  523. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  524. // Must clear before adding the layer.
  525. _clear_internals();
  526. layers.insert(p_to_pos, TileMapLayer());
  527. _recreate_internals();
  528. notify_property_list_changed();
  529. emit_signal(SNAME("changed"));
  530. update_configuration_warnings();
  531. }
  532. void TileMap::move_layer(int p_layer, int p_to_pos) {
  533. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  534. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  535. // Clear before shuffling layers.
  536. _clear_internals();
  537. TileMapLayer tl = layers[p_layer];
  538. layers.insert(p_to_pos, tl);
  539. layers.remove_at(p_to_pos < p_layer ? p_layer + 1 : p_layer);
  540. _recreate_internals();
  541. notify_property_list_changed();
  542. if (selected_layer == p_layer) {
  543. selected_layer = p_to_pos < p_layer ? p_to_pos - 1 : p_to_pos;
  544. }
  545. emit_signal(SNAME("changed"));
  546. update_configuration_warnings();
  547. }
  548. void TileMap::remove_layer(int p_layer) {
  549. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  550. // Clear before removing the layer.
  551. _clear_internals();
  552. layers.remove_at(p_layer);
  553. _recreate_internals();
  554. notify_property_list_changed();
  555. if (selected_layer >= p_layer) {
  556. selected_layer -= 1;
  557. }
  558. emit_signal(SNAME("changed"));
  559. update_configuration_warnings();
  560. }
  561. void TileMap::set_layer_name(int p_layer, String p_name) {
  562. if (p_layer < 0) {
  563. p_layer = layers.size() + p_layer;
  564. }
  565. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  566. layers[p_layer].name = p_name;
  567. emit_signal(SNAME("changed"));
  568. }
  569. String TileMap::get_layer_name(int p_layer) const {
  570. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), String());
  571. return layers[p_layer].name;
  572. }
  573. void TileMap::set_layer_enabled(int p_layer, bool p_enabled) {
  574. if (p_layer < 0) {
  575. p_layer = layers.size() + p_layer;
  576. }
  577. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  578. layers[p_layer].enabled = p_enabled;
  579. _clear_layer_internals(p_layer);
  580. _recreate_layer_internals(p_layer);
  581. emit_signal(SNAME("changed"));
  582. update_configuration_warnings();
  583. }
  584. bool TileMap::is_layer_enabled(int p_layer) const {
  585. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  586. return layers[p_layer].enabled;
  587. }
  588. void TileMap::set_layer_modulate(int p_layer, Color p_modulate) {
  589. if (p_layer < 0) {
  590. p_layer = layers.size() + p_layer;
  591. }
  592. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  593. layers[p_layer].modulate = p_modulate;
  594. _clear_layer_internals(p_layer);
  595. _recreate_layer_internals(p_layer);
  596. emit_signal(SNAME("changed"));
  597. }
  598. Color TileMap::get_layer_modulate(int p_layer) const {
  599. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), Color());
  600. return layers[p_layer].modulate;
  601. }
  602. void TileMap::set_layer_y_sort_enabled(int p_layer, bool p_y_sort_enabled) {
  603. if (p_layer < 0) {
  604. p_layer = layers.size() + p_layer;
  605. }
  606. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  607. layers[p_layer].y_sort_enabled = p_y_sort_enabled;
  608. _clear_layer_internals(p_layer);
  609. _recreate_layer_internals(p_layer);
  610. emit_signal(SNAME("changed"));
  611. update_configuration_warnings();
  612. }
  613. bool TileMap::is_layer_y_sort_enabled(int p_layer) const {
  614. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  615. return layers[p_layer].y_sort_enabled;
  616. }
  617. void TileMap::set_layer_y_sort_origin(int p_layer, int p_y_sort_origin) {
  618. if (p_layer < 0) {
  619. p_layer = layers.size() + p_layer;
  620. }
  621. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  622. layers[p_layer].y_sort_origin = p_y_sort_origin;
  623. _clear_layer_internals(p_layer);
  624. _recreate_layer_internals(p_layer);
  625. emit_signal(SNAME("changed"));
  626. }
  627. int TileMap::get_layer_y_sort_origin(int p_layer) const {
  628. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  629. return layers[p_layer].y_sort_origin;
  630. }
  631. void TileMap::set_layer_z_index(int p_layer, int p_z_index) {
  632. if (p_layer < 0) {
  633. p_layer = layers.size() + p_layer;
  634. }
  635. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  636. layers[p_layer].z_index = p_z_index;
  637. _clear_layer_internals(p_layer);
  638. _recreate_layer_internals(p_layer);
  639. emit_signal(SNAME("changed"));
  640. update_configuration_warnings();
  641. }
  642. int TileMap::get_layer_z_index(int p_layer) const {
  643. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  644. return layers[p_layer].z_index;
  645. }
  646. void TileMap::set_collision_animatable(bool p_enabled) {
  647. collision_animatable = p_enabled;
  648. _clear_internals();
  649. set_notify_local_transform(p_enabled);
  650. set_physics_process_internal(p_enabled);
  651. _recreate_internals();
  652. emit_signal(SNAME("changed"));
  653. }
  654. bool TileMap::is_collision_animatable() const {
  655. return collision_animatable;
  656. }
  657. void TileMap::set_collision_visibility_mode(TileMap::VisibilityMode p_show_collision) {
  658. collision_visibility_mode = p_show_collision;
  659. _clear_internals();
  660. _recreate_internals();
  661. emit_signal(SNAME("changed"));
  662. }
  663. TileMap::VisibilityMode TileMap::get_collision_visibility_mode() {
  664. return collision_visibility_mode;
  665. }
  666. void TileMap::set_navigation_visibility_mode(TileMap::VisibilityMode p_show_navigation) {
  667. navigation_visibility_mode = p_show_navigation;
  668. _clear_internals();
  669. _recreate_internals();
  670. emit_signal(SNAME("changed"));
  671. }
  672. TileMap::VisibilityMode TileMap::get_navigation_visibility_mode() {
  673. return navigation_visibility_mode;
  674. }
  675. void TileMap::set_y_sort_enabled(bool p_enable) {
  676. Node2D::set_y_sort_enabled(p_enable);
  677. _clear_internals();
  678. _recreate_internals();
  679. emit_signal(SNAME("changed"));
  680. }
  681. Vector2i TileMap::_coords_to_quadrant_coords(int p_layer, const Vector2i &p_coords) const {
  682. int quadrant_size = get_effective_quadrant_size(p_layer);
  683. // Rounding down, instead of simply rounding towards zero (truncating)
  684. return Vector2i(
  685. p_coords.x > 0 ? p_coords.x / quadrant_size : (p_coords.x - (quadrant_size - 1)) / quadrant_size,
  686. p_coords.y > 0 ? p_coords.y / quadrant_size : (p_coords.y - (quadrant_size - 1)) / quadrant_size);
  687. }
  688. HashMap<Vector2i, TileMapQuadrant>::Iterator TileMap::_create_quadrant(int p_layer, const Vector2i &p_qk) {
  689. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  690. TileMapQuadrant q;
  691. q.layer = p_layer;
  692. q.coords = p_qk;
  693. rect_cache_dirty = true;
  694. // Create the debug canvas item.
  695. RenderingServer *rs = RenderingServer::get_singleton();
  696. q.debug_canvas_item = rs->canvas_item_create();
  697. rs->canvas_item_set_z_index(q.debug_canvas_item, RS::CANVAS_ITEM_Z_MAX - 1);
  698. rs->canvas_item_set_parent(q.debug_canvas_item, get_canvas_item());
  699. // Call the create_quadrant method on plugins
  700. if (tile_set.is_valid()) {
  701. _rendering_create_quadrant(&q);
  702. }
  703. return layers[p_layer].quadrant_map.insert(p_qk, q);
  704. }
  705. void TileMap::_make_quadrant_dirty(HashMap<Vector2i, TileMapQuadrant>::Iterator Q) {
  706. // Make the given quadrant dirty, then trigger an update later.
  707. TileMapQuadrant &q = Q->value;
  708. if (!q.dirty_list_element.in_list()) {
  709. layers[q.layer].dirty_quadrant_list.add(&q.dirty_list_element);
  710. }
  711. _queue_update_dirty_quadrants();
  712. }
  713. void TileMap::_make_all_quadrants_dirty() {
  714. // Make all quandrants dirty, then trigger an update later.
  715. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  716. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  717. if (!E.value.dirty_list_element.in_list()) {
  718. layers[layer].dirty_quadrant_list.add(&E.value.dirty_list_element);
  719. }
  720. }
  721. }
  722. _queue_update_dirty_quadrants();
  723. }
  724. void TileMap::_queue_update_dirty_quadrants() {
  725. if (pending_update || !is_inside_tree()) {
  726. return;
  727. }
  728. pending_update = true;
  729. call_deferred(SNAME("_update_dirty_quadrants"));
  730. }
  731. void TileMap::_update_dirty_quadrants() {
  732. if (!pending_update) {
  733. return;
  734. }
  735. if (!is_inside_tree() || !tile_set.is_valid()) {
  736. pending_update = false;
  737. return;
  738. }
  739. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  740. SelfList<TileMapQuadrant>::List &dirty_quadrant_list = layers[layer].dirty_quadrant_list;
  741. // Update the coords cache.
  742. for (SelfList<TileMapQuadrant> *q = dirty_quadrant_list.first(); q; q = q->next()) {
  743. q->self()->map_to_world.clear();
  744. q->self()->world_to_map.clear();
  745. for (const Vector2i &E : q->self()->cells) {
  746. Vector2i pk = E;
  747. Vector2i pk_world_coords = map_to_world(pk);
  748. q->self()->map_to_world[pk] = pk_world_coords;
  749. q->self()->world_to_map[pk_world_coords] = pk;
  750. }
  751. }
  752. // Find TileData that need a runtime modification.
  753. _build_runtime_update_tile_data(dirty_quadrant_list);
  754. // Call the update_dirty_quadrant method on plugins.
  755. _rendering_update_dirty_quadrants(dirty_quadrant_list);
  756. _physics_update_dirty_quadrants(dirty_quadrant_list);
  757. _navigation_update_dirty_quadrants(dirty_quadrant_list);
  758. _scenes_update_dirty_quadrants(dirty_quadrant_list);
  759. // Redraw the debug canvas_items.
  760. RenderingServer *rs = RenderingServer::get_singleton();
  761. for (SelfList<TileMapQuadrant> *q = dirty_quadrant_list.first(); q; q = q->next()) {
  762. rs->canvas_item_clear(q->self()->debug_canvas_item);
  763. Transform2D xform;
  764. xform.set_origin(map_to_world(q->self()->coords * get_effective_quadrant_size(layer)));
  765. rs->canvas_item_set_transform(q->self()->debug_canvas_item, xform);
  766. _rendering_draw_quadrant_debug(q->self());
  767. _physics_draw_quadrant_debug(q->self());
  768. _navigation_draw_quadrant_debug(q->self());
  769. _scenes_draw_quadrant_debug(q->self());
  770. }
  771. // Clear the list
  772. while (dirty_quadrant_list.first()) {
  773. // Clear the runtime tile data.
  774. for (const KeyValue<Vector2i, TileData *> &kv : dirty_quadrant_list.first()->self()->runtime_tile_data_cache) {
  775. memdelete(kv.value);
  776. }
  777. dirty_quadrant_list.remove(dirty_quadrant_list.first());
  778. }
  779. }
  780. pending_update = false;
  781. _recompute_rect_cache();
  782. }
  783. void TileMap::_recreate_layer_internals(int p_layer) {
  784. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  785. // Make sure that _clear_internals() was called prior.
  786. ERR_FAIL_COND_MSG(layers[p_layer].quadrant_map.size() > 0, "TileMap layer " + itos(p_layer) + " had a non-empty quadrant map.");
  787. if (!layers[p_layer].enabled) {
  788. return;
  789. }
  790. // Update the layer internals.
  791. _rendering_update_layer(p_layer);
  792. // Recreate the quadrants.
  793. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  794. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  795. Vector2i qk = _coords_to_quadrant_coords(p_layer, Vector2i(E.key.x, E.key.y));
  796. HashMap<Vector2i, TileMapQuadrant>::Iterator Q = layers[p_layer].quadrant_map.find(qk);
  797. if (!Q) {
  798. Q = _create_quadrant(p_layer, qk);
  799. layers[p_layer].dirty_quadrant_list.add(&Q->value.dirty_list_element);
  800. }
  801. Vector2i pk = E.key;
  802. Q->value.cells.insert(pk);
  803. _make_quadrant_dirty(Q);
  804. }
  805. _queue_update_dirty_quadrants();
  806. }
  807. void TileMap::_recreate_internals() {
  808. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  809. _recreate_layer_internals(layer);
  810. }
  811. }
  812. void TileMap::_erase_quadrant(HashMap<Vector2i, TileMapQuadrant>::Iterator Q) {
  813. // Remove a quadrant.
  814. TileMapQuadrant *q = &(Q->value);
  815. // Call the cleanup_quadrant method on plugins.
  816. if (tile_set.is_valid()) {
  817. _rendering_cleanup_quadrant(q);
  818. _physics_cleanup_quadrant(q);
  819. _navigation_cleanup_quadrant(q);
  820. _scenes_cleanup_quadrant(q);
  821. }
  822. // Remove the quadrant from the dirty_list if it is there.
  823. if (q->dirty_list_element.in_list()) {
  824. layers[q->layer].dirty_quadrant_list.remove(&(q->dirty_list_element));
  825. }
  826. // Free the debug canvas item.
  827. RenderingServer *rs = RenderingServer::get_singleton();
  828. rs->free(q->debug_canvas_item);
  829. layers[q->layer].quadrant_map.remove(Q);
  830. rect_cache_dirty = true;
  831. }
  832. void TileMap::_clear_layer_internals(int p_layer) {
  833. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  834. // Clear quadrants.
  835. while (layers[p_layer].quadrant_map.size()) {
  836. _erase_quadrant(layers[p_layer].quadrant_map.begin());
  837. }
  838. // Clear the layers internals.
  839. _rendering_cleanup_layer(p_layer);
  840. // Clear the dirty quadrants list.
  841. while (layers[p_layer].dirty_quadrant_list.first()) {
  842. layers[p_layer].dirty_quadrant_list.remove(layers[p_layer].dirty_quadrant_list.first());
  843. }
  844. }
  845. void TileMap::_clear_internals() {
  846. // Clear quadrants.
  847. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  848. _clear_layer_internals(layer);
  849. }
  850. }
  851. void TileMap::_recompute_rect_cache() {
  852. // Compute the displayed area of the tilemap.
  853. #ifdef DEBUG_ENABLED
  854. if (!rect_cache_dirty) {
  855. return;
  856. }
  857. Rect2 r_total;
  858. bool first = true;
  859. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  860. for (const KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  861. Rect2 r;
  862. r.position = map_to_world(E.key * get_effective_quadrant_size(layer));
  863. r.expand_to(map_to_world((E.key + Vector2i(1, 0)) * get_effective_quadrant_size(layer)));
  864. r.expand_to(map_to_world((E.key + Vector2i(1, 1)) * get_effective_quadrant_size(layer)));
  865. r.expand_to(map_to_world((E.key + Vector2i(0, 1)) * get_effective_quadrant_size(layer)));
  866. if (first) {
  867. r_total = r;
  868. first = false;
  869. } else {
  870. r_total = r_total.merge(r);
  871. }
  872. }
  873. }
  874. rect_cache = r_total;
  875. item_rect_changed();
  876. rect_cache_dirty = false;
  877. #endif
  878. }
  879. /////////////////////////////// Rendering //////////////////////////////////////
  880. void TileMap::_rendering_notification(int p_what) {
  881. switch (p_what) {
  882. case NOTIFICATION_VISIBILITY_CHANGED: {
  883. bool visible = is_visible_in_tree();
  884. for (int layer = 0; layer < (int)layers.size(); layer++) {
  885. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layers[layer].quadrant_map) {
  886. TileMapQuadrant &q = E_quadrant.value;
  887. // Update occluders transform.
  888. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.world_to_map) {
  889. Transform2D xform;
  890. xform.set_origin(E_cell.key);
  891. for (const RID &occluder : q.occluders) {
  892. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, visible);
  893. }
  894. }
  895. }
  896. }
  897. } break;
  898. case NOTIFICATION_TRANSFORM_CHANGED: {
  899. if (!is_inside_tree()) {
  900. return;
  901. }
  902. for (int layer = 0; layer < (int)layers.size(); layer++) {
  903. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layers[layer].quadrant_map) {
  904. TileMapQuadrant &q = E_quadrant.value;
  905. // Update occluders transform.
  906. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.world_to_map) {
  907. Transform2D xform;
  908. xform.set_origin(E_cell.key);
  909. for (const RID &occluder : q.occluders) {
  910. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform() * xform);
  911. }
  912. }
  913. }
  914. }
  915. } break;
  916. case NOTIFICATION_DRAW: {
  917. if (tile_set.is_valid()) {
  918. RenderingServer::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), is_y_sort_enabled());
  919. }
  920. } break;
  921. }
  922. }
  923. void TileMap::_rendering_update_layer(int p_layer) {
  924. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  925. RenderingServer *rs = RenderingServer::get_singleton();
  926. if (!layers[p_layer].canvas_item.is_valid()) {
  927. RID ci = rs->canvas_item_create();
  928. rs->canvas_item_set_parent(ci, get_canvas_item());
  929. /*Transform2D xform;
  930. xform.set_origin(Vector2(0, p_layer));
  931. rs->canvas_item_set_transform(ci, xform);*/
  932. rs->canvas_item_set_draw_index(ci, p_layer);
  933. layers[p_layer].canvas_item = ci;
  934. }
  935. RID &ci = layers[p_layer].canvas_item;
  936. rs->canvas_item_set_sort_children_by_y(ci, layers[p_layer].y_sort_enabled);
  937. rs->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  938. rs->canvas_item_set_z_index(ci, layers[p_layer].z_index);
  939. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(get_texture_filter()));
  940. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(get_texture_repeat()));
  941. rs->canvas_item_set_light_mask(ci, get_light_mask());
  942. }
  943. void TileMap::_rendering_cleanup_layer(int p_layer) {
  944. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  945. RenderingServer *rs = RenderingServer::get_singleton();
  946. if (layers[p_layer].canvas_item.is_valid()) {
  947. rs->free(layers[p_layer].canvas_item);
  948. layers[p_layer].canvas_item = RID();
  949. }
  950. }
  951. void TileMap::_rendering_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  952. ERR_FAIL_COND(!is_inside_tree());
  953. ERR_FAIL_COND(!tile_set.is_valid());
  954. bool visible = is_visible_in_tree();
  955. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  956. while (q_list_element) {
  957. TileMapQuadrant &q = *q_list_element->self();
  958. RenderingServer *rs = RenderingServer::get_singleton();
  959. // Free the canvas items.
  960. for (const RID &ci : q.canvas_items) {
  961. rs->free(ci);
  962. }
  963. q.canvas_items.clear();
  964. // Free the occluders.
  965. for (const RID &occluder : q.occluders) {
  966. rs->free(occluder);
  967. }
  968. q.occluders.clear();
  969. // Those allow to group cell per material or z-index.
  970. Ref<Material> prev_material;
  971. int prev_z_index = 0;
  972. RID prev_canvas_item;
  973. Color modulate = get_self_modulate();
  974. modulate *= get_layer_modulate(q.layer);
  975. if (selected_layer >= 0) {
  976. int z1 = get_layer_z_index(q.layer);
  977. int z2 = get_layer_z_index(selected_layer);
  978. if (z1 < z2 || (z1 == z2 && q.layer < selected_layer)) {
  979. modulate = modulate.darkened(0.5);
  980. } else if (z1 > z2 || (z1 == z2 && q.layer > selected_layer)) {
  981. modulate = modulate.darkened(0.5);
  982. modulate.a *= 0.3;
  983. }
  984. }
  985. // Iterate over the cells of the quadrant.
  986. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.world_to_map) {
  987. TileMapCell c = get_cell(q.layer, E_cell.value, true);
  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. continue;
  993. }
  994. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  995. if (atlas_source) {
  996. // Get the tile data.
  997. const TileData *tile_data;
  998. if (q.runtime_tile_data_cache.has(E_cell.value)) {
  999. tile_data = q.runtime_tile_data_cache[E_cell.value];
  1000. } else {
  1001. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1002. }
  1003. Ref<Material> mat = tile_data->get_material();
  1004. int z_index = tile_data->get_z_index();
  1005. // Quandrant pos.
  1006. Vector2 position = map_to_world(q.coords * get_effective_quadrant_size(q.layer));
  1007. if (is_y_sort_enabled() && layers[q.layer].y_sort_enabled) {
  1008. // When Y-sorting, the quandrant size is sure to be 1, we can thus offset the CanvasItem.
  1009. position.y += layers[q.layer].y_sort_origin + tile_data->get_y_sort_origin();
  1010. }
  1011. // --- CanvasItems ---
  1012. // Create two canvas items, for rendering and debug.
  1013. RID canvas_item;
  1014. // Check if the material or the z_index changed.
  1015. if (prev_canvas_item == RID() || prev_material != mat || prev_z_index != z_index) {
  1016. // If so, create a new CanvasItem.
  1017. canvas_item = rs->canvas_item_create();
  1018. if (mat.is_valid()) {
  1019. rs->canvas_item_set_material(canvas_item, mat->get_rid());
  1020. }
  1021. rs->canvas_item_set_parent(canvas_item, layers[q.layer].canvas_item);
  1022. rs->canvas_item_set_use_parent_material(canvas_item, get_use_parent_material() || get_material().is_valid());
  1023. Transform2D xform;
  1024. xform.set_origin(position);
  1025. rs->canvas_item_set_transform(canvas_item, xform);
  1026. rs->canvas_item_set_light_mask(canvas_item, get_light_mask());
  1027. rs->canvas_item_set_z_as_relative_to_parent(canvas_item, true);
  1028. rs->canvas_item_set_z_index(canvas_item, z_index);
  1029. rs->canvas_item_set_default_texture_filter(canvas_item, RS::CanvasItemTextureFilter(get_texture_filter()));
  1030. rs->canvas_item_set_default_texture_repeat(canvas_item, RS::CanvasItemTextureRepeat(get_texture_repeat()));
  1031. q.canvas_items.push_back(canvas_item);
  1032. prev_canvas_item = canvas_item;
  1033. prev_material = mat;
  1034. prev_z_index = z_index;
  1035. } else {
  1036. // Keep the same canvas_item to draw on.
  1037. canvas_item = prev_canvas_item;
  1038. }
  1039. // Drawing the tile in the canvas item.
  1040. draw_tile(canvas_item, E_cell.key - position, tile_set, c.source_id, c.get_atlas_coords(), c.alternative_tile, -1, modulate, tile_data);
  1041. // --- Occluders ---
  1042. for (int i = 0; i < tile_set->get_occlusion_layers_count(); i++) {
  1043. Transform2D xform;
  1044. xform.set_origin(E_cell.key);
  1045. if (tile_data->get_occluder(i).is_valid()) {
  1046. RID occluder_id = rs->canvas_light_occluder_create();
  1047. rs->canvas_light_occluder_set_enabled(occluder_id, visible);
  1048. rs->canvas_light_occluder_set_transform(occluder_id, get_global_transform() * xform);
  1049. rs->canvas_light_occluder_set_polygon(occluder_id, tile_data->get_occluder(i)->get_rid());
  1050. rs->canvas_light_occluder_attach_to_canvas(occluder_id, get_canvas());
  1051. rs->canvas_light_occluder_set_light_mask(occluder_id, tile_set->get_occlusion_layer_light_mask(i));
  1052. q.occluders.push_back(occluder_id);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. _rendering_quadrant_order_dirty = true;
  1059. q_list_element = q_list_element->next();
  1060. }
  1061. // Reset the drawing indices
  1062. if (_rendering_quadrant_order_dirty) {
  1063. int index = -(int64_t)0x80000000; //always must be drawn below children.
  1064. for (int layer = 0; layer < (int)layers.size(); layer++) {
  1065. // Sort the quadrants coords per world coordinates
  1066. RBMap<Vector2i, Vector2i, TileMapQuadrant::CoordsWorldComparator> world_to_map;
  1067. for (const KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  1068. world_to_map[map_to_world(E.key)] = E.key;
  1069. }
  1070. // Sort the quadrants
  1071. for (const KeyValue<Vector2i, Vector2i> &E : world_to_map) {
  1072. TileMapQuadrant &q = layers[layer].quadrant_map[E.value];
  1073. for (const RID &ci : q.canvas_items) {
  1074. RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
  1075. }
  1076. }
  1077. }
  1078. _rendering_quadrant_order_dirty = false;
  1079. }
  1080. }
  1081. void TileMap::_rendering_create_quadrant(TileMapQuadrant *p_quadrant) {
  1082. ERR_FAIL_COND(!tile_set.is_valid());
  1083. _rendering_quadrant_order_dirty = true;
  1084. }
  1085. void TileMap::_rendering_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1086. // Free the canvas items.
  1087. for (const RID &ci : p_quadrant->canvas_items) {
  1088. RenderingServer::get_singleton()->free(ci);
  1089. }
  1090. p_quadrant->canvas_items.clear();
  1091. // Free the occluders.
  1092. for (const RID &occluder : p_quadrant->occluders) {
  1093. RenderingServer::get_singleton()->free(occluder);
  1094. }
  1095. p_quadrant->occluders.clear();
  1096. }
  1097. void TileMap::_rendering_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1098. ERR_FAIL_COND(!tile_set.is_valid());
  1099. if (!Engine::get_singleton()->is_editor_hint()) {
  1100. return;
  1101. }
  1102. // Draw a placeholder for scenes needing one.
  1103. RenderingServer *rs = RenderingServer::get_singleton();
  1104. Vector2 quadrant_pos = map_to_world(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1105. for (const Vector2i &E_cell : p_quadrant->cells) {
  1106. const TileMapCell &c = get_cell(p_quadrant->layer, E_cell, true);
  1107. TileSetSource *source;
  1108. if (tile_set->has_source(c.source_id)) {
  1109. source = *tile_set->get_source(c.source_id);
  1110. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1111. continue;
  1112. }
  1113. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1114. if (atlas_source) {
  1115. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  1116. if (!atlas_source->get_runtime_texture().is_valid() || c.get_atlas_coords().x >= grid_size.x || c.get_atlas_coords().y >= grid_size.y) {
  1117. // Generate a random color from the hashed values of the tiles.
  1118. Array to_hash;
  1119. to_hash.push_back(c.source_id);
  1120. to_hash.push_back(c.get_atlas_coords());
  1121. to_hash.push_back(c.alternative_tile);
  1122. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1123. Color color;
  1124. color = color.from_hsv(
  1125. (float)((hash >> 24) & 0xFF) / 256.0,
  1126. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1127. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1128. 0.8);
  1129. // Draw a placeholder tile.
  1130. Transform2D xform;
  1131. xform.set_origin(map_to_world(E_cell) - quadrant_pos);
  1132. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, xform);
  1133. rs->canvas_item_add_circle(p_quadrant->debug_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
  1134. }
  1135. }
  1136. }
  1137. }
  1138. }
  1139. void TileMap::draw_tile(RID p_canvas_item, Vector2i p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, Vector2i p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override) {
  1140. ERR_FAIL_COND(!p_tile_set.is_valid());
  1141. ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id));
  1142. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords));
  1143. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_alternative_tile(p_atlas_coords, p_alternative_tile));
  1144. TileSetSource *source = *p_tile_set->get_source(p_atlas_source_id);
  1145. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1146. if (atlas_source) {
  1147. // Check for the frame.
  1148. if (p_frame >= 0) {
  1149. ERR_FAIL_INDEX(p_frame, atlas_source->get_tile_animation_frames_count(p_atlas_coords));
  1150. }
  1151. // Get the texture.
  1152. Ref<Texture2D> tex = atlas_source->get_runtime_texture();
  1153. if (!tex.is_valid()) {
  1154. return;
  1155. }
  1156. // Check if we are in the texture, return otherwise.
  1157. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  1158. if (p_atlas_coords.x >= grid_size.x || p_atlas_coords.y >= grid_size.y) {
  1159. return;
  1160. }
  1161. // Get tile data.
  1162. const TileData *tile_data = p_tile_data_override ? p_tile_data_override : atlas_source->get_tile_data(p_atlas_coords, p_alternative_tile);
  1163. // Get the tile modulation.
  1164. Color modulate = tile_data->get_modulate() * p_modulation;
  1165. // Compute the offset.
  1166. Vector2i tile_offset = atlas_source->get_tile_effective_texture_offset(p_atlas_coords, p_alternative_tile);
  1167. // Get destination rect.
  1168. Rect2 dest_rect;
  1169. dest_rect.size = atlas_source->get_runtime_tile_texture_region(p_atlas_coords).size;
  1170. dest_rect.size.x += FP_ADJUST;
  1171. dest_rect.size.y += FP_ADJUST;
  1172. bool transpose = tile_data->get_transpose();
  1173. if (transpose) {
  1174. dest_rect.position = (p_position - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset);
  1175. } else {
  1176. dest_rect.position = (p_position - dest_rect.size / 2 - tile_offset);
  1177. }
  1178. if (tile_data->get_flip_h()) {
  1179. dest_rect.size.x = -dest_rect.size.x;
  1180. }
  1181. if (tile_data->get_flip_v()) {
  1182. dest_rect.size.y = -dest_rect.size.y;
  1183. }
  1184. // Draw the tile.
  1185. if (p_frame >= 0) {
  1186. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, p_frame);
  1187. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  1188. } else if (atlas_source->get_tile_animation_frames_count(p_atlas_coords) == 1) {
  1189. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, 0);
  1190. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  1191. } else {
  1192. real_t speed = atlas_source->get_tile_animation_speed(p_atlas_coords);
  1193. real_t animation_duration = atlas_source->get_tile_animation_total_duration(p_atlas_coords) / speed;
  1194. real_t time = 0.0;
  1195. for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(p_atlas_coords); frame++) {
  1196. real_t frame_duration = atlas_source->get_tile_animation_frame_duration(p_atlas_coords, frame) / speed;
  1197. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, time, time + frame_duration, 0.0);
  1198. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, frame);
  1199. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  1200. time += frame_duration;
  1201. }
  1202. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, 1.0, 0.0, 1.0, 0.0);
  1203. }
  1204. }
  1205. }
  1206. /////////////////////////////// Physics //////////////////////////////////////
  1207. void TileMap::_physics_notification(int p_what) {
  1208. switch (p_what) {
  1209. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  1210. bool in_editor = false;
  1211. #ifdef TOOLS_ENABLED
  1212. in_editor = Engine::get_singleton()->is_editor_hint();
  1213. #endif
  1214. if (is_inside_tree() && collision_animatable && !in_editor) {
  1215. // Update transform on the physics tick when in animatable mode.
  1216. last_valid_transform = new_transform;
  1217. set_notify_local_transform(false);
  1218. set_global_transform(new_transform);
  1219. set_notify_local_transform(true);
  1220. }
  1221. } break;
  1222. case NOTIFICATION_TRANSFORM_CHANGED: {
  1223. bool in_editor = false;
  1224. #ifdef TOOLS_ENABLED
  1225. in_editor = Engine::get_singleton()->is_editor_hint();
  1226. #endif
  1227. if (is_inside_tree() && (!collision_animatable || in_editor)) {
  1228. // Update the new transform directly if we are not in animatable mode.
  1229. Transform2D global_transform = get_global_transform();
  1230. for (int layer = 0; layer < (int)layers.size(); layer++) {
  1231. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  1232. TileMapQuadrant &q = E.value;
  1233. for (RID body : q.bodies) {
  1234. Transform2D xform;
  1235. xform.set_origin(map_to_world(bodies_coords[body]));
  1236. xform = global_transform * xform;
  1237. PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  1238. }
  1239. }
  1240. }
  1241. }
  1242. } break;
  1243. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  1244. bool in_editor = false;
  1245. #ifdef TOOLS_ENABLED
  1246. in_editor = Engine::get_singleton()->is_editor_hint();
  1247. #endif
  1248. if (is_inside_tree() && !in_editor && collision_animatable) {
  1249. // Only active when animatable. Send the new transform to the physics...
  1250. new_transform = get_global_transform();
  1251. for (int layer = 0; layer < (int)layers.size(); layer++) {
  1252. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  1253. TileMapQuadrant &q = E.value;
  1254. for (RID body : q.bodies) {
  1255. Transform2D xform;
  1256. xform.set_origin(map_to_world(bodies_coords[body]));
  1257. xform = new_transform * xform;
  1258. PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  1259. }
  1260. }
  1261. }
  1262. // ... but then revert changes.
  1263. set_notify_local_transform(false);
  1264. set_global_transform(last_valid_transform);
  1265. set_notify_local_transform(true);
  1266. }
  1267. } break;
  1268. }
  1269. }
  1270. void TileMap::_physics_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1271. ERR_FAIL_COND(!is_inside_tree());
  1272. ERR_FAIL_COND(!tile_set.is_valid());
  1273. Transform2D global_transform = get_global_transform();
  1274. last_valid_transform = global_transform;
  1275. new_transform = global_transform;
  1276. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  1277. RID space = get_world_2d()->get_space();
  1278. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1279. while (q_list_element) {
  1280. TileMapQuadrant &q = *q_list_element->self();
  1281. // Clear bodies.
  1282. for (RID body : q.bodies) {
  1283. bodies_coords.erase(body);
  1284. ps->free(body);
  1285. }
  1286. q.bodies.clear();
  1287. // Recreate bodies and shapes.
  1288. for (const Vector2i &E_cell : q.cells) {
  1289. TileMapCell c = get_cell(q.layer, E_cell, true);
  1290. TileSetSource *source;
  1291. if (tile_set->has_source(c.source_id)) {
  1292. source = *tile_set->get_source(c.source_id);
  1293. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1294. continue;
  1295. }
  1296. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1297. if (atlas_source) {
  1298. const TileData *tile_data;
  1299. if (q.runtime_tile_data_cache.has(E_cell)) {
  1300. tile_data = q.runtime_tile_data_cache[E_cell];
  1301. } else {
  1302. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1303. }
  1304. for (int tile_set_physics_layer = 0; tile_set_physics_layer < tile_set->get_physics_layers_count(); tile_set_physics_layer++) {
  1305. Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
  1306. uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
  1307. uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
  1308. // Create the body.
  1309. RID body = ps->body_create();
  1310. bodies_coords[body] = E_cell;
  1311. ps->body_set_mode(body, collision_animatable ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
  1312. ps->body_set_space(body, space);
  1313. Transform2D xform;
  1314. xform.set_origin(map_to_world(E_cell));
  1315. xform = global_transform * xform;
  1316. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  1317. ps->body_attach_object_instance_id(body, get_instance_id());
  1318. ps->body_set_collision_layer(body, physics_layer);
  1319. ps->body_set_collision_mask(body, physics_mask);
  1320. ps->body_set_pickable(body, false);
  1321. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, tile_data->get_constant_linear_velocity(tile_set_physics_layer));
  1322. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, tile_data->get_constant_angular_velocity(tile_set_physics_layer));
  1323. if (!physics_material.is_valid()) {
  1324. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  1325. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  1326. } else {
  1327. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
  1328. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, physics_material->computed_friction());
  1329. }
  1330. q.bodies.push_back(body);
  1331. // Add the shapes to the body.
  1332. int body_shape_index = 0;
  1333. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(tile_set_physics_layer); polygon_index++) {
  1334. // Iterate over the polygons.
  1335. bool one_way_collision = tile_data->is_collision_polygon_one_way(tile_set_physics_layer, polygon_index);
  1336. float one_way_collision_margin = tile_data->get_collision_polygon_one_way_margin(tile_set_physics_layer, polygon_index);
  1337. int shapes_count = tile_data->get_collision_polygon_shapes_count(tile_set_physics_layer, polygon_index);
  1338. for (int shape_index = 0; shape_index < shapes_count; shape_index++) {
  1339. // Add decomposed convex shapes.
  1340. Ref<ConvexPolygonShape2D> shape = tile_data->get_collision_polygon_shape(tile_set_physics_layer, polygon_index, shape_index);
  1341. ps->body_add_shape(body, shape->get_rid());
  1342. ps->body_set_shape_as_one_way_collision(body, body_shape_index, one_way_collision, one_way_collision_margin);
  1343. body_shape_index++;
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }
  1349. }
  1350. q_list_element = q_list_element->next();
  1351. }
  1352. }
  1353. void TileMap::_physics_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1354. // Remove a quadrant.
  1355. for (RID body : p_quadrant->bodies) {
  1356. bodies_coords.erase(body);
  1357. PhysicsServer2D::get_singleton()->free(body);
  1358. }
  1359. p_quadrant->bodies.clear();
  1360. }
  1361. void TileMap::_physics_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1362. // Draw the debug collision shapes.
  1363. ERR_FAIL_COND(!tile_set.is_valid());
  1364. if (!get_tree()) {
  1365. return;
  1366. }
  1367. bool show_collision = false;
  1368. switch (collision_visibility_mode) {
  1369. case TileMap::VISIBILITY_MODE_DEFAULT:
  1370. show_collision = !Engine::get_singleton()->is_editor_hint() && (get_tree() && get_tree()->is_debugging_collisions_hint());
  1371. break;
  1372. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  1373. show_collision = false;
  1374. break;
  1375. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  1376. show_collision = true;
  1377. break;
  1378. }
  1379. if (!show_collision) {
  1380. return;
  1381. }
  1382. RenderingServer *rs = RenderingServer::get_singleton();
  1383. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  1384. Color debug_collision_color = get_tree()->get_debug_collisions_color();
  1385. Vector<Color> color;
  1386. color.push_back(debug_collision_color);
  1387. Vector2 quadrant_pos = map_to_world(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1388. Transform2D qudrant_xform;
  1389. qudrant_xform.set_origin(quadrant_pos);
  1390. Transform2D global_transform_inv = (get_global_transform() * qudrant_xform).affine_inverse();
  1391. for (RID body : p_quadrant->bodies) {
  1392. Transform2D xform = Transform2D(ps->body_get_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM)) * global_transform_inv;
  1393. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, xform);
  1394. for (int shape_index = 0; shape_index < ps->body_get_shape_count(body); shape_index++) {
  1395. const RID &shape = ps->body_get_shape(body, shape_index);
  1396. PhysicsServer2D::ShapeType type = ps->shape_get_type(shape);
  1397. if (type == PhysicsServer2D::SHAPE_CONVEX_POLYGON) {
  1398. Vector<Vector2> polygon = ps->shape_get_data(shape);
  1399. rs->canvas_item_add_polygon(p_quadrant->debug_canvas_item, polygon, color);
  1400. } else {
  1401. WARN_PRINT("Wrong shape type for a tile, should be SHAPE_CONVEX_POLYGON.");
  1402. }
  1403. }
  1404. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, Transform2D());
  1405. }
  1406. };
  1407. /////////////////////////////// Navigation //////////////////////////////////////
  1408. void TileMap::_navigation_notification(int p_what) {
  1409. switch (p_what) {
  1410. case NOTIFICATION_TRANSFORM_CHANGED: {
  1411. if (is_inside_tree()) {
  1412. for (int layer = 0; layer < (int)layers.size(); layer++) {
  1413. Transform2D tilemap_xform = get_global_transform();
  1414. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layers[layer].quadrant_map) {
  1415. TileMapQuadrant &q = E_quadrant.value;
  1416. for (const KeyValue<Vector2i, Vector<RID>> &E_region : q.navigation_regions) {
  1417. for (int layer_index = 0; layer_index < E_region.value.size(); layer_index++) {
  1418. RID region = E_region.value[layer_index];
  1419. if (!region.is_valid()) {
  1420. continue;
  1421. }
  1422. Transform2D tile_transform;
  1423. tile_transform.set_origin(map_to_world(E_region.key));
  1424. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  1425. }
  1426. }
  1427. }
  1428. }
  1429. }
  1430. } break;
  1431. }
  1432. }
  1433. void TileMap::_navigation_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1434. ERR_FAIL_COND(!is_inside_tree());
  1435. ERR_FAIL_COND(!tile_set.is_valid());
  1436. // Get colors for debug.
  1437. SceneTree *st = SceneTree::get_singleton();
  1438. Color debug_navigation_color;
  1439. bool debug_navigation = st && st->is_debugging_navigation_hint();
  1440. if (debug_navigation) {
  1441. debug_navigation_color = st->get_debug_navigation_color();
  1442. }
  1443. Transform2D tilemap_xform = get_global_transform();
  1444. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1445. while (q_list_element) {
  1446. TileMapQuadrant &q = *q_list_element->self();
  1447. // Clear navigation shapes in the quadrant.
  1448. for (const KeyValue<Vector2i, Vector<RID>> &E : q.navigation_regions) {
  1449. for (int i = 0; i < E.value.size(); i++) {
  1450. RID region = E.value[i];
  1451. if (!region.is_valid()) {
  1452. continue;
  1453. }
  1454. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  1455. }
  1456. }
  1457. q.navigation_regions.clear();
  1458. // Get the navigation polygons and create regions.
  1459. for (const Vector2i &E_cell : q.cells) {
  1460. TileMapCell c = get_cell(q.layer, E_cell, true);
  1461. TileSetSource *source;
  1462. if (tile_set->has_source(c.source_id)) {
  1463. source = *tile_set->get_source(c.source_id);
  1464. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1465. continue;
  1466. }
  1467. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1468. if (atlas_source) {
  1469. const TileData *tile_data;
  1470. if (q.runtime_tile_data_cache.has(E_cell)) {
  1471. tile_data = q.runtime_tile_data_cache[E_cell];
  1472. } else {
  1473. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1474. }
  1475. q.navigation_regions[E_cell].resize(tile_set->get_navigation_layers_count());
  1476. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1477. Ref<NavigationPolygon> navpoly;
  1478. navpoly = tile_data->get_navigation_polygon(layer_index);
  1479. if (navpoly.is_valid()) {
  1480. Transform2D tile_transform;
  1481. tile_transform.set_origin(map_to_world(E_cell));
  1482. RID region = NavigationServer2D::get_singleton()->region_create();
  1483. NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  1484. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  1485. NavigationServer2D::get_singleton()->region_set_navpoly(region, navpoly);
  1486. q.navigation_regions[E_cell].write[layer_index] = region;
  1487. }
  1488. }
  1489. }
  1490. }
  1491. }
  1492. q_list_element = q_list_element->next();
  1493. }
  1494. }
  1495. void TileMap::_navigation_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1496. // Clear navigation shapes in the quadrant.
  1497. for (const KeyValue<Vector2i, Vector<RID>> &E : p_quadrant->navigation_regions) {
  1498. for (int i = 0; i < E.value.size(); i++) {
  1499. RID region = E.value[i];
  1500. if (!region.is_valid()) {
  1501. continue;
  1502. }
  1503. NavigationServer2D::get_singleton()->free(region);
  1504. }
  1505. }
  1506. p_quadrant->navigation_regions.clear();
  1507. }
  1508. void TileMap::_navigation_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1509. // Draw the debug collision shapes.
  1510. ERR_FAIL_COND(!tile_set.is_valid());
  1511. if (!get_tree()) {
  1512. return;
  1513. }
  1514. bool show_navigation = false;
  1515. switch (navigation_visibility_mode) {
  1516. case TileMap::VISIBILITY_MODE_DEFAULT:
  1517. show_navigation = !Engine::get_singleton()->is_editor_hint() && (get_tree() && get_tree()->is_debugging_navigation_hint());
  1518. break;
  1519. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  1520. show_navigation = false;
  1521. break;
  1522. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  1523. show_navigation = true;
  1524. break;
  1525. }
  1526. if (!show_navigation) {
  1527. return;
  1528. }
  1529. RenderingServer *rs = RenderingServer::get_singleton();
  1530. Color color = get_tree()->get_debug_navigation_color();
  1531. RandomPCG rand;
  1532. Vector2 quadrant_pos = map_to_world(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1533. for (const Vector2i &E_cell : p_quadrant->cells) {
  1534. TileMapCell c = get_cell(p_quadrant->layer, E_cell, true);
  1535. TileSetSource *source;
  1536. if (tile_set->has_source(c.source_id)) {
  1537. source = *tile_set->get_source(c.source_id);
  1538. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1539. continue;
  1540. }
  1541. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1542. if (atlas_source) {
  1543. const TileData *tile_data;
  1544. if (p_quadrant->runtime_tile_data_cache.has(E_cell)) {
  1545. tile_data = p_quadrant->runtime_tile_data_cache[E_cell];
  1546. } else {
  1547. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1548. }
  1549. Transform2D xform;
  1550. xform.set_origin(map_to_world(E_cell) - quadrant_pos);
  1551. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, xform);
  1552. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1553. Ref<NavigationPolygon> navpoly = tile_data->get_navigation_polygon(layer_index);
  1554. if (navpoly.is_valid()) {
  1555. PackedVector2Array navigation_polygon_vertices = navpoly->get_vertices();
  1556. for (int i = 0; i < navpoly->get_polygon_count(); i++) {
  1557. // An array of vertices for this polygon.
  1558. Vector<int> polygon = navpoly->get_polygon(i);
  1559. Vector<Vector2> vertices;
  1560. vertices.resize(polygon.size());
  1561. for (int j = 0; j < polygon.size(); j++) {
  1562. ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
  1563. vertices.write[j] = navigation_polygon_vertices[polygon[j]];
  1564. }
  1565. // Generate the polygon color, slightly randomly modified from the settings one.
  1566. Color random_variation_color;
  1567. random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.05, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.1);
  1568. random_variation_color.a = color.a;
  1569. Vector<Color> colors;
  1570. colors.push_back(random_variation_color);
  1571. rs->canvas_item_add_polygon(p_quadrant->debug_canvas_item, vertices, colors);
  1572. }
  1573. }
  1574. }
  1575. }
  1576. }
  1577. }
  1578. }
  1579. /////////////////////////////// Scenes //////////////////////////////////////
  1580. void TileMap::_scenes_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1581. ERR_FAIL_COND(!tile_set.is_valid());
  1582. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1583. while (q_list_element) {
  1584. TileMapQuadrant &q = *q_list_element->self();
  1585. // Clear the scenes.
  1586. for (const KeyValue<Vector2i, String> &E : q.scenes) {
  1587. Node *node = get_node_or_null(E.value);
  1588. if (node) {
  1589. node->queue_delete();
  1590. }
  1591. }
  1592. q.scenes.clear();
  1593. // Recreate the scenes.
  1594. for (const Vector2i &E_cell : q.cells) {
  1595. const TileMapCell &c = get_cell(q.layer, E_cell, true);
  1596. TileSetSource *source;
  1597. if (tile_set->has_source(c.source_id)) {
  1598. source = *tile_set->get_source(c.source_id);
  1599. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1600. continue;
  1601. }
  1602. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1603. if (scenes_collection_source) {
  1604. Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
  1605. if (packed_scene.is_valid()) {
  1606. Node *scene = packed_scene->instantiate();
  1607. add_child(scene);
  1608. Control *scene_as_control = Object::cast_to<Control>(scene);
  1609. Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
  1610. if (scene_as_control) {
  1611. scene_as_control->set_position(map_to_world(E_cell) + scene_as_control->get_position());
  1612. } else if (scene_as_node2d) {
  1613. Transform2D xform;
  1614. xform.set_origin(map_to_world(E_cell));
  1615. scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
  1616. }
  1617. q.scenes[E_cell] = scene->get_name();
  1618. }
  1619. }
  1620. }
  1621. }
  1622. q_list_element = q_list_element->next();
  1623. }
  1624. }
  1625. void TileMap::_scenes_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1626. // Clear the scenes.
  1627. for (const KeyValue<Vector2i, String> &E : p_quadrant->scenes) {
  1628. Node *node = get_node_or_null(E.value);
  1629. if (node) {
  1630. node->queue_delete();
  1631. }
  1632. }
  1633. p_quadrant->scenes.clear();
  1634. }
  1635. void TileMap::_scenes_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1636. ERR_FAIL_COND(!tile_set.is_valid());
  1637. if (!Engine::get_singleton()->is_editor_hint()) {
  1638. return;
  1639. }
  1640. // Draw a placeholder for scenes needing one.
  1641. RenderingServer *rs = RenderingServer::get_singleton();
  1642. Vector2 quadrant_pos = map_to_world(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1643. for (const Vector2i &E_cell : p_quadrant->cells) {
  1644. const TileMapCell &c = get_cell(p_quadrant->layer, E_cell, true);
  1645. TileSetSource *source;
  1646. if (tile_set->has_source(c.source_id)) {
  1647. source = *tile_set->get_source(c.source_id);
  1648. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1649. continue;
  1650. }
  1651. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1652. if (scenes_collection_source) {
  1653. if (!scenes_collection_source->get_scene_tile_scene(c.alternative_tile).is_valid() || scenes_collection_source->get_scene_tile_display_placeholder(c.alternative_tile)) {
  1654. // Generate a random color from the hashed values of the tiles.
  1655. Array to_hash;
  1656. to_hash.push_back(c.source_id);
  1657. to_hash.push_back(c.alternative_tile);
  1658. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1659. Color color;
  1660. color = color.from_hsv(
  1661. (float)((hash >> 24) & 0xFF) / 256.0,
  1662. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1663. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1664. 0.8);
  1665. // Draw a placeholder tile.
  1666. Transform2D xform;
  1667. xform.set_origin(map_to_world(E_cell) - quadrant_pos);
  1668. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, xform);
  1669. rs->canvas_item_add_circle(p_quadrant->debug_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
  1670. }
  1671. }
  1672. }
  1673. }
  1674. }
  1675. void TileMap::set_cell(int p_layer, const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  1676. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  1677. // Set the current cell tile (using integer position).
  1678. HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1679. Vector2i pk(p_coords);
  1680. HashMap<Vector2i, TileMapCell>::Iterator E = tile_map.find(pk);
  1681. int source_id = p_source_id;
  1682. Vector2i atlas_coords = p_atlas_coords;
  1683. int alternative_tile = p_alternative_tile;
  1684. if ((source_id == TileSet::INVALID_SOURCE || atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE) &&
  1685. (source_id != TileSet::INVALID_SOURCE || atlas_coords != TileSetSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetSource::INVALID_TILE_ALTERNATIVE)) {
  1686. WARN_PRINT("Setting a cell as empty requires both source_id, atlas_coord and alternative_tile to be set to their respective \"invalid\" values. Values were thus changes accordingly.");
  1687. source_id = TileSet::INVALID_SOURCE;
  1688. atlas_coords = TileSetSource::INVALID_ATLAS_COORDS;
  1689. alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1690. }
  1691. if (!E && source_id == TileSet::INVALID_SOURCE) {
  1692. return; // Nothing to do, the tile is already empty.
  1693. }
  1694. // Get the quadrant
  1695. Vector2i qk = _coords_to_quadrant_coords(p_layer, pk);
  1696. HashMap<Vector2i, TileMapQuadrant>::Iterator Q = layers[p_layer].quadrant_map.find(qk);
  1697. if (source_id == TileSet::INVALID_SOURCE) {
  1698. // Erase existing cell in the tile map.
  1699. tile_map.erase(pk);
  1700. // Erase existing cell in the quadrant.
  1701. ERR_FAIL_COND(!Q);
  1702. TileMapQuadrant &q = Q->value;
  1703. q.cells.erase(pk);
  1704. // Remove or make the quadrant dirty.
  1705. if (q.cells.size() == 0) {
  1706. _erase_quadrant(Q);
  1707. } else {
  1708. _make_quadrant_dirty(Q);
  1709. }
  1710. used_rect_cache_dirty = true;
  1711. } else {
  1712. if (!E) {
  1713. // Insert a new cell in the tile map.
  1714. E = tile_map.insert(pk, TileMapCell());
  1715. // Create a new quadrant if needed, then insert the cell if needed.
  1716. if (!Q) {
  1717. Q = _create_quadrant(p_layer, qk);
  1718. }
  1719. TileMapQuadrant &q = Q->value;
  1720. q.cells.insert(pk);
  1721. } else {
  1722. ERR_FAIL_COND(!Q); // TileMapQuadrant should exist...
  1723. if (E->value.source_id == source_id && E->value.get_atlas_coords() == atlas_coords && E->value.alternative_tile == alternative_tile) {
  1724. return; // Nothing changed.
  1725. }
  1726. }
  1727. TileMapCell &c = E->value;
  1728. c.source_id = source_id;
  1729. c.set_atlas_coords(atlas_coords);
  1730. c.alternative_tile = alternative_tile;
  1731. _make_quadrant_dirty(Q);
  1732. used_rect_cache_dirty = true;
  1733. }
  1734. }
  1735. void TileMap::erase_cell(int p_layer, const Vector2i &p_coords) {
  1736. set_cell(p_layer, p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1737. }
  1738. int TileMap::get_cell_source_id(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1739. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSet::INVALID_SOURCE);
  1740. // Get a cell source id from position
  1741. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1742. HashMap<Vector2i, TileMapCell>::ConstIterator E = tile_map.find(p_coords);
  1743. if (!E) {
  1744. return TileSet::INVALID_SOURCE;
  1745. }
  1746. if (p_use_proxies && tile_set.is_valid()) {
  1747. Array proxyed = tile_set->map_tile_proxy(E->value.source_id, E->value.get_atlas_coords(), E->value.alternative_tile);
  1748. return proxyed[0];
  1749. }
  1750. return E->value.source_id;
  1751. }
  1752. Vector2i TileMap::get_cell_atlas_coords(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1753. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSetSource::INVALID_ATLAS_COORDS);
  1754. // Get a cell source id from position
  1755. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1756. HashMap<Vector2i, TileMapCell>::ConstIterator E = tile_map.find(p_coords);
  1757. if (!E) {
  1758. return TileSetSource::INVALID_ATLAS_COORDS;
  1759. }
  1760. if (p_use_proxies && tile_set.is_valid()) {
  1761. Array proxyed = tile_set->map_tile_proxy(E->value.source_id, E->value.get_atlas_coords(), E->value.alternative_tile);
  1762. return proxyed[1];
  1763. }
  1764. return E->value.get_atlas_coords();
  1765. }
  1766. int TileMap::get_cell_alternative_tile(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1767. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  1768. // Get a cell source id from position
  1769. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1770. HashMap<Vector2i, TileMapCell>::ConstIterator E = tile_map.find(p_coords);
  1771. if (!E) {
  1772. return TileSetSource::INVALID_TILE_ALTERNATIVE;
  1773. }
  1774. if (p_use_proxies && tile_set.is_valid()) {
  1775. Array proxyed = tile_set->map_tile_proxy(E->value.source_id, E->value.get_atlas_coords(), E->value.alternative_tile);
  1776. return proxyed[2];
  1777. }
  1778. return E->value.alternative_tile;
  1779. }
  1780. TileData *TileMap::get_cell_tile_data(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1781. int source_id = get_cell_source_id(p_layer, p_coords, p_use_proxies);
  1782. ERR_FAIL_COND_V_MSG(source_id == TileSet::INVALID_SOURCE, nullptr, vformat("Invalid TileSetSource at cell %s. Make sure a tile exists at this cell.", p_coords));
  1783. Ref<TileSetAtlasSource> source = tile_set->get_source(source_id);
  1784. if (source.is_valid()) {
  1785. return source->get_tile_data(get_cell_atlas_coords(p_layer, p_coords, p_use_proxies), get_cell_alternative_tile(p_layer, p_coords, p_use_proxies));
  1786. }
  1787. return nullptr;
  1788. }
  1789. Ref<TileMapPattern> TileMap::get_pattern(int p_layer, TypedArray<Vector2i> p_coords_array) {
  1790. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  1791. ERR_FAIL_COND_V(!tile_set.is_valid(), nullptr);
  1792. Ref<TileMapPattern> output;
  1793. output.instantiate();
  1794. if (p_coords_array.is_empty()) {
  1795. return output;
  1796. }
  1797. Vector2i min = Vector2i(p_coords_array[0]);
  1798. for (int i = 1; i < p_coords_array.size(); i++) {
  1799. min = min.min(p_coords_array[i]);
  1800. }
  1801. Vector<Vector2i> coords_in_pattern_array;
  1802. coords_in_pattern_array.resize(p_coords_array.size());
  1803. Vector2i ensure_positive_offset;
  1804. for (int i = 0; i < p_coords_array.size(); i++) {
  1805. Vector2i coords = p_coords_array[i];
  1806. Vector2i coords_in_pattern = coords - min;
  1807. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  1808. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  1809. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1810. coords_in_pattern.x -= 1;
  1811. if (coords_in_pattern.x < 0) {
  1812. ensure_positive_offset.x = 1;
  1813. }
  1814. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1815. coords_in_pattern.y -= 1;
  1816. if (coords_in_pattern.y < 0) {
  1817. ensure_positive_offset.y = 1;
  1818. }
  1819. }
  1820. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1821. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1822. coords_in_pattern.x += 1;
  1823. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1824. coords_in_pattern.y += 1;
  1825. }
  1826. }
  1827. }
  1828. coords_in_pattern_array.write[i] = coords_in_pattern;
  1829. }
  1830. for (int i = 0; i < coords_in_pattern_array.size(); i++) {
  1831. Vector2i coords = p_coords_array[i];
  1832. Vector2i coords_in_pattern = coords_in_pattern_array[i];
  1833. output->set_cell(coords_in_pattern + ensure_positive_offset, get_cell_source_id(p_layer, coords), get_cell_atlas_coords(p_layer, coords), get_cell_alternative_tile(p_layer, coords));
  1834. }
  1835. return output;
  1836. }
  1837. Vector2i TileMap::map_pattern(Vector2i p_position_in_tilemap, Vector2i p_coords_in_pattern, Ref<TileMapPattern> p_pattern) {
  1838. ERR_FAIL_COND_V(p_pattern.is_null(), Vector2i());
  1839. ERR_FAIL_COND_V(!p_pattern->has_cell(p_coords_in_pattern), Vector2i());
  1840. Vector2i output = p_position_in_tilemap + p_coords_in_pattern;
  1841. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  1842. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  1843. 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)) {
  1844. output.x += 1;
  1845. } 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)) {
  1846. output.y += 1;
  1847. }
  1848. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1849. 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)) {
  1850. output.x -= 1;
  1851. } 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)) {
  1852. output.y -= 1;
  1853. }
  1854. }
  1855. }
  1856. return output;
  1857. }
  1858. void TileMap::set_pattern(int p_layer, Vector2i p_position, const Ref<TileMapPattern> p_pattern) {
  1859. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  1860. ERR_FAIL_COND(!tile_set.is_valid());
  1861. TypedArray<Vector2i> used_cells = p_pattern->get_used_cells();
  1862. for (int i = 0; i < used_cells.size(); i++) {
  1863. Vector2i coords = map_pattern(p_position, used_cells[i], p_pattern);
  1864. set_cell(p_layer, 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]));
  1865. }
  1866. }
  1867. TileSet::TerrainsPattern TileMap::_get_best_terrain_pattern_for_constraints(int p_terrain_set, const Vector2i &p_position, RBSet<TerrainConstraint> p_constraints) {
  1868. if (!tile_set.is_valid()) {
  1869. return TileSet::TerrainsPattern();
  1870. }
  1871. // Returns all tiles compatible with the given constraints.
  1872. RBMap<TileSet::TerrainsPattern, int> terrain_pattern_score;
  1873. RBSet<TileSet::TerrainsPattern> pattern_set = tile_set->get_terrains_pattern_set(p_terrain_set);
  1874. ERR_FAIL_COND_V(pattern_set.is_empty(), TileSet::TerrainsPattern());
  1875. for (TileSet::TerrainsPattern &terrain_pattern : pattern_set) {
  1876. int score = 0;
  1877. // Check the center bit constraint
  1878. TerrainConstraint terrain_constraint = TerrainConstraint(this, p_position, terrain_pattern.get_terrain());
  1879. RBSet<TerrainConstraint>::Element *in_set_constraint_element = p_constraints.find(terrain_constraint);
  1880. if (in_set_constraint_element && in_set_constraint_element->get().get_terrain() != terrain_constraint.get_terrain()) {
  1881. score += in_set_constraint_element->get().get_priority();
  1882. }
  1883. // Check the surrounding bits
  1884. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1885. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1886. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1887. // Check if the bit is compatible with the constraints.
  1888. TerrainConstraint terrain_bit_constraint = TerrainConstraint(this, p_position, bit, terrain_pattern.get_terrain_peering_bit(bit));
  1889. in_set_constraint_element = p_constraints.find(terrain_bit_constraint);
  1890. if (in_set_constraint_element && in_set_constraint_element->get().get_terrain() != terrain_bit_constraint.get_terrain()) {
  1891. score += in_set_constraint_element->get().get_priority();
  1892. }
  1893. }
  1894. }
  1895. terrain_pattern_score[terrain_pattern] = score;
  1896. }
  1897. // Compute the minimum score
  1898. TileSet::TerrainsPattern min_score_pattern;
  1899. int min_score = INT32_MAX;
  1900. for (KeyValue<TileSet::TerrainsPattern, int> E : terrain_pattern_score) {
  1901. if (E.value < min_score) {
  1902. min_score_pattern = E.key;
  1903. min_score = E.value;
  1904. }
  1905. }
  1906. return min_score_pattern;
  1907. }
  1908. RBSet<TileMap::TerrainConstraint> TileMap::_get_terrain_constraints_from_added_pattern(Vector2i p_position, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  1909. if (!tile_set.is_valid()) {
  1910. return RBSet<TerrainConstraint>();
  1911. }
  1912. // Compute the constraints needed from the surrounding tiles.
  1913. RBSet<TerrainConstraint> output;
  1914. output.insert(TerrainConstraint(this, p_position, p_terrains_pattern.get_terrain()));
  1915. for (uint32_t i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1916. TileSet::CellNeighbor side = TileSet::CellNeighbor(i);
  1917. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, side)) {
  1918. TerrainConstraint c = TerrainConstraint(this, p_position, side, p_terrains_pattern.get_terrain_peering_bit(side));
  1919. output.insert(c);
  1920. }
  1921. }
  1922. return output;
  1923. }
  1924. RBSet<TileMap::TerrainConstraint> TileMap::_get_terrain_constraints_from_cells_list(int p_layer, const RBSet<Vector2i> &p_cell_list, int p_terrain_set, bool p_ignore_empty_terrains) const {
  1925. if (!tile_set.is_valid()) {
  1926. return RBSet<TerrainConstraint>();
  1927. }
  1928. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), RBSet<TerrainConstraint>());
  1929. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), RBSet<TerrainConstraint>());
  1930. // Build a set of dummy constraints to get the constrained points.
  1931. RBSet<TerrainConstraint> dummy_constraints;
  1932. for (const Vector2i &E : p_cell_list) {
  1933. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { // Iterates over sides.
  1934. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1935. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1936. dummy_constraints.insert(TerrainConstraint(this, E, bit, -1));
  1937. }
  1938. }
  1939. }
  1940. // For each constrained point, we get all overlapping tiles, and select the most adequate terrain for it.
  1941. RBSet<TerrainConstraint> constraints;
  1942. for (const TerrainConstraint &E_constraint : dummy_constraints) {
  1943. HashMap<int, int> terrain_count;
  1944. // Count the number of occurrences per terrain.
  1945. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = E_constraint.get_overlapping_coords_and_peering_bits();
  1946. for (const KeyValue<Vector2i, TileSet::CellNeighbor> &E_overlapping : overlapping_terrain_bits) {
  1947. TileData *neighbor_tile_data = nullptr;
  1948. TileMapCell neighbor_cell = get_cell(p_layer, E_overlapping.key);
  1949. if (neighbor_cell.source_id != TileSet::INVALID_SOURCE) {
  1950. Ref<TileSetSource> source = tile_set->get_source(neighbor_cell.source_id);
  1951. Ref<TileSetAtlasSource> atlas_source = source;
  1952. if (atlas_source.is_valid()) {
  1953. TileData *tile_data = atlas_source->get_tile_data(neighbor_cell.get_atlas_coords(), neighbor_cell.alternative_tile);
  1954. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1955. neighbor_tile_data = tile_data;
  1956. }
  1957. }
  1958. }
  1959. int terrain = neighbor_tile_data ? neighbor_tile_data->get_terrain_peering_bit(TileSet::CellNeighbor(E_overlapping.value)) : -1;
  1960. if (!p_ignore_empty_terrains || terrain >= 0) {
  1961. if (!terrain_count.has(terrain)) {
  1962. terrain_count[terrain] = 0;
  1963. }
  1964. terrain_count[terrain] += 1;
  1965. }
  1966. }
  1967. // Get the terrain with the max number of occurrences.
  1968. int max = 0;
  1969. int max_terrain = -1;
  1970. for (const KeyValue<int, int> &E_terrain_count : terrain_count) {
  1971. if (E_terrain_count.value > max) {
  1972. max = E_terrain_count.value;
  1973. max_terrain = E_terrain_count.key;
  1974. }
  1975. }
  1976. // Set the adequate terrain.
  1977. if (max > 0) {
  1978. TerrainConstraint c = E_constraint;
  1979. c.set_terrain(max_terrain);
  1980. constraints.insert(c);
  1981. }
  1982. }
  1983. // Add the centers as constraints
  1984. for (Vector2i E_coords : p_cell_list) {
  1985. TileData *tile_data = nullptr;
  1986. TileMapCell cell = get_cell(p_layer, E_coords);
  1987. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1988. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1989. Ref<TileSetAtlasSource> atlas_source = source;
  1990. if (atlas_source.is_valid()) {
  1991. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1992. }
  1993. }
  1994. int terrain = (tile_data && tile_data->get_terrain_set() == p_terrain_set) ? tile_data->get_terrain() : -1;
  1995. if (!p_ignore_empty_terrains || terrain >= 0) {
  1996. constraints.insert(TerrainConstraint(this, E_coords, terrain));
  1997. }
  1998. }
  1999. return constraints;
  2000. }
  2001. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_constraints(const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> p_constraints) {
  2002. if (!tile_set.is_valid()) {
  2003. return HashMap<Vector2i, TileSet::TerrainsPattern>();
  2004. }
  2005. // Copy the constraints set.
  2006. RBSet<TerrainConstraint> constraints = p_constraints;
  2007. // Output map.
  2008. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2009. // Add all positions to a set.
  2010. for (int i = 0; i < p_to_replace.size(); i++) {
  2011. const Vector2i &coords = p_to_replace[i];
  2012. // Select the best pattern for the given constraints
  2013. TileSet::TerrainsPattern pattern = _get_best_terrain_pattern_for_constraints(p_terrain_set, coords, constraints);
  2014. // Update the constraint set with the new ones
  2015. RBSet<TerrainConstraint> new_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, pattern);
  2016. for (const TerrainConstraint &E_constraint : new_constraints) {
  2017. if (constraints.has(E_constraint)) {
  2018. constraints.erase(E_constraint);
  2019. }
  2020. TerrainConstraint c = E_constraint;
  2021. c.set_priority(5);
  2022. constraints.insert(c);
  2023. }
  2024. output[coords] = pattern;
  2025. }
  2026. return output;
  2027. }
  2028. 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) {
  2029. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2030. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2031. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  2032. // Build list and set of tiles that can be modified (painted and their surroundings)
  2033. Vector<Vector2i> can_modify_list;
  2034. RBSet<Vector2i> can_modify_set;
  2035. RBSet<Vector2i> painted_set;
  2036. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  2037. const Vector2i &coords = p_coords_array[i];
  2038. can_modify_list.push_back(coords);
  2039. can_modify_set.insert(coords);
  2040. painted_set.insert(coords);
  2041. }
  2042. for (Vector2i coords : p_coords_array) {
  2043. // Find the adequate neighbor
  2044. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2045. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2046. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2047. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2048. if (!can_modify_set.has(neighbor)) {
  2049. can_modify_list.push_back(neighbor);
  2050. can_modify_set.insert(neighbor);
  2051. }
  2052. }
  2053. }
  2054. }
  2055. // 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
  2056. RBSet<Vector2i> cells_with_terrain_center_bit;
  2057. for (Vector2i coords : can_modify_set) {
  2058. bool connect = false;
  2059. if (painted_set.has(coords)) {
  2060. connect = true;
  2061. } else {
  2062. // Get the center bit of the cell
  2063. TileData *tile_data = nullptr;
  2064. TileMapCell cell = get_cell(p_layer, coords);
  2065. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2066. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  2067. Ref<TileSetAtlasSource> atlas_source = source;
  2068. if (atlas_source.is_valid()) {
  2069. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2070. }
  2071. }
  2072. if (tile_data && tile_data->get_terrain_set() == p_terrain_set && tile_data->get_terrain() == p_terrain) {
  2073. connect = true;
  2074. }
  2075. }
  2076. if (connect) {
  2077. cells_with_terrain_center_bit.insert(coords);
  2078. }
  2079. }
  2080. RBSet<TerrainConstraint> constraints;
  2081. // Add new constraints from the path drawn.
  2082. for (Vector2i coords : p_coords_array) {
  2083. // Constraints on the center bit.
  2084. TerrainConstraint c = TerrainConstraint(this, coords, p_terrain);
  2085. c.set_priority(10);
  2086. constraints.insert(c);
  2087. // Constraints on the connecting bits.
  2088. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2089. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2090. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2091. c = TerrainConstraint(this, coords, bit, p_terrain);
  2092. c.set_priority(10);
  2093. if ((int(bit) % 2) == 0) {
  2094. // Side peering bits: add the constraint if the center is of the same terrain
  2095. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2096. if (cells_with_terrain_center_bit.has(neighbor)) {
  2097. constraints.insert(c);
  2098. }
  2099. } else {
  2100. // Corner peering bits: add the constraint if all tiles on the constraint has the same center bit
  2101. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = c.get_overlapping_coords_and_peering_bits();
  2102. bool valid = true;
  2103. for (KeyValue<Vector2i, TileSet::CellNeighbor> kv : overlapping_terrain_bits) {
  2104. if (!cells_with_terrain_center_bit.has(kv.key)) {
  2105. valid = false;
  2106. break;
  2107. }
  2108. }
  2109. if (valid) {
  2110. constraints.insert(c);
  2111. }
  2112. }
  2113. }
  2114. }
  2115. }
  2116. // Fills in the constraint list from existing tiles.
  2117. for (TerrainConstraint c : _get_terrain_constraints_from_cells_list(p_layer, can_modify_set, p_terrain_set, p_ignore_empty_terrains)) {
  2118. constraints.insert(c);
  2119. }
  2120. // Fill the terrains.
  2121. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  2122. return output;
  2123. }
  2124. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_path(int p_layer, const Vector<Vector2i> &p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2125. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2126. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2127. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  2128. // Make sure the path is correct and build the peering bit list while doing it.
  2129. Vector<TileSet::CellNeighbor> neighbor_list;
  2130. for (int i = 0; i < p_path.size() - 1; i++) {
  2131. // Find the adequate neighbor
  2132. TileSet::CellNeighbor found_bit = TileSet::CELL_NEIGHBOR_MAX;
  2133. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2134. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2135. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2136. if (get_neighbor_cell(p_path[i], bit) == p_path[i + 1]) {
  2137. found_bit = bit;
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. ERR_FAIL_COND_V_MSG(found_bit == TileSet::CELL_NEIGHBOR_MAX, output, vformat("Invalid terrain path, %s is not a neighbouring tile of %s", p_path[i + 1], p_path[i]));
  2143. neighbor_list.push_back(found_bit);
  2144. }
  2145. // Build list and set of tiles that can be modified (painted and their surroundings)
  2146. Vector<Vector2i> can_modify_list;
  2147. RBSet<Vector2i> can_modify_set;
  2148. for (int i = p_path.size() - 1; i >= 0; i--) {
  2149. const Vector2i &coords = p_path[i];
  2150. can_modify_list.push_back(coords);
  2151. can_modify_set.insert(coords);
  2152. }
  2153. for (Vector2i coords : p_path) {
  2154. // Find the adequate neighbor
  2155. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2156. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2157. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2158. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2159. if (!can_modify_set.has(neighbor)) {
  2160. can_modify_list.push_back(neighbor);
  2161. can_modify_set.insert(neighbor);
  2162. }
  2163. }
  2164. }
  2165. }
  2166. RBSet<TerrainConstraint> constraints;
  2167. // Add new constraints from the path drawn.
  2168. for (Vector2i coords : p_path) {
  2169. // Constraints on the center bit
  2170. TerrainConstraint c = TerrainConstraint(this, coords, p_terrain);
  2171. c.set_priority(10);
  2172. constraints.insert(c);
  2173. }
  2174. for (int i = 0; i < p_path.size() - 1; i++) {
  2175. // Constraints on the peering bits.
  2176. TerrainConstraint c = TerrainConstraint(this, p_path[i], neighbor_list[i], p_terrain);
  2177. c.set_priority(10);
  2178. constraints.insert(c);
  2179. }
  2180. // Fills in the constraint list from existing tiles.
  2181. for (TerrainConstraint c : _get_terrain_constraints_from_cells_list(p_layer, can_modify_set, p_terrain_set, p_ignore_empty_terrains)) {
  2182. constraints.insert(c);
  2183. }
  2184. // Fill the terrains.
  2185. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  2186. return output;
  2187. }
  2188. 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) {
  2189. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2190. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2191. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  2192. // Build list and set of tiles that can be modified (painted and their surroundings).
  2193. Vector<Vector2i> can_modify_list;
  2194. RBSet<Vector2i> can_modify_set;
  2195. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  2196. const Vector2i &coords = p_coords_array[i];
  2197. can_modify_list.push_back(coords);
  2198. can_modify_set.insert(coords);
  2199. }
  2200. for (Vector2i coords : p_coords_array) {
  2201. // Find the adequate neighbor
  2202. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2203. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2204. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2205. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2206. if (!can_modify_set.has(neighbor)) {
  2207. can_modify_list.push_back(neighbor);
  2208. can_modify_set.insert(neighbor);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. // Add constraint by the new ones.
  2214. RBSet<TerrainConstraint> constraints;
  2215. // Add new constraints from the path drawn.
  2216. for (Vector2i coords : p_coords_array) {
  2217. // Constraints on the center bit
  2218. RBSet<TerrainConstraint> added_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, p_terrains_pattern);
  2219. for (TerrainConstraint c : added_constraints) {
  2220. c.set_priority(10);
  2221. constraints.insert(c);
  2222. }
  2223. }
  2224. // Fills in the constraint list from modified tiles border.
  2225. for (TerrainConstraint c : _get_terrain_constraints_from_cells_list(p_layer, can_modify_set, p_terrain_set, p_ignore_empty_terrains)) {
  2226. constraints.insert(c);
  2227. }
  2228. // Fill the terrains.
  2229. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  2230. return output;
  2231. }
  2232. 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) {
  2233. ERR_FAIL_COND(!tile_set.is_valid());
  2234. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2235. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2236. Vector<Vector2i> vector_cells;
  2237. for (int i = 0; i < p_cells.size(); i++) {
  2238. vector_cells.push_back(p_cells[i]);
  2239. }
  2240. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_connect(p_layer, vector_cells, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2241. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &E : terrain_fill_output) {
  2242. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, E.value);
  2243. set_cell(p_layer, E.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2244. }
  2245. }
  2246. 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) {
  2247. ERR_FAIL_COND(!tile_set.is_valid());
  2248. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2249. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2250. Vector<Vector2i> vector_path;
  2251. for (int i = 0; i < p_path.size(); i++) {
  2252. vector_path.push_back(p_path[i]);
  2253. }
  2254. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_path(p_layer, vector_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2255. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &E : terrain_fill_output) {
  2256. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, E.value);
  2257. set_cell(p_layer, E.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2258. }
  2259. }
  2260. TileMapCell TileMap::get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  2261. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileMapCell());
  2262. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  2263. if (!tile_map.has(p_coords)) {
  2264. return TileMapCell();
  2265. } else {
  2266. TileMapCell c = tile_map.find(p_coords)->value;
  2267. if (p_use_proxies && tile_set.is_valid()) {
  2268. Array proxyed = tile_set->map_tile_proxy(c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2269. c.source_id = proxyed[0];
  2270. c.set_atlas_coords(proxyed[1]);
  2271. c.alternative_tile = proxyed[2];
  2272. }
  2273. return c;
  2274. }
  2275. }
  2276. HashMap<Vector2i, TileMapQuadrant> *TileMap::get_quadrant_map(int p_layer) {
  2277. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  2278. return &layers[p_layer].quadrant_map;
  2279. }
  2280. Vector2i TileMap::get_coords_for_body_rid(RID p_physics_body) {
  2281. ERR_FAIL_COND_V_MSG(!bodies_coords.has(p_physics_body), Vector2i(), vformat("No tiles for the given body RID %d.", p_physics_body));
  2282. return bodies_coords[p_physics_body];
  2283. }
  2284. void TileMap::fix_invalid_tiles() {
  2285. ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot fix invalid tiles if Tileset is not open.");
  2286. for (unsigned int i = 0; i < layers.size(); i++) {
  2287. const HashMap<Vector2i, TileMapCell> &tile_map = layers[i].tile_map;
  2288. RBSet<Vector2i> coords;
  2289. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  2290. TileSetSource *source = *tile_set->get_source(E.value.source_id);
  2291. if (!source || !source->has_tile(E.value.get_atlas_coords()) || !source->has_alternative_tile(E.value.get_atlas_coords(), E.value.alternative_tile)) {
  2292. coords.insert(E.key);
  2293. }
  2294. }
  2295. for (const Vector2i &E : coords) {
  2296. set_cell(i, E, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  2297. }
  2298. }
  2299. }
  2300. void TileMap::clear_layer(int p_layer) {
  2301. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2302. // Remove all tiles.
  2303. _clear_layer_internals(p_layer);
  2304. layers[p_layer].tile_map.clear();
  2305. _recreate_layer_internals(p_layer);
  2306. used_rect_cache_dirty = true;
  2307. }
  2308. void TileMap::clear() {
  2309. // Remove all tiles.
  2310. _clear_internals();
  2311. for (unsigned int i = 0; i < layers.size(); i++) {
  2312. layers[i].tile_map.clear();
  2313. }
  2314. _recreate_internals();
  2315. used_rect_cache_dirty = true;
  2316. }
  2317. void TileMap::force_update(int p_layer) {
  2318. if (p_layer >= 0) {
  2319. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2320. _clear_layer_internals(p_layer);
  2321. _recreate_layer_internals(p_layer);
  2322. } else {
  2323. _clear_internals();
  2324. _recreate_internals();
  2325. }
  2326. }
  2327. void TileMap::_set_tile_data(int p_layer, const Vector<int> &p_data) {
  2328. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2329. ERR_FAIL_COND(format > FORMAT_3);
  2330. // Set data for a given tile from raw data.
  2331. int c = p_data.size();
  2332. const int *r = p_data.ptr();
  2333. int offset = (format >= FORMAT_2) ? 3 : 2;
  2334. ERR_FAIL_COND_MSG(c % offset != 0, "Corrupted tile data.");
  2335. clear_layer(p_layer);
  2336. #ifdef DISABLE_DEPRECATED
  2337. ERR_FAIL_COND_MSG(format != FORMAT_3, vformat("Cannot handle deprecated TileMap data format version %d. This Godot version was compiled with no support for deprecated data.", format));
  2338. #endif
  2339. for (int i = 0; i < c; i += offset) {
  2340. const uint8_t *ptr = (const uint8_t *)&r[i];
  2341. uint8_t local[12];
  2342. for (int j = 0; j < ((format >= FORMAT_2) ? 12 : 8); j++) {
  2343. local[j] = ptr[j];
  2344. }
  2345. #ifdef BIG_ENDIAN_ENABLED
  2346. SWAP(local[0], local[3]);
  2347. SWAP(local[1], local[2]);
  2348. SWAP(local[4], local[7]);
  2349. SWAP(local[5], local[6]);
  2350. //TODO: ask someone to check this...
  2351. if (FORMAT >= FORMAT_2) {
  2352. SWAP(local[8], local[11]);
  2353. SWAP(local[9], local[10]);
  2354. }
  2355. #endif
  2356. // Extracts position in TileMap.
  2357. int16_t x = decode_uint16(&local[0]);
  2358. int16_t y = decode_uint16(&local[2]);
  2359. if (format == FORMAT_3) {
  2360. uint16_t source_id = decode_uint16(&local[4]);
  2361. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  2362. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  2363. uint16_t alternative_tile = decode_uint16(&local[10]);
  2364. set_cell(p_layer, Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  2365. } else {
  2366. #ifndef DISABLE_DEPRECATED
  2367. // Previous decated format.
  2368. uint32_t v = decode_uint32(&local[4]);
  2369. // Extract the transform flags that used to be in the tilemap.
  2370. bool flip_h = v & (1UL << 29);
  2371. bool flip_v = v & (1UL << 30);
  2372. bool transpose = v & (1UL << 31);
  2373. v &= (1UL << 29) - 1;
  2374. // Extract autotile/atlas coords.
  2375. int16_t coord_x = 0;
  2376. int16_t coord_y = 0;
  2377. if (format == FORMAT_2) {
  2378. coord_x = decode_uint16(&local[8]);
  2379. coord_y = decode_uint16(&local[10]);
  2380. }
  2381. if (tile_set.is_valid()) {
  2382. Array a = tile_set->compatibility_tilemap_map(v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose);
  2383. if (a.size() == 3) {
  2384. set_cell(p_layer, Vector2i(x, y), a[0], a[1], a[2]);
  2385. } else {
  2386. 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));
  2387. }
  2388. } else {
  2389. int compatibility_alternative_tile = ((int)flip_h) + ((int)flip_v << 1) + ((int)transpose << 2);
  2390. set_cell(p_layer, Vector2i(x, y), v, Vector2i(coord_x, coord_y), compatibility_alternative_tile);
  2391. }
  2392. #endif
  2393. }
  2394. }
  2395. emit_signal(SNAME("changed"));
  2396. }
  2397. Vector<int> TileMap::_get_tile_data(int p_layer) const {
  2398. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), Vector<int>());
  2399. // Export tile data to raw format
  2400. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  2401. Vector<int> data;
  2402. data.resize(tile_map.size() * 3);
  2403. int *w = data.ptrw();
  2404. // Save in highest format
  2405. int idx = 0;
  2406. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  2407. uint8_t *ptr = (uint8_t *)&w[idx];
  2408. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  2409. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  2410. encode_uint16(E.value.source_id, &ptr[4]);
  2411. encode_uint16(E.value.coord_x, &ptr[6]);
  2412. encode_uint16(E.value.coord_y, &ptr[8]);
  2413. encode_uint16(E.value.alternative_tile, &ptr[10]);
  2414. idx += 3;
  2415. }
  2416. return data;
  2417. }
  2418. void TileMap::_build_runtime_update_tile_data(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  2419. if (GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update)) {
  2420. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  2421. while (q_list_element) {
  2422. TileMapQuadrant &q = *q_list_element->self();
  2423. // Iterate over the cells of the quadrant.
  2424. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.world_to_map) {
  2425. TileMapCell c = get_cell(q.layer, E_cell.value, true);
  2426. TileSetSource *source;
  2427. if (tile_set->has_source(c.source_id)) {
  2428. source = *tile_set->get_source(c.source_id);
  2429. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  2430. continue;
  2431. }
  2432. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2433. if (atlas_source) {
  2434. bool ret = false;
  2435. if (GDVIRTUAL_CALL(_use_tile_data_runtime_update, q.layer, E_cell.value, ret) && ret) {
  2436. TileData *tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  2437. // Create the runtime TileData.
  2438. TileData *tile_data_runtime_use = tile_data->duplicate();
  2439. tile_data->set_allow_transform(true);
  2440. q.runtime_tile_data_cache[E_cell.value] = tile_data_runtime_use;
  2441. GDVIRTUAL_CALL(_tile_data_runtime_update, q.layer, E_cell.value, tile_data_runtime_use);
  2442. }
  2443. }
  2444. }
  2445. }
  2446. q_list_element = q_list_element->next();
  2447. }
  2448. }
  2449. }
  2450. #ifdef TOOLS_ENABLED
  2451. Rect2 TileMap::_edit_get_rect() const {
  2452. // Return the visible rect of the tilemap
  2453. if (pending_update) {
  2454. const_cast<TileMap *>(this)->_update_dirty_quadrants();
  2455. } else {
  2456. const_cast<TileMap *>(this)->_recompute_rect_cache();
  2457. }
  2458. return rect_cache;
  2459. }
  2460. #endif
  2461. bool TileMap::_set(const StringName &p_name, const Variant &p_value) {
  2462. Vector<String> components = String(p_name).split("/", true, 2);
  2463. if (p_name == "format") {
  2464. if (p_value.get_type() == Variant::INT) {
  2465. format = (DataFormat)(p_value.operator int64_t()); // Set format used for loading
  2466. return true;
  2467. }
  2468. } else if (p_name == "tile_data") { // Kept for compatibility reasons.
  2469. if (p_value.is_array()) {
  2470. if (layers.size() < 1) {
  2471. layers.resize(1);
  2472. }
  2473. _set_tile_data(0, p_value);
  2474. return true;
  2475. }
  2476. return false;
  2477. } else if (components.size() == 2 && components[0].begins_with("layer_") && components[0].trim_prefix("layer_").is_valid_int()) {
  2478. int index = components[0].trim_prefix("layer_").to_int();
  2479. if (index < 0) {
  2480. return false;
  2481. }
  2482. if (index >= (int)layers.size()) {
  2483. _clear_internals();
  2484. while (index >= (int)layers.size()) {
  2485. layers.push_back(TileMapLayer());
  2486. }
  2487. _recreate_internals();
  2488. notify_property_list_changed();
  2489. emit_signal(SNAME("changed"));
  2490. update_configuration_warnings();
  2491. }
  2492. if (components[1] == "name") {
  2493. set_layer_name(index, p_value);
  2494. return true;
  2495. } else if (components[1] == "enabled") {
  2496. set_layer_enabled(index, p_value);
  2497. return true;
  2498. } else if (components[1] == "modulate") {
  2499. set_layer_modulate(index, p_value);
  2500. return true;
  2501. } else if (components[1] == "y_sort_enabled") {
  2502. set_layer_y_sort_enabled(index, p_value);
  2503. return true;
  2504. } else if (components[1] == "y_sort_origin") {
  2505. set_layer_y_sort_origin(index, p_value);
  2506. return true;
  2507. } else if (components[1] == "z_index") {
  2508. set_layer_z_index(index, p_value);
  2509. return true;
  2510. } else if (components[1] == "tile_data") {
  2511. _set_tile_data(index, p_value);
  2512. return true;
  2513. } else {
  2514. return false;
  2515. }
  2516. }
  2517. return false;
  2518. }
  2519. bool TileMap::_get(const StringName &p_name, Variant &r_ret) const {
  2520. Vector<String> components = String(p_name).split("/", true, 2);
  2521. if (p_name == "format") {
  2522. r_ret = FORMAT_3; // When saving, always save highest format
  2523. return true;
  2524. } else if (components.size() == 2 && components[0].begins_with("layer_") && components[0].trim_prefix("layer_").is_valid_int()) {
  2525. int index = components[0].trim_prefix("layer_").to_int();
  2526. if (index < 0 || index >= (int)layers.size()) {
  2527. return false;
  2528. }
  2529. if (components[1] == "name") {
  2530. r_ret = get_layer_name(index);
  2531. return true;
  2532. } else if (components[1] == "enabled") {
  2533. r_ret = is_layer_enabled(index);
  2534. return true;
  2535. } else if (components[1] == "modulate") {
  2536. r_ret = get_layer_modulate(index);
  2537. return true;
  2538. } else if (components[1] == "y_sort_enabled") {
  2539. r_ret = is_layer_y_sort_enabled(index);
  2540. return true;
  2541. } else if (components[1] == "y_sort_origin") {
  2542. r_ret = get_layer_y_sort_origin(index);
  2543. return true;
  2544. } else if (components[1] == "z_index") {
  2545. r_ret = get_layer_z_index(index);
  2546. return true;
  2547. } else if (components[1] == "tile_data") {
  2548. r_ret = _get_tile_data(index);
  2549. return true;
  2550. } else {
  2551. return false;
  2552. }
  2553. }
  2554. return false;
  2555. }
  2556. void TileMap::_get_property_list(List<PropertyInfo> *p_list) const {
  2557. p_list->push_back(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  2558. p_list->push_back(PropertyInfo(Variant::NIL, "Layers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2559. for (unsigned int i = 0; i < layers.size(); i++) {
  2560. p_list->push_back(PropertyInfo(Variant::STRING, vformat("layer_%d/name", i), PROPERTY_HINT_NONE));
  2561. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("layer_%d/enabled", i), PROPERTY_HINT_NONE));
  2562. p_list->push_back(PropertyInfo(Variant::COLOR, vformat("layer_%d/modulate", i), PROPERTY_HINT_NONE));
  2563. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("layer_%d/y_sort_enabled", i), PROPERTY_HINT_NONE));
  2564. p_list->push_back(PropertyInfo(Variant::INT, vformat("layer_%d/y_sort_origin", i), PROPERTY_HINT_NONE, "suffix:px"));
  2565. p_list->push_back(PropertyInfo(Variant::INT, vformat("layer_%d/z_index", i), PROPERTY_HINT_NONE));
  2566. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("layer_%d/tile_data", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2567. }
  2568. }
  2569. Vector2 TileMap::map_to_world(const Vector2i &p_pos) const {
  2570. // SHOULD RETURN THE CENTER OF THE CELL
  2571. ERR_FAIL_COND_V(!tile_set.is_valid(), Vector2());
  2572. Vector2 ret = p_pos;
  2573. TileSet::TileShape tile_shape = tile_set->get_tile_shape();
  2574. TileSet::TileOffsetAxis tile_offset_axis = tile_set->get_tile_offset_axis();
  2575. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2576. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  2577. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap
  2578. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2579. switch (tile_set->get_tile_layout()) {
  2580. case TileSet::TILE_LAYOUT_STACKED:
  2581. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 0 ? 0.0 : 0.5), ret.y);
  2582. break;
  2583. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2584. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 1 ? 0.0 : 0.5), ret.y);
  2585. break;
  2586. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2587. ret = Vector2(ret.x + ret.y / 2, ret.y);
  2588. break;
  2589. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2590. ret = Vector2(ret.x / 2, ret.y * 2 + ret.x);
  2591. break;
  2592. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2593. ret = Vector2((ret.x + ret.y) / 2, ret.y - ret.x);
  2594. break;
  2595. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2596. ret = Vector2((ret.x - ret.y) / 2, ret.y + ret.x);
  2597. break;
  2598. }
  2599. } else { // TILE_OFFSET_AXIS_VERTICAL
  2600. switch (tile_set->get_tile_layout()) {
  2601. case TileSet::TILE_LAYOUT_STACKED:
  2602. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 0 ? 0.0 : 0.5));
  2603. break;
  2604. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2605. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 1 ? 0.0 : 0.5));
  2606. break;
  2607. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2608. ret = Vector2(ret.x * 2 + ret.y, ret.y / 2);
  2609. break;
  2610. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2611. ret = Vector2(ret.x, ret.y + ret.x / 2);
  2612. break;
  2613. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2614. ret = Vector2(ret.x + ret.y, (ret.y - ret.x) / 2);
  2615. break;
  2616. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2617. ret = Vector2(ret.x - ret.y, (ret.y + ret.x) / 2);
  2618. break;
  2619. }
  2620. }
  2621. }
  2622. // Multiply by the overlapping ratio
  2623. double overlapping_ratio = 1.0;
  2624. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2625. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2626. overlapping_ratio = 0.5;
  2627. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2628. overlapping_ratio = 0.75;
  2629. }
  2630. ret.y *= overlapping_ratio;
  2631. } else { // TILE_OFFSET_AXIS_VERTICAL
  2632. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2633. overlapping_ratio = 0.5;
  2634. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2635. overlapping_ratio = 0.75;
  2636. }
  2637. ret.x *= overlapping_ratio;
  2638. }
  2639. return (ret + Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  2640. }
  2641. Vector2i TileMap::world_to_map(const Vector2 &p_pos) const {
  2642. ERR_FAIL_COND_V(!tile_set.is_valid(), Vector2i());
  2643. Vector2 ret = p_pos;
  2644. ret /= tile_set->get_tile_size();
  2645. TileSet::TileShape tile_shape = tile_set->get_tile_shape();
  2646. TileSet::TileOffsetAxis tile_offset_axis = tile_set->get_tile_offset_axis();
  2647. TileSet::TileLayout tile_layout = tile_set->get_tile_layout();
  2648. // Divide by the overlapping ratio
  2649. double overlapping_ratio = 1.0;
  2650. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2651. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2652. overlapping_ratio = 0.5;
  2653. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2654. overlapping_ratio = 0.75;
  2655. }
  2656. ret.y /= overlapping_ratio;
  2657. } else { // TILE_OFFSET_AXIS_VERTICAL
  2658. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2659. overlapping_ratio = 0.5;
  2660. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2661. overlapping_ratio = 0.75;
  2662. }
  2663. ret.x /= overlapping_ratio;
  2664. }
  2665. // For each half-offset shape, we check if we are in the corner of the tile, and thus should correct the world position accordingly.
  2666. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2667. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  2668. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap
  2669. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2670. // Smart floor of the position
  2671. Vector2 raw_pos = ret;
  2672. if (Math::posmod(Math::floor(ret.y), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  2673. ret = Vector2(Math::floor(ret.x + 0.5) - 0.5, Math::floor(ret.y));
  2674. } else {
  2675. ret = ret.floor();
  2676. }
  2677. // Compute the tile offset, and if we might the output for a neighbour top tile
  2678. Vector2 in_tile_pos = raw_pos - ret;
  2679. bool in_top_left_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(-0.5, 1.0 / overlapping_ratio - 1)) <= 0;
  2680. bool in_top_right_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(0.5, 1.0 / overlapping_ratio - 1)) > 0;
  2681. switch (tile_layout) {
  2682. case TileSet::TILE_LAYOUT_STACKED:
  2683. ret = ret.floor();
  2684. if (in_top_left_triangle) {
  2685. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : -1, -1);
  2686. } else if (in_top_right_triangle) {
  2687. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 1 : 0, -1);
  2688. }
  2689. break;
  2690. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2691. ret = ret.floor();
  2692. if (in_top_left_triangle) {
  2693. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? -1 : 0, -1);
  2694. } else if (in_top_right_triangle) {
  2695. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : 1, -1);
  2696. }
  2697. break;
  2698. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2699. ret = Vector2(ret.x - ret.y / 2, ret.y).floor();
  2700. if (in_top_left_triangle) {
  2701. ret += Vector2i(0, -1);
  2702. } else if (in_top_right_triangle) {
  2703. ret += Vector2i(1, -1);
  2704. }
  2705. break;
  2706. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2707. ret = Vector2(ret.x * 2, ret.y / 2 - ret.x).floor();
  2708. if (in_top_left_triangle) {
  2709. ret += Vector2i(-1, 0);
  2710. } else if (in_top_right_triangle) {
  2711. ret += Vector2i(1, -1);
  2712. }
  2713. break;
  2714. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2715. ret = Vector2(ret.x - ret.y / 2, ret.y / 2 + ret.x).floor();
  2716. if (in_top_left_triangle) {
  2717. ret += Vector2i(0, -1);
  2718. } else if (in_top_right_triangle) {
  2719. ret += Vector2i(1, 0);
  2720. }
  2721. break;
  2722. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2723. ret = Vector2(ret.x + ret.y / 2, ret.y / 2 - ret.x).floor();
  2724. if (in_top_left_triangle) {
  2725. ret += Vector2i(-1, 0);
  2726. } else if (in_top_right_triangle) {
  2727. ret += Vector2i(0, -1);
  2728. }
  2729. break;
  2730. }
  2731. } else { // TILE_OFFSET_AXIS_VERTICAL
  2732. // Smart floor of the position
  2733. Vector2 raw_pos = ret;
  2734. if (Math::posmod(Math::floor(ret.x), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  2735. ret = Vector2(Math::floor(ret.x), Math::floor(ret.y + 0.5) - 0.5);
  2736. } else {
  2737. ret = ret.floor();
  2738. }
  2739. // Compute the tile offset, and if we might the output for a neighbour top tile
  2740. Vector2 in_tile_pos = raw_pos - ret;
  2741. bool in_top_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, -0.5)) > 0;
  2742. bool in_bottom_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, 0.5)) <= 0;
  2743. switch (tile_layout) {
  2744. case TileSet::TILE_LAYOUT_STACKED:
  2745. ret = ret.floor();
  2746. if (in_top_left_triangle) {
  2747. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : -1);
  2748. } else if (in_bottom_left_triangle) {
  2749. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 1 : 0);
  2750. }
  2751. break;
  2752. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2753. ret = ret.floor();
  2754. if (in_top_left_triangle) {
  2755. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? -1 : 0);
  2756. } else if (in_bottom_left_triangle) {
  2757. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : 1);
  2758. }
  2759. break;
  2760. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2761. ret = Vector2(ret.x / 2 - ret.y, ret.y * 2).floor();
  2762. if (in_top_left_triangle) {
  2763. ret += Vector2i(0, -1);
  2764. } else if (in_bottom_left_triangle) {
  2765. ret += Vector2i(-1, 1);
  2766. }
  2767. break;
  2768. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2769. ret = Vector2(ret.x, ret.y - ret.x / 2).floor();
  2770. if (in_top_left_triangle) {
  2771. ret += Vector2i(-1, 0);
  2772. } else if (in_bottom_left_triangle) {
  2773. ret += Vector2i(-1, 1);
  2774. }
  2775. break;
  2776. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2777. ret = Vector2(ret.x / 2 - ret.y, ret.y + ret.x / 2).floor();
  2778. if (in_top_left_triangle) {
  2779. ret += Vector2i(0, -1);
  2780. } else if (in_bottom_left_triangle) {
  2781. ret += Vector2i(-1, 0);
  2782. }
  2783. break;
  2784. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2785. ret = Vector2(ret.x / 2 + ret.y, ret.y - ret.x / 2).floor();
  2786. if (in_top_left_triangle) {
  2787. ret += Vector2i(-1, 0);
  2788. } else if (in_bottom_left_triangle) {
  2789. ret += Vector2i(0, 1);
  2790. }
  2791. break;
  2792. }
  2793. }
  2794. } else {
  2795. ret = (ret + Vector2(0.00005, 0.00005)).floor();
  2796. }
  2797. return Vector2i(ret);
  2798. }
  2799. bool TileMap::is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const {
  2800. ERR_FAIL_COND_V(!tile_set.is_valid(), false);
  2801. TileSet::TileShape shape = tile_set->get_tile_shape();
  2802. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2803. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  2804. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  2805. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  2806. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  2807. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  2808. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  2809. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  2810. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2811. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2812. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  2813. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  2814. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  2815. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  2816. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  2817. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  2818. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  2819. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2820. } else {
  2821. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2822. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  2823. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  2824. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  2825. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  2826. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  2827. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2828. } else {
  2829. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  2830. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  2831. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  2832. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  2833. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  2834. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2835. }
  2836. }
  2837. }
  2838. Vector2i TileMap::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  2839. ERR_FAIL_COND_V(!tile_set.is_valid(), p_coords);
  2840. TileSet::TileShape shape = tile_set->get_tile_shape();
  2841. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2842. switch (p_cell_neighbor) {
  2843. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2844. return p_coords + Vector2i(1, 0);
  2845. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2846. return p_coords + Vector2i(1, 1);
  2847. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2848. return p_coords + Vector2i(0, 1);
  2849. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2850. return p_coords + Vector2i(-1, 1);
  2851. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2852. return p_coords + Vector2i(-1, 0);
  2853. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2854. return p_coords + Vector2i(-1, -1);
  2855. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2856. return p_coords + Vector2i(0, -1);
  2857. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2858. return p_coords + Vector2i(1, -1);
  2859. default:
  2860. ERR_FAIL_V(p_coords);
  2861. }
  2862. } else { // Half-offset shapes (square and hexagon)
  2863. switch (tile_set->get_tile_layout()) {
  2864. case TileSet::TILE_LAYOUT_STACKED: {
  2865. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2866. bool is_offset = p_coords.y % 2;
  2867. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  2868. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  2869. return p_coords + Vector2i(1, 0);
  2870. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  2871. return p_coords + Vector2i(is_offset ? 1 : 0, 1);
  2872. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  2873. return p_coords + Vector2i(0, 2);
  2874. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  2875. return p_coords + Vector2i(is_offset ? 0 : -1, 1);
  2876. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  2877. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  2878. return p_coords + Vector2i(-1, 0);
  2879. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  2880. return p_coords + Vector2i(is_offset ? 0 : -1, -1);
  2881. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  2882. return p_coords + Vector2i(0, -2);
  2883. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  2884. return p_coords + Vector2i(is_offset ? 1 : 0, -1);
  2885. } else {
  2886. ERR_FAIL_V(p_coords);
  2887. }
  2888. } else {
  2889. bool is_offset = p_coords.x % 2;
  2890. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  2891. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  2892. return p_coords + Vector2i(0, 1);
  2893. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  2894. return p_coords + Vector2i(1, is_offset ? 1 : 0);
  2895. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  2896. return p_coords + Vector2i(2, 0);
  2897. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  2898. return p_coords + Vector2i(1, is_offset ? 0 : -1);
  2899. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  2900. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  2901. return p_coords + Vector2i(0, -1);
  2902. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  2903. return p_coords + Vector2i(-1, is_offset ? 0 : -1);
  2904. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  2905. return p_coords + Vector2i(-2, 0);
  2906. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  2907. return p_coords + Vector2i(-1, is_offset ? 1 : 0);
  2908. } else {
  2909. ERR_FAIL_V(p_coords);
  2910. }
  2911. }
  2912. } break;
  2913. case TileSet::TILE_LAYOUT_STACKED_OFFSET: {
  2914. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2915. bool is_offset = p_coords.y % 2;
  2916. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  2917. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  2918. return p_coords + Vector2i(1, 0);
  2919. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  2920. return p_coords + Vector2i(is_offset ? 0 : 1, 1);
  2921. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  2922. return p_coords + Vector2i(0, 2);
  2923. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  2924. return p_coords + Vector2i(is_offset ? -1 : 0, 1);
  2925. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  2926. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  2927. return p_coords + Vector2i(-1, 0);
  2928. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  2929. return p_coords + Vector2i(is_offset ? -1 : 0, -1);
  2930. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  2931. return p_coords + Vector2i(0, -2);
  2932. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  2933. return p_coords + Vector2i(is_offset ? 0 : 1, -1);
  2934. } else {
  2935. ERR_FAIL_V(p_coords);
  2936. }
  2937. } else {
  2938. bool is_offset = p_coords.x % 2;
  2939. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  2940. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  2941. return p_coords + Vector2i(0, 1);
  2942. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  2943. return p_coords + Vector2i(1, is_offset ? 0 : 1);
  2944. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  2945. return p_coords + Vector2i(2, 0);
  2946. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  2947. return p_coords + Vector2i(1, is_offset ? -1 : 0);
  2948. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  2949. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  2950. return p_coords + Vector2i(0, -1);
  2951. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  2952. return p_coords + Vector2i(-1, is_offset ? -1 : 0);
  2953. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  2954. return p_coords + Vector2i(-2, 0);
  2955. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  2956. return p_coords + Vector2i(-1, is_offset ? 0 : 1);
  2957. } else {
  2958. ERR_FAIL_V(p_coords);
  2959. }
  2960. }
  2961. } break;
  2962. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2963. case TileSet::TILE_LAYOUT_STAIRS_DOWN: {
  2964. if ((tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2965. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2966. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  2967. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  2968. return p_coords + Vector2i(1, 0);
  2969. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  2970. return p_coords + Vector2i(0, 1);
  2971. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  2972. return p_coords + Vector2i(-1, 2);
  2973. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  2974. return p_coords + Vector2i(-1, 1);
  2975. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  2976. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  2977. return p_coords + Vector2i(-1, 0);
  2978. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  2979. return p_coords + Vector2i(0, -1);
  2980. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  2981. return p_coords + Vector2i(1, -2);
  2982. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  2983. return p_coords + Vector2i(1, -1);
  2984. } else {
  2985. ERR_FAIL_V(p_coords);
  2986. }
  2987. } else {
  2988. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  2989. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  2990. return p_coords + Vector2i(0, 1);
  2991. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  2992. return p_coords + Vector2i(1, 0);
  2993. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  2994. return p_coords + Vector2i(2, -1);
  2995. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  2996. return p_coords + Vector2i(1, -1);
  2997. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  2998. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  2999. return p_coords + Vector2i(0, -1);
  3000. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3001. return p_coords + Vector2i(-1, 0);
  3002. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3003. return p_coords + Vector2i(-2, 1);
  3004. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3005. return p_coords + Vector2i(-1, 1);
  3006. } else {
  3007. ERR_FAIL_V(p_coords);
  3008. }
  3009. }
  3010. } else {
  3011. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3012. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3013. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3014. return p_coords + Vector2i(2, -1);
  3015. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3016. return p_coords + Vector2i(1, 0);
  3017. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3018. return p_coords + Vector2i(0, 1);
  3019. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3020. return p_coords + Vector2i(-1, 1);
  3021. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3022. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3023. return p_coords + Vector2i(-2, 1);
  3024. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3025. return p_coords + Vector2i(-1, 0);
  3026. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3027. return p_coords + Vector2i(0, -1);
  3028. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3029. return p_coords + Vector2i(1, -1);
  3030. } else {
  3031. ERR_FAIL_V(p_coords);
  3032. }
  3033. } else {
  3034. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3035. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3036. return p_coords + Vector2i(-1, 2);
  3037. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3038. return p_coords + Vector2i(0, 1);
  3039. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3040. return p_coords + Vector2i(1, 0);
  3041. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3042. return p_coords + Vector2i(1, -1);
  3043. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3044. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3045. return p_coords + Vector2i(1, -2);
  3046. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3047. return p_coords + Vector2i(0, -1);
  3048. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3049. return p_coords + Vector2i(-1, 0);
  3050. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3051. return p_coords + Vector2i(-1, 1);
  3052. } else {
  3053. ERR_FAIL_V(p_coords);
  3054. }
  3055. }
  3056. }
  3057. } break;
  3058. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3059. case TileSet::TILE_LAYOUT_DIAMOND_DOWN: {
  3060. if ((tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  3061. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3062. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3063. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3064. return p_coords + Vector2i(1, 1);
  3065. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3066. return p_coords + Vector2i(0, 1);
  3067. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3068. return p_coords + Vector2i(-1, 1);
  3069. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3070. return p_coords + Vector2i(-1, 0);
  3071. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3072. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3073. return p_coords + Vector2i(-1, -1);
  3074. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3075. return p_coords + Vector2i(0, -1);
  3076. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3077. return p_coords + Vector2i(1, -1);
  3078. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3079. return p_coords + Vector2i(1, 0);
  3080. } else {
  3081. ERR_FAIL_V(p_coords);
  3082. }
  3083. } else {
  3084. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3085. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3086. return p_coords + Vector2i(1, 1);
  3087. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3088. return p_coords + Vector2i(1, 0);
  3089. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3090. return p_coords + Vector2i(1, -1);
  3091. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3092. return p_coords + Vector2i(0, -1);
  3093. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3094. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3095. return p_coords + Vector2i(-1, -1);
  3096. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3097. return p_coords + Vector2i(-1, 0);
  3098. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3099. return p_coords + Vector2i(-1, 1);
  3100. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3101. return p_coords + Vector2i(0, 1);
  3102. } else {
  3103. ERR_FAIL_V(p_coords);
  3104. }
  3105. }
  3106. } else {
  3107. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3108. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3109. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3110. return p_coords + Vector2i(1, -1);
  3111. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3112. return p_coords + Vector2i(1, 0);
  3113. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3114. return p_coords + Vector2i(1, 1);
  3115. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3116. return p_coords + Vector2i(0, 1);
  3117. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3118. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3119. return p_coords + Vector2i(-1, 1);
  3120. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3121. return p_coords + Vector2i(-1, 0);
  3122. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3123. return p_coords + Vector2i(-1, -1);
  3124. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3125. return p_coords + Vector2i(0, -1);
  3126. } else {
  3127. ERR_FAIL_V(p_coords);
  3128. }
  3129. } else {
  3130. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3131. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3132. return p_coords + Vector2i(-1, 1);
  3133. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3134. return p_coords + Vector2i(0, 1);
  3135. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3136. return p_coords + Vector2i(1, 1);
  3137. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3138. return p_coords + Vector2i(1, 0);
  3139. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3140. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3141. return p_coords + Vector2i(1, -1);
  3142. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3143. return p_coords + Vector2i(0, -1);
  3144. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3145. return p_coords + Vector2i(-1, -1);
  3146. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3147. return p_coords + Vector2i(-1, 0);
  3148. } else {
  3149. ERR_FAIL_V(p_coords);
  3150. }
  3151. }
  3152. }
  3153. } break;
  3154. default:
  3155. ERR_FAIL_V(p_coords);
  3156. }
  3157. }
  3158. }
  3159. TypedArray<Vector2i> TileMap::get_used_cells(int p_layer) const {
  3160. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TypedArray<Vector2i>());
  3161. // Returns the cells used in the tilemap.
  3162. TypedArray<Vector2i> a;
  3163. a.resize(layers[p_layer].tile_map.size());
  3164. int i = 0;
  3165. for (const KeyValue<Vector2i, TileMapCell> &E : layers[p_layer].tile_map) {
  3166. Vector2i p(E.key.x, E.key.y);
  3167. a[i++] = p;
  3168. }
  3169. return a;
  3170. }
  3171. Rect2 TileMap::get_used_rect() { // Not const because of cache
  3172. // Return the rect of the currently used area
  3173. if (used_rect_cache_dirty) {
  3174. bool first = true;
  3175. used_rect_cache = Rect2i();
  3176. for (unsigned int i = 0; i < layers.size(); i++) {
  3177. const HashMap<Vector2i, TileMapCell> &tile_map = layers[i].tile_map;
  3178. if (tile_map.size() > 0) {
  3179. if (first) {
  3180. used_rect_cache = Rect2i(tile_map.begin()->key.x, tile_map.begin()->key.y, 0, 0);
  3181. first = false;
  3182. }
  3183. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  3184. used_rect_cache.expand_to(Vector2i(E.key.x, E.key.y));
  3185. }
  3186. }
  3187. }
  3188. if (!first) { // first is true if every layer is empty.
  3189. used_rect_cache.size += Vector2i(1, 1); // The cache expands to top-left coordinate, so we add one full tile.
  3190. }
  3191. used_rect_cache_dirty = false;
  3192. }
  3193. return used_rect_cache;
  3194. }
  3195. // --- Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems ---
  3196. void TileMap::set_light_mask(int p_light_mask) {
  3197. // Occlusion: set light mask.
  3198. CanvasItem::set_light_mask(p_light_mask);
  3199. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3200. for (const KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  3201. for (const RID &ci : E.value.canvas_items) {
  3202. RenderingServer::get_singleton()->canvas_item_set_light_mask(ci, get_light_mask());
  3203. }
  3204. }
  3205. _rendering_update_layer(layer);
  3206. }
  3207. }
  3208. void TileMap::set_material(const Ref<Material> &p_material) {
  3209. // Set material for the whole tilemap.
  3210. CanvasItem::set_material(p_material);
  3211. // Update material for the whole tilemap.
  3212. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3213. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  3214. TileMapQuadrant &q = E.value;
  3215. for (const RID &ci : q.canvas_items) {
  3216. RS::get_singleton()->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  3217. }
  3218. }
  3219. _rendering_update_layer(layer);
  3220. }
  3221. }
  3222. void TileMap::set_use_parent_material(bool p_use_parent_material) {
  3223. // Set use_parent_material for the whole tilemap.
  3224. CanvasItem::set_use_parent_material(p_use_parent_material);
  3225. // Update use_parent_material for the whole tilemap.
  3226. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3227. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  3228. TileMapQuadrant &q = E.value;
  3229. for (const RID &ci : q.canvas_items) {
  3230. RS::get_singleton()->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  3231. }
  3232. }
  3233. _rendering_update_layer(layer);
  3234. }
  3235. }
  3236. void TileMap::set_texture_filter(TextureFilter p_texture_filter) {
  3237. // Set a default texture filter for the whole tilemap
  3238. CanvasItem::set_texture_filter(p_texture_filter);
  3239. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3240. for (HashMap<Vector2i, TileMapQuadrant>::Iterator F = layers[layer].quadrant_map.begin(); F; ++F) {
  3241. TileMapQuadrant &q = F->value;
  3242. for (const RID &ci : q.canvas_items) {
  3243. RenderingServer::get_singleton()->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(p_texture_filter));
  3244. _make_quadrant_dirty(F);
  3245. }
  3246. }
  3247. _rendering_update_layer(layer);
  3248. }
  3249. }
  3250. void TileMap::set_texture_repeat(CanvasItem::TextureRepeat p_texture_repeat) {
  3251. // Set a default texture repeat for the whole tilemap
  3252. CanvasItem::set_texture_repeat(p_texture_repeat);
  3253. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3254. for (HashMap<Vector2i, TileMapQuadrant>::Iterator F = layers[layer].quadrant_map.begin(); F; ++F) {
  3255. TileMapQuadrant &q = F->value;
  3256. for (const RID &ci : q.canvas_items) {
  3257. RenderingServer::get_singleton()->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(p_texture_repeat));
  3258. _make_quadrant_dirty(F);
  3259. }
  3260. }
  3261. _rendering_update_layer(layer);
  3262. }
  3263. }
  3264. TypedArray<Vector2i> TileMap::get_surrounding_tiles(Vector2i coords) {
  3265. if (!tile_set.is_valid()) {
  3266. return TypedArray<Vector2i>();
  3267. }
  3268. TypedArray<Vector2i> around;
  3269. TileSet::TileShape shape = tile_set->get_tile_shape();
  3270. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3271. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  3272. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  3273. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  3274. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  3275. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3276. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  3277. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  3278. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  3279. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  3280. } else {
  3281. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3282. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  3283. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  3284. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  3285. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  3286. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  3287. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  3288. } else {
  3289. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  3290. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  3291. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  3292. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  3293. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  3294. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  3295. }
  3296. }
  3297. return around;
  3298. }
  3299. void TileMap::draw_cells_outline(Control *p_control, RBSet<Vector2i> p_cells, Color p_color, Transform2D p_transform) {
  3300. if (!tile_set.is_valid()) {
  3301. return;
  3302. }
  3303. // Create a set.
  3304. Vector2i tile_size = tile_set->get_tile_size();
  3305. Vector<Vector2> polygon = tile_set->get_tile_shape_polygon();
  3306. TileSet::TileShape shape = tile_set->get_tile_shape();
  3307. for (const Vector2i &E : p_cells) {
  3308. Vector2 center = map_to_world(E);
  3309. #define DRAW_SIDE_IF_NEEDED(side, polygon_index_from, polygon_index_to) \
  3310. if (!p_cells.has(get_neighbor_cell(E, side))) { \
  3311. Vector2 from = p_transform.xform(center + polygon[polygon_index_from] * tile_size); \
  3312. Vector2 to = p_transform.xform(center + polygon[polygon_index_to] * tile_size); \
  3313. p_control->draw_line(from, to, p_color); \
  3314. }
  3315. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3316. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 1, 2);
  3317. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 2, 3);
  3318. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 3, 0);
  3319. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 0, 1);
  3320. } else {
  3321. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3322. if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3323. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  3324. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 2, 3);
  3325. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3326. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 5, 0);
  3327. } else {
  3328. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  3329. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 2, 3);
  3330. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 1, 2);
  3331. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3332. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 5, 0);
  3333. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 4, 5);
  3334. }
  3335. } else {
  3336. if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3337. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  3338. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 5, 0);
  3339. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3340. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 2, 3);
  3341. } else {
  3342. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  3343. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 4, 5);
  3344. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 5, 0);
  3345. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3346. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 1, 2);
  3347. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 2, 3);
  3348. }
  3349. }
  3350. }
  3351. }
  3352. #undef DRAW_SIDE_IF_NEEDED
  3353. }
  3354. TypedArray<String> TileMap::get_configuration_warnings() const {
  3355. TypedArray<String> warnings = Node::get_configuration_warnings();
  3356. // Retrieve the set of Z index values with a Y-sorted layer.
  3357. RBSet<int> y_sorted_z_index;
  3358. for (int layer = 0; layer < (int)layers.size(); layer++) {
  3359. if (layers[layer].y_sort_enabled) {
  3360. y_sorted_z_index.insert(layers[layer].z_index);
  3361. }
  3362. }
  3363. // Check if we have a non-sorted layer in a Z-index with a Y-sorted layer.
  3364. for (int layer = 0; layer < (int)layers.size(); layer++) {
  3365. if (!layers[layer].y_sort_enabled && y_sorted_z_index.has(layers[layer].z_index)) {
  3366. 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."));
  3367. break;
  3368. }
  3369. }
  3370. return warnings;
  3371. }
  3372. void TileMap::_bind_methods() {
  3373. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  3374. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  3375. ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
  3376. ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
  3377. ClassDB::bind_method(D_METHOD("get_layers_count"), &TileMap::get_layers_count);
  3378. ClassDB::bind_method(D_METHOD("add_layer", "to_position"), &TileMap::add_layer);
  3379. ClassDB::bind_method(D_METHOD("move_layer", "layer", "to_position"), &TileMap::move_layer);
  3380. ClassDB::bind_method(D_METHOD("remove_layer", "layer"), &TileMap::remove_layer);
  3381. ClassDB::bind_method(D_METHOD("set_layer_name", "layer", "name"), &TileMap::set_layer_name);
  3382. ClassDB::bind_method(D_METHOD("get_layer_name", "layer"), &TileMap::get_layer_name);
  3383. ClassDB::bind_method(D_METHOD("set_layer_enabled", "layer", "enabled"), &TileMap::set_layer_enabled);
  3384. ClassDB::bind_method(D_METHOD("is_layer_enabled", "layer"), &TileMap::is_layer_enabled);
  3385. ClassDB::bind_method(D_METHOD("set_layer_modulate", "layer", "modulate"), &TileMap::set_layer_modulate);
  3386. ClassDB::bind_method(D_METHOD("get_layer_modulate", "layer"), &TileMap::get_layer_modulate);
  3387. ClassDB::bind_method(D_METHOD("set_layer_y_sort_enabled", "layer", "y_sort_enabled"), &TileMap::set_layer_y_sort_enabled);
  3388. ClassDB::bind_method(D_METHOD("is_layer_y_sort_enabled", "layer"), &TileMap::is_layer_y_sort_enabled);
  3389. ClassDB::bind_method(D_METHOD("set_layer_y_sort_origin", "layer", "y_sort_origin"), &TileMap::set_layer_y_sort_origin);
  3390. ClassDB::bind_method(D_METHOD("get_layer_y_sort_origin", "layer"), &TileMap::get_layer_y_sort_origin);
  3391. ClassDB::bind_method(D_METHOD("set_layer_z_index", "layer", "z_index"), &TileMap::set_layer_z_index);
  3392. ClassDB::bind_method(D_METHOD("get_layer_z_index", "layer"), &TileMap::get_layer_z_index);
  3393. ClassDB::bind_method(D_METHOD("set_collision_animatable", "enabled"), &TileMap::set_collision_animatable);
  3394. ClassDB::bind_method(D_METHOD("is_collision_animatable"), &TileMap::is_collision_animatable);
  3395. ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "collision_visibility_mode"), &TileMap::set_collision_visibility_mode);
  3396. ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMap::get_collision_visibility_mode);
  3397. ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "navigation_visibility_mode"), &TileMap::set_navigation_visibility_mode);
  3398. ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode);
  3399. 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));
  3400. ClassDB::bind_method(D_METHOD("erase_cell", "layer", "coords"), &TileMap::erase_cell);
  3401. ClassDB::bind_method(D_METHOD("get_cell_source_id", "layer", "coords", "use_proxies"), &TileMap::get_cell_source_id, DEFVAL(false));
  3402. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "layer", "coords", "use_proxies"), &TileMap::get_cell_atlas_coords, DEFVAL(false));
  3403. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "layer", "coords", "use_proxies"), &TileMap::get_cell_alternative_tile, DEFVAL(false));
  3404. ClassDB::bind_method(D_METHOD("get_cell_tile_data", "layer", "coords", "use_proxies"), &TileMap::get_cell_tile_data, DEFVAL(false));
  3405. ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMap::get_coords_for_body_rid);
  3406. ClassDB::bind_method(D_METHOD("get_pattern", "layer", "coords_array"), &TileMap::get_pattern);
  3407. ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMap::map_pattern);
  3408. ClassDB::bind_method(D_METHOD("set_pattern", "layer", "position", "pattern"), &TileMap::set_pattern);
  3409. ClassDB::bind_method(D_METHOD("set_cells_terrain_connect", "layer", "cells", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_connect, DEFVAL(true));
  3410. ClassDB::bind_method(D_METHOD("set_cells_terrain_path", "layer", "path", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_path, DEFVAL(true));
  3411. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
  3412. ClassDB::bind_method(D_METHOD("clear_layer", "layer"), &TileMap::clear_layer);
  3413. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  3414. ClassDB::bind_method(D_METHOD("force_update", "layer"), &TileMap::force_update, DEFVAL(-1));
  3415. ClassDB::bind_method(D_METHOD("get_surrounding_tiles", "coords"), &TileMap::get_surrounding_tiles);
  3416. ClassDB::bind_method(D_METHOD("get_used_cells", "layer"), &TileMap::get_used_cells);
  3417. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  3418. ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &TileMap::map_to_world);
  3419. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &TileMap::world_to_map);
  3420. ClassDB::bind_method(D_METHOD("get_neighbor_cell", "coords", "neighbor"), &TileMap::get_neighbor_cell);
  3421. ClassDB::bind_method(D_METHOD("_update_dirty_quadrants"), &TileMap::_update_dirty_quadrants);
  3422. ClassDB::bind_method(D_METHOD("_tile_set_changed_deferred_update"), &TileMap::_tile_set_changed_deferred_update);
  3423. GDVIRTUAL_BIND(_use_tile_data_runtime_update, "layer", "coords");
  3424. GDVIRTUAL_BIND(_tile_data_runtime_update, "layer", "coords", "tile_data");
  3425. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  3426. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
  3427. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_animatable"), "set_collision_animatable", "is_collision_animatable");
  3428. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
  3429. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
  3430. ADD_ARRAY("layers", "layer_");
  3431. ADD_PROPERTY_DEFAULT("format", FORMAT_1);
  3432. ADD_SIGNAL(MethodInfo("changed"));
  3433. BIND_ENUM_CONSTANT(VISIBILITY_MODE_DEFAULT);
  3434. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_HIDE);
  3435. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_SHOW);
  3436. }
  3437. void TileMap::_tile_set_changed() {
  3438. emit_signal(SNAME("changed"));
  3439. _tile_set_changed_deferred_update_needed = true;
  3440. call_deferred(SNAME("_tile_set_changed_deferred_update"));
  3441. }
  3442. void TileMap::_tile_set_changed_deferred_update() {
  3443. if (_tile_set_changed_deferred_update_needed) {
  3444. _clear_internals();
  3445. _recreate_internals();
  3446. _tile_set_changed_deferred_update_needed = false;
  3447. }
  3448. }
  3449. TileMap::TileMap() {
  3450. set_notify_transform(true);
  3451. set_notify_local_transform(false);
  3452. layers.resize(1);
  3453. }
  3454. TileMap::~TileMap() {
  3455. if (tile_set.is_valid()) {
  3456. tile_set->disconnect("changed", callable_mp(this, &TileMap::_tile_set_changed));
  3457. }
  3458. _clear_internals();
  3459. }