tile_map.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /*************************************************************************/
  2. /* tile_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 = c->cast_to<Navigation2D>();
  47. if (navigation) {
  48. break;
  49. }
  50. c = c->get_parent()->cast_to<Node2D>();
  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. Transform2D xform;
  263. xform.set_origin(q.pos);
  264. vs->canvas_item_set_transform(canvas_item, xform);
  265. vs->canvas_item_set_light_mask(canvas_item, get_light_mask());
  266. q.canvas_items.push_back(canvas_item);
  267. if (debug_shapes) {
  268. debug_canvas_item = vs->canvas_item_create();
  269. vs->canvas_item_set_parent(debug_canvas_item, canvas_item);
  270. vs->canvas_item_set_z_as_relative_to_parent(debug_canvas_item, false);
  271. vs->canvas_item_set_z(debug_canvas_item, VS::CANVAS_ITEM_Z_MAX - 1);
  272. q.canvas_items.push_back(debug_canvas_item);
  273. prev_debug_canvas_item = debug_canvas_item;
  274. }
  275. prev_canvas_item = canvas_item;
  276. prev_material = mat;
  277. } else {
  278. canvas_item = prev_canvas_item;
  279. if (debug_shapes) {
  280. debug_canvas_item = prev_debug_canvas_item;
  281. }
  282. }
  283. Rect2 r = tile_set->tile_get_region(c.id);
  284. Size2 s = tex->get_size();
  285. if (r == Rect2())
  286. s = tex->get_size();
  287. else {
  288. s = r.size;
  289. r.position.x += fp_adjust;
  290. r.position.y += fp_adjust;
  291. r.size.x -= fp_adjust * 2.0;
  292. r.size.y -= fp_adjust * 2.0;
  293. }
  294. Rect2 rect;
  295. rect.position = offset.floor();
  296. rect.size = s;
  297. if (rect.size.y > rect.size.x) {
  298. if ((c.flip_h && (c.flip_v || c.transpose)) || (c.flip_v && !c.transpose))
  299. tile_ofs.y += rect.size.y - rect.size.x;
  300. } else if (rect.size.y < rect.size.x) {
  301. if ((c.flip_v && (c.flip_h || c.transpose)) || (c.flip_h && !c.transpose))
  302. tile_ofs.x += rect.size.x - rect.size.y;
  303. }
  304. /* rect.size.x+=fp_adjust;
  305. rect.size.y+=fp_adjust;*/
  306. if (c.transpose)
  307. SWAP(tile_ofs.x, tile_ofs.y);
  308. if (c.flip_h) {
  309. rect.size.x = -rect.size.x;
  310. tile_ofs.x = -tile_ofs.x;
  311. }
  312. if (c.flip_v) {
  313. rect.size.y = -rect.size.y;
  314. tile_ofs.y = -tile_ofs.y;
  315. }
  316. Vector2 center_ofs;
  317. if (tile_origin == TILE_ORIGIN_TOP_LEFT) {
  318. rect.position += tile_ofs;
  319. } else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
  320. rect.position += tile_ofs;
  321. if (c.transpose) {
  322. if (c.flip_h)
  323. rect.position.x -= cell_size.x;
  324. else
  325. rect.position.x += cell_size.x;
  326. } else {
  327. if (c.flip_v)
  328. rect.position.y -= cell_size.y;
  329. else
  330. rect.position.y += cell_size.y;
  331. }
  332. } else if (tile_origin == TILE_ORIGIN_CENTER) {
  333. rect.position += tcenter;
  334. Vector2 center = (s / 2) - tile_ofs;
  335. center_ofs = tcenter - (s / 2);
  336. if (c.flip_h)
  337. rect.position.x -= s.x - center.x;
  338. else
  339. rect.position.x -= center.x;
  340. if (c.flip_v)
  341. rect.position.y -= s.y - center.y;
  342. else
  343. rect.position.y -= center.y;
  344. }
  345. Ref<Texture> normal_map = tile_set->tile_get_normal_map(c.id);
  346. Color modulate = tile_set->tile_get_modulate(c.id);
  347. Color self_modulate = get_self_modulate();
  348. modulate = Color(modulate.r * self_modulate.r, modulate.g * self_modulate.g,
  349. modulate.b * self_modulate.b, modulate.a * self_modulate.a);
  350. if (r == Rect2()) {
  351. tex->draw_rect(canvas_item, rect, false, modulate, c.transpose, normal_map);
  352. } else {
  353. tex->draw_rect_region(canvas_item, rect, r, modulate, c.transpose, normal_map);
  354. }
  355. Vector<TileSet::ShapeData> shapes = tile_set->tile_get_shapes(c.id);
  356. for (int i = 0; i < shapes.size(); i++) {
  357. Ref<Shape2D> shape = shapes[i].shape;
  358. if (shape.is_valid()) {
  359. Transform2D xform;
  360. xform.set_origin(offset.floor());
  361. _fix_cell_transform(xform, c, shapes[i].shape_offset + center_ofs, s);
  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. shape_idx++;
  368. ps->body_set_shape_as_one_way_collision(q.body, shape_idx, shapes[i].one_way_collision);
  369. ps->body_set_shape_metadata(q.body, shape_idx, Vector2(E->key().x, E->key().y));
  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::clear() {
  601. _clear_quadrants();
  602. tile_map.clear();
  603. used_size_cache_dirty = true;
  604. }
  605. void TileMap::_set_tile_data(const PoolVector<int> &p_data) {
  606. int c = p_data.size();
  607. PoolVector<int>::Read r = p_data.read();
  608. for (int i = 0; i < c; i += 2) {
  609. const uint8_t *ptr = (const uint8_t *)&r[i];
  610. uint8_t local[8];
  611. for (int j = 0; j < 8; j++)
  612. local[j] = ptr[j];
  613. #ifdef BIG_ENDIAN_ENABLED
  614. SWAP(local[0], local[3]);
  615. SWAP(local[1], local[2]);
  616. SWAP(local[4], local[7]);
  617. SWAP(local[5], local[6]);
  618. #endif
  619. int16_t x = decode_uint16(&local[0]);
  620. int16_t y = decode_uint16(&local[2]);
  621. uint32_t v = decode_uint32(&local[4]);
  622. bool flip_h = v & (1 << 29);
  623. bool flip_v = v & (1 << 30);
  624. bool transpose = v & (1 << 31);
  625. v &= (1 << 29) - 1;
  626. /*
  627. if (x<-20 || y <-20 || x>4000 || y>4000)
  628. continue;
  629. */
  630. set_cell(x, y, v, flip_h, flip_v, transpose);
  631. }
  632. }
  633. PoolVector<int> TileMap::_get_tile_data() const {
  634. PoolVector<int> data;
  635. data.resize(tile_map.size() * 2);
  636. PoolVector<int>::Write w = data.write();
  637. int idx = 0;
  638. for (const Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  639. uint8_t *ptr = (uint8_t *)&w[idx];
  640. encode_uint16(E->key().x, &ptr[0]);
  641. encode_uint16(E->key().y, &ptr[2]);
  642. uint32_t val = E->get().id;
  643. if (E->get().flip_h)
  644. val |= (1 << 29);
  645. if (E->get().flip_v)
  646. val |= (1 << 30);
  647. if (E->get().transpose)
  648. val |= (1 << 31);
  649. encode_uint32(val, &ptr[4]);
  650. idx += 2;
  651. }
  652. w = PoolVector<int>::Write();
  653. return data;
  654. }
  655. Rect2 TileMap::get_item_rect() const {
  656. const_cast<TileMap *>(this)->_update_dirty_quadrants();
  657. return rect_cache;
  658. }
  659. void TileMap::set_collision_layer(uint32_t p_layer) {
  660. collision_layer = p_layer;
  661. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  662. Quadrant &q = E->get();
  663. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  664. }
  665. }
  666. void TileMap::set_collision_mask(uint32_t p_mask) {
  667. collision_mask = p_mask;
  668. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  669. Quadrant &q = E->get();
  670. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  671. }
  672. }
  673. void TileMap::set_collision_layer_bit(int p_bit, bool p_value) {
  674. uint32_t layer = get_collision_layer();
  675. if (p_value)
  676. layer |= 1 << p_bit;
  677. else
  678. layer &= ~(1 << p_bit);
  679. set_collision_layer(layer);
  680. }
  681. void TileMap::set_collision_mask_bit(int p_bit, bool p_value) {
  682. uint32_t mask = get_collision_mask();
  683. if (p_value)
  684. mask |= 1 << p_bit;
  685. else
  686. mask &= ~(1 << p_bit);
  687. set_collision_mask(mask);
  688. }
  689. bool TileMap::get_collision_use_kinematic() const {
  690. return use_kinematic;
  691. }
  692. void TileMap::set_collision_use_kinematic(bool p_use_kinematic) {
  693. _clear_quadrants();
  694. use_kinematic = p_use_kinematic;
  695. _recreate_quadrants();
  696. }
  697. void TileMap::set_collision_friction(float p_friction) {
  698. friction = p_friction;
  699. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  700. Quadrant &q = E->get();
  701. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, p_friction);
  702. }
  703. }
  704. float TileMap::get_collision_friction() const {
  705. return friction;
  706. }
  707. void TileMap::set_collision_bounce(float p_bounce) {
  708. bounce = p_bounce;
  709. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  710. Quadrant &q = E->get();
  711. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, p_bounce);
  712. }
  713. }
  714. float TileMap::get_collision_bounce() const {
  715. return bounce;
  716. }
  717. uint32_t TileMap::get_collision_layer() const {
  718. return collision_layer;
  719. }
  720. uint32_t TileMap::get_collision_mask() const {
  721. return collision_mask;
  722. }
  723. bool TileMap::get_collision_layer_bit(int p_bit) const {
  724. return get_collision_layer() & (1 << p_bit);
  725. }
  726. bool TileMap::get_collision_mask_bit(int p_bit) const {
  727. return get_collision_mask() & (1 << p_bit);
  728. }
  729. void TileMap::set_mode(Mode p_mode) {
  730. _clear_quadrants();
  731. mode = p_mode;
  732. _recreate_quadrants();
  733. emit_signal("settings_changed");
  734. }
  735. TileMap::Mode TileMap::get_mode() const {
  736. return mode;
  737. }
  738. void TileMap::set_half_offset(HalfOffset p_half_offset) {
  739. _clear_quadrants();
  740. half_offset = p_half_offset;
  741. _recreate_quadrants();
  742. emit_signal("settings_changed");
  743. }
  744. void TileMap::set_tile_origin(TileOrigin p_tile_origin) {
  745. _clear_quadrants();
  746. tile_origin = p_tile_origin;
  747. _recreate_quadrants();
  748. emit_signal("settings_changed");
  749. }
  750. TileMap::TileOrigin TileMap::get_tile_origin() const {
  751. return tile_origin;
  752. }
  753. Vector2 TileMap::get_cell_draw_offset() const {
  754. switch (mode) {
  755. case MODE_SQUARE: {
  756. return Vector2();
  757. } break;
  758. case MODE_ISOMETRIC: {
  759. return Vector2(-cell_size.x * 0.5, 0);
  760. } break;
  761. case MODE_CUSTOM: {
  762. Vector2 min;
  763. min.x = MIN(custom_transform[0].x, min.x);
  764. min.y = MIN(custom_transform[0].y, min.y);
  765. min.x = MIN(custom_transform[1].x, min.x);
  766. min.y = MIN(custom_transform[1].y, min.y);
  767. return min;
  768. } break;
  769. }
  770. return Vector2();
  771. }
  772. TileMap::HalfOffset TileMap::get_half_offset() const {
  773. return half_offset;
  774. }
  775. Transform2D TileMap::get_cell_transform() const {
  776. switch (mode) {
  777. case MODE_SQUARE: {
  778. Transform2D m;
  779. m[0] *= cell_size.x;
  780. m[1] *= cell_size.y;
  781. return m;
  782. } break;
  783. case MODE_ISOMETRIC: {
  784. //isometric only makes sense when y is positive in both x and y vectors, otherwise
  785. //the drawing of tiles will overlap
  786. Transform2D m;
  787. m[0] = Vector2(cell_size.x * 0.5, cell_size.y * 0.5);
  788. m[1] = Vector2(-cell_size.x * 0.5, cell_size.y * 0.5);
  789. return m;
  790. } break;
  791. case MODE_CUSTOM: {
  792. return custom_transform;
  793. } break;
  794. }
  795. return Transform2D();
  796. }
  797. void TileMap::set_custom_transform(const Transform2D &p_xform) {
  798. _clear_quadrants();
  799. custom_transform = p_xform;
  800. _recreate_quadrants();
  801. emit_signal("settings_changed");
  802. }
  803. Transform2D TileMap::get_custom_transform() const {
  804. return custom_transform;
  805. }
  806. Vector2 TileMap::_map_to_world(int x, int y, bool p_ignore_ofs) const {
  807. Vector2 ret = get_cell_transform().xform(Vector2(x, y));
  808. if (!p_ignore_ofs) {
  809. switch (half_offset) {
  810. case HALF_OFFSET_X: {
  811. if (ABS(y) & 1) {
  812. ret += get_cell_transform()[0] * 0.5;
  813. }
  814. } break;
  815. case HALF_OFFSET_Y: {
  816. if (ABS(x) & 1) {
  817. ret += get_cell_transform()[1] * 0.5;
  818. }
  819. } break;
  820. default: {}
  821. }
  822. }
  823. return ret;
  824. }
  825. Vector2 TileMap::map_to_world(const Vector2 &p_pos, bool p_ignore_ofs) const {
  826. return _map_to_world(p_pos.x, p_pos.y, p_ignore_ofs);
  827. }
  828. Vector2 TileMap::world_to_map(const Vector2 &p_pos) const {
  829. Vector2 ret = get_cell_transform().affine_inverse().xform(p_pos);
  830. switch (half_offset) {
  831. case HALF_OFFSET_X: {
  832. if (ret.y > 0 ? int(ret.y) & 1 : (int(ret.y) - 1) & 1) {
  833. ret.x -= 0.5;
  834. }
  835. } break;
  836. case HALF_OFFSET_Y: {
  837. if (ret.x > 0 ? int(ret.x) & 1 : (int(ret.x) - 1) & 1) {
  838. ret.y -= 0.5;
  839. }
  840. } break;
  841. default: {}
  842. }
  843. return ret.floor();
  844. }
  845. void TileMap::set_y_sort_mode(bool p_enable) {
  846. _clear_quadrants();
  847. y_sort_mode = p_enable;
  848. VS::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), y_sort_mode);
  849. _recreate_quadrants();
  850. emit_signal("settings_changed");
  851. }
  852. bool TileMap::is_y_sort_mode_enabled() const {
  853. return y_sort_mode;
  854. }
  855. Array TileMap::get_used_cells() const {
  856. Array a;
  857. a.resize(tile_map.size());
  858. int i = 0;
  859. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  860. Vector2 p(E->key().x, E->key().y);
  861. a[i++] = p;
  862. }
  863. return a;
  864. }
  865. Rect2 TileMap::get_used_rect() { // Not const because of cache
  866. if (used_size_cache_dirty) {
  867. if (tile_map.size() > 0) {
  868. used_size_cache = Rect2(tile_map.front()->key().x, tile_map.front()->key().y, 0, 0);
  869. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  870. used_size_cache.expand_to(Vector2(E->key().x, E->key().y));
  871. }
  872. used_size_cache.size += Vector2(1, 1);
  873. } else {
  874. used_size_cache = Rect2();
  875. }
  876. used_size_cache_dirty = false;
  877. }
  878. return used_size_cache;
  879. }
  880. void TileMap::set_occluder_light_mask(int p_mask) {
  881. occluder_light_mask = p_mask;
  882. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  883. for (Map<PosKey, Quadrant::Occluder>::Element *F = E->get().occluder_instances.front(); F; F = F->next()) {
  884. VisualServer::get_singleton()->canvas_light_occluder_set_light_mask(F->get().id, occluder_light_mask);
  885. }
  886. }
  887. }
  888. int TileMap::get_occluder_light_mask() const {
  889. return occluder_light_mask;
  890. }
  891. void TileMap::set_light_mask(int p_light_mask) {
  892. CanvasItem::set_light_mask(p_light_mask);
  893. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  894. for (List<RID>::Element *F = E->get().canvas_items.front(); F; F = F->next()) {
  895. VisualServer::get_singleton()->canvas_item_set_light_mask(F->get(), get_light_mask());
  896. }
  897. }
  898. }
  899. void TileMap::_bind_methods() {
  900. ClassDB::bind_method(D_METHOD("set_tileset", "tileset:TileSet"), &TileMap::set_tileset);
  901. ClassDB::bind_method(D_METHOD("get_tileset:TileSet"), &TileMap::get_tileset);
  902. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &TileMap::set_mode);
  903. ClassDB::bind_method(D_METHOD("get_mode"), &TileMap::get_mode);
  904. ClassDB::bind_method(D_METHOD("set_half_offset", "half_offset"), &TileMap::set_half_offset);
  905. ClassDB::bind_method(D_METHOD("get_half_offset"), &TileMap::get_half_offset);
  906. ClassDB::bind_method(D_METHOD("set_custom_transform", "custom_transform"), &TileMap::set_custom_transform);
  907. ClassDB::bind_method(D_METHOD("get_custom_transform"), &TileMap::get_custom_transform);
  908. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &TileMap::set_cell_size);
  909. ClassDB::bind_method(D_METHOD("get_cell_size"), &TileMap::get_cell_size);
  910. ClassDB::bind_method(D_METHOD("_set_old_cell_size", "size"), &TileMap::_set_old_cell_size);
  911. ClassDB::bind_method(D_METHOD("_get_old_cell_size"), &TileMap::_get_old_cell_size);
  912. ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
  913. ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
  914. ClassDB::bind_method(D_METHOD("set_tile_origin", "origin"), &TileMap::set_tile_origin);
  915. ClassDB::bind_method(D_METHOD("get_tile_origin"), &TileMap::get_tile_origin);
  916. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &TileMap::set_center_x);
  917. ClassDB::bind_method(D_METHOD("get_center_x"), &TileMap::get_center_x);
  918. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &TileMap::set_center_y);
  919. ClassDB::bind_method(D_METHOD("get_center_y"), &TileMap::get_center_y);
  920. ClassDB::bind_method(D_METHOD("set_y_sort_mode", "enable"), &TileMap::set_y_sort_mode);
  921. ClassDB::bind_method(D_METHOD("is_y_sort_mode_enabled"), &TileMap::is_y_sort_mode_enabled);
  922. ClassDB::bind_method(D_METHOD("set_collision_use_kinematic", "use_kinematic"), &TileMap::set_collision_use_kinematic);
  923. ClassDB::bind_method(D_METHOD("get_collision_use_kinematic"), &TileMap::get_collision_use_kinematic);
  924. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &TileMap::set_collision_layer);
  925. ClassDB::bind_method(D_METHOD("get_collision_layer"), &TileMap::get_collision_layer);
  926. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &TileMap::set_collision_mask);
  927. ClassDB::bind_method(D_METHOD("get_collision_mask"), &TileMap::get_collision_mask);
  928. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &TileMap::set_collision_layer_bit);
  929. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &TileMap::get_collision_layer_bit);
  930. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &TileMap::set_collision_mask_bit);
  931. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &TileMap::get_collision_mask_bit);
  932. ClassDB::bind_method(D_METHOD("set_collision_friction", "value"), &TileMap::set_collision_friction);
  933. ClassDB::bind_method(D_METHOD("get_collision_friction"), &TileMap::get_collision_friction);
  934. ClassDB::bind_method(D_METHOD("set_collision_bounce", "value"), &TileMap::set_collision_bounce);
  935. ClassDB::bind_method(D_METHOD("get_collision_bounce"), &TileMap::get_collision_bounce);
  936. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &TileMap::set_occluder_light_mask);
  937. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &TileMap::get_occluder_light_mask);
  938. ClassDB::bind_method(D_METHOD("set_cell", "x", "y", "tile", "flip_x", "flip_y", "transpose"), &TileMap::set_cell, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  939. ClassDB::bind_method(D_METHOD("set_cellv", "pos", "tile", "flip_x", "flip_y", "transpose"), &TileMap::set_cellv, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  940. ClassDB::bind_method(D_METHOD("get_cell", "x", "y"), &TileMap::get_cell);
  941. ClassDB::bind_method(D_METHOD("get_cellv", "pos"), &TileMap::get_cellv);
  942. ClassDB::bind_method(D_METHOD("is_cell_x_flipped", "x", "y"), &TileMap::is_cell_x_flipped);
  943. ClassDB::bind_method(D_METHOD("is_cell_y_flipped", "x", "y"), &TileMap::is_cell_y_flipped);
  944. ClassDB::bind_method(D_METHOD("is_cell_transposed", "x", "y"), &TileMap::is_cell_transposed);
  945. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  946. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells);
  947. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  948. ClassDB::bind_method(D_METHOD("map_to_world", "mappos", "ignore_half_ofs"), &TileMap::map_to_world, DEFVAL(false));
  949. ClassDB::bind_method(D_METHOD("world_to_map", "worldpos"), &TileMap::world_to_map);
  950. ClassDB::bind_method(D_METHOD("_clear_quadrants"), &TileMap::_clear_quadrants);
  951. ClassDB::bind_method(D_METHOD("_recreate_quadrants"), &TileMap::_recreate_quadrants);
  952. ClassDB::bind_method(D_METHOD("_update_dirty_quadrants"), &TileMap::_update_dirty_quadrants);
  953. ClassDB::bind_method(D_METHOD("_set_tile_data"), &TileMap::_set_tile_data);
  954. ClassDB::bind_method(D_METHOD("_get_tile_data"), &TileMap::_get_tile_data);
  955. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Square,Isometric,Custom"), "set_mode", "get_mode");
  956. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  957. ADD_GROUP("Cell", "cell_");
  958. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size", PROPERTY_HINT_RANGE, "1,8192,1"), "set_cell_size", "get_cell_size");
  959. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
  960. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "cell_custom_transform"), "set_custom_transform", "get_custom_transform");
  961. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_half_offset", PROPERTY_HINT_ENUM, "Offset X,Offset Y,Disabled"), "set_half_offset", "get_half_offset");
  962. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_tile_origin", PROPERTY_HINT_ENUM, "Top Left,Center,Bottom Left"), "set_tile_origin", "get_tile_origin");
  963. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_y_sort"), "set_y_sort_mode", "is_y_sort_mode_enabled");
  964. ADD_GROUP("Collision", "collision_");
  965. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_use_kinematic", PROPERTY_HINT_NONE, ""), "set_collision_use_kinematic", "get_collision_use_kinematic");
  966. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_friction", "get_collision_friction");
  967. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_bounce", "get_collision_bounce");
  968. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  969. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  970. ADD_GROUP("Occluder", "occluder_");
  971. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  972. ADD_GROUP("", "");
  973. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_tile_data", "_get_tile_data");
  974. ADD_SIGNAL(MethodInfo("settings_changed"));
  975. BIND_CONSTANT(INVALID_CELL);
  976. BIND_CONSTANT(MODE_SQUARE);
  977. BIND_CONSTANT(MODE_ISOMETRIC);
  978. BIND_CONSTANT(MODE_CUSTOM);
  979. BIND_CONSTANT(HALF_OFFSET_X);
  980. BIND_CONSTANT(HALF_OFFSET_Y);
  981. BIND_CONSTANT(HALF_OFFSET_DISABLED);
  982. BIND_CONSTANT(TILE_ORIGIN_TOP_LEFT);
  983. BIND_CONSTANT(TILE_ORIGIN_CENTER);
  984. BIND_CONSTANT(TILE_ORIGIN_BOTTOM_LEFT);
  985. }
  986. TileMap::TileMap() {
  987. rect_cache_dirty = true;
  988. used_size_cache_dirty = true;
  989. pending_update = false;
  990. quadrant_order_dirty = false;
  991. quadrant_size = 16;
  992. cell_size = Size2(64, 64);
  993. center_x = false;
  994. center_y = false;
  995. collision_layer = 1;
  996. collision_mask = 1;
  997. friction = 1;
  998. bounce = 0;
  999. mode = MODE_SQUARE;
  1000. half_offset = HALF_OFFSET_DISABLED;
  1001. use_kinematic = false;
  1002. navigation = NULL;
  1003. y_sort_mode = false;
  1004. occluder_light_mask = 1;
  1005. fp_adjust = 0.00001;
  1006. tile_origin = TILE_ORIGIN_TOP_LEFT;
  1007. set_notify_transform(true);
  1008. }
  1009. TileMap::~TileMap() {
  1010. clear();
  1011. }