tile_set.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*************************************************************************/
  2. /* tile_set.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "array.h"
  32. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  33. String n = p_name;
  34. int slash = n.find("/");
  35. if (slash == -1)
  36. return false;
  37. int id = String::to_int(n.c_str(), slash);
  38. if (!tile_map.has(id))
  39. create_tile(id);
  40. String what = n.substr(slash + 1, n.length());
  41. if (what == "name")
  42. tile_set_name(id, p_value);
  43. else if (what == "texture")
  44. tile_set_texture(id, p_value);
  45. else if (what == "normal_map")
  46. tile_set_normal_map(id, p_value);
  47. else if (what == "tex_offset")
  48. tile_set_texture_offset(id, p_value);
  49. else if (what == "material")
  50. tile_set_material(id, p_value);
  51. else if (what == "modulate")
  52. tile_set_modulate(id, p_value);
  53. else if (what == "region")
  54. tile_set_region(id, p_value);
  55. else if (what == "is_autotile")
  56. tile_set_is_autotile(id, p_value);
  57. else if (what.left(9) == "autotile/") {
  58. what = what.right(9);
  59. if (what == "bitmask_mode")
  60. autotile_set_bitmask_mode(id, (BitmaskMode)((int)p_value));
  61. else if (what == "icon_coordinate")
  62. autotile_set_icon_coordinate(id, p_value);
  63. else if (what == "tile_size")
  64. autotile_set_size(id, p_value);
  65. else if (what == "spacing")
  66. autotile_set_spacing(id, p_value);
  67. else if (what == "bitmask_flags") {
  68. tile_map[id].autotile_data.flags.clear();
  69. if (p_value.is_array()) {
  70. Array p = p_value;
  71. Vector2 last_coord;
  72. while (p.size() > 0) {
  73. if (p[0].get_type() == Variant::VECTOR2) {
  74. last_coord = p[0];
  75. } else if (p[0].get_type() == Variant::INT) {
  76. autotile_set_bitmask(id, last_coord, p[0]);
  77. }
  78. p.pop_front();
  79. }
  80. }
  81. } else if (what == "occluder_map") {
  82. tile_map[id].autotile_data.occluder_map.clear();
  83. Array p = p_value;
  84. Vector2 last_coord;
  85. while (p.size() > 0) {
  86. if (p[0].get_type() == Variant::VECTOR2) {
  87. last_coord = p[0];
  88. } else if (p[0].get_type() == Variant::OBJECT) {
  89. autotile_set_light_occluder(id, p[0], last_coord);
  90. }
  91. p.pop_front();
  92. }
  93. } else if (what == "navpoly_map") {
  94. tile_map[id].autotile_data.navpoly_map.clear();
  95. Array p = p_value;
  96. Vector2 last_coord;
  97. while (p.size() > 0) {
  98. if (p[0].get_type() == Variant::VECTOR2) {
  99. last_coord = p[0];
  100. } else if (p[0].get_type() == Variant::OBJECT) {
  101. autotile_set_navigation_polygon(id, p[0], last_coord);
  102. }
  103. p.pop_front();
  104. }
  105. } else if (what == "priority_map") {
  106. tile_map[id].autotile_data.priority_map.clear();
  107. Array p = p_value;
  108. Vector3 val;
  109. Vector2 v;
  110. int priority;
  111. while (p.size() > 0) {
  112. val = p[0];
  113. if (val.z > 1) {
  114. v.x = val.x;
  115. v.y = val.y;
  116. priority = (int)val.z;
  117. tile_map[id].autotile_data.priority_map[v] = priority;
  118. }
  119. p.pop_front();
  120. }
  121. }
  122. } else if (what == "shape")
  123. tile_set_shape(id, 0, p_value);
  124. else if (what == "shape_offset")
  125. tile_set_shape_offset(id, 0, p_value);
  126. else if (what == "shape_transform")
  127. tile_set_shape_transform(id, 0, p_value);
  128. else if (what == "shape_one_way")
  129. tile_set_shape_one_way(id, 0, p_value);
  130. else if (what == "shapes")
  131. _tile_set_shapes(id, p_value);
  132. else if (what == "occluder")
  133. tile_set_light_occluder(id, p_value);
  134. else if (what == "occluder_offset")
  135. tile_set_occluder_offset(id, p_value);
  136. else if (what == "navigation")
  137. tile_set_navigation_polygon(id, p_value);
  138. else if (what == "navigation_offset")
  139. tile_set_navigation_polygon_offset(id, p_value);
  140. else
  141. return false;
  142. return true;
  143. }
  144. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  145. String n = p_name;
  146. int slash = n.find("/");
  147. if (slash == -1)
  148. return false;
  149. int id = String::to_int(n.c_str(), slash);
  150. ERR_FAIL_COND_V(!tile_map.has(id), false);
  151. String what = n.substr(slash + 1, n.length());
  152. if (what == "name")
  153. r_ret = tile_get_name(id);
  154. else if (what == "texture")
  155. r_ret = tile_get_texture(id);
  156. else if (what == "normal_map")
  157. r_ret = tile_get_normal_map(id);
  158. else if (what == "tex_offset")
  159. r_ret = tile_get_texture_offset(id);
  160. else if (what == "material")
  161. r_ret = tile_get_material(id);
  162. else if (what == "modulate")
  163. r_ret = tile_get_modulate(id);
  164. else if (what == "region")
  165. r_ret = tile_get_region(id);
  166. else if (what == "is_autotile")
  167. r_ret = tile_get_is_autotile(id);
  168. else if (what.left(9) == "autotile/") {
  169. what = what.right(9);
  170. if (what == "bitmask_mode")
  171. r_ret = autotile_get_bitmask_mode(id);
  172. else if (what == "icon_coordinate")
  173. r_ret = autotile_get_icon_coordinate(id);
  174. else if (what == "tile_size")
  175. r_ret = autotile_get_size(id);
  176. else if (what == "spacing")
  177. r_ret = autotile_get_spacing(id);
  178. else if (what == "bitmask_flags") {
  179. Array p;
  180. for (Map<Vector2, uint16_t>::Element *E = tile_map[id].autotile_data.flags.front(); E; E = E->next()) {
  181. p.push_back(E->key());
  182. p.push_back(E->value());
  183. }
  184. r_ret = p;
  185. } else if (what == "occluder_map") {
  186. Array p;
  187. for (Map<Vector2, Ref<OccluderPolygon2D> >::Element *E = tile_map[id].autotile_data.occluder_map.front(); E; E = E->next()) {
  188. p.push_back(E->key());
  189. p.push_back(E->value());
  190. }
  191. r_ret = p;
  192. } else if (what == "navpoly_map") {
  193. Array p;
  194. for (Map<Vector2, Ref<NavigationPolygon> >::Element *E = tile_map[id].autotile_data.navpoly_map.front(); E; E = E->next()) {
  195. p.push_back(E->key());
  196. p.push_back(E->value());
  197. }
  198. r_ret = p;
  199. } else if (what == "priority_map") {
  200. Array p;
  201. Vector3 v;
  202. for (Map<Vector2, int>::Element *E = tile_map[id].autotile_data.priority_map.front(); E; E = E->next()) {
  203. if (E->value() > 1) {
  204. //Don't save default value
  205. v.x = E->key().x;
  206. v.y = E->key().y;
  207. v.z = E->value();
  208. p.push_back(v);
  209. }
  210. }
  211. r_ret = p;
  212. }
  213. } else if (what == "shape")
  214. r_ret = tile_get_shape(id, 0);
  215. else if (what == "shape_offset")
  216. r_ret = tile_get_shape_offset(id, 0);
  217. else if (what == "shape_transform")
  218. r_ret = tile_get_shape_transform(id, 0);
  219. else if (what == "shape_one_way")
  220. r_ret = tile_get_shape_one_way(id, 0);
  221. else if (what == "shapes")
  222. r_ret = _tile_get_shapes(id);
  223. else if (what == "occluder")
  224. r_ret = tile_get_light_occluder(id);
  225. else if (what == "occluder_offset")
  226. r_ret = tile_get_occluder_offset(id);
  227. else if (what == "navigation")
  228. r_ret = tile_get_navigation_polygon(id);
  229. else if (what == "navigation_offset")
  230. r_ret = tile_get_navigation_polygon_offset(id);
  231. else
  232. return false;
  233. return true;
  234. }
  235. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  236. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  237. int id = E->key();
  238. String pre = itos(id) + "/";
  239. p_list->push_back(PropertyInfo(Variant::STRING, pre + "name"));
  240. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  241. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  242. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "tex_offset"));
  243. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial"));
  244. p_list->push_back(PropertyInfo(Variant::COLOR, pre + "modulate"));
  245. p_list->push_back(PropertyInfo(Variant::RECT2, pre + "region"));
  246. p_list->push_back(PropertyInfo(Variant::BOOL, pre + "is_autotile", PROPERTY_HINT_NONE, ""));
  247. if (tile_get_is_autotile(id)) {
  248. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/bitmask_mode", PROPERTY_HINT_ENUM, "2X2,3X3", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  249. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/icon_coordinate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  250. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/tile_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  251. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/spacing", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  252. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/bitmask_flags", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  253. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/occluder_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  254. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/navpoly_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  255. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/priority_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  256. }
  257. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "occluder_offset"));
  258. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"));
  259. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "navigation_offset"));
  260. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "navigation", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"));
  261. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  262. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  263. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D", PROPERTY_USAGE_EDITOR));
  264. p_list->push_back(PropertyInfo(Variant::BOOL, pre + "shape_one_way", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  265. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "shapes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  266. }
  267. }
  268. void TileSet::create_tile(int p_id) {
  269. ERR_FAIL_COND(tile_map.has(p_id));
  270. tile_map[p_id] = TileData();
  271. tile_map[p_id].autotile_data = AutotileData();
  272. _change_notify("");
  273. emit_changed();
  274. }
  275. void TileSet::autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode) {
  276. ERR_FAIL_COND(!tile_map.has(p_id));
  277. tile_map[p_id].autotile_data.bitmask_mode = p_mode;
  278. _change_notify("");
  279. emit_changed();
  280. }
  281. TileSet::BitmaskMode TileSet::autotile_get_bitmask_mode(int p_id) const {
  282. ERR_FAIL_COND_V(!tile_map.has(p_id), BITMASK_2X2);
  283. return tile_map[p_id].autotile_data.bitmask_mode;
  284. }
  285. void TileSet::tile_set_texture(int p_id, const Ref<Texture> &p_texture) {
  286. ERR_FAIL_COND(!tile_map.has(p_id));
  287. tile_map[p_id].texture = p_texture;
  288. emit_changed();
  289. _change_notify("texture");
  290. }
  291. Ref<Texture> TileSet::tile_get_texture(int p_id) const {
  292. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
  293. return tile_map[p_id].texture;
  294. }
  295. void TileSet::tile_set_normal_map(int p_id, const Ref<Texture> &p_normal_map) {
  296. ERR_FAIL_COND(!tile_map.has(p_id));
  297. tile_map[p_id].normal_map = p_normal_map;
  298. emit_changed();
  299. }
  300. Ref<Texture> TileSet::tile_get_normal_map(int p_id) const {
  301. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
  302. return tile_map[p_id].normal_map;
  303. }
  304. void TileSet::tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material) {
  305. ERR_FAIL_COND(!tile_map.has(p_id));
  306. tile_map[p_id].material = p_material;
  307. emit_changed();
  308. }
  309. Ref<ShaderMaterial> TileSet::tile_get_material(int p_id) const {
  310. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<ShaderMaterial>());
  311. return tile_map[p_id].material;
  312. }
  313. void TileSet::tile_set_modulate(int p_id, const Color &p_modulate) {
  314. ERR_FAIL_COND(!tile_map.has(p_id));
  315. tile_map[p_id].modulate = p_modulate;
  316. emit_changed();
  317. }
  318. Color TileSet::tile_get_modulate(int p_id) const {
  319. ERR_FAIL_COND_V(!tile_map.has(p_id), Color(1, 1, 1));
  320. return tile_map[p_id].modulate;
  321. }
  322. void TileSet::tile_set_texture_offset(int p_id, const Vector2 &p_offset) {
  323. ERR_FAIL_COND(!tile_map.has(p_id));
  324. tile_map[p_id].offset = p_offset;
  325. emit_changed();
  326. }
  327. Vector2 TileSet::tile_get_texture_offset(int p_id) const {
  328. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  329. return tile_map[p_id].offset;
  330. }
  331. void TileSet::tile_set_region(int p_id, const Rect2 &p_region) {
  332. ERR_FAIL_COND(!tile_map.has(p_id));
  333. tile_map[p_id].region = p_region;
  334. emit_changed();
  335. }
  336. Rect2 TileSet::tile_get_region(int p_id) const {
  337. ERR_FAIL_COND_V(!tile_map.has(p_id), Rect2());
  338. return tile_map[p_id].region;
  339. }
  340. void TileSet::tile_set_is_autotile(int p_id, bool p_is_autotile) {
  341. ERR_FAIL_COND(!tile_map.has(p_id));
  342. tile_map[p_id].is_autotile = p_is_autotile;
  343. emit_changed();
  344. _change_notify("is_autotile");
  345. }
  346. bool TileSet::tile_get_is_autotile(int p_id) const {
  347. ERR_FAIL_COND_V(!tile_map.has(p_id), false);
  348. return tile_map[p_id].is_autotile;
  349. }
  350. void TileSet::autotile_set_icon_coordinate(int p_id, Vector2 coord) {
  351. ERR_FAIL_COND(!tile_map.has(p_id));
  352. tile_map[p_id].autotile_data.icon_coord = coord;
  353. emit_changed();
  354. }
  355. Vector2 TileSet::autotile_get_icon_coordinate(int p_id) const {
  356. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  357. return tile_map[p_id].autotile_data.icon_coord;
  358. }
  359. void TileSet::autotile_set_spacing(int p_id, int p_spacing) {
  360. ERR_FAIL_COND(!tile_map.has(p_id));
  361. ERR_FAIL_COND(p_spacing < 0);
  362. tile_map[p_id].autotile_data.spacing = p_spacing;
  363. emit_changed();
  364. }
  365. int TileSet::autotile_get_spacing(int p_id) const {
  366. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  367. return tile_map[p_id].autotile_data.spacing;
  368. }
  369. void TileSet::autotile_set_size(int p_id, Size2 p_size) {
  370. ERR_FAIL_COND(!tile_map.has(p_id));
  371. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  372. tile_map[p_id].autotile_data.size = p_size;
  373. }
  374. Size2 TileSet::autotile_get_size(int p_id) const {
  375. ERR_FAIL_COND_V(!tile_map.has(p_id), Size2());
  376. return tile_map[p_id].autotile_data.size;
  377. }
  378. void TileSet::autotile_clear_bitmask_map(int p_id) {
  379. ERR_FAIL_COND(!tile_map.has(p_id));
  380. tile_map[p_id].autotile_data.flags.clear();
  381. }
  382. void TileSet::autotile_set_subtile_priority(int p_id, const Vector2 &p_coord, int p_priority) {
  383. ERR_FAIL_COND(!tile_map.has(p_id));
  384. ERR_FAIL_COND(p_priority <= 0);
  385. tile_map[p_id].autotile_data.priority_map[p_coord] = p_priority;
  386. }
  387. int TileSet::autotile_get_subtile_priority(int p_id, const Vector2 &p_coord) {
  388. ERR_FAIL_COND_V(!tile_map.has(p_id), 1);
  389. if (tile_map[p_id].autotile_data.priority_map.has(p_coord)) {
  390. return tile_map[p_id].autotile_data.priority_map[p_coord];
  391. }
  392. //When not custom priority set return the default value
  393. return 1;
  394. }
  395. const Map<Vector2, int> &TileSet::autotile_get_priority_map(int p_id) const {
  396. static Map<Vector2, int> dummy;
  397. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  398. return tile_map[p_id].autotile_data.priority_map;
  399. }
  400. void TileSet::autotile_set_bitmask(int p_id, Vector2 p_coord, uint16_t p_flag) {
  401. ERR_FAIL_COND(!tile_map.has(p_id));
  402. if (p_flag == 0) {
  403. if (tile_map[p_id].autotile_data.flags.has(p_coord))
  404. tile_map[p_id].autotile_data.flags.erase(p_coord);
  405. } else {
  406. tile_map[p_id].autotile_data.flags[p_coord] = p_flag;
  407. }
  408. }
  409. uint16_t TileSet::autotile_get_bitmask(int p_id, Vector2 p_coord) {
  410. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  411. if (!tile_map[p_id].autotile_data.flags.has(p_coord)) {
  412. return 0;
  413. }
  414. return tile_map[p_id].autotile_data.flags[p_coord];
  415. }
  416. const Map<Vector2, uint16_t> &TileSet::autotile_get_bitmask_map(int p_id) {
  417. static Map<Vector2, uint16_t> dummy;
  418. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  419. return tile_map[p_id].autotile_data.flags;
  420. }
  421. Vector2 TileSet::autotile_get_subtile_for_bitmask(int p_id, uint16_t p_bitmask, const Node *p_tilemap_node, const Vector2 &p_tile_location) {
  422. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  423. //First try to forward selection to script
  424. if (p_tilemap_node->get_class_name() == "TileMap") {
  425. if (get_script_instance() != NULL) {
  426. if (get_script_instance()->has_method("_forward_subtile_selection")) {
  427. Variant ret = get_script_instance()->call("_forward_subtile_selection", p_id, p_bitmask, p_tilemap_node, p_tile_location);
  428. if (ret.get_type() == Variant::VECTOR2) {
  429. return ret;
  430. }
  431. }
  432. }
  433. }
  434. List<Vector2> coords;
  435. uint16_t mask;
  436. for (Map<Vector2, uint16_t>::Element *E = tile_map[p_id].autotile_data.flags.front(); E; E = E->next()) {
  437. mask = E->get();
  438. if (tile_map[p_id].autotile_data.bitmask_mode == BITMASK_2X2) {
  439. mask &= (BIND_BOTTOMLEFT | BIND_BOTTOMRIGHT | BIND_TOPLEFT | BIND_TOPRIGHT);
  440. }
  441. if (mask == p_bitmask) {
  442. for (int i = 0; i < autotile_get_subtile_priority(p_id, E->key()); i++) {
  443. coords.push_back(E->key());
  444. }
  445. }
  446. }
  447. if (coords.size() == 0) {
  448. return autotile_get_icon_coordinate(p_id);
  449. } else {
  450. return coords[Math::rand() % coords.size()];
  451. }
  452. }
  453. void TileSet::tile_set_name(int p_id, const String &p_name) {
  454. ERR_FAIL_COND(!tile_map.has(p_id));
  455. tile_map[p_id].name = p_name;
  456. emit_changed();
  457. }
  458. String TileSet::tile_get_name(int p_id) const {
  459. ERR_FAIL_COND_V(!tile_map.has(p_id), String());
  460. return tile_map[p_id].name;
  461. }
  462. void TileSet::tile_clear_shapes(int p_id) {
  463. tile_map[p_id].shapes_data.clear();
  464. }
  465. 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) {
  466. ERR_FAIL_COND(!tile_map.has(p_id));
  467. ShapeData new_data = ShapeData();
  468. new_data.shape = p_shape;
  469. new_data.shape_transform = p_transform;
  470. new_data.one_way_collision = p_one_way;
  471. new_data.autotile_coord = p_autotile_coord;
  472. tile_map[p_id].shapes_data.push_back(new_data);
  473. }
  474. int TileSet::tile_get_shape_count(int p_id) const {
  475. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  476. return tile_map[p_id].shapes_data.size();
  477. }
  478. void TileSet::tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape) {
  479. ERR_FAIL_COND(!tile_map.has(p_id));
  480. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  481. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  482. tile_map[p_id].shapes_data[p_shape_id].shape = p_shape;
  483. emit_changed();
  484. }
  485. Ref<Shape2D> TileSet::tile_get_shape(int p_id, int p_shape_id) const {
  486. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Shape2D>());
  487. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  488. return tile_map[p_id].shapes_data[p_shape_id].shape;
  489. return Ref<Shape2D>();
  490. }
  491. void TileSet::tile_set_shape_transform(int p_id, int p_shape_id, const Transform2D &p_offset) {
  492. ERR_FAIL_COND(!tile_map.has(p_id));
  493. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  494. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  495. tile_map[p_id].shapes_data[p_shape_id].shape_transform = p_offset;
  496. emit_changed();
  497. }
  498. Transform2D TileSet::tile_get_shape_transform(int p_id, int p_shape_id) const {
  499. ERR_FAIL_COND_V(!tile_map.has(p_id), Transform2D());
  500. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  501. return tile_map[p_id].shapes_data[p_shape_id].shape_transform;
  502. return Transform2D();
  503. }
  504. void TileSet::tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset) {
  505. Transform2D transform = tile_get_shape_transform(p_id, p_shape_id);
  506. transform.set_origin(p_offset);
  507. tile_set_shape_transform(p_id, p_shape_id, transform);
  508. }
  509. Vector2 TileSet::tile_get_shape_offset(int p_id, int p_shape_id) const {
  510. return tile_get_shape_transform(p_id, p_shape_id).get_origin();
  511. }
  512. void TileSet::tile_set_shape_one_way(int p_id, int p_shape_id, const bool p_one_way) {
  513. ERR_FAIL_COND(!tile_map.has(p_id));
  514. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  515. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  516. tile_map[p_id].shapes_data[p_shape_id].one_way_collision = p_one_way;
  517. emit_changed();
  518. }
  519. bool TileSet::tile_get_shape_one_way(int p_id, int p_shape_id) const {
  520. ERR_FAIL_COND_V(!tile_map.has(p_id), false);
  521. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  522. return tile_map[p_id].shapes_data[p_shape_id].one_way_collision;
  523. return false;
  524. }
  525. void TileSet::tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder) {
  526. ERR_FAIL_COND(!tile_map.has(p_id));
  527. tile_map[p_id].occluder = p_light_occluder;
  528. }
  529. Ref<OccluderPolygon2D> TileSet::tile_get_light_occluder(int p_id) const {
  530. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
  531. return tile_map[p_id].occluder;
  532. }
  533. void TileSet::autotile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder, const Vector2 &p_coord) {
  534. ERR_FAIL_COND(!tile_map.has(p_id));
  535. if (p_light_occluder.is_null()) {
  536. if (tile_map[p_id].autotile_data.occluder_map.has(p_coord)) {
  537. tile_map[p_id].autotile_data.occluder_map.erase(p_coord);
  538. }
  539. } else {
  540. tile_map[p_id].autotile_data.occluder_map[p_coord] = p_light_occluder;
  541. }
  542. }
  543. Ref<OccluderPolygon2D> TileSet::autotile_get_light_occluder(int p_id, const Vector2 &p_coord) const {
  544. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
  545. if (!tile_map[p_id].autotile_data.occluder_map.has(p_coord)) {
  546. return Ref<OccluderPolygon2D>();
  547. } else {
  548. return tile_map[p_id].autotile_data.occluder_map[p_coord];
  549. }
  550. }
  551. void TileSet::tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset) {
  552. ERR_FAIL_COND(!tile_map.has(p_id));
  553. tile_map[p_id].navigation_polygon_offset = p_offset;
  554. }
  555. Vector2 TileSet::tile_get_navigation_polygon_offset(int p_id) const {
  556. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  557. return tile_map[p_id].navigation_polygon_offset;
  558. }
  559. void TileSet::tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon) {
  560. ERR_FAIL_COND(!tile_map.has(p_id));
  561. tile_map[p_id].navigation_polygon = p_navigation_polygon;
  562. }
  563. Ref<NavigationPolygon> TileSet::tile_get_navigation_polygon(int p_id) const {
  564. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
  565. return tile_map[p_id].navigation_polygon;
  566. }
  567. const Map<Vector2, Ref<OccluderPolygon2D> > &TileSet::autotile_get_light_oclusion_map(int p_id) const {
  568. static Map<Vector2, Ref<OccluderPolygon2D> > dummy;
  569. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  570. return tile_map[p_id].autotile_data.occluder_map;
  571. }
  572. void TileSet::autotile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon, const Vector2 &p_coord) {
  573. ERR_FAIL_COND(!tile_map.has(p_id));
  574. if (p_navigation_polygon.is_null()) {
  575. if (tile_map[p_id].autotile_data.navpoly_map.has(p_coord)) {
  576. tile_map[p_id].autotile_data.navpoly_map.erase(p_coord);
  577. }
  578. } else {
  579. tile_map[p_id].autotile_data.navpoly_map[p_coord] = p_navigation_polygon;
  580. }
  581. }
  582. Ref<NavigationPolygon> TileSet::autotile_get_navigation_polygon(int p_id, const Vector2 &p_coord) const {
  583. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
  584. if (!tile_map[p_id].autotile_data.navpoly_map.has(p_coord)) {
  585. return Ref<NavigationPolygon>();
  586. } else {
  587. return tile_map[p_id].autotile_data.navpoly_map[p_coord];
  588. }
  589. }
  590. const Map<Vector2, Ref<NavigationPolygon> > &TileSet::autotile_get_navigation_map(int p_id) const {
  591. static Map<Vector2, Ref<NavigationPolygon> > dummy;
  592. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  593. return tile_map[p_id].autotile_data.navpoly_map;
  594. }
  595. void TileSet::tile_set_occluder_offset(int p_id, const Vector2 &p_offset) {
  596. ERR_FAIL_COND(!tile_map.has(p_id));
  597. tile_map[p_id].occluder_offset = p_offset;
  598. }
  599. Vector2 TileSet::tile_get_occluder_offset(int p_id) const {
  600. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  601. return tile_map[p_id].occluder_offset;
  602. }
  603. void TileSet::tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes) {
  604. ERR_FAIL_COND(!tile_map.has(p_id));
  605. tile_map[p_id].shapes_data = p_shapes;
  606. emit_changed();
  607. }
  608. Vector<TileSet::ShapeData> TileSet::tile_get_shapes(int p_id) const {
  609. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector<ShapeData>());
  610. return tile_map[p_id].shapes_data;
  611. }
  612. void TileSet::_tile_set_shapes(int p_id, const Array &p_shapes) {
  613. ERR_FAIL_COND(!tile_map.has(p_id));
  614. Vector<ShapeData> shapes_data;
  615. Transform2D default_transform = tile_get_shape_transform(p_id, 0);
  616. bool default_one_way = tile_get_shape_one_way(p_id, 0);
  617. Vector2 default_autotile_coord = Vector2();
  618. for (int i = 0; i < p_shapes.size(); i++) {
  619. ShapeData s = ShapeData();
  620. if (p_shapes[i].get_type() == Variant::OBJECT) {
  621. Ref<Shape2D> shape = p_shapes[i];
  622. if (shape.is_null()) continue;
  623. s.shape = shape;
  624. s.shape_transform = default_transform;
  625. s.one_way_collision = default_one_way;
  626. s.autotile_coord = default_autotile_coord;
  627. } else if (p_shapes[i].get_type() == Variant::DICTIONARY) {
  628. Dictionary d = p_shapes[i];
  629. if (d.has("shape") && d["shape"].get_type() == Variant::OBJECT)
  630. s.shape = d["shape"];
  631. else
  632. continue;
  633. if (d.has("shape_transform") && d["shape_transform"].get_type() == Variant::TRANSFORM2D)
  634. s.shape_transform = d["shape_transform"];
  635. else if (d.has("shape_offset") && d["shape_offset"].get_type() == Variant::VECTOR2)
  636. s.shape_transform = Transform2D(0, (Vector2)d["shape_offset"]);
  637. else
  638. s.shape_transform = default_transform;
  639. if (d.has("one_way") && d["one_way"].get_type() == Variant::BOOL)
  640. s.one_way_collision = d["one_way"];
  641. else
  642. s.one_way_collision = default_one_way;
  643. if (d.has("autotile_coord") && d["autotile_coord"].get_type() == Variant::VECTOR2)
  644. s.autotile_coord = d["autotile_coord"];
  645. else
  646. s.autotile_coord = default_autotile_coord;
  647. } else {
  648. ERR_EXPLAIN("Expected an array of objects or dictionaries for tile_set_shapes");
  649. ERR_CONTINUE(true);
  650. }
  651. shapes_data.push_back(s);
  652. }
  653. tile_map[p_id].shapes_data = shapes_data;
  654. }
  655. Array TileSet::_tile_get_shapes(int p_id) const {
  656. ERR_FAIL_COND_V(!tile_map.has(p_id), Array());
  657. Array arr;
  658. Vector<ShapeData> data = tile_map[p_id].shapes_data;
  659. for (int i = 0; i < data.size(); i++) {
  660. Dictionary shape_data;
  661. shape_data["shape"] = data[i].shape;
  662. shape_data["shape_transform"] = data[i].shape_transform;
  663. shape_data["one_way"] = data[i].one_way_collision;
  664. shape_data["autotile_coord"] = data[i].autotile_coord;
  665. arr.push_back(shape_data);
  666. }
  667. return arr;
  668. }
  669. Array TileSet::_get_tiles_ids() const {
  670. Array arr;
  671. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  672. arr.push_back(E->key());
  673. }
  674. return arr;
  675. }
  676. void TileSet::get_tile_list(List<int> *p_tiles) const {
  677. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  678. p_tiles->push_back(E->key());
  679. }
  680. }
  681. bool TileSet::has_tile(int p_id) const {
  682. return tile_map.has(p_id);
  683. }
  684. bool TileSet::is_tile_bound(int p_drawn_id, int p_neighbor_id) {
  685. if (p_drawn_id == p_neighbor_id) {
  686. return true;
  687. } else if (get_script_instance() != NULL) {
  688. if (get_script_instance()->has_method("_is_tile_bound")) {
  689. Variant ret = get_script_instance()->call("_is_tile_bound", p_drawn_id, p_neighbor_id);
  690. if (ret.get_type() == Variant::BOOL) {
  691. return ret;
  692. }
  693. }
  694. }
  695. return false;
  696. }
  697. void TileSet::remove_tile(int p_id) {
  698. ERR_FAIL_COND(!tile_map.has(p_id));
  699. tile_map.erase(p_id);
  700. _change_notify("");
  701. emit_changed();
  702. }
  703. int TileSet::get_last_unused_tile_id() const {
  704. if (tile_map.size())
  705. return tile_map.back()->key() + 1;
  706. else
  707. return 0;
  708. }
  709. int TileSet::find_tile_by_name(const String &p_name) const {
  710. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  711. if (p_name == E->get().name)
  712. return E->key();
  713. }
  714. return -1;
  715. }
  716. void TileSet::clear() {
  717. tile_map.clear();
  718. _change_notify("");
  719. emit_changed();
  720. }
  721. void TileSet::_bind_methods() {
  722. ClassDB::bind_method(D_METHOD("create_tile", "id"), &TileSet::create_tile);
  723. ClassDB::bind_method(D_METHOD("autotile_set_bitmask_mode", "id", "mode"), &TileSet::autotile_set_bitmask_mode);
  724. ClassDB::bind_method(D_METHOD("autotile_get_bitmask_mode", "id"), &TileSet::autotile_get_bitmask_mode);
  725. ClassDB::bind_method(D_METHOD("tile_set_name", "id", "name"), &TileSet::tile_set_name);
  726. ClassDB::bind_method(D_METHOD("tile_get_name", "id"), &TileSet::tile_get_name);
  727. ClassDB::bind_method(D_METHOD("tile_set_texture", "id", "texture"), &TileSet::tile_set_texture);
  728. ClassDB::bind_method(D_METHOD("tile_get_texture", "id"), &TileSet::tile_get_texture);
  729. ClassDB::bind_method(D_METHOD("tile_set_normal_map", "id", "normal_map"), &TileSet::tile_set_normal_map);
  730. ClassDB::bind_method(D_METHOD("tile_get_normal_map", "id"), &TileSet::tile_get_normal_map);
  731. ClassDB::bind_method(D_METHOD("tile_set_material", "id", "material"), &TileSet::tile_set_material);
  732. ClassDB::bind_method(D_METHOD("tile_get_material", "id"), &TileSet::tile_get_material);
  733. ClassDB::bind_method(D_METHOD("tile_set_texture_offset", "id", "texture_offset"), &TileSet::tile_set_texture_offset);
  734. ClassDB::bind_method(D_METHOD("tile_get_texture_offset", "id"), &TileSet::tile_get_texture_offset);
  735. ClassDB::bind_method(D_METHOD("tile_set_region", "id", "region"), &TileSet::tile_set_region);
  736. ClassDB::bind_method(D_METHOD("tile_get_region", "id"), &TileSet::tile_get_region);
  737. ClassDB::bind_method(D_METHOD("tile_set_shape", "id", "shape_id", "shape"), &TileSet::tile_set_shape);
  738. ClassDB::bind_method(D_METHOD("tile_get_shape", "id", "shape_id"), &TileSet::tile_get_shape);
  739. ClassDB::bind_method(D_METHOD("tile_set_shape_transform", "id", "shape_id", "shape_transform"), &TileSet::tile_set_shape_transform);
  740. ClassDB::bind_method(D_METHOD("tile_get_shape_transform", "id", "shape_id"), &TileSet::tile_get_shape_transform);
  741. ClassDB::bind_method(D_METHOD("tile_set_shape_one_way", "id", "shape_id", "one_way"), &TileSet::tile_set_shape_one_way);
  742. ClassDB::bind_method(D_METHOD("tile_get_shape_one_way", "id", "shape_id"), &TileSet::tile_get_shape_one_way);
  743. ClassDB::bind_method(D_METHOD("tile_add_shape", "id", "shape", "shape_transform", "one_way", "autotile_coord"), &TileSet::tile_add_shape, DEFVAL(false), DEFVAL(Vector2()));
  744. ClassDB::bind_method(D_METHOD("tile_get_shape_count", "id"), &TileSet::tile_get_shape_count);
  745. ClassDB::bind_method(D_METHOD("tile_set_shapes", "id", "shapes"), &TileSet::_tile_set_shapes);
  746. ClassDB::bind_method(D_METHOD("tile_get_shapes", "id"), &TileSet::_tile_get_shapes);
  747. ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon", "id", "navigation_polygon"), &TileSet::tile_set_navigation_polygon);
  748. ClassDB::bind_method(D_METHOD("tile_get_navigation_polygon", "id"), &TileSet::tile_get_navigation_polygon);
  749. ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon_offset", "id", "navigation_polygon_offset"), &TileSet::tile_set_navigation_polygon_offset);
  750. ClassDB::bind_method(D_METHOD("tile_get_navigation_polygon_offset", "id"), &TileSet::tile_get_navigation_polygon_offset);
  751. ClassDB::bind_method(D_METHOD("tile_set_light_occluder", "id", "light_occluder"), &TileSet::tile_set_light_occluder);
  752. ClassDB::bind_method(D_METHOD("tile_get_light_occluder", "id"), &TileSet::tile_get_light_occluder);
  753. ClassDB::bind_method(D_METHOD("tile_set_occluder_offset", "id", "occluder_offset"), &TileSet::tile_set_occluder_offset);
  754. ClassDB::bind_method(D_METHOD("tile_get_occluder_offset", "id"), &TileSet::tile_get_occluder_offset);
  755. ClassDB::bind_method(D_METHOD("remove_tile", "id"), &TileSet::remove_tile);
  756. ClassDB::bind_method(D_METHOD("clear"), &TileSet::clear);
  757. ClassDB::bind_method(D_METHOD("get_last_unused_tile_id"), &TileSet::get_last_unused_tile_id);
  758. ClassDB::bind_method(D_METHOD("find_tile_by_name", "name"), &TileSet::find_tile_by_name);
  759. ClassDB::bind_method(D_METHOD("get_tiles_ids"), &TileSet::_get_tiles_ids);
  760. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_is_tile_bound", PropertyInfo(Variant::INT, "drawn_id"), PropertyInfo(Variant::INT, "neighbor_id")));
  761. 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")));
  762. BIND_ENUM_CONSTANT(BITMASK_2X2);
  763. BIND_ENUM_CONSTANT(BITMASK_3X3);
  764. BIND_ENUM_CONSTANT(BIND_TOPLEFT);
  765. BIND_ENUM_CONSTANT(BIND_TOP);
  766. BIND_ENUM_CONSTANT(BIND_TOPRIGHT);
  767. BIND_ENUM_CONSTANT(BIND_LEFT);
  768. BIND_ENUM_CONSTANT(BIND_RIGHT);
  769. BIND_ENUM_CONSTANT(BIND_BOTTOMLEFT);
  770. BIND_ENUM_CONSTANT(BIND_BOTTOM);
  771. BIND_ENUM_CONSTANT(BIND_BOTTOMRIGHT);
  772. }
  773. TileSet::TileSet() {
  774. }