navigation.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**************************************************************************/
  2. /* navigation.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.h"
  31. #include "servers/navigation_server.h"
  32. Vector<Vector3> Navigation::get_simple_path(const Vector3 &p_start, const Vector3 &p_end, bool p_optimize) const {
  33. WARN_DEPRECATED_MSG("'Navigation' node and 'Navigation.get_simple_path()' are deprecated and will be removed in a future version. Use 'NavigationServer.map_get_path()' instead.");
  34. return NavigationServer::get_singleton()->map_get_path(map, p_start, p_end, p_optimize, navigation_layers);
  35. }
  36. String Navigation::get_configuration_warning() const {
  37. String warning = Spatial::get_configuration_warning();
  38. if (warning != String()) {
  39. warning += "\n\n";
  40. }
  41. warning += TTR("'Navigation' node and 'Navigation.get_simple_path()' are deprecated and will be removed in a future version. Use 'NavigationServer.map_get_path()' instead.");
  42. return warning;
  43. }
  44. Vector3 Navigation::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision) const {
  45. return NavigationServer::get_singleton()->map_get_closest_point_to_segment(map, p_from, p_to, p_use_collision);
  46. }
  47. Vector3 Navigation::get_closest_point(const Vector3 &p_point) const {
  48. return NavigationServer::get_singleton()->map_get_closest_point(map, p_point);
  49. }
  50. Vector3 Navigation::get_closest_point_normal(const Vector3 &p_point) const {
  51. return NavigationServer::get_singleton()->map_get_closest_point_normal(map, p_point);
  52. }
  53. RID Navigation::get_closest_point_owner(const Vector3 &p_point) const {
  54. return NavigationServer::get_singleton()->map_get_closest_point_owner(map, p_point);
  55. }
  56. void Navigation::set_up_vector(const Vector3 &p_up) {
  57. up = p_up;
  58. NavigationServer::get_singleton()->map_set_up(map, up);
  59. }
  60. Vector3 Navigation::get_up_vector() const {
  61. return up;
  62. }
  63. void Navigation::set_cell_size(float p_cell_size) {
  64. cell_size = p_cell_size;
  65. NavigationServer::get_singleton()->map_set_cell_size(map, cell_size);
  66. }
  67. void Navigation::set_cell_height(float p_cell_height) {
  68. cell_height = p_cell_height;
  69. NavigationServer::get_singleton()->map_set_cell_height(map, cell_height);
  70. }
  71. void Navigation::set_edge_connection_margin(float p_edge_connection_margin) {
  72. edge_connection_margin = p_edge_connection_margin;
  73. NavigationServer::get_singleton()->map_set_edge_connection_margin(map, edge_connection_margin);
  74. }
  75. void Navigation::set_navigation_layers(uint32_t p_navigation_layers) {
  76. navigation_layers = p_navigation_layers;
  77. }
  78. uint32_t Navigation::get_navigation_layers() const {
  79. return navigation_layers;
  80. }
  81. void Navigation::_bind_methods() {
  82. ClassDB::bind_method(D_METHOD("get_rid"), &Navigation::get_rid);
  83. ClassDB::bind_method(D_METHOD("get_simple_path", "start", "end", "optimize"), &Navigation::get_simple_path, DEFVAL(true));
  84. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "start", "end", "use_collision"), &Navigation::get_closest_point_to_segment, DEFVAL(false));
  85. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Navigation::get_closest_point);
  86. ClassDB::bind_method(D_METHOD("get_closest_point_normal", "to_point"), &Navigation::get_closest_point_normal);
  87. ClassDB::bind_method(D_METHOD("get_closest_point_owner", "to_point"), &Navigation::get_closest_point_owner);
  88. ClassDB::bind_method(D_METHOD("set_up_vector", "up"), &Navigation::set_up_vector);
  89. ClassDB::bind_method(D_METHOD("get_up_vector"), &Navigation::get_up_vector);
  90. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &Navigation::set_cell_size);
  91. ClassDB::bind_method(D_METHOD("get_cell_size"), &Navigation::get_cell_size);
  92. ClassDB::bind_method(D_METHOD("set_cell_height", "cell_height"), &Navigation::set_cell_height);
  93. ClassDB::bind_method(D_METHOD("get_cell_height"), &Navigation::get_cell_height);
  94. ClassDB::bind_method(D_METHOD("set_edge_connection_margin", "margin"), &Navigation::set_edge_connection_margin);
  95. ClassDB::bind_method(D_METHOD("get_edge_connection_margin"), &Navigation::get_edge_connection_margin);
  96. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &Navigation::set_navigation_layers);
  97. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &Navigation::get_navigation_layers);
  98. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "up_vector"), "set_up_vector", "get_up_vector");
  99. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_size"), "set_cell_size", "get_cell_size");
  100. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_height"), "set_cell_height", "get_cell_height");
  101. ADD_PROPERTY(PropertyInfo(Variant::REAL, "edge_connection_margin"), "set_edge_connection_margin", "get_edge_connection_margin");
  102. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  103. ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::_RID, "map")));
  104. }
  105. void Navigation::_notification(int p_what) {
  106. switch (p_what) {
  107. case NOTIFICATION_READY: {
  108. NavigationServer::get_singleton()->map_set_active(map, true);
  109. } break;
  110. case NOTIFICATION_EXIT_TREE: {
  111. // FIXME 3.5 with this old navigation node only
  112. // if the node gets deleted this exit causes annoying error prints in debug
  113. // It tries to deactivate a map that itself has sent a free command to the server.
  114. //NavigationServer::get_singleton()->map_set_active(map, false);
  115. } break;
  116. }
  117. }
  118. Navigation::Navigation() {
  119. map = NavigationServer::get_singleton()->map_create();
  120. NavigationServer::get_singleton()->map_set_active(map, true);
  121. NavigationServer::get_singleton()->map_set_up(map, get_up_vector());
  122. NavigationServer::get_singleton()->map_set_cell_size(map, get_cell_size());
  123. NavigationServer::get_singleton()->map_set_cell_height(map, get_cell_height());
  124. NavigationServer::get_singleton()->map_set_edge_connection_margin(map, get_edge_connection_margin());
  125. }
  126. Navigation::~Navigation() {
  127. NavigationServer::get_singleton()->free(map);
  128. }