tile_set.cpp 51 KB

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