2
0

tile_set.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*************************************************************************/
  2. /* tile_set.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_set.h"
  31. #include "core/array.h"
  32. #include "core/engine.h"
  33. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  34. String n = p_name;
  35. int slash = n.find("/");
  36. if (slash == -1)
  37. return false;
  38. int id = String::to_int(n.c_str(), slash);
  39. if (!tile_map.has(id))
  40. create_tile(id);
  41. String what = n.substr(slash + 1, n.length());
  42. if (what == "name")
  43. tile_set_name(id, p_value);
  44. else if (what == "texture")
  45. tile_set_texture(id, p_value);
  46. else if (what == "normal_map")
  47. tile_set_normal_map(id, p_value);
  48. else if (what == "tex_offset")
  49. tile_set_texture_offset(id, p_value);
  50. else if (what == "material")
  51. tile_set_material(id, p_value);
  52. else if (what == "modulate")
  53. tile_set_modulate(id, p_value);
  54. else if (what == "region")
  55. tile_set_region(id, p_value);
  56. else if (what == "tile_mode")
  57. tile_set_tile_mode(id, (TileMode)((int)p_value));
  58. else if (what == "is_autotile") {
  59. // backward compatibility for Godot 3.0.x
  60. // autotile used to be a bool, it's now an enum
  61. bool is_autotile = p_value;
  62. if (is_autotile)
  63. tile_set_tile_mode(id, AUTO_TILE);
  64. } else if (what.left(9) == "autotile/") {
  65. what = what.right(9);
  66. if (what == "bitmask_mode")
  67. autotile_set_bitmask_mode(id, (BitmaskMode)((int)p_value));
  68. else if (what == "icon_coordinate")
  69. autotile_set_icon_coordinate(id, p_value);
  70. else if (what == "tile_size")
  71. autotile_set_size(id, p_value);
  72. else if (what == "spacing")
  73. autotile_set_spacing(id, p_value);
  74. else if (what == "bitmask_flags") {
  75. tile_map[id].autotile_data.flags.clear();
  76. if (p_value.is_array()) {
  77. Array p = p_value;
  78. Vector2 last_coord;
  79. while (p.size() > 0) {
  80. if (p[0].get_type() == Variant::VECTOR2) {
  81. last_coord = p[0];
  82. } else if (p[0].get_type() == Variant::INT) {
  83. autotile_set_bitmask(id, last_coord, p[0]);
  84. }
  85. p.pop_front();
  86. }
  87. }
  88. } else if (what == "occluder_map") {
  89. tile_map[id].autotile_data.occluder_map.clear();
  90. Array p = p_value;
  91. Vector2 last_coord;
  92. while (p.size() > 0) {
  93. if (p[0].get_type() == Variant::VECTOR2) {
  94. last_coord = p[0];
  95. } else if (p[0].get_type() == Variant::OBJECT) {
  96. autotile_set_light_occluder(id, p[0], last_coord);
  97. }
  98. p.pop_front();
  99. }
  100. } else if (what == "navpoly_map") {
  101. tile_map[id].autotile_data.navpoly_map.clear();
  102. Array p = p_value;
  103. Vector2 last_coord;
  104. while (p.size() > 0) {
  105. if (p[0].get_type() == Variant::VECTOR2) {
  106. last_coord = p[0];
  107. } else if (p[0].get_type() == Variant::OBJECT) {
  108. autotile_set_navigation_polygon(id, p[0], last_coord);
  109. }
  110. p.pop_front();
  111. }
  112. } else if (what == "priority_map") {
  113. tile_map[id].autotile_data.priority_map.clear();
  114. Array p = p_value;
  115. Vector3 val;
  116. Vector2 v;
  117. int priority;
  118. while (p.size() > 0) {
  119. val = p[0];
  120. if (val.z > 1) {
  121. v.x = val.x;
  122. v.y = val.y;
  123. priority = (int)val.z;
  124. tile_map[id].autotile_data.priority_map[v] = priority;
  125. }
  126. p.pop_front();
  127. }
  128. } else if (what == "z_index_map") {
  129. tile_map[id].autotile_data.z_index_map.clear();
  130. Array p = p_value;
  131. Vector3 val;
  132. Vector2 v;
  133. int z_index;
  134. while (p.size() > 0) {
  135. val = p[0];
  136. if (val.z != 0) {
  137. v.x = val.x;
  138. v.y = val.y;
  139. z_index = (int)val.z;
  140. tile_map[id].autotile_data.z_index_map[v] = z_index;
  141. }
  142. p.pop_front();
  143. }
  144. }
  145. } else if (what == "shape")
  146. if (tile_get_shape_count(id) > 0) {
  147. for (int i = 0; i < tile_get_shape_count(id); i++) {
  148. tile_set_shape(id, i, p_value);
  149. }
  150. } else {
  151. tile_set_shape(id, 0, p_value);
  152. }
  153. else if (what == "shape_offset")
  154. if (tile_get_shape_count(id) > 0) {
  155. for (int i = 0; i < tile_get_shape_count(id); i++) {
  156. tile_set_shape_offset(id, i, p_value);
  157. }
  158. } else {
  159. tile_set_shape_offset(id, 0, p_value);
  160. }
  161. else if (what == "shape_transform")
  162. if (tile_get_shape_count(id) > 0) {
  163. for (int i = 0; i < tile_get_shape_count(id); i++) {
  164. tile_set_shape_transform(id, i, p_value);
  165. }
  166. } else {
  167. tile_set_shape_transform(id, 0, p_value);
  168. }
  169. else if (what == "shape_one_way")
  170. if (tile_get_shape_count(id) > 0) {
  171. for (int i = 0; i < tile_get_shape_count(id); i++) {
  172. tile_set_shape_one_way(id, i, p_value);
  173. }
  174. } else {
  175. tile_set_shape_one_way(id, 0, p_value);
  176. }
  177. else if (what == "shape_one_way_margin")
  178. if (tile_get_shape_count(id) > 0) {
  179. for (int i = 0; i < tile_get_shape_count(id); i++) {
  180. tile_set_shape_one_way_margin(id, i, p_value);
  181. }
  182. } else {
  183. tile_set_shape_one_way_margin(id, 0, p_value);
  184. }
  185. else if (what == "shapes")
  186. _tile_set_shapes(id, p_value);
  187. else if (what == "occluder")
  188. tile_set_light_occluder(id, p_value);
  189. else if (what == "occluder_offset")
  190. tile_set_occluder_offset(id, p_value);
  191. else if (what == "navigation")
  192. tile_set_navigation_polygon(id, p_value);
  193. else if (what == "navigation_offset")
  194. tile_set_navigation_polygon_offset(id, p_value);
  195. else if (what == "z_index")
  196. tile_set_z_index(id, p_value);
  197. else
  198. return false;
  199. return true;
  200. }
  201. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  202. String n = p_name;
  203. int slash = n.find("/");
  204. if (slash == -1)
  205. return false;
  206. int id = String::to_int(n.c_str(), slash);
  207. ERR_FAIL_COND_V(!tile_map.has(id), false);
  208. String what = n.substr(slash + 1, n.length());
  209. if (what == "name")
  210. r_ret = tile_get_name(id);
  211. else if (what == "texture")
  212. r_ret = tile_get_texture(id);
  213. else if (what == "normal_map")
  214. r_ret = tile_get_normal_map(id);
  215. else if (what == "tex_offset")
  216. r_ret = tile_get_texture_offset(id);
  217. else if (what == "material")
  218. r_ret = tile_get_material(id);
  219. else if (what == "modulate")
  220. r_ret = tile_get_modulate(id);
  221. else if (what == "region")
  222. r_ret = tile_get_region(id);
  223. else if (what == "tile_mode")
  224. r_ret = tile_get_tile_mode(id);
  225. else if (what.left(9) == "autotile/") {
  226. what = what.right(9);
  227. if (what == "bitmask_mode")
  228. r_ret = autotile_get_bitmask_mode(id);
  229. else if (what == "icon_coordinate")
  230. r_ret = autotile_get_icon_coordinate(id);
  231. else if (what == "tile_size")
  232. r_ret = autotile_get_size(id);
  233. else if (what == "spacing")
  234. r_ret = autotile_get_spacing(id);
  235. else if (what == "bitmask_flags") {
  236. Array p;
  237. for (Map<Vector2, uint32_t>::Element *E = tile_map[id].autotile_data.flags.front(); E; E = E->next()) {
  238. p.push_back(E->key());
  239. p.push_back(E->value());
  240. }
  241. r_ret = p;
  242. } else if (what == "occluder_map") {
  243. Array p;
  244. for (Map<Vector2, Ref<OccluderPolygon2D> >::Element *E = tile_map[id].autotile_data.occluder_map.front(); E; E = E->next()) {
  245. p.push_back(E->key());
  246. p.push_back(E->value());
  247. }
  248. r_ret = p;
  249. } else if (what == "navpoly_map") {
  250. Array p;
  251. for (Map<Vector2, Ref<NavigationPolygon> >::Element *E = tile_map[id].autotile_data.navpoly_map.front(); E; E = E->next()) {
  252. p.push_back(E->key());
  253. p.push_back(E->value());
  254. }
  255. r_ret = p;
  256. } else if (what == "priority_map") {
  257. Array p;
  258. Vector3 v;
  259. for (Map<Vector2, int>::Element *E = tile_map[id].autotile_data.priority_map.front(); E; E = E->next()) {
  260. if (E->value() > 1) {
  261. //Don't save default value
  262. v.x = E->key().x;
  263. v.y = E->key().y;
  264. v.z = E->value();
  265. p.push_back(v);
  266. }
  267. }
  268. r_ret = p;
  269. } else if (what == "z_index_map") {
  270. Array p;
  271. Vector3 v;
  272. for (Map<Vector2, int>::Element *E = tile_map[id].autotile_data.z_index_map.front(); E; E = E->next()) {
  273. if (E->value() != 0) {
  274. //Don't save default value
  275. v.x = E->key().x;
  276. v.y = E->key().y;
  277. v.z = E->value();
  278. p.push_back(v);
  279. }
  280. }
  281. r_ret = p;
  282. }
  283. } else if (what == "shape")
  284. r_ret = tile_get_shape(id, 0);
  285. else if (what == "shape_offset")
  286. r_ret = tile_get_shape_offset(id, 0);
  287. else if (what == "shape_transform")
  288. r_ret = tile_get_shape_transform(id, 0);
  289. else if (what == "shape_one_way")
  290. r_ret = tile_get_shape_one_way(id, 0);
  291. else if (what == "shape_one_way_margin")
  292. r_ret = tile_get_shape_one_way_margin(id, 0);
  293. else if (what == "shapes")
  294. r_ret = _tile_get_shapes(id);
  295. else if (what == "occluder")
  296. r_ret = tile_get_light_occluder(id);
  297. else if (what == "occluder_offset")
  298. r_ret = tile_get_occluder_offset(id);
  299. else if (what == "navigation")
  300. r_ret = tile_get_navigation_polygon(id);
  301. else if (what == "navigation_offset")
  302. r_ret = tile_get_navigation_polygon_offset(id);
  303. else if (what == "z_index")
  304. r_ret = tile_get_z_index(id);
  305. else
  306. return false;
  307. return true;
  308. }
  309. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  310. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  311. int id = E->key();
  312. String pre = itos(id) + "/";
  313. p_list->push_back(PropertyInfo(Variant::STRING, pre + "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  314. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_NOEDITOR));
  315. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_NOEDITOR));
  316. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "tex_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  317. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial", PROPERTY_USAGE_NOEDITOR));
  318. p_list->push_back(PropertyInfo(Variant::COLOR, pre + "modulate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  319. p_list->push_back(PropertyInfo(Variant::RECT2, pre + "region", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  320. p_list->push_back(PropertyInfo(Variant::INT, pre + "tile_mode", PROPERTY_HINT_ENUM, "SINGLE_TILE,AUTO_TILE,ATLAS_TILE", PROPERTY_USAGE_NOEDITOR));
  321. if (tile_get_tile_mode(id) == AUTO_TILE) {
  322. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/bitmask_mode", PROPERTY_HINT_ENUM, "2X2,3X3 (minimal),3X3", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  323. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/bitmask_flags", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  324. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/icon_coordinate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  325. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/tile_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  326. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/spacing", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  327. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/occluder_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  328. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/navpoly_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  329. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/priority_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  330. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/z_index_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  331. } else if (tile_get_tile_mode(id) == ATLAS_TILE) {
  332. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/icon_coordinate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  333. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/tile_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  334. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/spacing", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  335. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/occluder_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  336. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/navpoly_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  337. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/priority_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  338. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/z_index_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  339. }
  340. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "occluder_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  341. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D", PROPERTY_USAGE_NOEDITOR));
  342. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "navigation_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  343. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "navigation", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon", PROPERTY_USAGE_NOEDITOR));
  344. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  345. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  346. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D", PROPERTY_USAGE_NOEDITOR));
  347. p_list->push_back(PropertyInfo(Variant::BOOL, pre + "shape_one_way", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  348. p_list->push_back(PropertyInfo(Variant::REAL, pre + "shape_one_way_margin", PROPERTY_HINT_RANGE, "0,128,0.01", PROPERTY_USAGE_NOEDITOR));
  349. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "shapes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  350. p_list->push_back(PropertyInfo(Variant::INT, pre + "z_index", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1", PROPERTY_USAGE_NOEDITOR));
  351. }
  352. }
  353. void TileSet::create_tile(int p_id) {
  354. ERR_FAIL_COND(tile_map.has(p_id));
  355. tile_map[p_id] = TileData();
  356. tile_map[p_id].autotile_data = AutotileData();
  357. _change_notify("");
  358. emit_changed();
  359. }
  360. void TileSet::autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode) {
  361. ERR_FAIL_COND(!tile_map.has(p_id));
  362. tile_map[p_id].autotile_data.bitmask_mode = p_mode;
  363. _change_notify("");
  364. emit_changed();
  365. }
  366. TileSet::BitmaskMode TileSet::autotile_get_bitmask_mode(int p_id) const {
  367. ERR_FAIL_COND_V(!tile_map.has(p_id), BITMASK_2X2);
  368. return tile_map[p_id].autotile_data.bitmask_mode;
  369. }
  370. void TileSet::tile_set_texture(int p_id, const Ref<Texture> &p_texture) {
  371. ERR_FAIL_COND(!tile_map.has(p_id));
  372. tile_map[p_id].texture = p_texture;
  373. emit_changed();
  374. _change_notify("texture");
  375. }
  376. Ref<Texture> TileSet::tile_get_texture(int p_id) const {
  377. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
  378. return tile_map[p_id].texture;
  379. }
  380. void TileSet::tile_set_normal_map(int p_id, const Ref<Texture> &p_normal_map) {
  381. ERR_FAIL_COND(!tile_map.has(p_id));
  382. tile_map[p_id].normal_map = p_normal_map;
  383. emit_changed();
  384. }
  385. Ref<Texture> TileSet::tile_get_normal_map(int p_id) const {
  386. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
  387. return tile_map[p_id].normal_map;
  388. }
  389. void TileSet::tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material) {
  390. ERR_FAIL_COND(!tile_map.has(p_id));
  391. tile_map[p_id].material = p_material;
  392. emit_changed();
  393. }
  394. Ref<ShaderMaterial> TileSet::tile_get_material(int p_id) const {
  395. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<ShaderMaterial>());
  396. return tile_map[p_id].material;
  397. }
  398. void TileSet::tile_set_modulate(int p_id, const Color &p_modulate) {
  399. ERR_FAIL_COND(!tile_map.has(p_id));
  400. tile_map[p_id].modulate = p_modulate;
  401. emit_changed();
  402. _change_notify("modulate");
  403. }
  404. Color TileSet::tile_get_modulate(int p_id) const {
  405. ERR_FAIL_COND_V(!tile_map.has(p_id), Color(1, 1, 1));
  406. return tile_map[p_id].modulate;
  407. }
  408. void TileSet::tile_set_texture_offset(int p_id, const Vector2 &p_offset) {
  409. ERR_FAIL_COND(!tile_map.has(p_id));
  410. tile_map[p_id].offset = p_offset;
  411. emit_changed();
  412. }
  413. Vector2 TileSet::tile_get_texture_offset(int p_id) const {
  414. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  415. return tile_map[p_id].offset;
  416. }
  417. void TileSet::tile_set_region(int p_id, const Rect2 &p_region) {
  418. ERR_FAIL_COND(!tile_map.has(p_id));
  419. tile_map[p_id].region = p_region;
  420. emit_changed();
  421. _change_notify("region");
  422. }
  423. Rect2 TileSet::tile_get_region(int p_id) const {
  424. ERR_FAIL_COND_V(!tile_map.has(p_id), Rect2());
  425. return tile_map[p_id].region;
  426. }
  427. void TileSet::tile_set_tile_mode(int p_id, TileMode p_tile_mode) {
  428. ERR_FAIL_COND(!tile_map.has(p_id));
  429. tile_map[p_id].tile_mode = p_tile_mode;
  430. emit_changed();
  431. _change_notify("tile_mode");
  432. }
  433. TileSet::TileMode TileSet::tile_get_tile_mode(int p_id) const {
  434. ERR_FAIL_COND_V(!tile_map.has(p_id), SINGLE_TILE);
  435. return tile_map[p_id].tile_mode;
  436. }
  437. void TileSet::autotile_set_icon_coordinate(int p_id, Vector2 coord) {
  438. ERR_FAIL_COND(!tile_map.has(p_id));
  439. tile_map[p_id].autotile_data.icon_coord = coord;
  440. emit_changed();
  441. }
  442. Vector2 TileSet::autotile_get_icon_coordinate(int p_id) const {
  443. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  444. return tile_map[p_id].autotile_data.icon_coord;
  445. }
  446. void TileSet::autotile_set_spacing(int p_id, int p_spacing) {
  447. ERR_FAIL_COND(!tile_map.has(p_id));
  448. ERR_FAIL_COND(p_spacing < 0);
  449. tile_map[p_id].autotile_data.spacing = p_spacing;
  450. emit_changed();
  451. }
  452. int TileSet::autotile_get_spacing(int p_id) const {
  453. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  454. return tile_map[p_id].autotile_data.spacing;
  455. }
  456. void TileSet::autotile_set_size(int p_id, Size2 p_size) {
  457. ERR_FAIL_COND(!tile_map.has(p_id));
  458. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  459. tile_map[p_id].autotile_data.size = p_size;
  460. }
  461. Size2 TileSet::autotile_get_size(int p_id) const {
  462. ERR_FAIL_COND_V(!tile_map.has(p_id), Size2());
  463. return tile_map[p_id].autotile_data.size;
  464. }
  465. void TileSet::autotile_clear_bitmask_map(int p_id) {
  466. ERR_FAIL_COND(!tile_map.has(p_id));
  467. tile_map[p_id].autotile_data.flags.clear();
  468. }
  469. void TileSet::autotile_set_subtile_priority(int p_id, const Vector2 &p_coord, int p_priority) {
  470. ERR_FAIL_COND(!tile_map.has(p_id));
  471. ERR_FAIL_COND(p_priority <= 0);
  472. tile_map[p_id].autotile_data.priority_map[p_coord] = p_priority;
  473. }
  474. int TileSet::autotile_get_subtile_priority(int p_id, const Vector2 &p_coord) {
  475. ERR_FAIL_COND_V(!tile_map.has(p_id), 1);
  476. if (tile_map[p_id].autotile_data.priority_map.has(p_coord)) {
  477. return tile_map[p_id].autotile_data.priority_map[p_coord];
  478. }
  479. //When not custom priority set return the default value
  480. return 1;
  481. }
  482. const Map<Vector2, int> &TileSet::autotile_get_priority_map(int p_id) const {
  483. static Map<Vector2, int> dummy;
  484. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  485. return tile_map[p_id].autotile_data.priority_map;
  486. }
  487. void TileSet::autotile_set_z_index(int p_id, const Vector2 &p_coord, int p_z_index) {
  488. ERR_FAIL_COND(!tile_map.has(p_id));
  489. tile_map[p_id].autotile_data.z_index_map[p_coord] = p_z_index;
  490. emit_changed();
  491. }
  492. int TileSet::autotile_get_z_index(int p_id, const Vector2 &p_coord) {
  493. ERR_FAIL_COND_V(!tile_map.has(p_id), 1);
  494. if (tile_map[p_id].autotile_data.z_index_map.has(p_coord)) {
  495. return tile_map[p_id].autotile_data.z_index_map[p_coord];
  496. }
  497. //When not custom z index set return the default value
  498. return 0;
  499. }
  500. const Map<Vector2, int> &TileSet::autotile_get_z_index_map(int p_id) const {
  501. static Map<Vector2, int> dummy;
  502. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  503. return tile_map[p_id].autotile_data.z_index_map;
  504. }
  505. void TileSet::autotile_set_bitmask(int p_id, Vector2 p_coord, uint32_t p_flag) {
  506. ERR_FAIL_COND(!tile_map.has(p_id));
  507. if (p_flag == 0) {
  508. if (tile_map[p_id].autotile_data.flags.has(p_coord))
  509. tile_map[p_id].autotile_data.flags.erase(p_coord);
  510. } else {
  511. tile_map[p_id].autotile_data.flags[p_coord] = p_flag;
  512. }
  513. }
  514. uint32_t TileSet::autotile_get_bitmask(int p_id, Vector2 p_coord) {
  515. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  516. if (!tile_map[p_id].autotile_data.flags.has(p_coord)) {
  517. return 0;
  518. }
  519. return tile_map[p_id].autotile_data.flags[p_coord];
  520. }
  521. const Map<Vector2, uint32_t> &TileSet::autotile_get_bitmask_map(int p_id) {
  522. static Map<Vector2, uint32_t> dummy;
  523. static Map<Vector2, uint32_t> dummy_atlas;
  524. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  525. if (tile_get_tile_mode(p_id) == ATLAS_TILE) {
  526. dummy_atlas = Map<Vector2, uint32_t>();
  527. Rect2 region = tile_get_region(p_id);
  528. Size2 size = autotile_get_size(p_id);
  529. float spacing = autotile_get_spacing(p_id);
  530. for (int x = 0; x < (region.size.x / (size.x + spacing)); x++) {
  531. for (int y = 0; y < (region.size.y / (size.y + spacing)); y++) {
  532. dummy_atlas.insert(Vector2(x, y), 0);
  533. }
  534. }
  535. return dummy_atlas;
  536. } else
  537. return tile_map[p_id].autotile_data.flags;
  538. }
  539. Vector2 TileSet::autotile_get_subtile_for_bitmask(int p_id, uint16_t p_bitmask, const Node *p_tilemap_node, const Vector2 &p_tile_location) {
  540. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  541. //First try to forward selection to script
  542. if (p_tilemap_node->get_class_name() == "TileMap") {
  543. if (get_script_instance() != NULL) {
  544. if (get_script_instance()->has_method("_forward_subtile_selection")) {
  545. Variant ret = get_script_instance()->call("_forward_subtile_selection", p_id, p_bitmask, p_tilemap_node, p_tile_location);
  546. if (ret.get_type() == Variant::VECTOR2) {
  547. return ret;
  548. }
  549. }
  550. }
  551. }
  552. List<Vector2> coords;
  553. List<uint32_t> priorities;
  554. uint32_t priority_sum = 0;
  555. uint32_t mask;
  556. uint16_t mask_;
  557. uint16_t mask_ignore;
  558. for (Map<Vector2, uint32_t>::Element *E = tile_map[p_id].autotile_data.flags.front(); E; E = E->next()) {
  559. mask = E->get();
  560. if (tile_map[p_id].autotile_data.bitmask_mode == BITMASK_2X2) {
  561. mask |= (BIND_IGNORE_TOP | BIND_IGNORE_LEFT | BIND_IGNORE_CENTER | BIND_IGNORE_RIGHT | BIND_IGNORE_BOTTOM);
  562. }
  563. mask_ = mask & 0xFFFF;
  564. mask_ignore = mask >> 16;
  565. if (((mask_ & (~mask_ignore)) == (p_bitmask & (~mask_ignore))) && (((~mask_) | mask_ignore) == ((~p_bitmask) | mask_ignore))) {
  566. uint32_t priority = autotile_get_subtile_priority(p_id, E->key());
  567. priority_sum += priority;
  568. priorities.push_back(priority);
  569. coords.push_back(E->key());
  570. }
  571. }
  572. if (coords.size() == 0) {
  573. return autotile_get_icon_coordinate(p_id);
  574. } else {
  575. uint32_t picked_value = Math::rand() % priority_sum;
  576. uint32_t upper_bound;
  577. uint32_t lower_bound = 0;
  578. Vector2 result = coords.front()->get();
  579. List<Vector2>::Element *coords_E = coords.front();
  580. List<uint32_t>::Element *priorities_E = priorities.front();
  581. while (priorities_E) {
  582. upper_bound = lower_bound + priorities_E->get();
  583. if (lower_bound <= picked_value && picked_value < upper_bound) {
  584. result = coords_E->get();
  585. break;
  586. }
  587. lower_bound = upper_bound;
  588. priorities_E = priorities_E->next();
  589. coords_E = coords_E->next();
  590. }
  591. return result;
  592. }
  593. }
  594. Vector2 TileSet::atlastile_get_subtile_by_priority(int p_id, const Node *p_tilemap_node, const Vector2 &p_tile_location) {
  595. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  596. //First try to forward selection to script
  597. if (get_script_instance() != NULL) {
  598. if (get_script_instance()->has_method("_forward_atlas_subtile_selection")) {
  599. Variant ret = get_script_instance()->call("_forward_atlas_subtile_selection", p_id, p_tilemap_node, p_tile_location);
  600. if (ret.get_type() == Variant::VECTOR2) {
  601. return ret;
  602. }
  603. }
  604. }
  605. Vector2 coord = tile_get_region(p_id).size / autotile_get_size(p_id);
  606. List<Vector2> coords;
  607. for (int x = 0; x < coord.x; x++) {
  608. for (int y = 0; y < coord.y; y++) {
  609. for (int i = 0; i < autotile_get_subtile_priority(p_id, Vector2(x, y)); i++) {
  610. coords.push_back(Vector2(x, y));
  611. }
  612. }
  613. }
  614. if (coords.size() == 0) {
  615. return autotile_get_icon_coordinate(p_id);
  616. } else {
  617. return coords[Math::random(0, (int)coords.size())];
  618. }
  619. }
  620. void TileSet::tile_set_name(int p_id, const String &p_name) {
  621. ERR_FAIL_COND(!tile_map.has(p_id));
  622. tile_map[p_id].name = p_name;
  623. emit_changed();
  624. _change_notify("name");
  625. }
  626. String TileSet::tile_get_name(int p_id) const {
  627. ERR_FAIL_COND_V(!tile_map.has(p_id), String());
  628. return tile_map[p_id].name;
  629. }
  630. void TileSet::tile_clear_shapes(int p_id) {
  631. ERR_FAIL_COND(!tile_map.has(p_id));
  632. tile_map[p_id].shapes_data.clear();
  633. }
  634. void TileSet::tile_add_shape(int p_id, const Ref<Shape2D> &p_shape, const Transform2D &p_transform, bool p_one_way, const Vector2 &p_autotile_coord) {
  635. ERR_FAIL_COND(!tile_map.has(p_id));
  636. ShapeData new_data = ShapeData();
  637. new_data.shape = p_shape;
  638. new_data.shape_transform = p_transform;
  639. new_data.one_way_collision = p_one_way;
  640. new_data.autotile_coord = p_autotile_coord;
  641. tile_map[p_id].shapes_data.push_back(new_data);
  642. }
  643. int TileSet::tile_get_shape_count(int p_id) const {
  644. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  645. return tile_map[p_id].shapes_data.size();
  646. }
  647. void TileSet::tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape) {
  648. ERR_FAIL_COND(!tile_map.has(p_id));
  649. ERR_FAIL_COND(p_shape_id < 0);
  650. if (p_shape_id >= tile_map[p_id].shapes_data.size())
  651. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  652. tile_map[p_id].shapes_data.write[p_shape_id].shape = p_shape;
  653. _decompose_convex_shape(p_shape);
  654. emit_changed();
  655. }
  656. Ref<Shape2D> TileSet::tile_get_shape(int p_id, int p_shape_id) const {
  657. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Shape2D>());
  658. ERR_FAIL_COND_V(p_shape_id < 0, Ref<Shape2D>());
  659. if (p_shape_id < tile_map[p_id].shapes_data.size())
  660. return tile_map[p_id].shapes_data[p_shape_id].shape;
  661. return Ref<Shape2D>();
  662. }
  663. void TileSet::tile_set_shape_transform(int p_id, int p_shape_id, const Transform2D &p_offset) {
  664. ERR_FAIL_COND(!tile_map.has(p_id));
  665. ERR_FAIL_COND(p_shape_id < 0);
  666. if (p_shape_id >= tile_map[p_id].shapes_data.size())
  667. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  668. tile_map[p_id].shapes_data.write[p_shape_id].shape_transform = p_offset;
  669. emit_changed();
  670. }
  671. Transform2D TileSet::tile_get_shape_transform(int p_id, int p_shape_id) const {
  672. ERR_FAIL_COND_V(!tile_map.has(p_id), Transform2D());
  673. ERR_FAIL_COND_V(p_shape_id < 0, Transform2D());
  674. if (p_shape_id < tile_map[p_id].shapes_data.size())
  675. return tile_map[p_id].shapes_data[p_shape_id].shape_transform;
  676. return Transform2D();
  677. }
  678. void TileSet::tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset) {
  679. Transform2D transform = tile_get_shape_transform(p_id, p_shape_id);
  680. transform.set_origin(p_offset);
  681. tile_set_shape_transform(p_id, p_shape_id, transform);
  682. }
  683. Vector2 TileSet::tile_get_shape_offset(int p_id, int p_shape_id) const {
  684. return tile_get_shape_transform(p_id, p_shape_id).get_origin();
  685. }
  686. void TileSet::tile_set_shape_one_way(int p_id, int p_shape_id, const bool p_one_way) {
  687. ERR_FAIL_COND(!tile_map.has(p_id));
  688. ERR_FAIL_COND(p_shape_id < 0);
  689. if (p_shape_id >= tile_map[p_id].shapes_data.size())
  690. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  691. tile_map[p_id].shapes_data.write[p_shape_id].one_way_collision = p_one_way;
  692. emit_changed();
  693. }
  694. bool TileSet::tile_get_shape_one_way(int p_id, int p_shape_id) const {
  695. ERR_FAIL_COND_V(!tile_map.has(p_id), false);
  696. ERR_FAIL_COND_V(p_shape_id < 0, false);
  697. if (p_shape_id < tile_map[p_id].shapes_data.size())
  698. return tile_map[p_id].shapes_data[p_shape_id].one_way_collision;
  699. return false;
  700. }
  701. void TileSet::tile_set_shape_one_way_margin(int p_id, int p_shape_id, float p_margin) {
  702. ERR_FAIL_COND(!tile_map.has(p_id));
  703. ERR_FAIL_COND(p_shape_id < 0);
  704. if (p_shape_id >= tile_map[p_id].shapes_data.size())
  705. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  706. tile_map[p_id].shapes_data.write[p_shape_id].one_way_collision_margin = p_margin;
  707. emit_changed();
  708. }
  709. float TileSet::tile_get_shape_one_way_margin(int p_id, int p_shape_id) const {
  710. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  711. ERR_FAIL_COND_V(p_shape_id < 0, 0);
  712. if (p_shape_id < tile_map[p_id].shapes_data.size())
  713. return tile_map[p_id].shapes_data[p_shape_id].one_way_collision_margin;
  714. return 0;
  715. }
  716. void TileSet::tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder) {
  717. ERR_FAIL_COND(!tile_map.has(p_id));
  718. tile_map[p_id].occluder = p_light_occluder;
  719. }
  720. Ref<OccluderPolygon2D> TileSet::tile_get_light_occluder(int p_id) const {
  721. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
  722. return tile_map[p_id].occluder;
  723. }
  724. void TileSet::autotile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder, const Vector2 &p_coord) {
  725. ERR_FAIL_COND(!tile_map.has(p_id));
  726. if (p_light_occluder.is_null()) {
  727. if (tile_map[p_id].autotile_data.occluder_map.has(p_coord)) {
  728. tile_map[p_id].autotile_data.occluder_map.erase(p_coord);
  729. }
  730. } else {
  731. tile_map[p_id].autotile_data.occluder_map[p_coord] = p_light_occluder;
  732. }
  733. }
  734. Ref<OccluderPolygon2D> TileSet::autotile_get_light_occluder(int p_id, const Vector2 &p_coord) const {
  735. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
  736. if (!tile_map[p_id].autotile_data.occluder_map.has(p_coord)) {
  737. return Ref<OccluderPolygon2D>();
  738. } else {
  739. return tile_map[p_id].autotile_data.occluder_map[p_coord];
  740. }
  741. }
  742. void TileSet::tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset) {
  743. ERR_FAIL_COND(!tile_map.has(p_id));
  744. tile_map[p_id].navigation_polygon_offset = p_offset;
  745. }
  746. Vector2 TileSet::tile_get_navigation_polygon_offset(int p_id) const {
  747. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  748. return tile_map[p_id].navigation_polygon_offset;
  749. }
  750. void TileSet::tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon) {
  751. ERR_FAIL_COND(!tile_map.has(p_id));
  752. tile_map[p_id].navigation_polygon = p_navigation_polygon;
  753. }
  754. Ref<NavigationPolygon> TileSet::tile_get_navigation_polygon(int p_id) const {
  755. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
  756. return tile_map[p_id].navigation_polygon;
  757. }
  758. const Map<Vector2, Ref<OccluderPolygon2D> > &TileSet::autotile_get_light_oclusion_map(int p_id) const {
  759. static Map<Vector2, Ref<OccluderPolygon2D> > dummy;
  760. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  761. return tile_map[p_id].autotile_data.occluder_map;
  762. }
  763. void TileSet::autotile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon, const Vector2 &p_coord) {
  764. ERR_FAIL_COND(!tile_map.has(p_id));
  765. if (p_navigation_polygon.is_null()) {
  766. if (tile_map[p_id].autotile_data.navpoly_map.has(p_coord)) {
  767. tile_map[p_id].autotile_data.navpoly_map.erase(p_coord);
  768. }
  769. } else {
  770. tile_map[p_id].autotile_data.navpoly_map[p_coord] = p_navigation_polygon;
  771. }
  772. }
  773. Ref<NavigationPolygon> TileSet::autotile_get_navigation_polygon(int p_id, const Vector2 &p_coord) const {
  774. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
  775. if (!tile_map[p_id].autotile_data.navpoly_map.has(p_coord)) {
  776. return Ref<NavigationPolygon>();
  777. } else {
  778. return tile_map[p_id].autotile_data.navpoly_map[p_coord];
  779. }
  780. }
  781. const Map<Vector2, Ref<NavigationPolygon> > &TileSet::autotile_get_navigation_map(int p_id) const {
  782. static Map<Vector2, Ref<NavigationPolygon> > dummy;
  783. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  784. return tile_map[p_id].autotile_data.navpoly_map;
  785. }
  786. void TileSet::tile_set_occluder_offset(int p_id, const Vector2 &p_offset) {
  787. ERR_FAIL_COND(!tile_map.has(p_id));
  788. tile_map[p_id].occluder_offset = p_offset;
  789. }
  790. Vector2 TileSet::tile_get_occluder_offset(int p_id) const {
  791. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  792. return tile_map[p_id].occluder_offset;
  793. }
  794. void TileSet::tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes) {
  795. ERR_FAIL_COND(!tile_map.has(p_id));
  796. tile_map[p_id].shapes_data = p_shapes;
  797. for (int i = 0; i < p_shapes.size(); i++) {
  798. _decompose_convex_shape(p_shapes[i].shape);
  799. }
  800. emit_changed();
  801. }
  802. Vector<TileSet::ShapeData> TileSet::tile_get_shapes(int p_id) const {
  803. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector<ShapeData>());
  804. return tile_map[p_id].shapes_data;
  805. }
  806. int TileSet::tile_get_z_index(int p_id) const {
  807. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  808. return tile_map[p_id].z_index;
  809. }
  810. void TileSet::tile_set_z_index(int p_id, int p_z_index) {
  811. ERR_FAIL_COND(!tile_map.has(p_id));
  812. tile_map[p_id].z_index = p_z_index;
  813. emit_changed();
  814. }
  815. void TileSet::_tile_set_shapes(int p_id, const Array &p_shapes) {
  816. ERR_FAIL_COND(!tile_map.has(p_id));
  817. Vector<ShapeData> shapes_data;
  818. Transform2D default_transform = tile_get_shape_transform(p_id, 0);
  819. bool default_one_way = tile_get_shape_one_way(p_id, 0);
  820. Vector2 default_autotile_coord = Vector2();
  821. for (int i = 0; i < p_shapes.size(); i++) {
  822. ShapeData s = ShapeData();
  823. if (p_shapes[i].get_type() == Variant::OBJECT) {
  824. Ref<Shape2D> shape = p_shapes[i];
  825. if (shape.is_null()) continue;
  826. s.shape = shape;
  827. s.shape_transform = default_transform;
  828. s.one_way_collision = default_one_way;
  829. s.autotile_coord = default_autotile_coord;
  830. } else if (p_shapes[i].get_type() == Variant::DICTIONARY) {
  831. Dictionary d = p_shapes[i];
  832. if (d.has("shape") && d["shape"].get_type() == Variant::OBJECT) {
  833. s.shape = d["shape"];
  834. _decompose_convex_shape(s.shape);
  835. } else
  836. continue;
  837. if (d.has("shape_transform") && d["shape_transform"].get_type() == Variant::TRANSFORM2D)
  838. s.shape_transform = d["shape_transform"];
  839. else if (d.has("shape_offset") && d["shape_offset"].get_type() == Variant::VECTOR2)
  840. s.shape_transform = Transform2D(0, (Vector2)d["shape_offset"]);
  841. else
  842. s.shape_transform = default_transform;
  843. if (d.has("one_way") && d["one_way"].get_type() == Variant::BOOL)
  844. s.one_way_collision = d["one_way"];
  845. else
  846. s.one_way_collision = default_one_way;
  847. if (d.has("one_way_margin") && d["one_way_margin"].is_num())
  848. s.one_way_collision_margin = d["one_way_margin"];
  849. else
  850. s.one_way_collision_margin = 1.0;
  851. if (d.has("autotile_coord") && d["autotile_coord"].get_type() == Variant::VECTOR2)
  852. s.autotile_coord = d["autotile_coord"];
  853. else
  854. s.autotile_coord = default_autotile_coord;
  855. } else {
  856. ERR_CONTINUE_MSG(true, "Expected an array of objects or dictionaries for tile_set_shapes.");
  857. }
  858. shapes_data.push_back(s);
  859. }
  860. tile_map[p_id].shapes_data = shapes_data;
  861. emit_changed();
  862. }
  863. Array TileSet::_tile_get_shapes(int p_id) const {
  864. ERR_FAIL_COND_V(!tile_map.has(p_id), Array());
  865. Array arr;
  866. Vector<ShapeData> data = tile_map[p_id].shapes_data;
  867. for (int i = 0; i < data.size(); i++) {
  868. Dictionary shape_data;
  869. shape_data["shape"] = data[i].shape;
  870. shape_data["shape_transform"] = data[i].shape_transform;
  871. shape_data["one_way"] = data[i].one_way_collision;
  872. shape_data["one_way_margin"] = data[i].one_way_collision_margin;
  873. shape_data["autotile_coord"] = data[i].autotile_coord;
  874. arr.push_back(shape_data);
  875. }
  876. return arr;
  877. }
  878. Array TileSet::_get_tiles_ids() const {
  879. Array arr;
  880. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  881. arr.push_back(E->key());
  882. }
  883. return arr;
  884. }
  885. void TileSet::_decompose_convex_shape(Ref<Shape2D> p_shape) {
  886. if (Engine::get_singleton()->is_editor_hint())
  887. return;
  888. Ref<ConvexPolygonShape2D> convex = p_shape;
  889. if (!convex.is_valid())
  890. return;
  891. Vector<Vector<Vector2> > decomp = Geometry::decompose_polygon_in_convex(convex->get_points());
  892. if (decomp.size() > 1) {
  893. Array sub_shapes;
  894. for (int i = 0; i < decomp.size(); i++) {
  895. Ref<ConvexPolygonShape2D> _convex = memnew(ConvexPolygonShape2D);
  896. _convex->set_points(decomp[i]);
  897. sub_shapes.append(_convex);
  898. }
  899. convex->set_meta("decomposed", sub_shapes);
  900. } else {
  901. convex->set_meta("decomposed", Variant());
  902. }
  903. }
  904. void TileSet::get_tile_list(List<int> *p_tiles) const {
  905. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  906. p_tiles->push_back(E->key());
  907. }
  908. }
  909. bool TileSet::has_tile(int p_id) const {
  910. return tile_map.has(p_id);
  911. }
  912. bool TileSet::is_tile_bound(int p_drawn_id, int p_neighbor_id) {
  913. if (p_drawn_id == p_neighbor_id) {
  914. return true;
  915. } else if (get_script_instance() != NULL) {
  916. if (get_script_instance()->has_method("_is_tile_bound")) {
  917. Variant ret = get_script_instance()->call("_is_tile_bound", p_drawn_id, p_neighbor_id);
  918. if (ret.get_type() == Variant::BOOL) {
  919. return ret;
  920. }
  921. }
  922. }
  923. return false;
  924. }
  925. void TileSet::remove_tile(int p_id) {
  926. ERR_FAIL_COND(!tile_map.has(p_id));
  927. tile_map.erase(p_id);
  928. _change_notify("");
  929. emit_changed();
  930. }
  931. int TileSet::get_last_unused_tile_id() const {
  932. if (tile_map.size())
  933. return tile_map.back()->key() + 1;
  934. else
  935. return 0;
  936. }
  937. int TileSet::find_tile_by_name(const String &p_name) const {
  938. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  939. if (p_name == E->get().name)
  940. return E->key();
  941. }
  942. return -1;
  943. }
  944. void TileSet::clear() {
  945. tile_map.clear();
  946. _change_notify("");
  947. emit_changed();
  948. }
  949. void TileSet::_bind_methods() {
  950. ClassDB::bind_method(D_METHOD("create_tile", "id"), &TileSet::create_tile);
  951. ClassDB::bind_method(D_METHOD("autotile_clear_bitmask_map", "id"), &TileSet::autotile_clear_bitmask_map);
  952. ClassDB::bind_method(D_METHOD("autotile_set_icon_coordinate", "id", "coord"), &TileSet::autotile_set_icon_coordinate);
  953. ClassDB::bind_method(D_METHOD("autotile_get_icon_coordinate", "id"), &TileSet::autotile_get_icon_coordinate);
  954. ClassDB::bind_method(D_METHOD("autotile_set_subtile_priority", "id", "coord", "priority"), &TileSet::autotile_set_subtile_priority);
  955. ClassDB::bind_method(D_METHOD("autotile_get_subtile_priority", "id", "coord"), &TileSet::autotile_get_subtile_priority);
  956. ClassDB::bind_method(D_METHOD("autotile_set_z_index", "id", "coord", "z_index"), &TileSet::autotile_set_z_index);
  957. ClassDB::bind_method(D_METHOD("autotile_get_z_index", "id", "coord"), &TileSet::autotile_get_z_index);
  958. ClassDB::bind_method(D_METHOD("autotile_set_light_occluder", "id", "light_occluder", "coord"), &TileSet::autotile_set_light_occluder);
  959. ClassDB::bind_method(D_METHOD("autotile_get_light_occluder", "id", "coord"), &TileSet::autotile_get_light_occluder);
  960. ClassDB::bind_method(D_METHOD("autotile_set_navigation_polygon", "id", "navigation_polygon", "coord"), &TileSet::autotile_set_navigation_polygon);
  961. ClassDB::bind_method(D_METHOD("autotile_get_navigation_polygon", "id", "coord"), &TileSet::autotile_get_navigation_polygon);
  962. ClassDB::bind_method(D_METHOD("autotile_set_bitmask", "id", "bitmask", "flag"), &TileSet::autotile_set_bitmask);
  963. ClassDB::bind_method(D_METHOD("autotile_get_bitmask", "id", "coord"), &TileSet::autotile_get_bitmask);
  964. ClassDB::bind_method(D_METHOD("autotile_set_bitmask_mode", "id", "mode"), &TileSet::autotile_set_bitmask_mode);
  965. ClassDB::bind_method(D_METHOD("autotile_get_bitmask_mode", "id"), &TileSet::autotile_get_bitmask_mode);
  966. ClassDB::bind_method(D_METHOD("autotile_set_spacing", "id", "spacing"), &TileSet::autotile_set_spacing);
  967. ClassDB::bind_method(D_METHOD("autotile_get_spacing", "id"), &TileSet::autotile_get_spacing);
  968. ClassDB::bind_method(D_METHOD("autotile_set_size", "id", "size"), &TileSet::autotile_set_size);
  969. ClassDB::bind_method(D_METHOD("autotile_get_size", "id"), &TileSet::autotile_get_size);
  970. ClassDB::bind_method(D_METHOD("tile_set_name", "id", "name"), &TileSet::tile_set_name);
  971. ClassDB::bind_method(D_METHOD("tile_get_name", "id"), &TileSet::tile_get_name);
  972. ClassDB::bind_method(D_METHOD("tile_set_texture", "id", "texture"), &TileSet::tile_set_texture);
  973. ClassDB::bind_method(D_METHOD("tile_get_texture", "id"), &TileSet::tile_get_texture);
  974. ClassDB::bind_method(D_METHOD("tile_set_normal_map", "id", "normal_map"), &TileSet::tile_set_normal_map);
  975. ClassDB::bind_method(D_METHOD("tile_get_normal_map", "id"), &TileSet::tile_get_normal_map);
  976. ClassDB::bind_method(D_METHOD("tile_set_material", "id", "material"), &TileSet::tile_set_material);
  977. ClassDB::bind_method(D_METHOD("tile_get_material", "id"), &TileSet::tile_get_material);
  978. ClassDB::bind_method(D_METHOD("tile_set_modulate", "id", "color"), &TileSet::tile_set_modulate);
  979. ClassDB::bind_method(D_METHOD("tile_get_modulate", "id"), &TileSet::tile_get_modulate);
  980. ClassDB::bind_method(D_METHOD("tile_set_texture_offset", "id", "texture_offset"), &TileSet::tile_set_texture_offset);
  981. ClassDB::bind_method(D_METHOD("tile_get_texture_offset", "id"), &TileSet::tile_get_texture_offset);
  982. ClassDB::bind_method(D_METHOD("tile_set_region", "id", "region"), &TileSet::tile_set_region);
  983. ClassDB::bind_method(D_METHOD("tile_get_region", "id"), &TileSet::tile_get_region);
  984. ClassDB::bind_method(D_METHOD("tile_set_shape", "id", "shape_id", "shape"), &TileSet::tile_set_shape);
  985. ClassDB::bind_method(D_METHOD("tile_get_shape", "id", "shape_id"), &TileSet::tile_get_shape);
  986. ClassDB::bind_method(D_METHOD("tile_set_shape_offset", "id", "shape_id", "shape_offset"), &TileSet::tile_set_shape_offset);
  987. ClassDB::bind_method(D_METHOD("tile_get_shape_offset", "id", "shape_id"), &TileSet::tile_get_shape_offset);
  988. ClassDB::bind_method(D_METHOD("tile_set_shape_transform", "id", "shape_id", "shape_transform"), &TileSet::tile_set_shape_transform);
  989. ClassDB::bind_method(D_METHOD("tile_get_shape_transform", "id", "shape_id"), &TileSet::tile_get_shape_transform);
  990. ClassDB::bind_method(D_METHOD("tile_set_shape_one_way", "id", "shape_id", "one_way"), &TileSet::tile_set_shape_one_way);
  991. ClassDB::bind_method(D_METHOD("tile_get_shape_one_way", "id", "shape_id"), &TileSet::tile_get_shape_one_way);
  992. ClassDB::bind_method(D_METHOD("tile_set_shape_one_way_margin", "id", "shape_id", "one_way"), &TileSet::tile_set_shape_one_way_margin);
  993. ClassDB::bind_method(D_METHOD("tile_get_shape_one_way_margin", "id", "shape_id"), &TileSet::tile_get_shape_one_way_margin);
  994. ClassDB::bind_method(D_METHOD("tile_add_shape", "id", "shape", "shape_transform", "one_way", "autotile_coord"), &TileSet::tile_add_shape, DEFVAL(false), DEFVAL(Vector2()));
  995. ClassDB::bind_method(D_METHOD("tile_get_shape_count", "id"), &TileSet::tile_get_shape_count);
  996. ClassDB::bind_method(D_METHOD("tile_set_shapes", "id", "shapes"), &TileSet::_tile_set_shapes);
  997. ClassDB::bind_method(D_METHOD("tile_get_shapes", "id"), &TileSet::_tile_get_shapes);
  998. ClassDB::bind_method(D_METHOD("tile_set_tile_mode", "id", "tilemode"), &TileSet::tile_set_tile_mode);
  999. ClassDB::bind_method(D_METHOD("tile_get_tile_mode", "id"), &TileSet::tile_get_tile_mode);
  1000. ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon", "id", "navigation_polygon"), &TileSet::tile_set_navigation_polygon);
  1001. ClassDB::bind_method(D_METHOD("tile_get_navigation_polygon", "id"), &TileSet::tile_get_navigation_polygon);
  1002. ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon_offset", "id", "navigation_polygon_offset"), &TileSet::tile_set_navigation_polygon_offset);
  1003. ClassDB::bind_method(D_METHOD("tile_get_navigation_polygon_offset", "id"), &TileSet::tile_get_navigation_polygon_offset);
  1004. ClassDB::bind_method(D_METHOD("tile_set_light_occluder", "id", "light_occluder"), &TileSet::tile_set_light_occluder);
  1005. ClassDB::bind_method(D_METHOD("tile_get_light_occluder", "id"), &TileSet::tile_get_light_occluder);
  1006. ClassDB::bind_method(D_METHOD("tile_set_occluder_offset", "id", "occluder_offset"), &TileSet::tile_set_occluder_offset);
  1007. ClassDB::bind_method(D_METHOD("tile_get_occluder_offset", "id"), &TileSet::tile_get_occluder_offset);
  1008. ClassDB::bind_method(D_METHOD("tile_set_z_index", "id", "z_index"), &TileSet::tile_set_z_index);
  1009. ClassDB::bind_method(D_METHOD("tile_get_z_index", "id"), &TileSet::tile_get_z_index);
  1010. ClassDB::bind_method(D_METHOD("remove_tile", "id"), &TileSet::remove_tile);
  1011. ClassDB::bind_method(D_METHOD("clear"), &TileSet::clear);
  1012. ClassDB::bind_method(D_METHOD("get_last_unused_tile_id"), &TileSet::get_last_unused_tile_id);
  1013. ClassDB::bind_method(D_METHOD("find_tile_by_name", "name"), &TileSet::find_tile_by_name);
  1014. ClassDB::bind_method(D_METHOD("get_tiles_ids"), &TileSet::_get_tiles_ids);
  1015. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_is_tile_bound", PropertyInfo(Variant::INT, "drawn_id"), PropertyInfo(Variant::INT, "neighbor_id")));
  1016. BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "_forward_subtile_selection", PropertyInfo(Variant::INT, "autotile_id"), PropertyInfo(Variant::INT, "bitmask"), PropertyInfo(Variant::OBJECT, "tilemap", PROPERTY_HINT_NONE, "TileMap"), PropertyInfo(Variant::VECTOR2, "tile_location")));
  1017. BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "_forward_atlas_subtile_selection", PropertyInfo(Variant::INT, "atlastile_id"), PropertyInfo(Variant::OBJECT, "tilemap", PROPERTY_HINT_NONE, "TileMap"), PropertyInfo(Variant::VECTOR2, "tile_location")));
  1018. BIND_ENUM_CONSTANT(BITMASK_2X2);
  1019. BIND_ENUM_CONSTANT(BITMASK_3X3_MINIMAL);
  1020. BIND_ENUM_CONSTANT(BITMASK_3X3);
  1021. BIND_ENUM_CONSTANT(BIND_TOPLEFT);
  1022. BIND_ENUM_CONSTANT(BIND_TOP);
  1023. BIND_ENUM_CONSTANT(BIND_TOPRIGHT);
  1024. BIND_ENUM_CONSTANT(BIND_LEFT);
  1025. BIND_ENUM_CONSTANT(BIND_CENTER);
  1026. BIND_ENUM_CONSTANT(BIND_RIGHT);
  1027. BIND_ENUM_CONSTANT(BIND_BOTTOMLEFT);
  1028. BIND_ENUM_CONSTANT(BIND_BOTTOM);
  1029. BIND_ENUM_CONSTANT(BIND_BOTTOMRIGHT);
  1030. BIND_ENUM_CONSTANT(SINGLE_TILE);
  1031. BIND_ENUM_CONSTANT(AUTO_TILE);
  1032. BIND_ENUM_CONSTANT(ATLAS_TILE);
  1033. }
  1034. TileSet::TileSet() {
  1035. }