nav_obstacle.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. void NavObstacle::set_agent(NavAgent *p_agent) {
  34. if (agent == p_agent) {
  35. return;
  36. }
  37. agent = p_agent;
  38. internal_update_agent();
  39. request_sync();
  40. }
  41. void NavObstacle::set_avoidance_enabled(bool p_enabled) {
  42. if (avoidance_enabled == p_enabled) {
  43. return;
  44. }
  45. avoidance_enabled = p_enabled;
  46. obstacle_dirty = true;
  47. internal_update_agent();
  48. request_sync();
  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. request_sync();
  60. }
  61. void NavObstacle::set_map(NavMap *p_map) {
  62. if (map == p_map) {
  63. return;
  64. }
  65. cancel_sync_request();
  66. if (map) {
  67. map->remove_obstacle(this);
  68. if (agent) {
  69. agent->set_map(nullptr);
  70. }
  71. }
  72. map = p_map;
  73. obstacle_dirty = true;
  74. if (map) {
  75. map->add_obstacle(this);
  76. internal_update_agent();
  77. request_sync();
  78. }
  79. }
  80. void NavObstacle::set_position(const Vector3 p_position) {
  81. if (position == p_position) {
  82. return;
  83. }
  84. position = p_position;
  85. obstacle_dirty = true;
  86. if (agent) {
  87. agent->set_position(position);
  88. }
  89. request_sync();
  90. }
  91. void NavObstacle::set_radius(real_t p_radius) {
  92. if (radius == p_radius) {
  93. return;
  94. }
  95. radius = p_radius;
  96. if (agent) {
  97. agent->set_radius(radius);
  98. }
  99. }
  100. void NavObstacle::set_height(const real_t p_height) {
  101. if (height == p_height) {
  102. return;
  103. }
  104. height = p_height;
  105. obstacle_dirty = true;
  106. if (agent) {
  107. agent->set_height(height);
  108. }
  109. request_sync();
  110. }
  111. void NavObstacle::set_velocity(const Vector3 p_velocity) {
  112. velocity = p_velocity;
  113. if (agent) {
  114. agent->set_velocity(velocity);
  115. }
  116. }
  117. void NavObstacle::set_vertices(const Vector<Vector3> &p_vertices) {
  118. if (vertices == p_vertices) {
  119. return;
  120. }
  121. vertices = p_vertices;
  122. obstacle_dirty = true;
  123. request_sync();
  124. }
  125. bool NavObstacle::is_map_changed() {
  126. if (map) {
  127. bool is_changed = map->get_iteration_id() != last_map_iteration_id;
  128. last_map_iteration_id = map->get_iteration_id();
  129. return is_changed;
  130. } else {
  131. return false;
  132. }
  133. }
  134. void NavObstacle::set_avoidance_layers(uint32_t p_layers) {
  135. if (avoidance_layers == p_layers) {
  136. return;
  137. }
  138. avoidance_layers = p_layers;
  139. obstacle_dirty = true;
  140. if (agent) {
  141. agent->set_avoidance_layers(avoidance_layers);
  142. }
  143. request_sync();
  144. }
  145. bool NavObstacle::is_dirty() const {
  146. return obstacle_dirty;
  147. }
  148. void NavObstacle::sync() {
  149. obstacle_dirty = false;
  150. }
  151. void NavObstacle::internal_update_agent() {
  152. if (agent) {
  153. agent->set_neighbor_distance(0.0);
  154. agent->set_max_neighbors(0.0);
  155. agent->set_time_horizon_agents(0.0);
  156. agent->set_time_horizon_obstacles(0.0);
  157. agent->set_avoidance_mask(0.0);
  158. agent->set_neighbor_distance(0.0);
  159. agent->set_avoidance_priority(1.0);
  160. agent->set_map(map);
  161. agent->set_paused(paused);
  162. agent->set_radius(radius);
  163. agent->set_height(height);
  164. agent->set_position(position);
  165. agent->set_avoidance_layers(avoidance_layers);
  166. agent->set_avoidance_enabled(avoidance_enabled);
  167. agent->set_use_3d_avoidance(use_3d_avoidance);
  168. }
  169. }
  170. void NavObstacle::set_paused(bool p_paused) {
  171. if (paused == p_paused) {
  172. return;
  173. }
  174. paused = p_paused;
  175. if (map) {
  176. if (paused) {
  177. map->remove_obstacle(this);
  178. } else {
  179. map->add_obstacle(this);
  180. }
  181. }
  182. internal_update_agent();
  183. }
  184. bool NavObstacle::get_paused() const {
  185. return paused;
  186. }
  187. void NavObstacle::request_sync() {
  188. if (map && !sync_dirty_request_list_element.in_list()) {
  189. map->add_obstacle_sync_dirty_request(&sync_dirty_request_list_element);
  190. }
  191. }
  192. void NavObstacle::cancel_sync_request() {
  193. if (map && sync_dirty_request_list_element.in_list()) {
  194. map->remove_obstacle_sync_dirty_request(&sync_dirty_request_list_element);
  195. }
  196. }
  197. NavObstacle::NavObstacle() :
  198. sync_dirty_request_list_element(this) {
  199. }
  200. NavObstacle::~NavObstacle() {
  201. cancel_sync_request();
  202. }