tile_map.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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, center_ofs, s);
  362. xform *= shapes[i].shape_transform;
  363. if (debug_canvas_item.is_valid()) {
  364. vs->canvas_item_add_set_transform(debug_canvas_item, xform);
  365. shape->draw(debug_canvas_item, debug_collision_color);
  366. }
  367. ps->body_add_shape(q.body, shape->get_rid(), xform);
  368. ps->body_set_shape_metadata(q.body, shape_idx, Vector2(E->key().x, E->key().y));
  369. ps->body_set_shape_as_one_way_collision(q.body, shape_idx, shapes[i].one_way_collision);
  370. shape_idx++;
  371. }
  372. }
  373. if (debug_canvas_item.is_valid()) {
  374. vs->canvas_item_add_set_transform(debug_canvas_item, Transform2D());
  375. }
  376. if (navigation) {
  377. Ref<NavigationPolygon> navpoly = tile_set->tile_get_navigation_polygon(c.id);
  378. if (navpoly.is_valid()) {
  379. Vector2 npoly_ofs = tile_set->tile_get_navigation_polygon_offset(c.id);
  380. Transform2D xform;
  381. xform.set_origin(offset.floor() + q.pos);
  382. _fix_cell_transform(xform, c, npoly_ofs + center_ofs, s);
  383. int pid = navigation->navpoly_create(navpoly, nav_rel * xform);
  384. Quadrant::NavPoly np;
  385. np.id = pid;
  386. np.xform = xform;
  387. q.navpoly_ids[E->key()] = np;
  388. }
  389. }
  390. Ref<OccluderPolygon2D> occluder = tile_set->tile_get_light_occluder(c.id);
  391. if (occluder.is_valid()) {
  392. Vector2 occluder_ofs = tile_set->tile_get_occluder_offset(c.id);
  393. Transform2D xform;
  394. xform.set_origin(offset.floor() + q.pos);
  395. _fix_cell_transform(xform, c, occluder_ofs + center_ofs, s);
  396. RID orid = VS::get_singleton()->canvas_light_occluder_create();
  397. VS::get_singleton()->canvas_light_occluder_set_transform(orid, get_global_transform() * xform);
  398. VS::get_singleton()->canvas_light_occluder_set_polygon(orid, occluder->get_rid());
  399. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(orid, get_canvas());
  400. VS::get_singleton()->canvas_light_occluder_set_light_mask(orid, occluder_light_mask);
  401. Quadrant::Occluder oc;
  402. oc.xform = xform;
  403. oc.id = orid;
  404. q.occluder_instances[E->key()] = oc;
  405. }
  406. }
  407. dirty_quadrant_list.remove(dirty_quadrant_list.first());
  408. quadrant_order_dirty = true;
  409. }
  410. pending_update = false;
  411. if (quadrant_order_dirty) {
  412. int index = -0x80000000; //always must be drawn below children
  413. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  414. Quadrant &q = E->get();
  415. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  416. VS::get_singleton()->canvas_item_set_draw_index(E->get(), index++);
  417. }
  418. }
  419. quadrant_order_dirty = false;
  420. }
  421. _recompute_rect_cache();
  422. }
  423. void TileMap::_recompute_rect_cache() {
  424. #ifdef DEBUG_ENABLED
  425. if (!rect_cache_dirty)
  426. return;
  427. Rect2 r_total;
  428. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  429. Rect2 r;
  430. r.position = _map_to_world(E->key().x * _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()));
  432. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size() + _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size()));
  433. r.expand_to(_map_to_world(E->key().x * _get_quadrant_size(), E->key().y * _get_quadrant_size() + _get_quadrant_size()));
  434. if (E == quadrant_map.front())
  435. r_total = r;
  436. else
  437. r_total = r_total.merge(r);
  438. }
  439. if (r_total == Rect2()) {
  440. rect_cache = Rect2(-10, -10, 20, 20);
  441. } else {
  442. rect_cache = r_total.grow(MAX(cell_size.x, cell_size.y) * _get_quadrant_size());
  443. }
  444. item_rect_changed();
  445. rect_cache_dirty = false;
  446. #endif
  447. }
  448. Map<TileMap::PosKey, TileMap::Quadrant>::Element *TileMap::_create_quadrant(const PosKey &p_qk) {
  449. Transform2D xform;
  450. //xform.set_origin(Point2(p_qk.x,p_qk.y)*cell_size*quadrant_size);
  451. Quadrant q;
  452. q.pos = _map_to_world(p_qk.x * _get_quadrant_size(), p_qk.y * _get_quadrant_size());
  453. q.pos += get_cell_draw_offset();
  454. if (tile_origin == TILE_ORIGIN_CENTER)
  455. q.pos += cell_size / 2;
  456. else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
  457. q.pos.y += cell_size.y;
  458. xform.set_origin(q.pos);
  459. //q.canvas_item = VisualServer::get_singleton()->canvas_item_create();
  460. q.body = Physics2DServer::get_singleton()->body_create(use_kinematic ? Physics2DServer::BODY_MODE_KINEMATIC : Physics2DServer::BODY_MODE_STATIC);
  461. Physics2DServer::get_singleton()->body_attach_object_instance_ID(q.body, get_instance_ID());
  462. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  463. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  464. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, friction);
  465. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, bounce);
  466. if (is_inside_tree()) {
  467. xform = get_global_transform() * xform;
  468. RID space = get_world_2d()->get_space();
  469. Physics2DServer::get_singleton()->body_set_space(q.body, space);
  470. }
  471. Physics2DServer::get_singleton()->body_set_state(q.body, Physics2DServer::BODY_STATE_TRANSFORM, xform);
  472. rect_cache_dirty = true;
  473. quadrant_order_dirty = true;
  474. return quadrant_map.insert(p_qk, q);
  475. }
  476. void TileMap::_erase_quadrant(Map<PosKey, Quadrant>::Element *Q) {
  477. Quadrant &q = Q->get();
  478. Physics2DServer::get_singleton()->free(q.body);
  479. for (List<RID>::Element *E = q.canvas_items.front(); E; E = E->next()) {
  480. VisualServer::get_singleton()->free(E->get());
  481. }
  482. q.canvas_items.clear();
  483. if (q.dirty_list.in_list())
  484. dirty_quadrant_list.remove(&q.dirty_list);
  485. if (navigation) {
  486. for (Map<PosKey, Quadrant::NavPoly>::Element *E = q.navpoly_ids.front(); E; E = E->next()) {
  487. navigation->navpoly_remove(E->get().id);
  488. }
  489. q.navpoly_ids.clear();
  490. }
  491. for (Map<PosKey, Quadrant::Occluder>::Element *E = q.occluder_instances.front(); E; E = E->next()) {
  492. VS::get_singleton()->free(E->get().id);
  493. }
  494. q.occluder_instances.clear();
  495. quadrant_map.erase(Q);
  496. rect_cache_dirty = true;
  497. }
  498. void TileMap::_make_quadrant_dirty(Map<PosKey, Quadrant>::Element *Q) {
  499. Quadrant &q = Q->get();
  500. if (!q.dirty_list.in_list())
  501. dirty_quadrant_list.add(&q.dirty_list);
  502. if (pending_update)
  503. return;
  504. pending_update = true;
  505. if (!is_inside_tree())
  506. return;
  507. call_deferred("_update_dirty_quadrants");
  508. }
  509. void TileMap::set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose) {
  510. set_cell(p_pos.x, p_pos.y, p_tile, p_flip_x, p_flip_y, p_transpose);
  511. }
  512. void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose) {
  513. PosKey pk(p_x, p_y);
  514. Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  515. if (!E && p_tile == INVALID_CELL)
  516. return; //nothing to do
  517. PosKey qk(p_x / _get_quadrant_size(), p_y / _get_quadrant_size());
  518. if (p_tile == INVALID_CELL) {
  519. //erase existing
  520. tile_map.erase(pk);
  521. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  522. ERR_FAIL_COND(!Q);
  523. Quadrant &q = Q->get();
  524. q.cells.erase(pk);
  525. if (q.cells.size() == 0)
  526. _erase_quadrant(Q);
  527. else
  528. _make_quadrant_dirty(Q);
  529. return;
  530. }
  531. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  532. if (!E) {
  533. E = tile_map.insert(pk, Cell());
  534. if (!Q) {
  535. Q = _create_quadrant(qk);
  536. }
  537. Quadrant &q = Q->get();
  538. q.cells.insert(pk);
  539. } else {
  540. ERR_FAIL_COND(!Q); // quadrant should exist...
  541. 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)
  542. return; //nothing changed
  543. }
  544. Cell &c = E->get();
  545. c.id = p_tile;
  546. c.flip_h = p_flip_x;
  547. c.flip_v = p_flip_y;
  548. c.transpose = p_transpose;
  549. _make_quadrant_dirty(Q);
  550. used_size_cache_dirty = true;
  551. }
  552. int TileMap::get_cellv(const Vector2 &p_pos) const {
  553. return get_cell(p_pos.x, p_pos.y);
  554. }
  555. int TileMap::get_cell(int p_x, int p_y) const {
  556. PosKey pk(p_x, p_y);
  557. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  558. if (!E)
  559. return INVALID_CELL;
  560. return E->get().id;
  561. }
  562. bool TileMap::is_cell_x_flipped(int p_x, int p_y) const {
  563. PosKey pk(p_x, p_y);
  564. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  565. if (!E)
  566. return false;
  567. return E->get().flip_h;
  568. }
  569. bool TileMap::is_cell_y_flipped(int p_x, int p_y) const {
  570. PosKey pk(p_x, p_y);
  571. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  572. if (!E)
  573. return false;
  574. return E->get().flip_v;
  575. }
  576. bool TileMap::is_cell_transposed(int p_x, int p_y) const {
  577. PosKey pk(p_x, p_y);
  578. const Map<PosKey, Cell>::Element *E = tile_map.find(pk);
  579. if (!E)
  580. return false;
  581. return E->get().transpose;
  582. }
  583. void TileMap::_recreate_quadrants() {
  584. _clear_quadrants();
  585. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  586. PosKey qk(E->key().x / _get_quadrant_size(), E->key().y / _get_quadrant_size());
  587. Map<PosKey, Quadrant>::Element *Q = quadrant_map.find(qk);
  588. if (!Q) {
  589. Q = _create_quadrant(qk);
  590. dirty_quadrant_list.add(&Q->get().dirty_list);
  591. }
  592. Q->get().cells.insert(E->key());
  593. _make_quadrant_dirty(Q);
  594. }
  595. }
  596. void TileMap::_clear_quadrants() {
  597. while (quadrant_map.size()) {
  598. _erase_quadrant(quadrant_map.front());
  599. }
  600. }
  601. void TileMap::clear() {
  602. _clear_quadrants();
  603. tile_map.clear();
  604. used_size_cache_dirty = true;
  605. }
  606. void TileMap::_set_tile_data(const PoolVector<int> &p_data) {
  607. int c = p_data.size();
  608. PoolVector<int>::Read r = p_data.read();
  609. for (int i = 0; i < c; i += 2) {
  610. const uint8_t *ptr = (const uint8_t *)&r[i];
  611. uint8_t local[8];
  612. for (int j = 0; j < 8; j++)
  613. local[j] = ptr[j];
  614. #ifdef BIG_ENDIAN_ENABLED
  615. SWAP(local[0], local[3]);
  616. SWAP(local[1], local[2]);
  617. SWAP(local[4], local[7]);
  618. SWAP(local[5], local[6]);
  619. #endif
  620. int16_t x = decode_uint16(&local[0]);
  621. int16_t y = decode_uint16(&local[2]);
  622. uint32_t v = decode_uint32(&local[4]);
  623. bool flip_h = v & (1 << 29);
  624. bool flip_v = v & (1 << 30);
  625. bool transpose = v & (1 << 31);
  626. v &= (1 << 29) - 1;
  627. /*
  628. if (x<-20 || y <-20 || x>4000 || y>4000)
  629. continue;
  630. */
  631. set_cell(x, y, v, flip_h, flip_v, transpose);
  632. }
  633. }
  634. PoolVector<int> TileMap::_get_tile_data() const {
  635. PoolVector<int> data;
  636. data.resize(tile_map.size() * 2);
  637. PoolVector<int>::Write w = data.write();
  638. int idx = 0;
  639. for (const Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  640. uint8_t *ptr = (uint8_t *)&w[idx];
  641. encode_uint16(E->key().x, &ptr[0]);
  642. encode_uint16(E->key().y, &ptr[2]);
  643. uint32_t val = E->get().id;
  644. if (E->get().flip_h)
  645. val |= (1 << 29);
  646. if (E->get().flip_v)
  647. val |= (1 << 30);
  648. if (E->get().transpose)
  649. val |= (1 << 31);
  650. encode_uint32(val, &ptr[4]);
  651. idx += 2;
  652. }
  653. w = PoolVector<int>::Write();
  654. return data;
  655. }
  656. Rect2 TileMap::get_item_rect() const {
  657. const_cast<TileMap *>(this)->_update_dirty_quadrants();
  658. return rect_cache;
  659. }
  660. void TileMap::set_collision_layer(uint32_t p_layer) {
  661. collision_layer = p_layer;
  662. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  663. Quadrant &q = E->get();
  664. Physics2DServer::get_singleton()->body_set_collision_layer(q.body, collision_layer);
  665. }
  666. }
  667. void TileMap::set_collision_mask(uint32_t p_mask) {
  668. collision_mask = p_mask;
  669. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  670. Quadrant &q = E->get();
  671. Physics2DServer::get_singleton()->body_set_collision_mask(q.body, collision_mask);
  672. }
  673. }
  674. void TileMap::set_collision_layer_bit(int p_bit, bool p_value) {
  675. uint32_t layer = get_collision_layer();
  676. if (p_value)
  677. layer |= 1 << p_bit;
  678. else
  679. layer &= ~(1 << p_bit);
  680. set_collision_layer(layer);
  681. }
  682. void TileMap::set_collision_mask_bit(int p_bit, bool p_value) {
  683. uint32_t mask = get_collision_mask();
  684. if (p_value)
  685. mask |= 1 << p_bit;
  686. else
  687. mask &= ~(1 << p_bit);
  688. set_collision_mask(mask);
  689. }
  690. bool TileMap::get_collision_use_kinematic() const {
  691. return use_kinematic;
  692. }
  693. void TileMap::set_collision_use_kinematic(bool p_use_kinematic) {
  694. _clear_quadrants();
  695. use_kinematic = p_use_kinematic;
  696. _recreate_quadrants();
  697. }
  698. void TileMap::set_collision_friction(float p_friction) {
  699. friction = p_friction;
  700. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  701. Quadrant &q = E->get();
  702. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_FRICTION, p_friction);
  703. }
  704. }
  705. float TileMap::get_collision_friction() const {
  706. return friction;
  707. }
  708. void TileMap::set_collision_bounce(float p_bounce) {
  709. bounce = p_bounce;
  710. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  711. Quadrant &q = E->get();
  712. Physics2DServer::get_singleton()->body_set_param(q.body, Physics2DServer::BODY_PARAM_BOUNCE, p_bounce);
  713. }
  714. }
  715. float TileMap::get_collision_bounce() const {
  716. return bounce;
  717. }
  718. uint32_t TileMap::get_collision_layer() const {
  719. return collision_layer;
  720. }
  721. uint32_t TileMap::get_collision_mask() const {
  722. return collision_mask;
  723. }
  724. bool TileMap::get_collision_layer_bit(int p_bit) const {
  725. return get_collision_layer() & (1 << p_bit);
  726. }
  727. bool TileMap::get_collision_mask_bit(int p_bit) const {
  728. return get_collision_mask() & (1 << p_bit);
  729. }
  730. void TileMap::set_mode(Mode p_mode) {
  731. _clear_quadrants();
  732. mode = p_mode;
  733. _recreate_quadrants();
  734. emit_signal("settings_changed");
  735. }
  736. TileMap::Mode TileMap::get_mode() const {
  737. return mode;
  738. }
  739. void TileMap::set_half_offset(HalfOffset p_half_offset) {
  740. _clear_quadrants();
  741. half_offset = p_half_offset;
  742. _recreate_quadrants();
  743. emit_signal("settings_changed");
  744. }
  745. void TileMap::set_tile_origin(TileOrigin p_tile_origin) {
  746. _clear_quadrants();
  747. tile_origin = p_tile_origin;
  748. _recreate_quadrants();
  749. emit_signal("settings_changed");
  750. }
  751. TileMap::TileOrigin TileMap::get_tile_origin() const {
  752. return tile_origin;
  753. }
  754. Vector2 TileMap::get_cell_draw_offset() const {
  755. switch (mode) {
  756. case MODE_SQUARE: {
  757. return Vector2();
  758. } break;
  759. case MODE_ISOMETRIC: {
  760. return Vector2(-cell_size.x * 0.5, 0);
  761. } break;
  762. case MODE_CUSTOM: {
  763. Vector2 min;
  764. min.x = MIN(custom_transform[0].x, min.x);
  765. min.y = MIN(custom_transform[0].y, min.y);
  766. min.x = MIN(custom_transform[1].x, min.x);
  767. min.y = MIN(custom_transform[1].y, min.y);
  768. return min;
  769. } break;
  770. }
  771. return Vector2();
  772. }
  773. TileMap::HalfOffset TileMap::get_half_offset() const {
  774. return half_offset;
  775. }
  776. Transform2D TileMap::get_cell_transform() const {
  777. switch (mode) {
  778. case MODE_SQUARE: {
  779. Transform2D m;
  780. m[0] *= cell_size.x;
  781. m[1] *= cell_size.y;
  782. return m;
  783. } break;
  784. case MODE_ISOMETRIC: {
  785. //isometric only makes sense when y is positive in both x and y vectors, otherwise
  786. //the drawing of tiles will overlap
  787. Transform2D m;
  788. m[0] = Vector2(cell_size.x * 0.5, cell_size.y * 0.5);
  789. m[1] = Vector2(-cell_size.x * 0.5, cell_size.y * 0.5);
  790. return m;
  791. } break;
  792. case MODE_CUSTOM: {
  793. return custom_transform;
  794. } break;
  795. }
  796. return Transform2D();
  797. }
  798. void TileMap::set_custom_transform(const Transform2D &p_xform) {
  799. _clear_quadrants();
  800. custom_transform = p_xform;
  801. _recreate_quadrants();
  802. emit_signal("settings_changed");
  803. }
  804. Transform2D TileMap::get_custom_transform() const {
  805. return custom_transform;
  806. }
  807. Vector2 TileMap::_map_to_world(int x, int y, bool p_ignore_ofs) const {
  808. Vector2 ret = get_cell_transform().xform(Vector2(x, y));
  809. if (!p_ignore_ofs) {
  810. switch (half_offset) {
  811. case HALF_OFFSET_X: {
  812. if (ABS(y) & 1) {
  813. ret += get_cell_transform()[0] * 0.5;
  814. }
  815. } break;
  816. case HALF_OFFSET_Y: {
  817. if (ABS(x) & 1) {
  818. ret += get_cell_transform()[1] * 0.5;
  819. }
  820. } break;
  821. default: {}
  822. }
  823. }
  824. return ret;
  825. }
  826. Vector2 TileMap::map_to_world(const Vector2 &p_pos, bool p_ignore_ofs) const {
  827. return _map_to_world(p_pos.x, p_pos.y, p_ignore_ofs);
  828. }
  829. Vector2 TileMap::world_to_map(const Vector2 &p_pos) const {
  830. Vector2 ret = get_cell_transform().affine_inverse().xform(p_pos);
  831. switch (half_offset) {
  832. case HALF_OFFSET_X: {
  833. if (ret.y > 0 ? int(ret.y) & 1 : (int(ret.y) - 1) & 1) {
  834. ret.x -= 0.5;
  835. }
  836. } break;
  837. case HALF_OFFSET_Y: {
  838. if (ret.x > 0 ? int(ret.x) & 1 : (int(ret.x) - 1) & 1) {
  839. ret.y -= 0.5;
  840. }
  841. } break;
  842. default: {}
  843. }
  844. return ret.floor();
  845. }
  846. void TileMap::set_y_sort_mode(bool p_enable) {
  847. _clear_quadrants();
  848. y_sort_mode = p_enable;
  849. VS::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), y_sort_mode);
  850. _recreate_quadrants();
  851. emit_signal("settings_changed");
  852. }
  853. bool TileMap::is_y_sort_mode_enabled() const {
  854. return y_sort_mode;
  855. }
  856. Array TileMap::get_used_cells() const {
  857. Array a;
  858. a.resize(tile_map.size());
  859. int i = 0;
  860. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  861. Vector2 p(E->key().x, E->key().y);
  862. a[i++] = p;
  863. }
  864. return a;
  865. }
  866. Rect2 TileMap::get_used_rect() { // Not const because of cache
  867. if (used_size_cache_dirty) {
  868. if (tile_map.size() > 0) {
  869. used_size_cache = Rect2(tile_map.front()->key().x, tile_map.front()->key().y, 0, 0);
  870. for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
  871. used_size_cache.expand_to(Vector2(E->key().x, E->key().y));
  872. }
  873. used_size_cache.size += Vector2(1, 1);
  874. } else {
  875. used_size_cache = Rect2();
  876. }
  877. used_size_cache_dirty = false;
  878. }
  879. return used_size_cache;
  880. }
  881. void TileMap::set_occluder_light_mask(int p_mask) {
  882. occluder_light_mask = p_mask;
  883. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  884. for (Map<PosKey, Quadrant::Occluder>::Element *F = E->get().occluder_instances.front(); F; F = F->next()) {
  885. VisualServer::get_singleton()->canvas_light_occluder_set_light_mask(F->get().id, occluder_light_mask);
  886. }
  887. }
  888. }
  889. int TileMap::get_occluder_light_mask() const {
  890. return occluder_light_mask;
  891. }
  892. void TileMap::set_light_mask(int p_light_mask) {
  893. CanvasItem::set_light_mask(p_light_mask);
  894. for (Map<PosKey, Quadrant>::Element *E = quadrant_map.front(); E; E = E->next()) {
  895. for (List<RID>::Element *F = E->get().canvas_items.front(); F; F = F->next()) {
  896. VisualServer::get_singleton()->canvas_item_set_light_mask(F->get(), get_light_mask());
  897. }
  898. }
  899. }
  900. void TileMap::_bind_methods() {
  901. ClassDB::bind_method(D_METHOD("set_tileset", "tileset:TileSet"), &TileMap::set_tileset);
  902. ClassDB::bind_method(D_METHOD("get_tileset:TileSet"), &TileMap::get_tileset);
  903. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &TileMap::set_mode);
  904. ClassDB::bind_method(D_METHOD("get_mode"), &TileMap::get_mode);
  905. ClassDB::bind_method(D_METHOD("set_half_offset", "half_offset"), &TileMap::set_half_offset);
  906. ClassDB::bind_method(D_METHOD("get_half_offset"), &TileMap::get_half_offset);
  907. ClassDB::bind_method(D_METHOD("set_custom_transform", "custom_transform"), &TileMap::set_custom_transform);
  908. ClassDB::bind_method(D_METHOD("get_custom_transform"), &TileMap::get_custom_transform);
  909. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &TileMap::set_cell_size);
  910. ClassDB::bind_method(D_METHOD("get_cell_size"), &TileMap::get_cell_size);
  911. ClassDB::bind_method(D_METHOD("_set_old_cell_size", "size"), &TileMap::_set_old_cell_size);
  912. ClassDB::bind_method(D_METHOD("_get_old_cell_size"), &TileMap::_get_old_cell_size);
  913. ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
  914. ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
  915. ClassDB::bind_method(D_METHOD("set_tile_origin", "origin"), &TileMap::set_tile_origin);
  916. ClassDB::bind_method(D_METHOD("get_tile_origin"), &TileMap::get_tile_origin);
  917. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &TileMap::set_center_x);
  918. ClassDB::bind_method(D_METHOD("get_center_x"), &TileMap::get_center_x);
  919. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &TileMap::set_center_y);
  920. ClassDB::bind_method(D_METHOD("get_center_y"), &TileMap::get_center_y);
  921. ClassDB::bind_method(D_METHOD("set_y_sort_mode", "enable"), &TileMap::set_y_sort_mode);
  922. ClassDB::bind_method(D_METHOD("is_y_sort_mode_enabled"), &TileMap::is_y_sort_mode_enabled);
  923. ClassDB::bind_method(D_METHOD("set_collision_use_kinematic", "use_kinematic"), &TileMap::set_collision_use_kinematic);
  924. ClassDB::bind_method(D_METHOD("get_collision_use_kinematic"), &TileMap::get_collision_use_kinematic);
  925. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &TileMap::set_collision_layer);
  926. ClassDB::bind_method(D_METHOD("get_collision_layer"), &TileMap::get_collision_layer);
  927. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &TileMap::set_collision_mask);
  928. ClassDB::bind_method(D_METHOD("get_collision_mask"), &TileMap::get_collision_mask);
  929. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &TileMap::set_collision_layer_bit);
  930. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &TileMap::get_collision_layer_bit);
  931. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &TileMap::set_collision_mask_bit);
  932. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &TileMap::get_collision_mask_bit);
  933. ClassDB::bind_method(D_METHOD("set_collision_friction", "value"), &TileMap::set_collision_friction);
  934. ClassDB::bind_method(D_METHOD("get_collision_friction"), &TileMap::get_collision_friction);
  935. ClassDB::bind_method(D_METHOD("set_collision_bounce", "value"), &TileMap::set_collision_bounce);
  936. ClassDB::bind_method(D_METHOD("get_collision_bounce"), &TileMap::get_collision_bounce);
  937. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &TileMap::set_occluder_light_mask);
  938. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &TileMap::get_occluder_light_mask);
  939. ClassDB::bind_method(D_METHOD("set_cell", "x", "y", "tile", "flip_x", "flip_y", "transpose"), &TileMap::set_cell, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  940. ClassDB::bind_method(D_METHOD("set_cellv", "pos", "tile", "flip_x", "flip_y", "transpose"), &TileMap::set_cellv, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  941. ClassDB::bind_method(D_METHOD("get_cell", "x", "y"), &TileMap::get_cell);
  942. ClassDB::bind_method(D_METHOD("get_cellv", "pos"), &TileMap::get_cellv);
  943. ClassDB::bind_method(D_METHOD("is_cell_x_flipped", "x", "y"), &TileMap::is_cell_x_flipped);
  944. ClassDB::bind_method(D_METHOD("is_cell_y_flipped", "x", "y"), &TileMap::is_cell_y_flipped);
  945. ClassDB::bind_method(D_METHOD("is_cell_transposed", "x", "y"), &TileMap::is_cell_transposed);
  946. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  947. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells);
  948. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  949. ClassDB::bind_method(D_METHOD("map_to_world", "mappos", "ignore_half_ofs"), &TileMap::map_to_world, DEFVAL(false));
  950. ClassDB::bind_method(D_METHOD("world_to_map", "worldpos"), &TileMap::world_to_map);
  951. ClassDB::bind_method(D_METHOD("_clear_quadrants"), &TileMap::_clear_quadrants);
  952. ClassDB::bind_method(D_METHOD("_recreate_quadrants"), &TileMap::_recreate_quadrants);
  953. ClassDB::bind_method(D_METHOD("_update_dirty_quadrants"), &TileMap::_update_dirty_quadrants);
  954. ClassDB::bind_method(D_METHOD("_set_tile_data"), &TileMap::_set_tile_data);
  955. ClassDB::bind_method(D_METHOD("_get_tile_data"), &TileMap::_get_tile_data);
  956. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Square,Isometric,Custom"), "set_mode", "get_mode");
  957. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  958. ADD_GROUP("Cell", "cell_");
  959. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size", PROPERTY_HINT_RANGE, "1,8192,1"), "set_cell_size", "get_cell_size");
  960. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
  961. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "cell_custom_transform"), "set_custom_transform", "get_custom_transform");
  962. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_half_offset", PROPERTY_HINT_ENUM, "Offset X,Offset Y,Disabled"), "set_half_offset", "get_half_offset");
  963. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_tile_origin", PROPERTY_HINT_ENUM, "Top Left,Center,Bottom Left"), "set_tile_origin", "get_tile_origin");
  964. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_y_sort"), "set_y_sort_mode", "is_y_sort_mode_enabled");
  965. ADD_GROUP("Collision", "collision_");
  966. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_use_kinematic", PROPERTY_HINT_NONE, ""), "set_collision_use_kinematic", "get_collision_use_kinematic");
  967. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_friction", "get_collision_friction");
  968. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision_bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_bounce", "get_collision_bounce");
  969. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  970. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  971. ADD_GROUP("Occluder", "occluder_");
  972. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  973. ADD_GROUP("", "");
  974. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_tile_data", "_get_tile_data");
  975. ADD_SIGNAL(MethodInfo("settings_changed"));
  976. BIND_CONSTANT(INVALID_CELL);
  977. BIND_CONSTANT(MODE_SQUARE);
  978. BIND_CONSTANT(MODE_ISOMETRIC);
  979. BIND_CONSTANT(MODE_CUSTOM);
  980. BIND_CONSTANT(HALF_OFFSET_X);
  981. BIND_CONSTANT(HALF_OFFSET_Y);
  982. BIND_CONSTANT(HALF_OFFSET_DISABLED);
  983. BIND_CONSTANT(TILE_ORIGIN_TOP_LEFT);
  984. BIND_CONSTANT(TILE_ORIGIN_CENTER);
  985. BIND_CONSTANT(TILE_ORIGIN_BOTTOM_LEFT);
  986. }
  987. TileMap::TileMap() {
  988. rect_cache_dirty = true;
  989. used_size_cache_dirty = true;
  990. pending_update = false;
  991. quadrant_order_dirty = false;
  992. quadrant_size = 16;
  993. cell_size = Size2(64, 64);
  994. center_x = false;
  995. center_y = false;
  996. collision_layer = 1;
  997. collision_mask = 1;
  998. friction = 1;
  999. bounce = 0;
  1000. mode = MODE_SQUARE;
  1001. half_offset = HALF_OFFSET_DISABLED;
  1002. use_kinematic = false;
  1003. navigation = NULL;
  1004. y_sort_mode = false;
  1005. occluder_light_mask = 1;
  1006. fp_adjust = 0.00001;
  1007. tile_origin = TILE_ORIGIN_TOP_LEFT;
  1008. set_notify_transform(true);
  1009. }
  1010. TileMap::~TileMap() {
  1011. clear();
  1012. }