tile_map.cpp 59 KB

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