navigation_2d.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**************************************************************************/
  2. /* navigation_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_2d.h"
  31. #include "servers/navigation_2d_server.h"
  32. void Navigation2D::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("get_rid"), &Navigation2D::get_rid);
  34. ClassDB::bind_method(D_METHOD("get_simple_path", "start", "end", "optimize"), &Navigation2D::get_simple_path, DEFVAL(true));
  35. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Navigation2D::get_closest_point);
  36. ClassDB::bind_method(D_METHOD("get_closest_point_owner", "to_point"), &Navigation2D::get_closest_point_owner);
  37. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &Navigation2D::set_cell_size);
  38. ClassDB::bind_method(D_METHOD("get_cell_size"), &Navigation2D::get_cell_size);
  39. ClassDB::bind_method(D_METHOD("set_edge_connection_margin", "margin"), &Navigation2D::set_edge_connection_margin);
  40. ClassDB::bind_method(D_METHOD("get_edge_connection_margin"), &Navigation2D::get_edge_connection_margin);
  41. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &Navigation2D::set_navigation_layers);
  42. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &Navigation2D::get_navigation_layers);
  43. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_size"), "set_cell_size", "get_cell_size");
  44. ADD_PROPERTY(PropertyInfo(Variant::REAL, "edge_connection_margin"), "set_edge_connection_margin", "get_edge_connection_margin");
  45. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  46. }
  47. void Navigation2D::_notification(int p_what) {
  48. switch (p_what) {
  49. case NOTIFICATION_READY: {
  50. Navigation2DServer::get_singleton()->map_set_active(map, true);
  51. } break;
  52. // FIXME 3.5 with this old navigation 2d node only
  53. // if the node gets deleted this exit causes annoying error prints in debug
  54. // It tries to deactivate a map that itself has sent a free command to the server.
  55. //case NOTIFICATION_EXIT_TREE: {
  56. // Navigation2DServer::get_singleton()->map_set_active(map, false);
  57. //} break;
  58. }
  59. }
  60. void Navigation2D::set_cell_size(float p_cell_size) {
  61. cell_size = p_cell_size;
  62. Navigation2DServer::get_singleton()->map_set_cell_size(map, cell_size);
  63. }
  64. void Navigation2D::set_edge_connection_margin(float p_edge_connection_margin) {
  65. edge_connection_margin = p_edge_connection_margin;
  66. Navigation2DServer::get_singleton()->map_set_edge_connection_margin(map, edge_connection_margin);
  67. }
  68. Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vector2 &p_end, bool p_optimize) const {
  69. WARN_DEPRECATED_MSG("'Navigation2D' node and 'Navigation2D.get_simple_path()' are deprecated and will be removed in a future version. Use 'Navigation2DServer.map_get_path()' instead.");
  70. return Navigation2DServer::get_singleton()->map_get_path(map, p_start, p_end, p_optimize, navigation_layers);
  71. }
  72. String Navigation2D::get_configuration_warning() const {
  73. String warning = Node2D::get_configuration_warning();
  74. if (warning != String()) {
  75. warning += "\n\n";
  76. }
  77. warning += TTR("'Navigation2D' node and 'Navigation2D.get_simple_path()' are deprecated and will be removed in a future version. Use 'Navigation2DServer.map_get_path()' instead.");
  78. return warning;
  79. }
  80. Vector2 Navigation2D::get_closest_point(const Vector2 &p_point) const {
  81. return Navigation2DServer::get_singleton()->map_get_closest_point(map, p_point);
  82. }
  83. RID Navigation2D::get_closest_point_owner(const Vector2 &p_point) const {
  84. return Navigation2DServer::get_singleton()->map_get_closest_point_owner(map, p_point);
  85. }
  86. void Navigation2D::set_navigation_layers(uint32_t p_navigation_layers) {
  87. navigation_layers = p_navigation_layers;
  88. }
  89. uint32_t Navigation2D::get_navigation_layers() const {
  90. return navigation_layers;
  91. }
  92. Navigation2D::Navigation2D() {
  93. map = Navigation2DServer::get_singleton()->map_create();
  94. Navigation2DServer::get_singleton()->map_set_active(map, true);
  95. Navigation2DServer::get_singleton()->map_set_cell_size(map, get_cell_size());
  96. Navigation2DServer::get_singleton()->map_set_edge_connection_margin(map, get_edge_connection_margin());
  97. }
  98. Navigation2D::~Navigation2D() {
  99. Navigation2DServer::get_singleton()->free(map);
  100. }