nav_obstacle.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**************************************************************************/
  2. /* nav_obstacle.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_obstacle.h"
  31. #include "nav_agent.h"
  32. #include "nav_map.h"
  33. NavObstacle::NavObstacle() {}
  34. NavObstacle::~NavObstacle() {}
  35. void NavObstacle::set_agent(NavAgent *p_agent) {
  36. if (agent == p_agent) {
  37. return;
  38. }
  39. agent = p_agent;
  40. internal_update_agent();
  41. }
  42. void NavObstacle::set_avoidance_enabled(bool p_enabled) {
  43. if (avoidance_enabled == p_enabled) {
  44. return;
  45. }
  46. avoidance_enabled = p_enabled;
  47. obstacle_dirty = true;
  48. internal_update_agent();
  49. }
  50. void NavObstacle::set_use_3d_avoidance(bool p_enabled) {
  51. if (use_3d_avoidance == p_enabled) {
  52. return;
  53. }
  54. use_3d_avoidance = p_enabled;
  55. obstacle_dirty = true;
  56. if (agent) {
  57. agent->set_use_3d_avoidance(use_3d_avoidance);
  58. }
  59. }
  60. void NavObstacle::set_map(NavMap *p_map) {
  61. if (map == p_map) {
  62. return;
  63. }
  64. if (map) {
  65. map->remove_obstacle(this);
  66. if (agent) {
  67. agent->set_map(nullptr);
  68. }
  69. }
  70. map = p_map;
  71. obstacle_dirty = true;
  72. if (map) {
  73. map->add_obstacle(this);
  74. internal_update_agent();
  75. }
  76. }
  77. void NavObstacle::set_position(const Vector3 p_position) {
  78. if (position == p_position) {
  79. return;
  80. }
  81. position = p_position;
  82. obstacle_dirty = true;
  83. if (agent) {
  84. agent->set_position(position);
  85. }
  86. }
  87. void NavObstacle::set_radius(real_t p_radius) {
  88. if (radius == p_radius) {
  89. return;
  90. }
  91. radius = p_radius;
  92. if (agent) {
  93. agent->set_radius(radius);
  94. }
  95. }
  96. void NavObstacle::set_height(const real_t p_height) {
  97. if (height == p_height) {
  98. return;
  99. }
  100. height = p_height;
  101. obstacle_dirty = true;
  102. if (agent) {
  103. agent->set_height(height);
  104. }
  105. }
  106. void NavObstacle::set_velocity(const Vector3 p_velocity) {
  107. velocity = p_velocity;
  108. if (agent) {
  109. agent->set_velocity(velocity);
  110. }
  111. }
  112. void NavObstacle::set_vertices(const Vector<Vector3> &p_vertices) {
  113. if (vertices == p_vertices) {
  114. return;
  115. }
  116. vertices = p_vertices;
  117. obstacle_dirty = true;
  118. }
  119. bool NavObstacle::is_map_changed() {
  120. if (map) {
  121. bool is_changed = map->get_iteration_id() != last_map_iteration_id;
  122. last_map_iteration_id = map->get_iteration_id();
  123. return is_changed;
  124. } else {
  125. return false;
  126. }
  127. }
  128. void NavObstacle::set_avoidance_layers(uint32_t p_layers) {
  129. if (avoidance_layers == p_layers) {
  130. return;
  131. }
  132. avoidance_layers = p_layers;
  133. obstacle_dirty = true;
  134. if (agent) {
  135. agent->set_avoidance_layers(avoidance_layers);
  136. }
  137. }
  138. bool NavObstacle::check_dirty() {
  139. const bool was_dirty = obstacle_dirty;
  140. obstacle_dirty = false;
  141. return was_dirty;
  142. }
  143. void NavObstacle::internal_update_agent() {
  144. if (agent) {
  145. agent->set_neighbor_distance(0.0);
  146. agent->set_max_neighbors(0.0);
  147. agent->set_time_horizon_agents(0.0);
  148. agent->set_time_horizon_obstacles(0.0);
  149. agent->set_avoidance_mask(0.0);
  150. agent->set_neighbor_distance(0.0);
  151. agent->set_avoidance_priority(1.0);
  152. agent->set_map(map);
  153. agent->set_paused(paused);
  154. agent->set_radius(radius);
  155. agent->set_height(height);
  156. agent->set_position(position);
  157. agent->set_avoidance_layers(avoidance_layers);
  158. agent->set_avoidance_enabled(avoidance_enabled);
  159. agent->set_use_3d_avoidance(use_3d_avoidance);
  160. }
  161. }
  162. void NavObstacle::set_paused(bool p_paused) {
  163. if (paused == p_paused) {
  164. return;
  165. }
  166. paused = p_paused;
  167. if (map) {
  168. if (paused) {
  169. map->remove_obstacle(this);
  170. } else {
  171. map->add_obstacle(this);
  172. }
  173. }
  174. internal_update_agent();
  175. }
  176. bool NavObstacle::get_paused() const {
  177. return paused;
  178. }