tile_map.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. /**************************************************************************/
  2. /* tile_map.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "tile_map.h"
  31. #include "collision_object_2d.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/method_bind_ext.gen.inc"
  34. #include "core/os/os.h"
  35. #include "scene/2d/area_2d.h"
  36. #include "servers/navigation_2d_server.h"
  37. #include "servers/physics_2d_server.h"
  38. void TileMap::Quadrant::clear_navpoly() {
  39. for (Map<PosKey, Quadrant::NavPoly>::Element *E = navpoly_ids.front(); E; E = E->next()) {
  40. RID region = E->get().region;
  41. Navigation2DServer::get_singleton()->region_set_map(region, RID());
  42. Navigation2DServer::get_singleton()->free(region);
  43. }
  44. navpoly_ids.clear();
  45. }
  46. int TileMap::_get_quadrant_size() const {
  47. if (y_sort_mode) {
  48. return 1;
  49. } else {
  50. return quadrant_size;
  51. }
  52. }
  53. void TileMap::_notification(int p_what) {
  54. switch (p_what) {
  55. case NOTIFICATION_ENTER_TREE: {
  56. Node2D *c = this;
  57. while (c) {
  58. navigation = Object::cast_to<Navigation2D>(c);
  59. if (navigation) {
  60. // only for <3.5 backward compatibility
  61. bake_navigation = true;
  62. break;
  63. }
  64. c = Object::cast_to<Node2D>(c->get_parent());
  65. }
  66. if (use_parent) {
  67. _clear_quadrants();
  68. collision_parent = Object::cast_to<CollisionObject2D>(get_parent());
  69. }
  70. pending_update = true;
  71. _recreate_quadrants();
  72. update_dirty_quadrants();
  73. RID space = get_world_2d()->get_space();
  74. _update_quadrant_transform();
  75. _update_quadrant_space(space);
  76. update_configuration_warning();
  77. } break;
  78. case NOTIFICATION_EXIT_TREE: {
  79. _update_quadrant_space(RID());
  80. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  81. Quadrant &q = E->get();
  82. if (bake_navigation) {
  83. q.clear_navpoly();
  84. }
  85. if (collision_parent) {
  86. collision_parent->remove_shape_owner(q.shape_owner_id);
  87. q.shape_owner_id = -1;
  88. }
  89. for (Map<PosKey, Quadrant::Occluder>::Element *F = q.occluder_instances.front(); F; F = F->next()) {
  90. if (F->get().id.is_valid()) {
  91. VS::get_singleton()->free(F->get().id);
  92. }
  93. }
  94. q.occluder_instances.clear();
  95. }
  96. collision_parent = nullptr;
  97. navigation = nullptr;
  98. } break;
  99. case NOTIFICATION_TRANSFORM_CHANGED: {
  100. //move stuff
  101. _update_quadrant_transform();
  102. } break;
  103. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  104. if (use_parent) {
  105. _recreate_quadrants();
  106. }
  107. } break;
  108. case NOTIFICATION_VISIBILITY_CHANGED: {
  109. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  110. for (Map<PosKey, Quadrant::Occluder>::Element *F = E->get().occluder_instances.front(); F; F = F->next()) {
  111. VS::get_singleton()->canvas_light_occluder_set_enabled(F->get().id, is_visible());
  112. }
  113. }
  114. } break;
  115. }
  116. }
  117. void TileMap::_update_quadrant_space(const RID &p_space) {
  118. if (!use_parent) {
  119. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  120. Quadrant &q = E->get();
  121. Physics2DServer::get_singleton()->body_set_space(q.body, p_space);
  122. }
  123. }
  124. }
  125. void TileMap::_update_quadrant_transform() {
  126. if (!is_inside_tree()) {
  127. return;
  128. }
  129. Transform2D global_transform = get_global_transform();
  130. Transform2D local_transform;
  131. if (collision_parent) {
  132. local_transform = get_transform();
  133. }
  134. Transform2D nav_rel;
  135. if (bake_navigation) {
  136. if (navigation) {
  137. nav_rel = get_relative_transform_to_parent(navigation);
  138. } else {
  139. nav_rel = get_global_transform();
  140. }
  141. }
  142. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  143. Quadrant &q = E->get();
  144. Transform2D xform;
  145. xform.set_origin(q.pos);
  146. if (!use_parent) {
  147. xform = global_transform * xform;
  148. Physics2DServer::get_singleton()->body_set_state(q.body, Physics2DServer::BODY_STATE_TRANSFORM, xform);
  149. }
  150. if (bake_navigation) {
  151. for (Map<PosKey, Quadrant::NavPoly>::Element *F = q.navpoly_ids.front(); F; F = F->next()) {
  152. Navigation2DServer::get_singleton()->region_set_transform(F->get().region, nav_rel * F->get().xform);
  153. }
  154. }
  155. for (Map<PosKey, Quadrant::Occluder>::Element *F = q.occluder_instances.front(); F; F = F->next()) {
  156. VS::get_singleton()->canvas_light_occluder_set_transform(F->get().id, global_transform * F->get().xform);
  157. }
  158. }
  159. }
  160. void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
  161. if (tile_set.is_valid()) {
  162. tile_set->disconnect("changed", this, "_recreate_quadrants");
  163. tile_set->remove_change_receptor(this);
  164. }
  165. _clear_quadrants();
  166. tile_set = p_tileset;
  167. if (tile_set.is_valid()) {
  168. tile_set->connect("changed", this, "_recreate_quadrants");
  169. tile_set->add_change_receptor(this);
  170. } else {
  171. clear();
  172. }
  173. _recreate_quadrants();
  174. emit_signal("settings_changed");
  175. }
  176. Ref<TileSet> TileMap::get_tileset() const {
  177. return tile_set;
  178. }
  179. void TileMap::set_cell_size(Size2 p_size) {
  180. ERR_FAIL_COND(p_size.x < 1 || p_size.y < 1);
  181. _clear_quadrants();
  182. cell_size = p_size;
  183. _recreate_quadrants();
  184. emit_signal("settings_changed");
  185. }
  186. Size2 TileMap::get_cell_size() const {
  187. return cell_size;
  188. }
  189. void TileMap::set_quadrant_size(int p_size) {
  190. ERR_FAIL_COND_MSG(p_size < 1, "Quadrant size cannot be smaller than 1.");
  191. _clear_quadrants();
  192. quadrant_size = p_size;
  193. _recreate_quadrants();
  194. emit_signal("settings_changed");
  195. }
  196. int TileMap::get_quadrant_size() const {
  197. return quadrant_size;
  198. }
  199. void TileMap::_fix_cell_transform(Transform2D &xform, const Cell &p_cell, const Vector2 &p_offset, const Size2 &p_sc) {
  200. Size2 s = p_sc;
  201. Vector2 offset = p_offset;
  202. if (compatibility_mode && !centered_textures) {
  203. if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  204. offset.y += cell_size.y;
  205. } else if (tile_origin == TILE_ORIGIN_CENTER) {
  206. offset += cell_size / 2;
  207. }
  208. if (s.y > s.x) {
  209. if ((p_cell.flip_h && (p_cell.flip_v || p_cell.transpose)) || (p_cell.flip_v && !p_cell.transpose)) {
  210. offset.y += s.y - s.x;
  211. }
  212. } else if (s.y < s.x) {
  213. if ((p_cell.flip_v && (p_cell.flip_h || p_cell.transpose)) || (p_cell.flip_h && !p_cell.transpose)) {
  214. offset.x += s.x - s.y;
  215. }
  216. }
  217. }
  218. if (p_cell.transpose) {
  219. SWAP(xform.elements[0].x, xform.elements[0].y);
  220. SWAP(xform.elements[1].x, xform.elements[1].y);
  221. SWAP(offset.x, offset.y);
  222. SWAP(s.x, s.y);
  223. }
  224. if (p_cell.flip_h) {
  225. xform.elements[0].x = -xform.elements[0].x;
  226. xform.elements[1].x = -xform.elements[1].x;
  227. if (compatibility_mode && !centered_textures) {
  228. if (tile_origin == TILE_ORIGIN_TOP_LEFT || tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  229. offset.x = s.x - offset.x;
  230. } else if (tile_origin == TILE_ORIGIN_CENTER) {
  231. offset.x = s.x - offset.x / 2;
  232. }
  233. } else {
  234. offset.x = s.x - offset.x;
  235. }
  236. }
  237. if (p_cell.flip_v) {
  238. xform.elements[0].y = -xform.elements[0].y;
  239. xform.elements[1].y = -xform.elements[1].y;
  240. if (compatibility_mode && !centered_textures) {
  241. if (tile_origin == TILE_ORIGIN_TOP_LEFT) {
  242. offset.y = s.y - offset.y;
  243. } else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  244. offset.y += s.y;
  245. } else if (tile_origin == TILE_ORIGIN_CENTER) {
  246. offset.y += s.y;
  247. }
  248. } else {
  249. offset.y = s.y - offset.y;
  250. }
  251. }
  252. if (centered_textures) {
  253. offset += cell_size / 2 - s / 2;
  254. }
  255. xform.elements[2] += offset;
  256. }
  257. void TileMap::_add_shape(int &shape_idx, const Quadrant &p_q, const Ref<Shape2D> &p_shape, const TileSet::ShapeData &p_shape_data, const Transform2D &p_xform, const Vector2 &p_metadata) {
  258. Physics2DServer *ps = Physics2DServer::get_singleton();
  259. if (!use_parent) {
  260. ps->body_add_shape(p_q.body, p_shape->get_rid(), p_xform);
  261. ps->body_set_shape_metadata(p_q.body, shape_idx, p_metadata);
  262. ps->body_set_shape_as_one_way_collision(p_q.body, shape_idx, p_shape_data.one_way_collision, p_shape_data.one_way_collision_margin);
  263. } else if (collision_parent) {
  264. Transform2D xform = p_xform;
  265. xform.set_origin(xform.get_origin() + p_q.pos);
  266. collision_parent->shape_owner_add_shape(p_q.shape_owner_id, p_shape);
  267. int real_index = collision_parent->shape_owner_get_shape_index(p_q.shape_owner_id, shape_idx);
  268. RID rid = collision_parent->get_rid();
  269. if (Object::cast_to<Area2D>(collision_parent) != nullptr) {
  270. ps->area_set_shape_transform(rid, real_index, get_transform() * xform);
  271. } else {
  272. ps->body_set_shape_transform(rid, real_index, get_transform() * xform);
  273. ps->body_set_shape_metadata(rid, real_index, p_metadata);
  274. ps->body_set_shape_as_one_way_collision(rid, real_index, p_shape_data.one_way_collision, p_shape_data.one_way_collision_margin);
  275. }
  276. }
  277. shape_idx++;
  278. }
  279. void TileMap::update_dirty_quadrants() {
  280. if (!pending_update) {
  281. return;
  282. }
  283. if (!is_inside_tree() || !tile_set.is_valid()) {
  284. pending_update = false;
  285. return;
  286. }
  287. VisualServer *vs = VisualServer::get_singleton();
  288. Physics2DServer *ps = Physics2DServer::get_singleton();
  289. Vector2 tofs = get_cell_draw_offset();
  290. Transform2D nav_rel;
  291. if (bake_navigation) {
  292. if (navigation) {
  293. nav_rel = get_relative_transform_to_parent(navigation);
  294. } else {
  295. nav_rel = get_global_transform();
  296. }
  297. }
  298. Vector2 qofs;
  299. SceneTree *st = SceneTree::get_singleton();
  300. Color debug_collision_color;
  301. Color debug_navigation_color;
  302. bool debug_shapes = false;
  303. if (st) {
  304. if (Engine::get_singleton()->is_editor_hint()) {
  305. debug_shapes = show_collision;
  306. } else {
  307. debug_shapes = st->is_debugging_collisions_hint();
  308. }
  309. if (debug_shapes) {
  310. debug_collision_color = st->get_debug_collisions_color();
  311. }
  312. }
  313. bool debug_navigation = st && st->is_debugging_navigation_hint();
  314. if (debug_navigation) {
  315. debug_navigation_color = st->get_debug_navigation_color();
  316. }
  317. while (dirty_quadrant_list.first()) {
  318. Quadrant &q = *dirty_quadrant_list.first()->self();
  319. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  320. if (E->get().is_valid()) {
  321. vs->free(E->get());
  322. }
  323. }
  324. q.canvas_items.clear();
  325. if (!use_parent) {
  326. ps->body_clear_shapes(q.body);
  327. } else if (collision_parent) {
  328. collision_parent->shape_owner_clear_shapes(q.shape_owner_id);
  329. }
  330. int shape_idx = 0;
  331. if (bake_navigation) {
  332. q.clear_navpoly();
  333. }
  334. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  335. if (E->get().id.is_valid()) {
  336. VS::get_singleton()->free(E->get().id);
  337. }
  338. }
  339. q.occluder_instances.clear();
  340. Ref<ShaderMaterial> prev_material;
  341. int prev_z_index = 0;
  342. RID prev_canvas_item;
  343. RID prev_debug_canvas_item;
  344. for (int i = 0; i < q.cells.size(); i++) {
  345. Map<PosKey, Cell>::Element *E = tile_map.find(q.cells[i]);
  346. Cell &c = E->get();
  347. //moment of truth
  348. if (!tile_set->has_tile(c.id)) {
  349. continue;
  350. }
  351. Ref<Texture> tex = tile_set->tile_get_texture(c.id);
  352. Vector2 tile_ofs = tile_set->tile_get_texture_offset(c.id);
  353. Vector2 wofs = _map_to_world(E->key().x, E->key().y);
  354. Vector2 offset = wofs - q.pos + tofs;
  355. if (!tex.is_valid()) {
  356. continue;
  357. }
  358. Ref<ShaderMaterial> mat = tile_set->tile_get_material(c.id);
  359. int z_index = tile_set->tile_get_z_index(c.id);
  360. if (tile_set->tile_get_tile_mode(c.id) == TileSet::AUTO_TILE || tile_set->tile_get_tile_mode(c.id) == TileSet::ATLAS_TILE) {
  361. z_index += tile_set->autotile_get_z_index(c.id, Vector2(c.autotile_coord_x, c.autotile_coord_y));
  362. }
  363. RID canvas_item;
  364. RID debug_canvas_item;
  365. if (prev_canvas_item == RID() || prev_material != mat || prev_z_index != z_index) {
  366. canvas_item = RID_PRIME(vs->canvas_item_create());
  367. if (mat.is_valid()) {
  368. vs->canvas_item_set_material(canvas_item, mat->get_rid());
  369. }
  370. vs->canvas_item_set_parent(canvas_item, get_canvas_item());
  371. _update_item_material_state(canvas_item);
  372. Transform2D xform;
  373. xform.set_origin(q.pos);
  374. vs->canvas_item_set_transform(canvas_item, xform);
  375. vs->canvas_item_set_light_mask(canvas_item, get_light_mask());
  376. vs->canvas_item_set_z_index(canvas_item, z_index);
  377. q.canvas_items.push_back(canvas_item);
  378. if (debug_shapes) {
  379. debug_canvas_item = RID_PRIME(vs->canvas_item_create());
  380. vs->canvas_item_set_parent(debug_canvas_item, canvas_item);
  381. vs->canvas_item_set_z_as_relative_to_parent(debug_canvas_item, false);
  382. vs->canvas_item_set_z_index(debug_canvas_item, VS::CANVAS_ITEM_Z_MAX - 1);
  383. q.canvas_items.push_back(debug_canvas_item);
  384. prev_debug_canvas_item = debug_canvas_item;
  385. }
  386. prev_canvas_item = canvas_item;
  387. prev_material = mat;
  388. prev_z_index = z_index;
  389. } else {
  390. canvas_item = prev_canvas_item;
  391. if (debug_shapes) {
  392. debug_canvas_item = prev_debug_canvas_item;
  393. }
  394. }
  395. Rect2 r = tile_set->tile_get_region(c.id);
  396. if (tile_set->tile_get_tile_mode(c.id) == TileSet::AUTO_TILE || tile_set->tile_get_tile_mode(c.id) == TileSet::ATLAS_TILE) {
  397. int spacing = tile_set->autotile_get_spacing(c.id);
  398. r.size = tile_set->autotile_get_size(c.id);
  399. r.position += (r.size + Vector2(spacing, spacing)) * Vector2(c.autotile_coord_x, c.autotile_coord_y);
  400. }
  401. Size2 s;
  402. if (r == Rect2()) {
  403. s = tex->get_size();
  404. } else {
  405. s = r.size;
  406. }
  407. Rect2 rect;
  408. rect.position = offset.floor();
  409. rect.size = s;
  410. rect.size.x += fp_adjust;
  411. rect.size.y += fp_adjust;
  412. if (compatibility_mode && !centered_textures) {
  413. if (rect.size.y > rect.size.x) {
  414. if ((c.flip_h && (c.flip_v || c.transpose)) || (c.flip_v && !c.transpose)) {
  415. tile_ofs.y += rect.size.y - rect.size.x;
  416. }
  417. } else if (rect.size.y < rect.size.x) {
  418. if ((c.flip_v && (c.flip_h || c.transpose)) || (c.flip_h && !c.transpose)) {
  419. tile_ofs.x += rect.size.x - rect.size.y;
  420. }
  421. }
  422. }
  423. if (c.transpose) {
  424. SWAP(tile_ofs.x, tile_ofs.y);
  425. if (centered_textures) {
  426. rect.position.x += cell_size.x / 2 - rect.size.y / 2;
  427. rect.position.y += cell_size.y / 2 - rect.size.x / 2;
  428. }
  429. } else if (centered_textures) {
  430. rect.position += cell_size / 2 - rect.size / 2;
  431. }
  432. if (c.flip_h) {
  433. rect.size.x = -rect.size.x;
  434. tile_ofs.x = -tile_ofs.x;
  435. }
  436. if (c.flip_v) {
  437. rect.size.y = -rect.size.y;
  438. tile_ofs.y = -tile_ofs.y;
  439. }
  440. if (compatibility_mode && !centered_textures) {
  441. if (tile_origin == TILE_ORIGIN_TOP_LEFT) {
  442. rect.position += tile_ofs;
  443. } else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  444. rect.position += tile_ofs;
  445. if (c.transpose) {
  446. if (c.flip_h) {
  447. rect.position.x -= cell_size.x;
  448. } else {
  449. rect.position.x += cell_size.x;
  450. }
  451. } else {
  452. if (c.flip_v) {
  453. rect.position.y -= cell_size.y;
  454. } else {
  455. rect.position.y += cell_size.y;
  456. }
  457. }
  458. } else if (tile_origin == TILE_ORIGIN_CENTER) {
  459. rect.position += tile_ofs;
  460. if (c.flip_h) {
  461. rect.position.x -= cell_size.x / 2;
  462. } else {
  463. rect.position.x += cell_size.x / 2;
  464. }
  465. if (c.flip_v) {
  466. rect.position.y -= cell_size.y / 2;
  467. } else {
  468. rect.position.y += cell_size.y / 2;
  469. }
  470. }
  471. } else {
  472. rect.position += tile_ofs;
  473. }
  474. Ref<Texture> normal_map = tile_set->tile_get_normal_map(c.id);
  475. Color modulate = tile_set->tile_get_modulate(c.id);
  476. Color self_modulate = get_self_modulate();
  477. modulate = Color(modulate.r * self_modulate.r, modulate.g * self_modulate.g,
  478. modulate.b * self_modulate.b, modulate.a * self_modulate.a);
  479. if (r == Rect2()) {
  480. tex->draw_rect(canvas_item, rect, false, modulate, c.transpose, normal_map);
  481. } else {
  482. tex->draw_rect_region(canvas_item, rect, r, modulate, c.transpose, normal_map, clip_uv);
  483. }
  484. Vector<TileSet::ShapeData> shapes = tile_set->tile_get_shapes(c.id);
  485. for (int j = 0; j < shapes.size(); j++) {
  486. Ref<Shape2D> shape = shapes[j].shape;
  487. if (shape.is_valid()) {
  488. if (tile_set->tile_get_tile_mode(c.id) == TileSet::SINGLE_TILE || (shapes[j].autotile_coord.x == c.autotile_coord_x && shapes[j].autotile_coord.y == c.autotile_coord_y)) {
  489. Transform2D xform;
  490. xform.set_origin(offset.floor() + tile_ofs);
  491. Vector2 shape_ofs = shapes[j].shape_transform.get_origin();
  492. _fix_cell_transform(xform, c, shape_ofs, s);
  493. xform *= shapes[j].shape_transform.untranslated();
  494. if (debug_canvas_item.is_valid()) {
  495. vs->canvas_item_add_set_transform(debug_canvas_item, xform);
  496. shape->draw(debug_canvas_item, debug_collision_color);
  497. }
  498. if (shape->has_meta("decomposed")) {
  499. Array _shapes = shape->get_meta("decomposed");
  500. for (int k = 0; k < _shapes.size(); k++) {
  501. Ref<ConvexPolygonShape2D> convex = _shapes[k];
  502. if (convex.is_valid()) {
  503. _add_shape(shape_idx, q, convex, shapes[j], xform, Vector2(E->key().x, E->key().y));
  504. #ifdef DEBUG_ENABLED
  505. } else {
  506. print_error("The TileSet assigned to the TileMap " + get_name() + " has an invalid convex shape.");
  507. #endif
  508. }
  509. }
  510. } else {
  511. _add_shape(shape_idx, q, shape, shapes[j], xform, Vector2(E->key().x, E->key().y));
  512. }
  513. }
  514. }
  515. }
  516. if (debug_canvas_item.is_valid()) {
  517. vs->canvas_item_add_set_transform(debug_canvas_item, Transform2D());
  518. }
  519. if (bake_navigation) {
  520. Ref<NavigationPolygon> navpoly;
  521. Vector2 npoly_ofs;
  522. if (tile_set->tile_get_tile_mode(c.id) == TileSet::AUTO_TILE || tile_set->tile_get_tile_mode(c.id) == TileSet::ATLAS_TILE) {
  523. navpoly = tile_set->autotile_get_navigation_polygon(c.id, Vector2(c.autotile_coord_x, c.autotile_coord_y));
  524. npoly_ofs = Vector2();
  525. } else {
  526. navpoly = tile_set->tile_get_navigation_polygon(c.id);
  527. npoly_ofs = tile_set->tile_get_navigation_polygon_offset(c.id);
  528. }
  529. if (navpoly.is_valid()) {
  530. Transform2D xform;
  531. xform.set_origin(offset.floor() + q.pos + tile_ofs);
  532. _fix_cell_transform(xform, c, npoly_ofs, s);
  533. RID region = Navigation2DServer::get_singleton()->region_create();
  534. if (navigation) {
  535. Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
  536. } else {
  537. Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  538. }
  539. Navigation2DServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  540. Navigation2DServer::get_singleton()->region_set_transform(region, nav_rel * xform);
  541. Navigation2DServer::get_singleton()->region_set_navpoly(region, navpoly);
  542. Quadrant::NavPoly np;
  543. np.region = region;
  544. np.xform = xform;
  545. q.navpoly_ids[E->key()] = np;
  546. if (debug_navigation) {
  547. RID debug_navigation_item = RID_PRIME(vs->canvas_item_create());
  548. vs->canvas_item_set_parent(debug_navigation_item, canvas_item);
  549. vs->canvas_item_set_z_as_relative_to_parent(debug_navigation_item, false);
  550. vs->canvas_item_set_z_index(debug_navigation_item, VS::CANVAS_ITEM_Z_MAX - 2); // Display one below collision debug
  551. if (debug_navigation_item.is_valid()) {
  552. PoolVector<Vector2> navigation_polygon_vertices = navpoly->get_vertices();
  553. int vsize = navigation_polygon_vertices.size();
  554. if (vsize > 2) {
  555. Vector<Color> colors;
  556. Vector<Vector2> vertices;
  557. vertices.resize(vsize);
  558. colors.resize(vsize);
  559. {
  560. PoolVector<Vector2>::Read vr = navigation_polygon_vertices.read();
  561. for (int j = 0; j < vsize; j++) {
  562. vertices.write[j] = vr[j];
  563. colors.write[j] = debug_navigation_color;
  564. }
  565. }
  566. Vector<int> indices;
  567. for (int j = 0; j < navpoly->get_polygon_count(); j++) {
  568. Vector<int> polygon = navpoly->get_polygon(j);
  569. for (int k = 2; k < polygon.size(); k++) {
  570. int kofs[3] = { 0, k - 1, k };
  571. for (int l = 0; l < 3; l++) {
  572. int idx = polygon[kofs[l]];
  573. ERR_FAIL_INDEX(idx, vsize);
  574. indices.push_back(idx);
  575. }
  576. }
  577. }
  578. Transform2D navxform;
  579. navxform.set_origin(offset.floor() + tile_ofs);
  580. _fix_cell_transform(navxform, c, npoly_ofs, s);
  581. vs->canvas_item_set_transform(debug_navigation_item, navxform);
  582. vs->canvas_item_add_triangle_array(debug_navigation_item, indices, vertices, colors);
  583. }
  584. }
  585. }
  586. }
  587. }
  588. Ref<OccluderPolygon2D> occluder;
  589. if (tile_set->tile_get_tile_mode(c.id) == TileSet::AUTO_TILE || tile_set->tile_get_tile_mode(c.id) == TileSet::ATLAS_TILE) {
  590. occluder = tile_set->autotile_get_light_occluder(c.id, Vector2(c.autotile_coord_x, c.autotile_coord_y));
  591. } else {
  592. occluder = tile_set->tile_get_light_occluder(c.id);
  593. }
  594. if (occluder.is_valid()) {
  595. Vector2 occluder_ofs = tile_set->tile_get_occluder_offset(c.id);
  596. Transform2D xform;
  597. xform.set_origin(offset.floor() + q.pos);
  598. _fix_cell_transform(xform, c, occluder_ofs, s);
  599. RID orid = RID_PRIME(VS::get_singleton()->canvas_light_occluder_create());
  600. VS::get_singleton()->canvas_light_occluder_set_transform(orid, get_global_transform() * xform);
  601. VS::get_singleton()->canvas_light_occluder_set_polygon(orid, occluder->get_rid());
  602. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(orid, get_canvas());
  603. VS::get_singleton()->canvas_light_occluder_set_light_mask(orid, occluder_light_mask);
  604. VS::get_singleton()->canvas_light_occluder_set_enabled(orid, is_visible());
  605. Quadrant::Occluder oc;
  606. oc.xform = xform;
  607. oc.id = orid;
  608. q.occluder_instances[E->key()] = oc;
  609. }
  610. }
  611. dirty_quadrant_list.remove(dirty_quadrant_list.first());
  612. quadrant_order_dirty = true;
  613. }
  614. pending_update = false;
  615. if (quadrant_order_dirty) {
  616. int index = -(int64_t)0x80000000; //always must be drawn below children
  617. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  618. Quadrant &q = E->get();
  619. for (List<RID>::Element *F = q.canvas_items.front(); F; F = F->next()) {
  620. VS::get_singleton()->canvas_item_set_draw_index(F->get(), index++);
  621. }
  622. }
  623. quadrant_order_dirty = false;
  624. }
  625. _recompute_rect_cache();
  626. }
  627. void TileMap::_recompute_rect_cache() {
  628. #ifdef DEBUG_ENABLED
  629. if (!rect_cache_dirty) {
  630. return;
  631. }
  632. Rect2 r_total;
  633. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  634. Rect2 r;
  635. r.position = _map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size());
  636. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size()));
  637. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size()));
  638. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size()));
  639. if (E == quadrant_map.front()) {
  640. r_total = r;
  641. } else {
  642. r_total = r_total.merge(r);
  643. }
  644. }
  645. rect_cache = r_total;
  646. item_rect_changed();
  647. rect_cache_dirty = false;
  648. #endif
  649. }
  650. Map<TileMap::PosKey, TileMap::Quadrant>::Element *TileMap::_create_quadrant(const PosKey &p_qk) {
  651. Transform2D xform;
  652. //xform.set_origin(Point2(p_qk.x,p_qk.y)*cell_size*quadrant_size);
  653. Quadrant q;
  654. q.pos = _map_to_world(p_qk.x * _get_quadrant_size(), p_qk.y * _get_quadrant_size());
  655. q.pos += get_cell_draw_offset();
  656. if (tile_origin == TILE_ORIGIN_CENTER) {
  657. q.pos += cell_size / 2;
  658. } else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  659. q.pos.y += cell_size.y;
  660. }
  661. xform.set_origin(q.pos);
  662. //q.canvas_item = VisualServer::get_singleton()->canvas_item_create();
  663. if (!use_parent) {
  664. q.body = RID_PRIME(Physics2DServer::get_singleton()->body_create());
  665. Physics2DServer::get_singleton()->body_set_mode(q.body, use_kinematic ? Physics2DServer::BODY_MODE_KINEMATIC : Physics2DServer::BODY_MODE_STATIC);
  666. Physics2DServer::get_singleton()->body_attach_object_instance_id(q.body, get_instance_id());
  667. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  668. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  669. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, friction);
  670. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, bounce);
  671. if (is_inside_tree()) {
  672. xform = get_global_transform() * xform;
  673. RID space = get_world_2d()->get_space();
  674. Physics2DServer::get_singleton()->body_set_space(q.body, space);
  675. }
  676. Physics2DServer::get_singleton()->body_set_state(q.body, Physics2DServer::BODY_STATE_TRANSFORM, xform);
  677. } else if (collision_parent) {
  678. xform = get_transform() * xform;
  679. q.shape_owner_id = collision_parent->create_shape_owner(this);
  680. } else {
  681. q.shape_owner_id = -1;
  682. }
  683. rect_cache_dirty = true;
  684. quadrant_order_dirty = true;
  685. return quadrant_map.insert(p_qk, q);
  686. }
  687. void TileMap::_erase_quadrant(Map<PosKey, Quadrant>::Element *Q) {
  688. Quadrant &q = Q->get();
  689. if (!use_parent) {
  690. if (q.body.is_valid()) {
  691. Physics2DServer::get_singleton()->free(q.body);
  692. q.body = RID();
  693. }
  694. } else if (collision_parent) {
  695. collision_parent->remove_shape_owner(q.shape_owner_id);
  696. }
  697. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  698. if (E->get().is_valid()) {
  699. VisualServer::get_singleton()->free(E->get());
  700. }
  701. }
  702. q.canvas_items.clear();
  703. if (q.dirty_list.in_list()) {
  704. dirty_quadrant_list.remove(&q.dirty_list);
  705. }
  706. if (bake_navigation) {
  707. q.clear_navpoly();
  708. }
  709. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  710. if (E->get().id.is_valid()) {
  711. VS::get_singleton()->free(E->get().id);
  712. }
  713. }
  714. q.occluder_instances.clear();
  715. quadrant_map.erase(Q);
  716. rect_cache_dirty = true;
  717. }
  718. void TileMap::_make_quadrant_dirty(Map<PosKey, Quadrant>::Element *Q, bool update) {
  719. Quadrant &q = Q->get();
  720. if (!q.dirty_list.in_list()) {
  721. dirty_quadrant_list.add(&q.dirty_list);
  722. }
  723. if (pending_update) {
  724. return;
  725. }
  726. pending_update = true;
  727. if (!is_inside_tree()) {
  728. return;
  729. }
  730. if (update) {
  731. call_deferred("update_dirty_quadrants");
  732. }
  733. }
  734. void TileMap::set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose, const Vector2 &p_autotile_coord) {
  735. set_cell(p_pos.x, p_pos.y, p_tile, p_flip_x, p_flip_y, p_transpose, p_autotile_coord);
  736. }
  737. void TileMap::_set_celld(const Vector2 &p_pos, const Dictionary &p_data) {
  738. Variant v_pos_x = p_pos.x, v_pos_y = p_pos.y, v_tile = p_data["id"], v_flip_h = p_data["flip_h"], v_flip_v = p_data["flip_y"], v_transpose = p_data["transpose"], v_autotile_coord = p_data["auto_coord"];
  739. const Variant *args[7] = { &v_pos_x, &v_pos_y, &v_tile, &v_flip_h, &v_flip_v, &v_transpose, &v_autotile_coord };
  740. Variant::CallError ce;
  741. call("set_cell", args, 7, ce);
  742. }
  743. void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose, const Vector2 &p_autotile_coord) {
  744. PosKey pk(p_x, p_y);
  745. Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  746. if (!E && p_tile == INVALID_CELL) {
  747. return; //nothing to do
  748. }
  749. PosKey qk = pk.to_quadrant(_get_quadrant_size());
  750. if (p_tile == INVALID_CELL) {
  751. //erase existing
  752. tile_map.erase(pk);
  753. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  754. ERR_FAIL_COND(!Q);
  755. Quadrant &q = Q->get();
  756. q.cells.erase(pk);
  757. if (q.cells.size() == 0) {
  758. _erase_quadrant(Q);
  759. } else {
  760. _make_quadrant_dirty(Q);
  761. }
  762. used_size_cache_dirty = true;
  763. return;
  764. }
  765. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  766. if (!E) {
  767. E = tile_map.insert(pk, Cell());
  768. if (!Q) {
  769. Q = _create_quadrant(qk);
  770. }
  771. Quadrant &q = Q->get();
  772. q.cells.insert(pk);
  773. } else {
  774. ERR_FAIL_COND(!Q); // quadrant should exist...
  775. if (E->get().id == p_tile && E->get().flip_h == p_flip_x && E->get().flip_v == p_flip_y && E->get().transpose == p_transpose && E->get().autotile_coord_x == (uint16_t)p_autotile_coord.x && E->get().autotile_coord_y == (uint16_t)p_autotile_coord.y) {
  776. return; //nothing changed
  777. }
  778. }
  779. Cell &c = E->get();
  780. c.id = p_tile;
  781. c.flip_h = p_flip_x;
  782. c.flip_v = p_flip_y;
  783. c.transpose = p_transpose;
  784. c.autotile_coord_x = (uint16_t)p_autotile_coord.x;
  785. c.autotile_coord_y = (uint16_t)p_autotile_coord.y;
  786. _make_quadrant_dirty(Q);
  787. used_size_cache_dirty = true;
  788. }
  789. int TileMap::get_cellv(const Vector2 &p_pos) const {
  790. return get_cell(p_pos.x, p_pos.y);
  791. }
  792. void TileMap::make_bitmask_area_dirty(const Vector2 &p_pos) {
  793. for (int x = p_pos.x - 1; x <= p_pos.x + 1; x++) {
  794. for (int y = p_pos.y - 1; y <= p_pos.y + 1; y++) {
  795. PosKey p(x, y);
  796. if (dirty_bitmask.find(p) == nullptr) {
  797. dirty_bitmask.push_back(p);
  798. }
  799. }
  800. }
  801. }
  802. void TileMap::update_bitmask_area(const Vector2 &p_pos) {
  803. for (int x = p_pos.x - 1; x <= p_pos.x + 1; x++) {
  804. for (int y = p_pos.y - 1; y <= p_pos.y + 1; y++) {
  805. update_cell_bitmask(x, y);
  806. }
  807. }
  808. }
  809. void TileMap::update_bitmask_region(const Vector2 &p_start, const Vector2 &p_end) {
  810. if ((p_end.x < p_start.x || p_end.y < p_start.y) || (p_end.x == p_start.x && p_end.y == p_start.y)) {
  811. Array a = get_used_cells();
  812. for (int i = 0; i < a.size(); i++) {
  813. Vector2 vector = (Vector2)a[i];
  814. update_cell_bitmask(vector.x, vector.y);
  815. }
  816. return;
  817. }
  818. for (int x = p_start.x - 1; x <= p_end.x + 1; x++) {
  819. for (int y = p_start.y - 1; y <= p_end.y + 1; y++) {
  820. update_cell_bitmask(x, y);
  821. }
  822. }
  823. }
  824. void TileMap::update_cell_bitmask(int p_x, int p_y) {
  825. ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot update cell bitmask if Tileset is not open.");
  826. PosKey p(p_x, p_y);
  827. Map<PosKey, Cell>::Element *E = tile_map.find(p);
  828. if (E != nullptr) {
  829. int id = get_cell(p_x, p_y);
  830. if (!tile_set->has_tile(id)) {
  831. return;
  832. }
  833. if (tile_set->tile_get_tile_mode(id) == TileSet::AUTO_TILE) {
  834. uint16_t mask = 0;
  835. if (tile_set->autotile_get_bitmask_mode(id) == TileSet::BITMASK_2X2) {
  836. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) {
  837. mask |= TileSet::BIND_TOPLEFT;
  838. }
  839. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) {
  840. mask |= TileSet::BIND_TOPRIGHT;
  841. }
  842. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) {
  843. mask |= TileSet::BIND_BOTTOMLEFT;
  844. }
  845. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) {
  846. mask |= TileSet::BIND_BOTTOMRIGHT;
  847. }
  848. } else {
  849. if (tile_set->autotile_get_bitmask_mode(id) == TileSet::BITMASK_3X3_MINIMAL) {
  850. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) {
  851. mask |= TileSet::BIND_TOPLEFT;
  852. }
  853. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) {
  854. mask |= TileSet::BIND_TOPRIGHT;
  855. }
  856. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) {
  857. mask |= TileSet::BIND_BOTTOMLEFT;
  858. }
  859. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) {
  860. mask |= TileSet::BIND_BOTTOMRIGHT;
  861. }
  862. } else {
  863. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y - 1))) {
  864. mask |= TileSet::BIND_TOPLEFT;
  865. }
  866. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y - 1))) {
  867. mask |= TileSet::BIND_TOPRIGHT;
  868. }
  869. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y + 1))) {
  870. mask |= TileSet::BIND_BOTTOMLEFT;
  871. }
  872. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y + 1))) {
  873. mask |= TileSet::BIND_BOTTOMRIGHT;
  874. }
  875. }
  876. if (tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1))) {
  877. mask |= TileSet::BIND_TOP;
  878. }
  879. if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) {
  880. mask |= TileSet::BIND_LEFT;
  881. }
  882. mask |= TileSet::BIND_CENTER;
  883. if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) {
  884. mask |= TileSet::BIND_RIGHT;
  885. }
  886. if (tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1))) {
  887. mask |= TileSet::BIND_BOTTOM;
  888. }
  889. }
  890. Vector2 coord = tile_set->autotile_get_subtile_for_bitmask(id, mask, this, Vector2(p_x, p_y));
  891. E->get().autotile_coord_x = (int)coord.x;
  892. E->get().autotile_coord_y = (int)coord.y;
  893. PosKey qk = p.to_quadrant(_get_quadrant_size());
  894. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  895. _make_quadrant_dirty(Q);
  896. } else if (tile_set->tile_get_tile_mode(id) == TileSet::SINGLE_TILE) {
  897. E->get().autotile_coord_x = 0;
  898. E->get().autotile_coord_y = 0;
  899. } else if (tile_set->tile_get_tile_mode(id) == TileSet::ATLAS_TILE) {
  900. if (tile_set->autotile_get_bitmask(id, Vector2(p_x, p_y)) == TileSet::BIND_CENTER) {
  901. Vector2 coord = tile_set->atlastile_get_subtile_by_priority(id, this, Vector2(p_x, p_y));
  902. E->get().autotile_coord_x = (int)coord.x;
  903. E->get().autotile_coord_y = (int)coord.y;
  904. }
  905. }
  906. }
  907. }
  908. void TileMap::update_dirty_bitmask() {
  909. while (dirty_bitmask.size() > 0) {
  910. update_cell_bitmask(dirty_bitmask[0].x, dirty_bitmask[0].y);
  911. dirty_bitmask.pop_front();
  912. }
  913. }
  914. void TileMap::fix_invalid_tiles() {
  915. ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot fix invalid tiles if Tileset is not open.");
  916. Map<PosKey, Cell> temp_tile_map = tile_map;
  917. for (Map<PosKey, Cell>::Element *E = temp_tile_map.front(); E; E = E->next()) {
  918. if (!tile_set->has_tile(get_cell(E->key().x, E->key().y))) {
  919. set_cell(E->key().x, E->key().y, INVALID_CELL);
  920. }
  921. }
  922. }
  923. int TileMap::get_cell(int p_x, int p_y) const {
  924. PosKey pk(p_x, p_y);
  925. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  926. if (!E) {
  927. return INVALID_CELL;
  928. }
  929. return E->get().id;
  930. }
  931. bool TileMap::is_cell_x_flipped(int p_x, int p_y) const {
  932. PosKey pk(p_x, p_y);
  933. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  934. if (!E) {
  935. return false;
  936. }
  937. return E->get().flip_h;
  938. }
  939. bool TileMap::is_cell_y_flipped(int p_x, int p_y) const {
  940. PosKey pk(p_x, p_y);
  941. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  942. if (!E) {
  943. return false;
  944. }
  945. return E->get().flip_v;
  946. }
  947. bool TileMap::is_cell_transposed(int p_x, int p_y) const {
  948. PosKey pk(p_x, p_y);
  949. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  950. if (!E) {
  951. return false;
  952. }
  953. return E->get().transpose;
  954. }
  955. void TileMap::set_cell_autotile_coord(int p_x, int p_y, const Vector2 &p_coord) {
  956. PosKey pk(p_x, p_y);
  957. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  958. if (!E) {
  959. return;
  960. }
  961. Cell c = E->get();
  962. c.autotile_coord_x = p_coord.x;
  963. c.autotile_coord_y = p_coord.y;
  964. tile_map[pk] = c;
  965. PosKey qk = pk.to_quadrant(_get_quadrant_size());
  966. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  967. if (!Q) {
  968. return;
  969. }
  970. _make_quadrant_dirty(Q);
  971. }
  972. Vector2 TileMap::get_cell_autotile_coord(int p_x, int p_y) const {
  973. PosKey pk(p_x, p_y);
  974. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  975. if (!E) {
  976. return Vector2();
  977. }
  978. return Vector2(E->get().autotile_coord_x, E->get().autotile_coord_y);
  979. }
  980. void TileMap::_recreate_quadrants() {
  981. _clear_quadrants();
  982. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  983. PosKey qk = PosKey(E->key().x, E->key().y).to_quadrant(_get_quadrant_size());
  984. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  985. if (!Q) {
  986. Q = _create_quadrant(qk);
  987. dirty_quadrant_list.add(&Q->get().dirty_list);
  988. }
  989. Q->get().cells.insert(E->key());
  990. _make_quadrant_dirty(Q, false);
  991. }
  992. update_dirty_quadrants();
  993. }
  994. void TileMap::_clear_quadrants() {
  995. while (quadrant_map.size()) {
  996. _erase_quadrant(quadrant_map.front());
  997. }
  998. }
  999. void TileMap::set_material(const Ref<Material> &p_material) {
  1000. CanvasItem::set_material(p_material);
  1001. _update_all_items_material_state();
  1002. }
  1003. void TileMap::set_use_parent_material(bool p_use_parent_material) {
  1004. CanvasItem::set_use_parent_material(p_use_parent_material);
  1005. _update_all_items_material_state();
  1006. }
  1007. void TileMap::_update_all_items_material_state() {
  1008. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1009. Quadrant &q = E->get();
  1010. for (List<RID>::Element *F = q.canvas_items.front(); F; F = F->next()) {
  1011. _update_item_material_state(F->get());
  1012. }
  1013. }
  1014. }
  1015. void TileMap::_update_item_material_state(const RID &p_canvas_item) {
  1016. VS::get_singleton()->canvas_item_set_use_parent_material(p_canvas_item, get_use_parent_material() || get_material().is_valid());
  1017. }
  1018. void TileMap::clear() {
  1019. _clear_quadrants();
  1020. tile_map.clear();
  1021. used_size_cache_dirty = true;
  1022. }
  1023. void TileMap::_set_tile_data(const PoolVector<int> &p_data) {
  1024. ERR_FAIL_COND(format > FORMAT_2);
  1025. int c = p_data.size();
  1026. PoolVector<int>::Read r = p_data.read();
  1027. int offset = (format == FORMAT_2) ? 3 : 2;
  1028. ERR_FAIL_COND_MSG(c % offset != 0, "Corrupted tile data.");
  1029. clear();
  1030. for (int i = 0; i < c; i += offset) {
  1031. const uint8_t *ptr = (const uint8_t *)&r[i];
  1032. uint8_t local[12];
  1033. const int buffer_size = (format == FORMAT_2) ? 12 : 8;
  1034. for (int j = 0; j < buffer_size; j++) {
  1035. local[j] = ptr[j];
  1036. }
  1037. #ifdef BIG_ENDIAN_ENABLED
  1038. SWAP(local[0], local[3]);
  1039. SWAP(local[1], local[2]);
  1040. SWAP(local[4], local[7]);
  1041. SWAP(local[5], local[6]);
  1042. //TODO: ask someone to check this...
  1043. if (FORMAT == FORMAT_2) {
  1044. SWAP(local[8], local[11]);
  1045. SWAP(local[9], local[10]);
  1046. }
  1047. #endif
  1048. uint16_t x = decode_uint16(&local[0]);
  1049. uint16_t y = decode_uint16(&local[2]);
  1050. uint32_t v = decode_uint32(&local[4]);
  1051. bool flip_h = v & (1 << 29);
  1052. bool flip_v = v & (1 << 30);
  1053. bool transpose = v & (1 << 31);
  1054. v &= (1 << 29) - 1;
  1055. int16_t coord_x = 0;
  1056. int16_t coord_y = 0;
  1057. if (format == FORMAT_2) {
  1058. coord_x = decode_uint16(&local[8]);
  1059. coord_y = decode_uint16(&local[10]);
  1060. }
  1061. set_cell(x, y, v, flip_h, flip_v, transpose, Vector2(coord_x, coord_y));
  1062. }
  1063. }
  1064. PoolVector<int> TileMap::_get_tile_data() const {
  1065. PoolVector<int> data;
  1066. data.resize(tile_map.size() * 3);
  1067. PoolVector<int>::Write w = data.write();
  1068. // Save in highest format
  1069. int idx = 0;
  1070. for (const Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  1071. uint8_t *ptr = (uint8_t *)&w[idx];
  1072. encode_uint16(E->key().x, &ptr[0]);
  1073. encode_uint16(E->key().y, &ptr[2]);
  1074. uint32_t val = E->get().id;
  1075. if (E->get().flip_h) {
  1076. val |= (1 << 29);
  1077. }
  1078. if (E->get().flip_v) {
  1079. val |= (1 << 30);
  1080. }
  1081. if (E->get().transpose) {
  1082. val |= (1 << 31);
  1083. }
  1084. encode_uint32(val, &ptr[4]);
  1085. encode_uint16(E->get().autotile_coord_x, &ptr[8]);
  1086. encode_uint16(E->get().autotile_coord_y, &ptr[10]);
  1087. idx += 3;
  1088. }
  1089. w.release();
  1090. return data;
  1091. }
  1092. #ifdef TOOLS_ENABLED
  1093. Rect2 TileMap::_edit_get_rect() const {
  1094. if (pending_update) {
  1095. const_cast<TileMap *>(this)->update_dirty_quadrants();
  1096. } else {
  1097. const_cast<TileMap *>(this)->_recompute_rect_cache();
  1098. }
  1099. return rect_cache;
  1100. }
  1101. #endif
  1102. void TileMap::set_collision_layer(uint32_t p_layer) {
  1103. collision_layer = p_layer;
  1104. if (!use_parent) {
  1105. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1106. Quadrant &q = E->get();
  1107. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  1108. }
  1109. }
  1110. }
  1111. void TileMap::set_collision_mask(uint32_t p_mask) {
  1112. collision_mask = p_mask;
  1113. if (!use_parent) {
  1114. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1115. Quadrant &q = E->get();
  1116. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  1117. }
  1118. }
  1119. }
  1120. void TileMap::set_collision_layer_bit(int p_bit, bool p_value) {
  1121. ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
  1122. uint32_t layer = get_collision_layer();
  1123. if (p_value) {
  1124. layer |= 1 << p_bit;
  1125. } else {
  1126. layer &= ~(1 << p_bit);
  1127. }
  1128. set_collision_layer(layer);
  1129. }
  1130. void TileMap::set_collision_mask_bit(int p_bit, bool p_value) {
  1131. ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
  1132. uint32_t mask = get_collision_mask();
  1133. if (p_value) {
  1134. mask |= 1 << p_bit;
  1135. } else {
  1136. mask &= ~(1 << p_bit);
  1137. }
  1138. set_collision_mask(mask);
  1139. }
  1140. bool TileMap::get_collision_use_kinematic() const {
  1141. return use_kinematic;
  1142. }
  1143. void TileMap::set_collision_use_kinematic(bool p_use_kinematic) {
  1144. _clear_quadrants();
  1145. use_kinematic = p_use_kinematic;
  1146. _recreate_quadrants();
  1147. }
  1148. bool TileMap::get_collision_use_parent() const {
  1149. return use_parent;
  1150. }
  1151. void TileMap::set_collision_use_parent(bool p_use_parent) {
  1152. if (use_parent == p_use_parent) {
  1153. return;
  1154. }
  1155. _clear_quadrants();
  1156. use_parent = p_use_parent;
  1157. set_notify_local_transform(use_parent);
  1158. if (use_parent && is_inside_tree()) {
  1159. collision_parent = Object::cast_to<CollisionObject2D>(get_parent());
  1160. } else {
  1161. collision_parent = nullptr;
  1162. }
  1163. _recreate_quadrants();
  1164. _change_notify();
  1165. update_configuration_warning();
  1166. }
  1167. void TileMap::set_collision_friction(float p_friction) {
  1168. friction = p_friction;
  1169. if (!use_parent) {
  1170. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1171. Quadrant &q = E->get();
  1172. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, p_friction);
  1173. }
  1174. }
  1175. }
  1176. float TileMap::get_collision_friction() const {
  1177. return friction;
  1178. }
  1179. void TileMap::set_collision_bounce(float p_bounce) {
  1180. bounce = p_bounce;
  1181. if (!use_parent) {
  1182. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1183. Quadrant &q = E->get();
  1184. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, p_bounce);
  1185. }
  1186. }
  1187. }
  1188. float TileMap::get_collision_bounce() const {
  1189. return bounce;
  1190. }
  1191. void TileMap::set_bake_navigation(bool p_bake_navigation) {
  1192. bake_navigation = p_bake_navigation;
  1193. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1194. _make_quadrant_dirty(E);
  1195. }
  1196. }
  1197. bool TileMap::is_baking_navigation() {
  1198. return bake_navigation;
  1199. }
  1200. void TileMap::set_navigation_layers(uint32_t p_navigation_layers) {
  1201. navigation_layers = p_navigation_layers;
  1202. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1203. _make_quadrant_dirty(E);
  1204. }
  1205. }
  1206. uint32_t TileMap::get_navigation_layers() {
  1207. return navigation_layers;
  1208. }
  1209. uint32_t TileMap::get_collision_layer() const {
  1210. return collision_layer;
  1211. }
  1212. uint32_t TileMap::get_collision_mask() const {
  1213. return collision_mask;
  1214. }
  1215. bool TileMap::get_collision_layer_bit(int p_bit) const {
  1216. ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
  1217. return get_collision_layer() & (1 << p_bit);
  1218. }
  1219. bool TileMap::get_collision_mask_bit(int p_bit) const {
  1220. ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
  1221. return get_collision_mask() & (1 << p_bit);
  1222. }
  1223. void TileMap::set_mode(Mode p_mode) {
  1224. _clear_quadrants();
  1225. mode = p_mode;
  1226. _recreate_quadrants();
  1227. emit_signal("settings_changed");
  1228. }
  1229. TileMap::Mode TileMap::get_mode() const {
  1230. return mode;
  1231. }
  1232. void TileMap::set_half_offset(HalfOffset p_half_offset) {
  1233. _clear_quadrants();
  1234. half_offset = p_half_offset;
  1235. _recreate_quadrants();
  1236. emit_signal("settings_changed");
  1237. }
  1238. void TileMap::set_tile_origin(TileOrigin p_tile_origin) {
  1239. _clear_quadrants();
  1240. tile_origin = p_tile_origin;
  1241. _recreate_quadrants();
  1242. emit_signal("settings_changed");
  1243. }
  1244. TileMap::TileOrigin TileMap::get_tile_origin() const {
  1245. return tile_origin;
  1246. }
  1247. Vector2 TileMap::get_cell_draw_offset() const {
  1248. switch (mode) {
  1249. case MODE_SQUARE: {
  1250. return Vector2();
  1251. } break;
  1252. case MODE_ISOMETRIC: {
  1253. return Vector2(-cell_size.x * 0.5, 0);
  1254. } break;
  1255. case MODE_CUSTOM: {
  1256. Vector2 min;
  1257. min.x = MIN(custom_transform[0].x, min.x);
  1258. min.y = MIN(custom_transform[0].y, min.y);
  1259. min.x = MIN(custom_transform[1].x, min.x);
  1260. min.y = MIN(custom_transform[1].y, min.y);
  1261. return min;
  1262. } break;
  1263. }
  1264. return Vector2();
  1265. }
  1266. TileMap::HalfOffset TileMap::get_half_offset() const {
  1267. return half_offset;
  1268. }
  1269. Transform2D TileMap::get_cell_transform() const {
  1270. switch (mode) {
  1271. case MODE_SQUARE: {
  1272. Transform2D m;
  1273. m[0] *= cell_size.x;
  1274. m[1] *= cell_size.y;
  1275. return m;
  1276. } break;
  1277. case MODE_ISOMETRIC: {
  1278. //isometric only makes sense when y is positive in both x and y vectors, otherwise
  1279. //the drawing of tiles will overlap
  1280. Transform2D m;
  1281. m[0] = Vector2(cell_size.x * 0.5, cell_size.y * 0.5);
  1282. m[1] = Vector2(-cell_size.x * 0.5, cell_size.y * 0.5);
  1283. return m;
  1284. } break;
  1285. case MODE_CUSTOM: {
  1286. return custom_transform;
  1287. } break;
  1288. }
  1289. return Transform2D();
  1290. }
  1291. void TileMap::set_custom_transform(const Transform2D &p_xform) {
  1292. _clear_quadrants();
  1293. custom_transform = p_xform;
  1294. _recreate_quadrants();
  1295. emit_signal("settings_changed");
  1296. }
  1297. Transform2D TileMap::get_custom_transform() const {
  1298. return custom_transform;
  1299. }
  1300. Vector2 TileMap::_map_to_world(int p_x, int p_y, bool p_ignore_ofs) const {
  1301. Vector2 ret = get_cell_transform().xform(Vector2(p_x, p_y));
  1302. if (!p_ignore_ofs) {
  1303. switch (half_offset) {
  1304. case HALF_OFFSET_X:
  1305. case HALF_OFFSET_NEGATIVE_X: {
  1306. if (ABS(p_y) & 1) {
  1307. ret += get_cell_transform()[0] * (half_offset == HALF_OFFSET_X ? 0.5 : -0.5);
  1308. }
  1309. } break;
  1310. case HALF_OFFSET_Y:
  1311. case HALF_OFFSET_NEGATIVE_Y: {
  1312. if (ABS(p_x) & 1) {
  1313. ret += get_cell_transform()[1] * (half_offset == HALF_OFFSET_Y ? 0.5 : -0.5);
  1314. }
  1315. } break;
  1316. case HALF_OFFSET_DISABLED: {
  1317. // Nothing to do.
  1318. }
  1319. }
  1320. }
  1321. return ret;
  1322. }
  1323. bool TileMap::_set(const StringName &p_name, const Variant &p_value) {
  1324. if (p_name == "format") {
  1325. if (p_value.get_type() == Variant::INT) {
  1326. format = (DataFormat)(p_value.operator int64_t()); // Set format used for loading
  1327. return true;
  1328. }
  1329. } else if (p_name == "tile_data") {
  1330. if (p_value.is_array()) {
  1331. _set_tile_data(p_value);
  1332. return true;
  1333. }
  1334. return false;
  1335. }
  1336. return false;
  1337. }
  1338. bool TileMap::_get(const StringName &p_name, Variant &r_ret) const {
  1339. if (p_name == "format") {
  1340. r_ret = FORMAT_2; // When saving, always save highest format
  1341. return true;
  1342. } else if (p_name == "tile_data") {
  1343. r_ret = _get_tile_data();
  1344. return true;
  1345. }
  1346. return false;
  1347. }
  1348. void TileMap::_get_property_list(List<PropertyInfo> *p_list) const {
  1349. PropertyInfo p(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL);
  1350. p_list->push_back(p);
  1351. p = PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL);
  1352. p_list->push_back(p);
  1353. }
  1354. void TileMap::_validate_property(PropertyInfo &property) const {
  1355. if (use_parent && property.name != "collision_use_parent" && property.name.begins_with("collision_")) {
  1356. property.usage = PROPERTY_USAGE_NOEDITOR;
  1357. }
  1358. }
  1359. Vector2 TileMap::map_to_world(const Vector2 &p_pos, bool p_ignore_ofs) const {
  1360. return _map_to_world(p_pos.x, p_pos.y, p_ignore_ofs);
  1361. }
  1362. Vector2 TileMap::world_to_map(const Vector2 &p_pos) const {
  1363. Vector2 ret = get_cell_transform().affine_inverse().xform(p_pos);
  1364. // Account for precision errors on the border (GH-23250).
  1365. // 0.00005 is 5*CMP_EPSILON, results would start being unpredictable if
  1366. // cell size is > 15,000, but we can hardly have more precision anyway with
  1367. // floating point.
  1368. ret += Vector2(0.00005, 0.00005);
  1369. switch (half_offset) {
  1370. case HALF_OFFSET_X: {
  1371. if (int(floor(ret.y)) & 1) {
  1372. ret.x -= 0.5;
  1373. }
  1374. } break;
  1375. case HALF_OFFSET_NEGATIVE_X: {
  1376. if (int(floor(ret.y)) & 1) {
  1377. ret.x += 0.5;
  1378. }
  1379. } break;
  1380. case HALF_OFFSET_Y: {
  1381. if (int(floor(ret.x)) & 1) {
  1382. ret.y -= 0.5;
  1383. }
  1384. } break;
  1385. case HALF_OFFSET_NEGATIVE_Y: {
  1386. if (int(floor(ret.x)) & 1) {
  1387. ret.y += 0.5;
  1388. }
  1389. } break;
  1390. case HALF_OFFSET_DISABLED: {
  1391. // Nothing to do.
  1392. }
  1393. }
  1394. return ret.floor();
  1395. }
  1396. void TileMap::set_y_sort_mode(bool p_enable) {
  1397. _clear_quadrants();
  1398. y_sort_mode = p_enable;
  1399. VS::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), y_sort_mode);
  1400. _recreate_quadrants();
  1401. emit_signal("settings_changed");
  1402. }
  1403. bool TileMap::is_y_sort_mode_enabled() const {
  1404. return y_sort_mode;
  1405. }
  1406. void TileMap::set_compatibility_mode(bool p_enable) {
  1407. _clear_quadrants();
  1408. compatibility_mode = p_enable;
  1409. _recreate_quadrants();
  1410. emit_signal("settings_changed");
  1411. }
  1412. bool TileMap::is_compatibility_mode_enabled() const {
  1413. return compatibility_mode;
  1414. }
  1415. void TileMap::set_centered_textures(bool p_enable) {
  1416. _clear_quadrants();
  1417. centered_textures = p_enable;
  1418. _recreate_quadrants();
  1419. emit_signal("settings_changed");
  1420. }
  1421. bool TileMap::is_centered_textures_enabled() const {
  1422. return centered_textures;
  1423. }
  1424. Array TileMap::get_used_cells() const {
  1425. Array a;
  1426. a.resize(tile_map.size());
  1427. int i = 0;
  1428. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  1429. Vector2 p(E->key().x, E->key().y);
  1430. a[i++] = p;
  1431. }
  1432. return a;
  1433. }
  1434. Array TileMap::get_used_cells_by_id(int p_id) const {
  1435. Array a;
  1436. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  1437. if (E->value().id == p_id) {
  1438. Vector2 p(E->key().x, E->key().y);
  1439. a.push_back(p);
  1440. }
  1441. }
  1442. return a;
  1443. }
  1444. Rect2 TileMap::get_used_rect() { // Not const because of cache
  1445. if (used_size_cache_dirty) {
  1446. if (tile_map.size() > 0) {
  1447. used_size_cache = Rect2(tile_map.front()->key().x, tile_map.front()->key().y, 0, 0);
  1448. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  1449. used_size_cache.expand_to(Vector2(E->key().x, E->key().y));
  1450. }
  1451. used_size_cache.size += Vector2(1, 1);
  1452. } else {
  1453. used_size_cache = Rect2();
  1454. }
  1455. used_size_cache_dirty = false;
  1456. }
  1457. return used_size_cache;
  1458. }
  1459. void TileMap::set_occluder_light_mask(int p_mask) {
  1460. occluder_light_mask = p_mask;
  1461. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1462. for (Map<PosKey, Quadrant::Occluder>::Element *F = E->get().occluder_instances.front(); F; F = F->next()) {
  1463. VisualServer::get_singleton()->canvas_light_occluder_set_light_mask(F->get().id, occluder_light_mask);
  1464. }
  1465. }
  1466. }
  1467. int TileMap::get_occluder_light_mask() const {
  1468. return occluder_light_mask;
  1469. }
  1470. void TileMap::set_light_mask(int p_light_mask) {
  1471. CanvasItem::set_light_mask(p_light_mask);
  1472. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  1473. for (List<RID>::Element *F = E->get().canvas_items.front(); F; F = F->next()) {
  1474. VisualServer::get_singleton()->canvas_item_set_light_mask(F->get(), get_light_mask());
  1475. }
  1476. }
  1477. }
  1478. void TileMap::set_clip_uv(bool p_enable) {
  1479. if (clip_uv == p_enable) {
  1480. return;
  1481. }
  1482. _clear_quadrants();
  1483. clip_uv = p_enable;
  1484. _recreate_quadrants();
  1485. }
  1486. bool TileMap::get_clip_uv() const {
  1487. return clip_uv;
  1488. }
  1489. String TileMap::get_configuration_warning() const {
  1490. String warning = Node2D::get_configuration_warning();
  1491. if (use_parent && !collision_parent) {
  1492. if (!warning.empty()) {
  1493. warning += "\n\n";
  1494. }
  1495. return TTR("TileMap with Use Parent on needs a parent CollisionObject2D to give shapes to. Please use it as a child of Area2D, StaticBody2D, RigidBody2D, KinematicBody2D, etc. to give them a shape.");
  1496. }
  1497. return warning;
  1498. }
  1499. void TileMap::set_show_collision(bool p_value) {
  1500. show_collision = p_value;
  1501. _recreate_quadrants();
  1502. }
  1503. bool TileMap::is_show_collision_enabled() const {
  1504. return show_collision;
  1505. }
  1506. void TileMap::_bind_methods() {
  1507. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  1508. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  1509. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &TileMap::set_mode);
  1510. ClassDB::bind_method(D_METHOD("get_mode"), &TileMap::get_mode);
  1511. ClassDB::bind_method(D_METHOD("set_half_offset", "half_offset"), &TileMap::set_half_offset);
  1512. ClassDB::bind_method(D_METHOD("get_half_offset"), &TileMap::get_half_offset);
  1513. ClassDB::bind_method(D_METHOD("set_custom_transform", "custom_transform"), &TileMap::set_custom_transform);
  1514. ClassDB::bind_method(D_METHOD("get_custom_transform"), &TileMap::get_custom_transform);
  1515. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &TileMap::set_cell_size);
  1516. ClassDB::bind_method(D_METHOD("get_cell_size"), &TileMap::get_cell_size);
  1517. ClassDB::bind_method(D_METHOD("_set_old_cell_size", "size"), &TileMap::_set_old_cell_size);
  1518. ClassDB::bind_method(D_METHOD("_get_old_cell_size"), &TileMap::_get_old_cell_size);
  1519. ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
  1520. ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
  1521. ClassDB::bind_method(D_METHOD("set_tile_origin", "origin"), &TileMap::set_tile_origin);
  1522. ClassDB::bind_method(D_METHOD("get_tile_origin"), &TileMap::get_tile_origin);
  1523. ClassDB::bind_method(D_METHOD("set_clip_uv", "enable"), &TileMap::set_clip_uv);
  1524. ClassDB::bind_method(D_METHOD("get_clip_uv"), &TileMap::get_clip_uv);
  1525. ClassDB::bind_method(D_METHOD("set_y_sort_mode", "enable"), &TileMap::set_y_sort_mode);
  1526. ClassDB::bind_method(D_METHOD("is_y_sort_mode_enabled"), &TileMap::is_y_sort_mode_enabled);
  1527. ClassDB::bind_method(D_METHOD("set_compatibility_mode", "enable"), &TileMap::set_compatibility_mode);
  1528. ClassDB::bind_method(D_METHOD("is_compatibility_mode_enabled"), &TileMap::is_compatibility_mode_enabled);
  1529. ClassDB::bind_method(D_METHOD("set_show_collision", "enable"), &TileMap::set_show_collision);
  1530. ClassDB::bind_method(D_METHOD("is_show_collision_enabled"), &TileMap::is_show_collision_enabled);
  1531. ClassDB::bind_method(D_METHOD("set_centered_textures", "enable"), &TileMap::set_centered_textures);
  1532. ClassDB::bind_method(D_METHOD("is_centered_textures_enabled"), &TileMap::is_centered_textures_enabled);
  1533. ClassDB::bind_method(D_METHOD("set_collision_use_kinematic", "use_kinematic"), &TileMap::set_collision_use_kinematic);
  1534. ClassDB::bind_method(D_METHOD("get_collision_use_kinematic"), &TileMap::get_collision_use_kinematic);
  1535. ClassDB::bind_method(D_METHOD("set_collision_use_parent", "use_parent"), &TileMap::set_collision_use_parent);
  1536. ClassDB::bind_method(D_METHOD("get_collision_use_parent"), &TileMap::get_collision_use_parent);
  1537. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &TileMap::set_collision_layer);
  1538. ClassDB::bind_method(D_METHOD("get_collision_layer"), &TileMap::get_collision_layer);
  1539. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &TileMap::set_collision_mask);
  1540. ClassDB::bind_method(D_METHOD("get_collision_mask"), &TileMap::get_collision_mask);
  1541. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &TileMap::set_collision_layer_bit);
  1542. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &TileMap::get_collision_layer_bit);
  1543. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &TileMap::set_collision_mask_bit);
  1544. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &TileMap::get_collision_mask_bit);
  1545. ClassDB::bind_method(D_METHOD("set_collision_friction", "value"), &TileMap::set_collision_friction);
  1546. ClassDB::bind_method(D_METHOD("get_collision_friction"), &TileMap::get_collision_friction);
  1547. ClassDB::bind_method(D_METHOD("set_collision_bounce", "value"), &TileMap::set_collision_bounce);
  1548. ClassDB::bind_method(D_METHOD("get_collision_bounce"), &TileMap::get_collision_bounce);
  1549. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &TileMap::set_bake_navigation);
  1550. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &TileMap::is_baking_navigation);
  1551. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &TileMap::set_navigation_layers);
  1552. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &TileMap::get_navigation_layers);
  1553. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &TileMap::set_occluder_light_mask);
  1554. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &TileMap::get_occluder_light_mask);
  1555. ClassDB::bind_method(D_METHOD("set_cell", "x", "y", "tile", "flip_x", "flip_y", "transpose", "autotile_coord"), &TileMap::set_cell, DEFVAL(false), DEFVAL(false), DEFVAL(false), DEFVAL(Vector2()));
  1556. ClassDB::bind_method(D_METHOD("set_cellv", "position", "tile", "flip_x", "flip_y", "transpose", "autotile_coord"), &TileMap::set_cellv, DEFVAL(false), DEFVAL(false), DEFVAL(false), DEFVAL(Vector2()));
  1557. ClassDB::bind_method(D_METHOD("_set_celld", "position", "data"), &TileMap::_set_celld);
  1558. ClassDB::bind_method(D_METHOD("get_cell", "x", "y"), &TileMap::get_cell);
  1559. ClassDB::bind_method(D_METHOD("get_cellv", "position"), &TileMap::get_cellv);
  1560. ClassDB::bind_method(D_METHOD("is_cell_x_flipped", "x", "y"), &TileMap::is_cell_x_flipped);
  1561. ClassDB::bind_method(D_METHOD("is_cell_y_flipped", "x", "y"), &TileMap::is_cell_y_flipped);
  1562. ClassDB::bind_method(D_METHOD("is_cell_transposed", "x", "y"), &TileMap::is_cell_transposed);
  1563. ClassDB::bind_method(D_METHOD("get_cell_autotile_coord", "x", "y"), &TileMap::get_cell_autotile_coord);
  1564. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
  1565. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  1566. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells);
  1567. ClassDB::bind_method(D_METHOD("get_used_cells_by_id", "id"), &TileMap::get_used_cells_by_id);
  1568. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  1569. ClassDB::bind_method(D_METHOD("map_to_world", "map_position", "ignore_half_ofs"), &TileMap::map_to_world, DEFVAL(false));
  1570. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &TileMap::world_to_map);
  1571. ClassDB::bind_method(D_METHOD("_clear_quadrants"), &TileMap::_clear_quadrants);
  1572. ClassDB::bind_method(D_METHOD("_recreate_quadrants"), &TileMap::_recreate_quadrants);
  1573. ClassDB::bind_method(D_METHOD("update_dirty_quadrants"), &TileMap::update_dirty_quadrants);
  1574. ClassDB::bind_method(D_METHOD("update_bitmask_area", "position"), &TileMap::update_bitmask_area);
  1575. ClassDB::bind_method(D_METHOD("update_bitmask_region", "start", "end"), &TileMap::update_bitmask_region, DEFVAL(Vector2()), DEFVAL(Vector2()));
  1576. ClassDB::bind_method(D_METHOD("_set_tile_data"), &TileMap::_set_tile_data);
  1577. ClassDB::bind_method(D_METHOD("_get_tile_data"), &TileMap::_get_tile_data);
  1578. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Square,Isometric,Custom"), "set_mode", "get_mode");
  1579. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  1580. ADD_GROUP("Cell", "cell_");
  1581. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size", PROPERTY_HINT_RANGE, "1,8192,1"), "set_cell_size", "get_cell_size");
  1582. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
  1583. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "cell_custom_transform"), "set_custom_transform", "get_custom_transform");
  1584. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_half_offset", PROPERTY_HINT_ENUM, "Offset X,Offset Y,Disabled,Offset Negative X,Offset Negative Y"), "set_half_offset", "get_half_offset");
  1585. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_tile_origin", PROPERTY_HINT_ENUM, "Top Left,Center,Bottom Left"), "set_tile_origin", "get_tile_origin");
  1586. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_y_sort"), "set_y_sort_mode", "is_y_sort_mode_enabled");
  1587. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_collision"), "set_show_collision", "is_show_collision_enabled");
  1588. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "compatibility_mode"), "set_compatibility_mode", "is_compatibility_mode_enabled");
  1589. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered_textures"), "set_centered_textures", "is_centered_textures_enabled");
  1590. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_clip_uv"), "set_clip_uv", "get_clip_uv");
  1591. ADD_GROUP("Collision", "collision_");
  1592. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_use_parent", PROPERTY_HINT_NONE, ""), "set_collision_use_parent", "get_collision_use_parent");
  1593. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_use_kinematic", PROPERTY_HINT_NONE, ""), "set_collision_use_kinematic", "get_collision_use_kinematic");
  1594. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_friction", "get_collision_friction");
  1595. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_bounce", "get_collision_bounce");
  1596. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  1597. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  1598. ADD_GROUP("Navigation", "");
  1599. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  1600. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  1601. ADD_GROUP("Occluder", "occluder_");
  1602. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  1603. ADD_PROPERTY_DEFAULT("format", FORMAT_1);
  1604. ADD_SIGNAL(MethodInfo("settings_changed"));
  1605. BIND_CONSTANT(INVALID_CELL);
  1606. BIND_ENUM_CONSTANT(MODE_SQUARE);
  1607. BIND_ENUM_CONSTANT(MODE_ISOMETRIC);
  1608. BIND_ENUM_CONSTANT(MODE_CUSTOM);
  1609. BIND_ENUM_CONSTANT(HALF_OFFSET_X);
  1610. BIND_ENUM_CONSTANT(HALF_OFFSET_Y);
  1611. BIND_ENUM_CONSTANT(HALF_OFFSET_DISABLED);
  1612. BIND_ENUM_CONSTANT(HALF_OFFSET_NEGATIVE_X);
  1613. BIND_ENUM_CONSTANT(HALF_OFFSET_NEGATIVE_Y);
  1614. BIND_ENUM_CONSTANT(TILE_ORIGIN_TOP_LEFT);
  1615. BIND_ENUM_CONSTANT(TILE_ORIGIN_CENTER);
  1616. BIND_ENUM_CONSTANT(TILE_ORIGIN_BOTTOM_LEFT);
  1617. }
  1618. void TileMap::_changed_callback(Object *p_changed, const char *p_prop) {
  1619. if (tile_set.is_valid() && tile_set.ptr() == p_changed) {
  1620. emit_signal("settings_changed");
  1621. }
  1622. }
  1623. TileMap::TileMap() {
  1624. rect_cache_dirty = true;
  1625. used_size_cache_dirty = true;
  1626. pending_update = false;
  1627. quadrant_order_dirty = false;
  1628. quadrant_size = 16;
  1629. cell_size = Size2(64, 64);
  1630. custom_transform = Transform2D(64, 0, 0, 64, 0, 0);
  1631. collision_layer = 1;
  1632. collision_mask = 1;
  1633. friction = 1;
  1634. bounce = 0;
  1635. mode = MODE_SQUARE;
  1636. half_offset = HALF_OFFSET_DISABLED;
  1637. use_parent = false;
  1638. collision_parent = nullptr;
  1639. use_kinematic = false;
  1640. navigation = nullptr;
  1641. y_sort_mode = false;
  1642. compatibility_mode = false;
  1643. centered_textures = false;
  1644. occluder_light_mask = 1;
  1645. clip_uv = false;
  1646. format = FORMAT_1; // Assume lowest possible format if none is present
  1647. fp_adjust = 0.00001;
  1648. tile_origin = TILE_ORIGIN_TOP_LEFT;
  1649. set_notify_transform(true);
  1650. set_notify_local_transform(false);
  1651. }
  1652. TileMap::~TileMap() {
  1653. if (tile_set.is_valid()) {
  1654. tile_set->remove_change_receptor(this);
  1655. }
  1656. clear();
  1657. }