nav_link_2d.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**************************************************************************/
  2. /* nav_link_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 "nav_link_2d.h"
  31. #include "nav_map_2d.h"
  32. void NavLink2D::set_map(NavMap2D *p_map) {
  33. if (map == p_map) {
  34. return;
  35. }
  36. cancel_sync_request();
  37. if (map) {
  38. map->remove_link(this);
  39. }
  40. map = p_map;
  41. link_dirty = true;
  42. if (map) {
  43. map->add_link(this);
  44. request_sync();
  45. }
  46. }
  47. void NavLink2D::set_enabled(bool p_enabled) {
  48. if (enabled == p_enabled) {
  49. return;
  50. }
  51. enabled = p_enabled;
  52. // TODO: This should not require a full rebuild as the link has not really changed.
  53. link_dirty = true;
  54. request_sync();
  55. }
  56. void NavLink2D::set_bidirectional(bool p_bidirectional) {
  57. if (bidirectional == p_bidirectional) {
  58. return;
  59. }
  60. bidirectional = p_bidirectional;
  61. link_dirty = true;
  62. request_sync();
  63. }
  64. void NavLink2D::set_start_position(const Vector2 &p_position) {
  65. if (start_position == p_position) {
  66. return;
  67. }
  68. start_position = p_position;
  69. link_dirty = true;
  70. request_sync();
  71. }
  72. void NavLink2D::set_end_position(const Vector2 &p_position) {
  73. if (end_position == p_position) {
  74. return;
  75. }
  76. end_position = p_position;
  77. link_dirty = true;
  78. request_sync();
  79. }
  80. void NavLink2D::set_navigation_layers(uint32_t p_navigation_layers) {
  81. if (navigation_layers == p_navigation_layers) {
  82. return;
  83. }
  84. navigation_layers = p_navigation_layers;
  85. link_dirty = true;
  86. request_sync();
  87. }
  88. void NavLink2D::set_enter_cost(real_t p_enter_cost) {
  89. real_t new_enter_cost = MAX(p_enter_cost, 0.0);
  90. if (enter_cost == new_enter_cost) {
  91. return;
  92. }
  93. enter_cost = new_enter_cost;
  94. link_dirty = true;
  95. request_sync();
  96. }
  97. void NavLink2D::set_travel_cost(real_t p_travel_cost) {
  98. real_t new_travel_cost = MAX(p_travel_cost, 0.0);
  99. if (travel_cost == new_travel_cost) {
  100. return;
  101. }
  102. travel_cost = new_travel_cost;
  103. link_dirty = true;
  104. request_sync();
  105. }
  106. void NavLink2D::set_owner_id(ObjectID p_owner_id) {
  107. if (owner_id == p_owner_id) {
  108. return;
  109. }
  110. owner_id = p_owner_id;
  111. link_dirty = true;
  112. request_sync();
  113. }
  114. bool NavLink2D::is_dirty() const {
  115. return link_dirty;
  116. }
  117. void NavLink2D::sync() {
  118. link_dirty = false;
  119. }
  120. void NavLink2D::request_sync() {
  121. if (map && !sync_dirty_request_list_element.in_list()) {
  122. map->add_link_sync_dirty_request(&sync_dirty_request_list_element);
  123. }
  124. }
  125. void NavLink2D::cancel_sync_request() {
  126. if (map && sync_dirty_request_list_element.in_list()) {
  127. map->remove_link_sync_dirty_request(&sync_dirty_request_list_element);
  128. }
  129. }
  130. NavLink2D::NavLink2D() :
  131. sync_dirty_request_list_element(this) {
  132. type = NavigationUtilities::PathSegmentType::PATH_SEGMENT_TYPE_LINK;
  133. }
  134. NavLink2D::~NavLink2D() {
  135. cancel_sync_request();
  136. }
  137. void NavLink2D::get_iteration_update(NavLinkIteration2D &r_iteration) {
  138. r_iteration.navigation_layers = get_navigation_layers();
  139. r_iteration.enter_cost = get_enter_cost();
  140. r_iteration.travel_cost = get_travel_cost();
  141. r_iteration.owner_object_id = get_owner_id();
  142. r_iteration.owner_type = get_type();
  143. r_iteration.owner_rid = get_self();
  144. r_iteration.enabled = get_enabled();
  145. r_iteration.start_position = get_start_position();
  146. r_iteration.end_position = get_end_position();
  147. r_iteration.bidirectional = is_bidirectional();
  148. }