tile_map.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /*************************************************************************/
  2. /* tile_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "io/marshalls.h"
  32. #include "method_bind_ext.gen.inc"
  33. #include "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. _update_dirty_quadrants();
  54. RID space = get_world_2d()->get_space();
  55. _update_quadrant_transform();
  56. _update_quadrant_space(space);
  57. } break;
  58. case NOTIFICATION_EXIT_TREE: {
  59. _update_quadrant_space(RID());
  60. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  61. Quadrant &q = E->get();
  62. if (navigation) {
  63. for (Map<PosKey, Quadrant::NavPoly>::Element *E = q.navpoly_ids.front(); E; E = E->next()) {
  64. navigation->navpoly_remove(E->get().id);
  65. }
  66. q.navpoly_ids.clear();
  67. }
  68. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  69. VS::get_singleton()->free(E->get().id);
  70. }
  71. q.occluder_instances.clear();
  72. }
  73. navigation = NULL;
  74. } break;
  75. case NOTIFICATION_TRANSFORM_CHANGED: {
  76. //move stuff
  77. _update_quadrant_transform();
  78. } break;
  79. }
  80. }
  81. void TileMap::_update_quadrant_space(const RID &p_space) {
  82. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  83. Quadrant &q = E->get();
  84. Physics2DServer::get_singleton()->body_set_space(q.body, p_space);
  85. }
  86. }
  87. void TileMap::_update_quadrant_transform() {
  88. if (!is_inside_tree())
  89. return;
  90. Transform2D global_transform = get_global_transform();
  91. Transform2D nav_rel;
  92. if (navigation)
  93. nav_rel = get_relative_transform_to_parent(navigation);
  94. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  95. Quadrant &q = E->get();
  96. Transform2D xform;
  97. xform.set_origin(q.pos);
  98. xform = global_transform * xform;
  99. Physics2DServer::get_singleton()->body_set_state(q.body, Physics2DServer::BODY_STATE_TRANSFORM, xform);
  100. if (navigation) {
  101. for (Map<PosKey, Quadrant::NavPoly>::Element *E = q.navpoly_ids.front(); E; E = E->next()) {
  102. navigation->navpoly_set_transform(E->get().id, nav_rel * E->get().xform);
  103. }
  104. }
  105. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  106. VS::get_singleton()->canvas_light_occluder_set_transform(E->get().id, global_transform * E->get().xform);
  107. }
  108. }
  109. }
  110. void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
  111. if (tile_set.is_valid())
  112. tile_set->disconnect("changed", this, "_recreate_quadrants");
  113. _clear_quadrants();
  114. tile_set = p_tileset;
  115. if (tile_set.is_valid())
  116. tile_set->connect("changed", this, "_recreate_quadrants");
  117. else
  118. clear();
  119. _recreate_quadrants();
  120. emit_signal("settings_changed");
  121. }
  122. Ref<TileSet> TileMap::get_tileset() const {
  123. return tile_set;
  124. }
  125. void TileMap::set_cell_size(Size2 p_size) {
  126. ERR_FAIL_COND(p_size.x < 1 || p_size.y < 1);
  127. _clear_quadrants();
  128. cell_size = p_size;
  129. _recreate_quadrants();
  130. emit_signal("settings_changed");
  131. }
  132. Size2 TileMap::get_cell_size() const {
  133. return cell_size;
  134. }
  135. void TileMap::set_quadrant_size(int p_size) {
  136. ERR_FAIL_COND(p_size < 1);
  137. _clear_quadrants();
  138. quadrant_size = p_size;
  139. _recreate_quadrants();
  140. emit_signal("settings_changed");
  141. }
  142. int TileMap::get_quadrant_size() const {
  143. return quadrant_size;
  144. }
  145. void TileMap::set_center_x(bool p_enable) {
  146. center_x = p_enable;
  147. _recreate_quadrants();
  148. emit_signal("settings_changed");
  149. }
  150. bool TileMap::get_center_x() const {
  151. return center_x;
  152. }
  153. void TileMap::set_center_y(bool p_enable) {
  154. center_y = p_enable;
  155. _recreate_quadrants();
  156. emit_signal("settings_changed");
  157. }
  158. bool TileMap::get_center_y() const {
  159. return center_y;
  160. }
  161. void TileMap::_fix_cell_transform(Transform2D &xform, const Cell &p_cell, const Vector2 &p_offset, const Size2 &p_sc) {
  162. Size2 s = p_sc;
  163. Vector2 offset = p_offset;
  164. if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
  165. offset.y += cell_size.y;
  166. if (s.y > s.x) {
  167. if ((p_cell.flip_h && (p_cell.flip_v || p_cell.transpose)) || (p_cell.flip_v && !p_cell.transpose))
  168. offset.y += s.y - s.x;
  169. } else if (s.y < s.x) {
  170. if ((p_cell.flip_v && (p_cell.flip_h || p_cell.transpose)) || (p_cell.flip_h && !p_cell.transpose))
  171. offset.x += s.x - s.y;
  172. }
  173. if (p_cell.transpose) {
  174. SWAP(xform.elements[0].x, xform.elements[0].y);
  175. SWAP(xform.elements[1].x, xform.elements[1].y);
  176. SWAP(offset.x, offset.y);
  177. SWAP(s.x, s.y);
  178. }
  179. if (p_cell.flip_h) {
  180. xform.elements[0].x = -xform.elements[0].x;
  181. xform.elements[1].x = -xform.elements[1].x;
  182. if (tile_origin == TILE_ORIGIN_TOP_LEFT || tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
  183. offset.x = s.x - offset.x;
  184. }
  185. if (p_cell.flip_v) {
  186. xform.elements[0].y = -xform.elements[0].y;
  187. xform.elements[1].y = -xform.elements[1].y;
  188. if (tile_origin == TILE_ORIGIN_TOP_LEFT)
  189. offset.y = s.y - offset.y;
  190. else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  191. if (p_cell.transpose)
  192. offset.y += s.y;
  193. else
  194. offset.y -= s.y;
  195. }
  196. }
  197. xform.elements[2].x += offset.x;
  198. xform.elements[2].y += offset.y;
  199. }
  200. void TileMap::_update_dirty_quadrants() {
  201. if (!pending_update)
  202. return;
  203. if (!is_inside_tree() || !tile_set.is_valid()) {
  204. pending_update = false;
  205. return;
  206. }
  207. VisualServer *vs = VisualServer::get_singleton();
  208. Physics2DServer *ps = Physics2DServer::get_singleton();
  209. Vector2 tofs = get_cell_draw_offset();
  210. Vector2 tcenter = cell_size / 2;
  211. Transform2D nav_rel;
  212. if (navigation)
  213. nav_rel = get_relative_transform_to_parent(navigation);
  214. Vector2 qofs;
  215. SceneTree *st = SceneTree::get_singleton();
  216. Color debug_collision_color;
  217. bool debug_shapes = st && st->is_debugging_collisions_hint();
  218. if (debug_shapes) {
  219. debug_collision_color = st->get_debug_collisions_color();
  220. }
  221. while (dirty_quadrant_list.first()) {
  222. Quadrant &q = *dirty_quadrant_list.first()->self();
  223. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  224. vs->free(E->get());
  225. }
  226. q.canvas_items.clear();
  227. ps->body_clear_shapes(q.body);
  228. int shape_idx = 0;
  229. if (navigation) {
  230. for (Map<PosKey, Quadrant::NavPoly>::Element *E = q.navpoly_ids.front(); E; E = E->next()) {
  231. navigation->navpoly_remove(E->get().id);
  232. }
  233. q.navpoly_ids.clear();
  234. }
  235. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  236. VS::get_singleton()->free(E->get().id);
  237. }
  238. q.occluder_instances.clear();
  239. Ref<ShaderMaterial> prev_material;
  240. RID prev_canvas_item;
  241. RID prev_debug_canvas_item;
  242. for (int i = 0; i < q.cells.size(); i++) {
  243. Map<PosKey, Cell>::Element *E = tile_map.find(q.cells[i]);
  244. Cell &c = E->get();
  245. //moment of truth
  246. if (!tile_set->has_tile(c.id))
  247. continue;
  248. Ref<Texture> tex = tile_set->tile_get_texture(c.id);
  249. Vector2 tile_ofs = tile_set->tile_get_texture_offset(c.id);
  250. Vector2 wofs = _map_to_world(E->key().x, E->key().y);
  251. Vector2 offset = wofs - q.pos + tofs;
  252. if (!tex.is_valid())
  253. continue;
  254. Ref<ShaderMaterial> mat = tile_set->tile_get_material(c.id);
  255. RID canvas_item;
  256. RID debug_canvas_item;
  257. if (prev_canvas_item == RID() || prev_material != mat) {
  258. canvas_item = vs->canvas_item_create();
  259. if (mat.is_valid())
  260. vs->canvas_item_set_material(canvas_item, mat->get_rid());
  261. vs->canvas_item_set_parent(canvas_item, get_canvas_item());
  262. _update_item_material_state(canvas_item);
  263. Transform2D xform;
  264. xform.set_origin(q.pos);
  265. vs->canvas_item_set_transform(canvas_item, xform);
  266. vs->canvas_item_set_light_mask(canvas_item, get_light_mask());
  267. q.canvas_items.push_back(canvas_item);
  268. if (debug_shapes) {
  269. debug_canvas_item = vs->canvas_item_create();
  270. vs->canvas_item_set_parent(debug_canvas_item, canvas_item);
  271. vs->canvas_item_set_z_as_relative_to_parent(debug_canvas_item, false);
  272. vs->canvas_item_set_z(debug_canvas_item, VS::CANVAS_ITEM_Z_MAX - 1);
  273. q.canvas_items.push_back(debug_canvas_item);
  274. prev_debug_canvas_item = debug_canvas_item;
  275. }
  276. prev_canvas_item = canvas_item;
  277. prev_material = mat;
  278. } else {
  279. canvas_item = prev_canvas_item;
  280. if (debug_shapes) {
  281. debug_canvas_item = prev_debug_canvas_item;
  282. }
  283. }
  284. Rect2 r = tile_set->tile_get_region(c.id);
  285. Size2 s = tex->get_size();
  286. if (r == Rect2())
  287. s = tex->get_size();
  288. else {
  289. s = r.size;
  290. }
  291. Rect2 rect;
  292. rect.position = offset.floor();
  293. rect.size = s;
  294. rect.size.x += fp_adjust;
  295. rect.size.y += fp_adjust;
  296. if (rect.size.y > rect.size.x) {
  297. if ((c.flip_h && (c.flip_v || c.transpose)) || (c.flip_v && !c.transpose))
  298. tile_ofs.y += rect.size.y - rect.size.x;
  299. } else if (rect.size.y < rect.size.x) {
  300. if ((c.flip_v && (c.flip_h || c.transpose)) || (c.flip_h && !c.transpose))
  301. tile_ofs.x += rect.size.x - rect.size.y;
  302. }
  303. /* rect.size.x+=fp_adjust;
  304. rect.size.y+=fp_adjust;*/
  305. if (c.transpose)
  306. SWAP(tile_ofs.x, tile_ofs.y);
  307. if (c.flip_h) {
  308. rect.size.x = -rect.size.x;
  309. tile_ofs.x = -tile_ofs.x;
  310. }
  311. if (c.flip_v) {
  312. rect.size.y = -rect.size.y;
  313. tile_ofs.y = -tile_ofs.y;
  314. }
  315. Vector2 center_ofs;
  316. if (tile_origin == TILE_ORIGIN_TOP_LEFT) {
  317. rect.position += tile_ofs;
  318. } else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  319. rect.position += tile_ofs;
  320. if (c.transpose) {
  321. if (c.flip_h)
  322. rect.position.x -= cell_size.x;
  323. else
  324. rect.position.x += cell_size.x;
  325. } else {
  326. if (c.flip_v)
  327. rect.position.y -= cell_size.y;
  328. else
  329. rect.position.y += cell_size.y;
  330. }
  331. } else if (tile_origin == TILE_ORIGIN_CENTER) {
  332. rect.position += tcenter;
  333. Vector2 center = (s / 2) - tile_ofs;
  334. center_ofs = tcenter - (s / 2);
  335. if (c.flip_h)
  336. rect.position.x -= s.x - center.x;
  337. else
  338. rect.position.x -= center.x;
  339. if (c.flip_v)
  340. rect.position.y -= s.y - center.y;
  341. else
  342. rect.position.y -= center.y;
  343. }
  344. Ref<Texture> normal_map = tile_set->tile_get_normal_map(c.id);
  345. Color modulate = tile_set->tile_get_modulate(c.id);
  346. Color self_modulate = get_self_modulate();
  347. modulate = Color(modulate.r * self_modulate.r, modulate.g * self_modulate.g,
  348. modulate.b * self_modulate.b, modulate.a * self_modulate.a);
  349. if (r == Rect2()) {
  350. tex->draw_rect(canvas_item, rect, false, modulate, c.transpose, normal_map);
  351. } else {
  352. tex->draw_rect_region(canvas_item, rect, r, modulate, c.transpose, normal_map);
  353. }
  354. Vector<TileSet::ShapeData> shapes = tile_set->tile_get_shapes(c.id);
  355. for (int i = 0; i < shapes.size(); i++) {
  356. Ref<Shape2D> shape = shapes[i].shape;
  357. if (shape.is_valid()) {
  358. Transform2D xform;
  359. xform.set_origin(offset.floor());
  360. _fix_cell_transform(xform, c, center_ofs, s);
  361. xform *= shapes[i].shape_transform;
  362. if (debug_canvas_item.is_valid()) {
  363. vs->canvas_item_add_set_transform(debug_canvas_item, xform);
  364. shape->draw(debug_canvas_item, debug_collision_color);
  365. }
  366. ps->body_add_shape(q.body, shape->get_rid(), xform);
  367. ps->body_set_shape_metadata(q.body, shape_idx, Vector2(E->key().x, E->key().y));
  368. ps->body_set_shape_as_one_way_collision(q.body, shape_idx, shapes[i].one_way_collision);
  369. shape_idx++;
  370. }
  371. }
  372. if (debug_canvas_item.is_valid()) {
  373. vs->canvas_item_add_set_transform(debug_canvas_item, Transform2D());
  374. }
  375. if (navigation) {
  376. Ref<NavigationPolygon> navpoly = tile_set->tile_get_navigation_polygon(c.id);
  377. if (navpoly.is_valid()) {
  378. Vector2 npoly_ofs = tile_set->tile_get_navigation_polygon_offset(c.id);
  379. Transform2D xform;
  380. xform.set_origin(offset.floor() + q.pos);
  381. _fix_cell_transform(xform, c, npoly_ofs + center_ofs, s);
  382. int pid = navigation->navpoly_create(navpoly, nav_rel * xform);
  383. Quadrant::NavPoly np;
  384. np.id = pid;
  385. np.xform = xform;
  386. q.navpoly_ids[E->key()] = np;
  387. }
  388. }
  389. Ref<OccluderPolygon2D> occluder = tile_set->tile_get_light_occluder(c.id);
  390. if (occluder.is_valid()) {
  391. Vector2 occluder_ofs = tile_set->tile_get_occluder_offset(c.id);
  392. Transform2D xform;
  393. xform.set_origin(offset.floor() + q.pos);
  394. _fix_cell_transform(xform, c, occluder_ofs + center_ofs, s);
  395. RID orid = VS::get_singleton()->canvas_light_occluder_create();
  396. VS::get_singleton()->canvas_light_occluder_set_transform(orid, get_global_transform() * xform);
  397. VS::get_singleton()->canvas_light_occluder_set_polygon(orid, occluder->get_rid());
  398. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(orid, get_canvas());
  399. VS::get_singleton()->canvas_light_occluder_set_light_mask(orid, occluder_light_mask);
  400. Quadrant::Occluder oc;
  401. oc.xform = xform;
  402. oc.id = orid;
  403. q.occluder_instances[E->key()] = oc;
  404. }
  405. }
  406. dirty_quadrant_list.remove(dirty_quadrant_list.first());
  407. quadrant_order_dirty = true;
  408. }
  409. pending_update = false;
  410. if (quadrant_order_dirty) {
  411. int index = -0x80000000; //always must be drawn below children
  412. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  413. Quadrant &q = E->get();
  414. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  415. VS::get_singleton()->canvas_item_set_draw_index(E->get(), index++);
  416. }
  417. }
  418. quadrant_order_dirty = false;
  419. }
  420. _recompute_rect_cache();
  421. }
  422. void TileMap::_recompute_rect_cache() {
  423. #ifdef DEBUG_ENABLED
  424. if (!rect_cache_dirty)
  425. return;
  426. Rect2 r_total;
  427. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  428. Rect2 r;
  429. r.position = _map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size());
  430. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size()));
  431. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size()));
  432. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size()));
  433. if (E == quadrant_map.front())
  434. r_total = r;
  435. else
  436. r_total = r_total.merge(r);
  437. }
  438. if (r_total == Rect2()) {
  439. rect_cache = Rect2(-10, -10, 20, 20);
  440. } else {
  441. rect_cache = r_total.grow(MAX(cell_size.x, cell_size.y) * _get_quadrant_size());
  442. }
  443. item_rect_changed();
  444. rect_cache_dirty = false;
  445. #endif
  446. }
  447. Map<TileMap::PosKey, TileMap::Quadrant>::Element *TileMap::_create_quadrant(const PosKey &p_qk) {
  448. Transform2D xform;
  449. //xform.set_origin(Point2(p_qk.x,p_qk.y)*cell_size*quadrant_size);
  450. Quadrant q;
  451. q.pos = _map_to_world(p_qk.x * _get_quadrant_size(), p_qk.y * _get_quadrant_size());
  452. q.pos += get_cell_draw_offset();
  453. if (tile_origin == TILE_ORIGIN_CENTER)
  454. q.pos += cell_size / 2;
  455. else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
  456. q.pos.y += cell_size.y;
  457. xform.set_origin(q.pos);
  458. //q.canvas_item = VisualServer::get_singleton()->canvas_item_create();
  459. q.body = Physics2DServer::get_singleton()->body_create(use_kinematic ? Physics2DServer::BODY_MODE_KINEMATIC : Physics2DServer::BODY_MODE_STATIC);
  460. Physics2DServer::get_singleton()->body_attach_object_instance_id(q.body, get_instance_id());
  461. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  462. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  463. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, friction);
  464. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, bounce);
  465. if (is_inside_tree()) {
  466. xform = get_global_transform() * xform;
  467. RID space = get_world_2d()->get_space();
  468. Physics2DServer::get_singleton()->body_set_space(q.body, space);
  469. }
  470. Physics2DServer::get_singleton()->body_set_state(q.body, Physics2DServer::BODY_STATE_TRANSFORM, xform);
  471. rect_cache_dirty = true;
  472. quadrant_order_dirty = true;
  473. return quadrant_map.insert(p_qk, q);
  474. }
  475. void TileMap::_erase_quadrant(Map<PosKey, Quadrant>::Element *Q) {
  476. Quadrant &q = Q->get();
  477. Physics2DServer::get_singleton()->free(q.body);
  478. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  479. VisualServer::get_singleton()->free(E->get());
  480. }
  481. q.canvas_items.clear();
  482. if (q.dirty_list.in_list())
  483. dirty_quadrant_list.remove(&q.dirty_list);
  484. if (navigation) {
  485. for (Map<PosKey, Quadrant::NavPoly>::Element *E = q.navpoly_ids.front(); E; E = E->next()) {
  486. navigation->navpoly_remove(E->get().id);
  487. }
  488. q.navpoly_ids.clear();
  489. }
  490. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  491. VS::get_singleton()->free(E->get().id);
  492. }
  493. q.occluder_instances.clear();
  494. quadrant_map.erase(Q);
  495. rect_cache_dirty = true;
  496. }
  497. void TileMap::_make_quadrant_dirty(Map<PosKey, Quadrant>::Element *Q) {
  498. Quadrant &q = Q->get();
  499. if (!q.dirty_list.in_list())
  500. dirty_quadrant_list.add(&q.dirty_list);
  501. if (pending_update)
  502. return;
  503. pending_update = true;
  504. if (!is_inside_tree())
  505. return;
  506. call_deferred("_update_dirty_quadrants");
  507. }
  508. void TileMap::set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose) {
  509. set_cell(p_pos.x, p_pos.y, p_tile, p_flip_x, p_flip_y, p_transpose);
  510. }
  511. void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose) {
  512. PosKey pk(p_x, p_y);
  513. Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  514. if (!E && p_tile == INVALID_CELL)
  515. return; //nothing to do
  516. PosKey qk(p_x / _get_quadrant_size(), p_y / _get_quadrant_size());
  517. if (p_tile == INVALID_CELL) {
  518. //erase existing
  519. tile_map.erase(pk);
  520. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  521. ERR_FAIL_COND(!Q);
  522. Quadrant &q = Q->get();
  523. q.cells.erase(pk);
  524. if (q.cells.size() == 0)
  525. _erase_quadrant(Q);
  526. else
  527. _make_quadrant_dirty(Q);
  528. return;
  529. }
  530. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  531. if (!E) {
  532. E = tile_map.insert(pk, Cell());
  533. if (!Q) {
  534. Q = _create_quadrant(qk);
  535. }
  536. Quadrant &q = Q->get();
  537. q.cells.insert(pk);
  538. } else {
  539. ERR_FAIL_COND(!Q); // quadrant should exist...
  540. 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)
  541. return; //nothing changed
  542. }
  543. Cell &c = E->get();
  544. c.id = p_tile;
  545. c.flip_h = p_flip_x;
  546. c.flip_v = p_flip_y;
  547. c.transpose = p_transpose;
  548. _make_quadrant_dirty(Q);
  549. used_size_cache_dirty = true;
  550. }
  551. int TileMap::get_cellv(const Vector2 &p_pos) const {
  552. return get_cell(p_pos.x, p_pos.y);
  553. }
  554. int TileMap::get_cell(int p_x, int p_y) const {
  555. PosKey pk(p_x, p_y);
  556. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  557. if (!E)
  558. return INVALID_CELL;
  559. return E->get().id;
  560. }
  561. bool TileMap::is_cell_x_flipped(int p_x, int p_y) const {
  562. PosKey pk(p_x, p_y);
  563. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  564. if (!E)
  565. return false;
  566. return E->get().flip_h;
  567. }
  568. bool TileMap::is_cell_y_flipped(int p_x, int p_y) const {
  569. PosKey pk(p_x, p_y);
  570. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  571. if (!E)
  572. return false;
  573. return E->get().flip_v;
  574. }
  575. bool TileMap::is_cell_transposed(int p_x, int p_y) const {
  576. PosKey pk(p_x, p_y);
  577. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  578. if (!E)
  579. return false;
  580. return E->get().transpose;
  581. }
  582. void TileMap::_recreate_quadrants() {
  583. _clear_quadrants();
  584. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  585. PosKey qk(E->key().x / _get_quadrant_size(), E->key().y / _get_quadrant_size());
  586. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  587. if (!Q) {
  588. Q = _create_quadrant(qk);
  589. dirty_quadrant_list.add(&Q->get().dirty_list);
  590. }
  591. Q->get().cells.insert(E->key());
  592. _make_quadrant_dirty(Q);
  593. }
  594. }
  595. void TileMap::_clear_quadrants() {
  596. while (quadrant_map.size()) {
  597. _erase_quadrant(quadrant_map.front());
  598. }
  599. }
  600. void TileMap::set_material(const Ref<Material> &p_material) {
  601. CanvasItem::set_material(p_material);
  602. _update_all_items_material_state();
  603. }
  604. void TileMap::set_use_parent_material(bool p_use_parent_material) {
  605. CanvasItem::set_use_parent_material(p_use_parent_material);
  606. _update_all_items_material_state();
  607. }
  608. void TileMap::_update_all_items_material_state() {
  609. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  610. Quadrant &q = E->get();
  611. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  612. _update_item_material_state(E->get());
  613. }
  614. }
  615. }
  616. void TileMap::_update_item_material_state(const RID &p_canvas_item) {
  617. VS::get_singleton()->canvas_item_set_use_parent_material(p_canvas_item, get_use_parent_material() || get_material().is_valid());
  618. }
  619. void TileMap::clear() {
  620. _clear_quadrants();
  621. tile_map.clear();
  622. used_size_cache_dirty = true;
  623. }
  624. void TileMap::_set_tile_data(const PoolVector<int> &p_data) {
  625. int c = p_data.size();
  626. PoolVector<int>::Read r = p_data.read();
  627. for (int i = 0; i < c; i += 2) {
  628. const uint8_t *ptr = (const uint8_t *)&r[i];
  629. uint8_t local[8];
  630. for (int j = 0; j < 8; j++)
  631. local[j] = ptr[j];
  632. #ifdef BIG_ENDIAN_ENABLED
  633. SWAP(local[0], local[3]);
  634. SWAP(local[1], local[2]);
  635. SWAP(local[4], local[7]);
  636. SWAP(local[5], local[6]);
  637. #endif
  638. int16_t x = decode_uint16(&local[0]);
  639. int16_t y = decode_uint16(&local[2]);
  640. uint32_t v = decode_uint32(&local[4]);
  641. bool flip_h = v & (1 << 29);
  642. bool flip_v = v & (1 << 30);
  643. bool transpose = v & (1 << 31);
  644. v &= (1 << 29) - 1;
  645. /*
  646. if (x<-20 || y <-20 || x>4000 || y>4000)
  647. continue;
  648. */
  649. set_cell(x, y, v, flip_h, flip_v, transpose);
  650. }
  651. }
  652. PoolVector<int> TileMap::_get_tile_data() const {
  653. PoolVector<int> data;
  654. data.resize(tile_map.size() * 2);
  655. PoolVector<int>::Write w = data.write();
  656. int idx = 0;
  657. for (const Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  658. uint8_t *ptr = (uint8_t *)&w[idx];
  659. encode_uint16(E->key().x, &ptr[0]);
  660. encode_uint16(E->key().y, &ptr[2]);
  661. uint32_t val = E->get().id;
  662. if (E->get().flip_h)
  663. val |= (1 << 29);
  664. if (E->get().flip_v)
  665. val |= (1 << 30);
  666. if (E->get().transpose)
  667. val |= (1 << 31);
  668. encode_uint32(val, &ptr[4]);
  669. idx += 2;
  670. }
  671. w = PoolVector<int>::Write();
  672. return data;
  673. }
  674. Rect2 TileMap::get_item_rect() const {
  675. const_cast<TileMap *>(this)->_update_dirty_quadrants();
  676. return rect_cache;
  677. }
  678. void TileMap::set_collision_layer(uint32_t p_layer) {
  679. collision_layer = p_layer;
  680. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  681. Quadrant &q = E->get();
  682. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  683. }
  684. }
  685. void TileMap::set_collision_mask(uint32_t p_mask) {
  686. collision_mask = p_mask;
  687. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  688. Quadrant &q = E->get();
  689. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  690. }
  691. }
  692. void TileMap::set_collision_layer_bit(int p_bit, bool p_value) {
  693. uint32_t layer = get_collision_layer();
  694. if (p_value)
  695. layer |= 1 << p_bit;
  696. else
  697. layer &= ~(1 << p_bit);
  698. set_collision_layer(layer);
  699. }
  700. void TileMap::set_collision_mask_bit(int p_bit, bool p_value) {
  701. uint32_t mask = get_collision_mask();
  702. if (p_value)
  703. mask |= 1 << p_bit;
  704. else
  705. mask &= ~(1 << p_bit);
  706. set_collision_mask(mask);
  707. }
  708. bool TileMap::get_collision_use_kinematic() const {
  709. return use_kinematic;
  710. }
  711. void TileMap::set_collision_use_kinematic(bool p_use_kinematic) {
  712. _clear_quadrants();
  713. use_kinematic = p_use_kinematic;
  714. _recreate_quadrants();
  715. }
  716. void TileMap::set_collision_friction(float p_friction) {
  717. friction = p_friction;
  718. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  719. Quadrant &q = E->get();
  720. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, p_friction);
  721. }
  722. }
  723. float TileMap::get_collision_friction() const {
  724. return friction;
  725. }
  726. void TileMap::set_collision_bounce(float p_bounce) {
  727. bounce = p_bounce;
  728. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  729. Quadrant &q = E->get();
  730. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, p_bounce);
  731. }
  732. }
  733. float TileMap::get_collision_bounce() const {
  734. return bounce;
  735. }
  736. uint32_t TileMap::get_collision_layer() const {
  737. return collision_layer;
  738. }
  739. uint32_t TileMap::get_collision_mask() const {
  740. return collision_mask;
  741. }
  742. bool TileMap::get_collision_layer_bit(int p_bit) const {
  743. return get_collision_layer() & (1 << p_bit);
  744. }
  745. bool TileMap::get_collision_mask_bit(int p_bit) const {
  746. return get_collision_mask() & (1 << p_bit);
  747. }
  748. void TileMap::set_mode(Mode p_mode) {
  749. _clear_quadrants();
  750. mode = p_mode;
  751. _recreate_quadrants();
  752. emit_signal("settings_changed");
  753. }
  754. TileMap::Mode TileMap::get_mode() const {
  755. return mode;
  756. }
  757. void TileMap::set_half_offset(HalfOffset p_half_offset) {
  758. _clear_quadrants();
  759. half_offset = p_half_offset;
  760. _recreate_quadrants();
  761. emit_signal("settings_changed");
  762. }
  763. void TileMap::set_tile_origin(TileOrigin p_tile_origin) {
  764. _clear_quadrants();
  765. tile_origin = p_tile_origin;
  766. _recreate_quadrants();
  767. emit_signal("settings_changed");
  768. }
  769. TileMap::TileOrigin TileMap::get_tile_origin() const {
  770. return tile_origin;
  771. }
  772. Vector2 TileMap::get_cell_draw_offset() const {
  773. switch (mode) {
  774. case MODE_SQUARE: {
  775. return Vector2();
  776. } break;
  777. case MODE_ISOMETRIC: {
  778. return Vector2(-cell_size.x * 0.5, 0);
  779. } break;
  780. case MODE_CUSTOM: {
  781. Vector2 min;
  782. min.x = MIN(custom_transform[0].x, min.x);
  783. min.y = MIN(custom_transform[0].y, min.y);
  784. min.x = MIN(custom_transform[1].x, min.x);
  785. min.y = MIN(custom_transform[1].y, min.y);
  786. return min;
  787. } break;
  788. }
  789. return Vector2();
  790. }
  791. TileMap::HalfOffset TileMap::get_half_offset() const {
  792. return half_offset;
  793. }
  794. Transform2D TileMap::get_cell_transform() const {
  795. switch (mode) {
  796. case MODE_SQUARE: {
  797. Transform2D m;
  798. m[0] *= cell_size.x;
  799. m[1] *= cell_size.y;
  800. return m;
  801. } break;
  802. case MODE_ISOMETRIC: {
  803. //isometric only makes sense when y is positive in both x and y vectors, otherwise
  804. //the drawing of tiles will overlap
  805. Transform2D m;
  806. m[0] = Vector2(cell_size.x * 0.5, cell_size.y * 0.5);
  807. m[1] = Vector2(-cell_size.x * 0.5, cell_size.y * 0.5);
  808. return m;
  809. } break;
  810. case MODE_CUSTOM: {
  811. return custom_transform;
  812. } break;
  813. }
  814. return Transform2D();
  815. }
  816. void TileMap::set_custom_transform(const Transform2D &p_xform) {
  817. _clear_quadrants();
  818. custom_transform = p_xform;
  819. _recreate_quadrants();
  820. emit_signal("settings_changed");
  821. }
  822. Transform2D TileMap::get_custom_transform() const {
  823. return custom_transform;
  824. }
  825. Vector2 TileMap::_map_to_world(int p_x, int p_y, bool p_ignore_ofs) const {
  826. Vector2 ret = get_cell_transform().xform(Vector2(p_x, p_y));
  827. if (!p_ignore_ofs) {
  828. switch (half_offset) {
  829. case HALF_OFFSET_X: {
  830. if (ABS(p_y) & 1) {
  831. ret += get_cell_transform()[0] * 0.5;
  832. }
  833. } break;
  834. case HALF_OFFSET_Y: {
  835. if (ABS(p_x) & 1) {
  836. ret += get_cell_transform()[1] * 0.5;
  837. }
  838. } break;
  839. default: {}
  840. }
  841. }
  842. return ret;
  843. }
  844. Vector2 TileMap::map_to_world(const Vector2 &p_pos, bool p_ignore_ofs) const {
  845. return _map_to_world(p_pos.x, p_pos.y, p_ignore_ofs);
  846. }
  847. Vector2 TileMap::world_to_map(const Vector2 &p_pos) const {
  848. Vector2 ret = get_cell_transform().affine_inverse().xform(p_pos);
  849. switch (half_offset) {
  850. case HALF_OFFSET_X: {
  851. if (ret.y > 0 ? int(ret.y) & 1 : (int(ret.y) - 1) & 1) {
  852. ret.x -= 0.5;
  853. }
  854. } break;
  855. case HALF_OFFSET_Y: {
  856. if (ret.x > 0 ? int(ret.x) & 1 : (int(ret.x) - 1) & 1) {
  857. ret.y -= 0.5;
  858. }
  859. } break;
  860. default: {}
  861. }
  862. return ret.floor();
  863. }
  864. void TileMap::set_y_sort_mode(bool p_enable) {
  865. _clear_quadrants();
  866. y_sort_mode = p_enable;
  867. VS::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), y_sort_mode);
  868. _recreate_quadrants();
  869. emit_signal("settings_changed");
  870. }
  871. bool TileMap::is_y_sort_mode_enabled() const {
  872. return y_sort_mode;
  873. }
  874. Array TileMap::get_used_cells() const {
  875. Array a;
  876. a.resize(tile_map.size());
  877. int i = 0;
  878. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  879. Vector2 p(E->key().x, E->key().y);
  880. a[i++] = p;
  881. }
  882. return a;
  883. }
  884. Array TileMap::get_used_cells_by_id(int p_id) const {
  885. Array a;
  886. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  887. if (E->value().id == p_id) {
  888. Vector2 p(E->key().x, E->key().y);
  889. a.push_back(p);
  890. }
  891. }
  892. return a;
  893. }
  894. Rect2 TileMap::get_used_rect() { // Not const because of cache
  895. if (used_size_cache_dirty) {
  896. if (tile_map.size() > 0) {
  897. used_size_cache = Rect2(tile_map.front()->key().x, tile_map.front()->key().y, 0, 0);
  898. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  899. used_size_cache.expand_to(Vector2(E->key().x, E->key().y));
  900. }
  901. used_size_cache.size += Vector2(1, 1);
  902. } else {
  903. used_size_cache = Rect2();
  904. }
  905. used_size_cache_dirty = false;
  906. }
  907. return used_size_cache;
  908. }
  909. void TileMap::set_occluder_light_mask(int p_mask) {
  910. occluder_light_mask = p_mask;
  911. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  912. for (Map<PosKey, Quadrant::Occluder>::Element *F = E->get().occluder_instances.front(); F; F = F->next()) {
  913. VisualServer::get_singleton()->canvas_light_occluder_set_light_mask(F->get().id, occluder_light_mask);
  914. }
  915. }
  916. }
  917. int TileMap::get_occluder_light_mask() const {
  918. return occluder_light_mask;
  919. }
  920. void TileMap::set_light_mask(int p_light_mask) {
  921. CanvasItem::set_light_mask(p_light_mask);
  922. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  923. for (List<RID>::Element *F = E->get().canvas_items.front(); F; F = F->next()) {
  924. VisualServer::get_singleton()->canvas_item_set_light_mask(F->get(), get_light_mask());
  925. }
  926. }
  927. }
  928. void TileMap::_bind_methods() {
  929. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  930. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  931. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &TileMap::set_mode);
  932. ClassDB::bind_method(D_METHOD("get_mode"), &TileMap::get_mode);
  933. ClassDB::bind_method(D_METHOD("set_half_offset", "half_offset"), &TileMap::set_half_offset);
  934. ClassDB::bind_method(D_METHOD("get_half_offset"), &TileMap::get_half_offset);
  935. ClassDB::bind_method(D_METHOD("set_custom_transform", "custom_transform"), &TileMap::set_custom_transform);
  936. ClassDB::bind_method(D_METHOD("get_custom_transform"), &TileMap::get_custom_transform);
  937. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &TileMap::set_cell_size);
  938. ClassDB::bind_method(D_METHOD("get_cell_size"), &TileMap::get_cell_size);
  939. ClassDB::bind_method(D_METHOD("_set_old_cell_size", "size"), &TileMap::_set_old_cell_size);
  940. ClassDB::bind_method(D_METHOD("_get_old_cell_size"), &TileMap::_get_old_cell_size);
  941. ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
  942. ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
  943. ClassDB::bind_method(D_METHOD("set_tile_origin", "origin"), &TileMap::set_tile_origin);
  944. ClassDB::bind_method(D_METHOD("get_tile_origin"), &TileMap::get_tile_origin);
  945. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &TileMap::set_center_x);
  946. ClassDB::bind_method(D_METHOD("get_center_x"), &TileMap::get_center_x);
  947. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &TileMap::set_center_y);
  948. ClassDB::bind_method(D_METHOD("get_center_y"), &TileMap::get_center_y);
  949. ClassDB::bind_method(D_METHOD("set_y_sort_mode", "enable"), &TileMap::set_y_sort_mode);
  950. ClassDB::bind_method(D_METHOD("is_y_sort_mode_enabled"), &TileMap::is_y_sort_mode_enabled);
  951. ClassDB::bind_method(D_METHOD("set_collision_use_kinematic", "use_kinematic"), &TileMap::set_collision_use_kinematic);
  952. ClassDB::bind_method(D_METHOD("get_collision_use_kinematic"), &TileMap::get_collision_use_kinematic);
  953. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &TileMap::set_collision_layer);
  954. ClassDB::bind_method(D_METHOD("get_collision_layer"), &TileMap::get_collision_layer);
  955. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &TileMap::set_collision_mask);
  956. ClassDB::bind_method(D_METHOD("get_collision_mask"), &TileMap::get_collision_mask);
  957. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &TileMap::set_collision_layer_bit);
  958. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &TileMap::get_collision_layer_bit);
  959. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &TileMap::set_collision_mask_bit);
  960. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &TileMap::get_collision_mask_bit);
  961. ClassDB::bind_method(D_METHOD("set_collision_friction", "value"), &TileMap::set_collision_friction);
  962. ClassDB::bind_method(D_METHOD("get_collision_friction"), &TileMap::get_collision_friction);
  963. ClassDB::bind_method(D_METHOD("set_collision_bounce", "value"), &TileMap::set_collision_bounce);
  964. ClassDB::bind_method(D_METHOD("get_collision_bounce"), &TileMap::get_collision_bounce);
  965. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &TileMap::set_occluder_light_mask);
  966. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &TileMap::get_occluder_light_mask);
  967. ClassDB::bind_method(D_METHOD("set_cell", "x", "y", "tile", "flip_x", "flip_y", "transpose"), &TileMap::set_cell, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  968. ClassDB::bind_method(D_METHOD("set_cellv", "position", "tile", "flip_x", "flip_y", "transpose"), &TileMap::set_cellv, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  969. ClassDB::bind_method(D_METHOD("get_cell", "x", "y"), &TileMap::get_cell);
  970. ClassDB::bind_method(D_METHOD("get_cellv", "position"), &TileMap::get_cellv);
  971. ClassDB::bind_method(D_METHOD("is_cell_x_flipped", "x", "y"), &TileMap::is_cell_x_flipped);
  972. ClassDB::bind_method(D_METHOD("is_cell_y_flipped", "x", "y"), &TileMap::is_cell_y_flipped);
  973. ClassDB::bind_method(D_METHOD("is_cell_transposed", "x", "y"), &TileMap::is_cell_transposed);
  974. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  975. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells);
  976. ClassDB::bind_method(D_METHOD("get_used_cells_by_id", "id"), &TileMap::get_used_cells_by_id);
  977. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  978. ClassDB::bind_method(D_METHOD("map_to_world", "map_position", "ignore_half_ofs"), &TileMap::map_to_world, DEFVAL(false));
  979. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &TileMap::world_to_map);
  980. ClassDB::bind_method(D_METHOD("_clear_quadrants"), &TileMap::_clear_quadrants);
  981. ClassDB::bind_method(D_METHOD("_recreate_quadrants"), &TileMap::_recreate_quadrants);
  982. ClassDB::bind_method(D_METHOD("_update_dirty_quadrants"), &TileMap::_update_dirty_quadrants);
  983. ClassDB::bind_method(D_METHOD("_set_tile_data"), &TileMap::_set_tile_data);
  984. ClassDB::bind_method(D_METHOD("_get_tile_data"), &TileMap::_get_tile_data);
  985. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Square,Isometric,Custom"), "set_mode", "get_mode");
  986. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  987. ADD_GROUP("Cell", "cell_");
  988. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size", PROPERTY_HINT_RANGE, "1,8192,1"), "set_cell_size", "get_cell_size");
  989. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
  990. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "cell_custom_transform"), "set_custom_transform", "get_custom_transform");
  991. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_half_offset", PROPERTY_HINT_ENUM, "Offset X,Offset Y,Disabled"), "set_half_offset", "get_half_offset");
  992. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_tile_origin", PROPERTY_HINT_ENUM, "Top Left,Center,Bottom Left"), "set_tile_origin", "get_tile_origin");
  993. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_y_sort"), "set_y_sort_mode", "is_y_sort_mode_enabled");
  994. ADD_GROUP("Collision", "collision_");
  995. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_use_kinematic", PROPERTY_HINT_NONE, ""), "set_collision_use_kinematic", "get_collision_use_kinematic");
  996. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_friction", "get_collision_friction");
  997. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_bounce", "get_collision_bounce");
  998. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  999. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  1000. ADD_GROUP("Occluder", "occluder_");
  1001. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  1002. ADD_GROUP("", "");
  1003. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_tile_data", "_get_tile_data");
  1004. ADD_SIGNAL(MethodInfo("settings_changed"));
  1005. BIND_CONSTANT(INVALID_CELL);
  1006. BIND_ENUM_CONSTANT(MODE_SQUARE);
  1007. BIND_ENUM_CONSTANT(MODE_ISOMETRIC);
  1008. BIND_ENUM_CONSTANT(MODE_CUSTOM);
  1009. BIND_ENUM_CONSTANT(HALF_OFFSET_X);
  1010. BIND_ENUM_CONSTANT(HALF_OFFSET_Y);
  1011. BIND_ENUM_CONSTANT(HALF_OFFSET_DISABLED);
  1012. BIND_ENUM_CONSTANT(TILE_ORIGIN_TOP_LEFT);
  1013. BIND_ENUM_CONSTANT(TILE_ORIGIN_CENTER);
  1014. BIND_ENUM_CONSTANT(TILE_ORIGIN_BOTTOM_LEFT);
  1015. }
  1016. TileMap::TileMap() {
  1017. rect_cache_dirty = true;
  1018. used_size_cache_dirty = true;
  1019. pending_update = false;
  1020. quadrant_order_dirty = false;
  1021. quadrant_size = 16;
  1022. cell_size = Size2(64, 64);
  1023. center_x = false;
  1024. center_y = false;
  1025. collision_layer = 1;
  1026. collision_mask = 1;
  1027. friction = 1;
  1028. bounce = 0;
  1029. mode = MODE_SQUARE;
  1030. half_offset = HALF_OFFSET_DISABLED;
  1031. use_kinematic = false;
  1032. navigation = NULL;
  1033. y_sort_mode = false;
  1034. occluder_light_mask = 1;
  1035. fp_adjust = 0.00001;
  1036. tile_origin = TILE_ORIGIN_TOP_LEFT;
  1037. set_notify_transform(true);
  1038. }
  1039. TileMap::~TileMap() {
  1040. clear();
  1041. }