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