tile_map.cpp 51 KB

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