tile_map.cpp 52 KB

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