navigation_region_2d.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /**************************************************************************/
  2. /* navigation_region_2d.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 "navigation_region_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "scene/2d/navigation_obstacle_2d.h"
  33. #include "scene/resources/world_2d.h"
  34. #include "servers/navigation_server_2d.h"
  35. RID NavigationRegion2D::get_rid() const {
  36. return region;
  37. }
  38. void NavigationRegion2D::set_enabled(bool p_enabled) {
  39. if (enabled == p_enabled) {
  40. return;
  41. }
  42. enabled = p_enabled;
  43. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  44. #ifdef DEBUG_ENABLED
  45. if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_navigation_enabled()) {
  46. queue_redraw();
  47. }
  48. #endif // DEBUG_ENABLED
  49. }
  50. bool NavigationRegion2D::is_enabled() const {
  51. return enabled;
  52. }
  53. void NavigationRegion2D::set_use_edge_connections(bool p_enabled) {
  54. if (use_edge_connections == p_enabled) {
  55. return;
  56. }
  57. use_edge_connections = p_enabled;
  58. NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  59. }
  60. bool NavigationRegion2D::get_use_edge_connections() const {
  61. return use_edge_connections;
  62. }
  63. void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
  64. if (navigation_layers == p_navigation_layers) {
  65. return;
  66. }
  67. navigation_layers = p_navigation_layers;
  68. NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  69. }
  70. uint32_t NavigationRegion2D::get_navigation_layers() const {
  71. return navigation_layers;
  72. }
  73. void NavigationRegion2D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  74. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  75. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  76. uint32_t _navigation_layers = get_navigation_layers();
  77. if (p_value) {
  78. _navigation_layers |= 1 << (p_layer_number - 1);
  79. } else {
  80. _navigation_layers &= ~(1 << (p_layer_number - 1));
  81. }
  82. set_navigation_layers(_navigation_layers);
  83. }
  84. bool NavigationRegion2D::get_navigation_layer_value(int p_layer_number) const {
  85. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  86. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  87. return get_navigation_layers() & (1 << (p_layer_number - 1));
  88. }
  89. void NavigationRegion2D::set_enter_cost(real_t p_enter_cost) {
  90. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  91. if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
  92. return;
  93. }
  94. enter_cost = p_enter_cost;
  95. NavigationServer2D::get_singleton()->region_set_enter_cost(region, enter_cost);
  96. }
  97. real_t NavigationRegion2D::get_enter_cost() const {
  98. return enter_cost;
  99. }
  100. void NavigationRegion2D::set_travel_cost(real_t p_travel_cost) {
  101. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  102. if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
  103. return;
  104. }
  105. travel_cost = p_travel_cost;
  106. NavigationServer2D::get_singleton()->region_set_travel_cost(region, travel_cost);
  107. }
  108. real_t NavigationRegion2D::get_travel_cost() const {
  109. return travel_cost;
  110. }
  111. RID NavigationRegion2D::get_region_rid() const {
  112. return get_rid();
  113. }
  114. #ifdef TOOLS_ENABLED
  115. Rect2 NavigationRegion2D::_edit_get_rect() const {
  116. return navigation_polygon.is_valid() ? navigation_polygon->_edit_get_rect() : Rect2();
  117. }
  118. bool NavigationRegion2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  119. return navigation_polygon.is_valid() ? navigation_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  120. }
  121. #endif
  122. void NavigationRegion2D::_notification(int p_what) {
  123. switch (p_what) {
  124. case NOTIFICATION_ENTER_TREE: {
  125. _region_enter_navigation_map();
  126. } break;
  127. case NOTIFICATION_TRANSFORM_CHANGED: {
  128. set_physics_process_internal(true);
  129. } break;
  130. case NOTIFICATION_EXIT_TREE: {
  131. _region_exit_navigation_map();
  132. } break;
  133. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  134. set_physics_process_internal(false);
  135. _region_update_transform();
  136. } break;
  137. case NOTIFICATION_DRAW: {
  138. #ifdef DEBUG_ENABLED
  139. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled()) && navigation_polygon.is_valid()) {
  140. _update_debug_mesh();
  141. _update_debug_edge_connections_mesh();
  142. }
  143. #endif // DEBUG_ENABLED
  144. } break;
  145. }
  146. }
  147. void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navigation_polygon) {
  148. if (navigation_polygon.is_valid()) {
  149. navigation_polygon->disconnect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
  150. }
  151. navigation_polygon = p_navigation_polygon;
  152. NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, p_navigation_polygon);
  153. if (navigation_polygon.is_valid()) {
  154. navigation_polygon->connect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
  155. }
  156. _navigation_polygon_changed();
  157. update_configuration_warnings();
  158. }
  159. Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {
  160. return navigation_polygon;
  161. }
  162. void NavigationRegion2D::set_navigation_map(RID p_navigation_map) {
  163. if (map_override == p_navigation_map) {
  164. return;
  165. }
  166. map_override = p_navigation_map;
  167. NavigationServer2D::get_singleton()->region_set_map(region, map_override);
  168. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  169. if (constrain_avoidance_obstacles[i].is_valid()) {
  170. NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map_override);
  171. }
  172. }
  173. }
  174. RID NavigationRegion2D::get_navigation_map() const {
  175. if (map_override.is_valid()) {
  176. return map_override;
  177. } else if (is_inside_tree()) {
  178. return get_world_2d()->get_navigation_map();
  179. }
  180. return RID();
  181. }
  182. void NavigationRegion2D::bake_navigation_polygon(bool p_on_thread) {
  183. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");
  184. ERR_FAIL_COND_MSG(!navigation_polygon.is_valid(), "Baking the navigation polygon requires a valid `NavigationPolygon` resource.");
  185. Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;
  186. source_geometry_data.instantiate();
  187. NavigationServer2D::get_singleton()->parse_source_geometry_data(navigation_polygon, source_geometry_data, this);
  188. if (p_on_thread) {
  189. NavigationServer2D::get_singleton()->bake_from_source_geometry_data_async(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished).bind(navigation_polygon));
  190. } else {
  191. NavigationServer2D::get_singleton()->bake_from_source_geometry_data(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished).bind(navigation_polygon));
  192. }
  193. }
  194. void NavigationRegion2D::_bake_finished(Ref<NavigationPolygon> p_navigation_polygon) {
  195. if (!Thread::is_main_thread()) {
  196. call_deferred(SNAME("_bake_finished"), p_navigation_polygon);
  197. return;
  198. }
  199. set_navigation_polygon(p_navigation_polygon);
  200. emit_signal(SNAME("bake_finished"));
  201. }
  202. void NavigationRegion2D::_navigation_polygon_changed() {
  203. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
  204. queue_redraw();
  205. }
  206. if (navigation_polygon.is_valid()) {
  207. NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon);
  208. }
  209. _update_avoidance_constrain();
  210. }
  211. #ifdef DEBUG_ENABLED
  212. void NavigationRegion2D::_navigation_map_changed(RID p_map) {
  213. if (is_inside_tree() && get_world_2d()->get_navigation_map() == p_map) {
  214. queue_redraw();
  215. }
  216. }
  217. #endif // DEBUG_ENABLED
  218. PackedStringArray NavigationRegion2D::get_configuration_warnings() const {
  219. PackedStringArray warnings = Node2D::get_configuration_warnings();
  220. if (is_visible_in_tree() && is_inside_tree()) {
  221. if (!navigation_polygon.is_valid()) {
  222. warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work. Please set a property or draw a polygon."));
  223. }
  224. }
  225. return warnings;
  226. }
  227. void NavigationRegion2D::_bind_methods() {
  228. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion2D::get_rid);
  229. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navigation_polygon"), &NavigationRegion2D::set_navigation_polygon);
  230. ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);
  231. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
  232. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);
  233. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion2D::set_navigation_map);
  234. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion2D::get_navigation_map);
  235. ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion2D::set_use_edge_connections);
  236. ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion2D::get_use_edge_connections);
  237. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
  238. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);
  239. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion2D::set_navigation_layer_value);
  240. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion2D::get_navigation_layer_value);
  241. ClassDB::bind_method(D_METHOD("set_constrain_avoidance", "enabled"), &NavigationRegion2D::set_constrain_avoidance);
  242. ClassDB::bind_method(D_METHOD("get_constrain_avoidance"), &NavigationRegion2D::get_constrain_avoidance);
  243. ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationRegion2D::set_avoidance_layers);
  244. ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationRegion2D::get_avoidance_layers);
  245. ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationRegion2D::set_avoidance_layer_value);
  246. ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationRegion2D::get_avoidance_layer_value);
  247. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);
  248. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);
  249. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);
  250. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);
  251. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
  252. ClassDB::bind_method(D_METHOD("bake_navigation_polygon", "on_thread"), &NavigationRegion2D::bake_navigation_polygon, DEFVAL(true));
  253. ClassDB::bind_method(D_METHOD("_navigation_polygon_changed"), &NavigationRegion2D::_navigation_polygon_changed);
  254. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_polygon", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
  255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
  257. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  258. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  259. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  260. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "constrain_avoidance"), "set_constrain_avoidance", "get_constrain_avoidance");
  261. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  262. ADD_SIGNAL(MethodInfo("navigation_polygon_changed"));
  263. ADD_SIGNAL(MethodInfo("bake_finished"));
  264. }
  265. #ifndef DISABLE_DEPRECATED
  266. // Compatibility with earlier 4.0 betas.
  267. bool NavigationRegion2D::_set(const StringName &p_name, const Variant &p_value) {
  268. if (p_name == "navpoly") {
  269. set_navigation_polygon(p_value);
  270. return true;
  271. }
  272. return false;
  273. }
  274. bool NavigationRegion2D::_get(const StringName &p_name, Variant &r_ret) const {
  275. if (p_name == "navpoly") {
  276. r_ret = get_navigation_polygon();
  277. return true;
  278. }
  279. return false;
  280. }
  281. #endif // DISABLE_DEPRECATED
  282. NavigationRegion2D::NavigationRegion2D() {
  283. set_notify_transform(true);
  284. set_hide_clip_children(true);
  285. region = NavigationServer2D::get_singleton()->region_create();
  286. NavigationServer2D::get_singleton()->region_set_owner_id(region, get_instance_id());
  287. NavigationServer2D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  288. NavigationServer2D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  289. NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  290. NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  291. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  292. #ifdef DEBUG_ENABLED
  293. NavigationServer2D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
  294. NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
  295. #endif // DEBUG_ENABLED
  296. }
  297. NavigationRegion2D::~NavigationRegion2D() {
  298. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  299. NavigationServer2D::get_singleton()->free(region);
  300. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  301. if (constrain_avoidance_obstacles[i].is_valid()) {
  302. NavigationServer2D::get_singleton()->free(constrain_avoidance_obstacles[i]);
  303. }
  304. }
  305. constrain_avoidance_obstacles.clear();
  306. #ifdef DEBUG_ENABLED
  307. NavigationServer2D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
  308. NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
  309. #endif // DEBUG_ENABLED
  310. }
  311. void NavigationRegion2D::_update_avoidance_constrain() {
  312. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  313. if (constrain_avoidance_obstacles[i].is_valid()) {
  314. NavigationServer2D::get_singleton()->free(constrain_avoidance_obstacles[i]);
  315. constrain_avoidance_obstacles[i] = RID();
  316. }
  317. }
  318. constrain_avoidance_obstacles.clear();
  319. if (!constrain_avoidance) {
  320. return;
  321. }
  322. if (get_navigation_polygon() == nullptr) {
  323. return;
  324. }
  325. Ref<NavigationPolygon> _navpoly = get_navigation_polygon();
  326. int _outline_count = _navpoly->get_outline_count();
  327. if (_outline_count == 0) {
  328. return;
  329. }
  330. for (int outline_index(0); outline_index < _outline_count; outline_index++) {
  331. const Vector<Vector2> &_outline = _navpoly->get_outline(outline_index);
  332. const int outline_size = _outline.size();
  333. if (outline_size < 3) {
  334. ERR_FAIL_COND_MSG(_outline.size() < 3, "NavigationPolygon outline needs to have at least 3 vertex to create avoidance obstacles to constrain avoidance agent's");
  335. continue;
  336. }
  337. RID obstacle_rid = NavigationServer2D::get_singleton()->obstacle_create();
  338. constrain_avoidance_obstacles.push_back(obstacle_rid);
  339. Vector<Vector2> new_obstacle_outline;
  340. if (outline_index == 0) {
  341. for (int i(0); i < outline_size; i++) {
  342. new_obstacle_outline.push_back(_outline[outline_size - i - 1]);
  343. }
  344. ERR_FAIL_COND_MSG(Geometry2D::is_polygon_clockwise(_outline), "Outer most outline needs to be clockwise to push avoidance agent inside");
  345. } else {
  346. for (int i(0); i < outline_size; i++) {
  347. new_obstacle_outline.push_back(_outline[i]);
  348. }
  349. }
  350. new_obstacle_outline.resize(outline_size);
  351. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle_rid, new_obstacle_outline);
  352. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle_rid, avoidance_layers);
  353. if (is_inside_tree()) {
  354. if (map_override.is_valid()) {
  355. NavigationServer2D::get_singleton()->obstacle_set_map(obstacle_rid, map_override);
  356. } else {
  357. NavigationServer2D::get_singleton()->obstacle_set_map(obstacle_rid, get_world_2d()->get_navigation_map());
  358. }
  359. NavigationServer2D::get_singleton()->obstacle_set_position(obstacle_rid, get_global_position());
  360. }
  361. }
  362. constrain_avoidance_obstacles.resize(_outline_count);
  363. }
  364. void NavigationRegion2D::set_constrain_avoidance(bool p_enabled) {
  365. constrain_avoidance = p_enabled;
  366. _update_avoidance_constrain();
  367. notify_property_list_changed();
  368. }
  369. bool NavigationRegion2D::get_constrain_avoidance() const {
  370. return constrain_avoidance;
  371. }
  372. void NavigationRegion2D::_validate_property(PropertyInfo &p_property) const {
  373. if (p_property.name == "avoidance_layers") {
  374. if (!constrain_avoidance) {
  375. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  376. }
  377. }
  378. }
  379. void NavigationRegion2D::set_avoidance_layers(uint32_t p_layers) {
  380. avoidance_layers = p_layers;
  381. if (constrain_avoidance_obstacles.size() > 0) {
  382. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  383. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(constrain_avoidance_obstacles[i], avoidance_layers);
  384. }
  385. }
  386. }
  387. uint32_t NavigationRegion2D::get_avoidance_layers() const {
  388. return avoidance_layers;
  389. }
  390. void NavigationRegion2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  391. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  392. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  393. uint32_t avoidance_layers_new = get_avoidance_layers();
  394. if (p_value) {
  395. avoidance_layers_new |= 1 << (p_layer_number - 1);
  396. } else {
  397. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  398. }
  399. set_avoidance_layers(avoidance_layers_new);
  400. }
  401. bool NavigationRegion2D::get_avoidance_layer_value(int p_layer_number) const {
  402. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  403. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  404. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  405. }
  406. void NavigationRegion2D::_region_enter_navigation_map() {
  407. if (!is_inside_tree()) {
  408. return;
  409. }
  410. if (map_override.is_valid()) {
  411. NavigationServer2D::get_singleton()->region_set_map(region, map_override);
  412. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  413. if (constrain_avoidance_obstacles[i].is_valid()) {
  414. NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map_override);
  415. }
  416. }
  417. } else {
  418. NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  419. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  420. if (constrain_avoidance_obstacles[i].is_valid()) {
  421. NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], get_world_2d()->get_navigation_map());
  422. }
  423. }
  424. }
  425. current_global_transform = get_global_transform();
  426. NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
  427. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  428. if (constrain_avoidance_obstacles[i].is_valid()) {
  429. NavigationServer2D::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
  430. }
  431. }
  432. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  433. queue_redraw();
  434. }
  435. void NavigationRegion2D::_region_exit_navigation_map() {
  436. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  437. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  438. if (constrain_avoidance_obstacles[i].is_valid()) {
  439. NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], RID());
  440. }
  441. }
  442. }
  443. void NavigationRegion2D::_region_update_transform() {
  444. if (!is_inside_tree()) {
  445. return;
  446. }
  447. Transform2D new_global_transform = get_global_transform();
  448. if (current_global_transform != new_global_transform) {
  449. current_global_transform = new_global_transform;
  450. NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
  451. for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
  452. if (constrain_avoidance_obstacles[i].is_valid()) {
  453. NavigationServer2D::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
  454. }
  455. }
  456. }
  457. queue_redraw();
  458. }
  459. #ifdef DEBUG_ENABLED
  460. void NavigationRegion2D::_update_debug_mesh() {
  461. Vector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices();
  462. if (navigation_polygon_vertices.size() < 3) {
  463. return;
  464. }
  465. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  466. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  467. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  468. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  469. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  470. if (!enabled) {
  471. debug_face_color = ns2d->get_debug_navigation_geometry_face_disabled_color();
  472. debug_edge_color = ns2d->get_debug_navigation_geometry_edge_disabled_color();
  473. }
  474. RandomPCG rand;
  475. for (int i = 0; i < navigation_polygon->get_polygon_count(); i++) {
  476. // An array of vertices for this polygon.
  477. Vector<int> polygon = navigation_polygon->get_polygon(i);
  478. Vector<Vector2> debug_polygon_vertices;
  479. debug_polygon_vertices.resize(polygon.size());
  480. for (int j = 0; j < polygon.size(); j++) {
  481. ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
  482. debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]];
  483. }
  484. // Generate the polygon color, slightly randomly modified from the settings one.
  485. Color random_variation_color = debug_face_color;
  486. if (enabled_geometry_face_random_color) {
  487. random_variation_color.set_hsv(
  488. debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1,
  489. debug_face_color.get_s(),
  490. debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  491. }
  492. random_variation_color.a = debug_face_color.a;
  493. Vector<Color> debug_face_colors;
  494. debug_face_colors.push_back(random_variation_color);
  495. RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), debug_polygon_vertices, debug_face_colors);
  496. if (enabled_edge_lines) {
  497. Vector<Color> debug_edge_colors;
  498. debug_edge_colors.push_back(debug_edge_color);
  499. debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline.
  500. RS::get_singleton()->canvas_item_add_polyline(get_canvas_item(), debug_polygon_vertices, debug_edge_colors);
  501. }
  502. }
  503. }
  504. #endif // DEBUG_ENABLED
  505. #ifdef DEBUG_ENABLED
  506. void NavigationRegion2D::_update_debug_edge_connections_mesh() {
  507. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  508. bool enable_edge_connections = use_edge_connections && ns2d->get_debug_navigation_enable_edge_connections() && ns2d->map_get_use_edge_connections(get_world_2d()->get_navigation_map());
  509. if (enable_edge_connections) {
  510. Color debug_edge_connection_color = ns2d->get_debug_navigation_edge_connection_color();
  511. // Draw the region edge connections.
  512. Transform2D xform = get_global_transform();
  513. real_t radius = ns2d->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0;
  514. for (int i = 0; i < ns2d->region_get_connections_count(region); i++) {
  515. // Two main points
  516. Vector2 a = ns2d->region_get_connection_pathway_start(region, i);
  517. a = xform.affine_inverse().xform(a);
  518. Vector2 b = ns2d->region_get_connection_pathway_end(region, i);
  519. b = xform.affine_inverse().xform(b);
  520. draw_line(a, b, debug_edge_connection_color);
  521. // Draw a circle to illustrate the margins.
  522. real_t angle = a.angle_to_point(b);
  523. draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, debug_edge_connection_color);
  524. draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, debug_edge_connection_color);
  525. }
  526. }
  527. }
  528. #endif // DEBUG_ENABLED