nav_agent_2d.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**************************************************************************/
  2. /* nav_agent_2d.h */
  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. #pragma once
  31. #include "nav_rid_2d.h"
  32. #include "core/object/class_db.h"
  33. #include "core/templates/self_list.h"
  34. #include <Agent2d.h>
  35. class NavMap2D;
  36. class NavAgent2D : public NavRid2D {
  37. Vector2 position;
  38. Vector2 target_position;
  39. Vector2 velocity;
  40. Vector2 velocity_forced;
  41. real_t radius = 1.0;
  42. real_t max_speed = 1.0;
  43. real_t time_horizon_agents = 1.0;
  44. real_t time_horizon_obstacles = 0.0;
  45. int max_neighbors = 5;
  46. real_t neighbor_distance = 5.0;
  47. Vector2 safe_velocity;
  48. bool clamp_speed = true; // Experimental, clamps velocity to max_speed.
  49. NavMap2D *map = nullptr;
  50. RVO2D::Agent2D rvo_agent;
  51. bool avoidance_enabled = false;
  52. uint32_t avoidance_layers = 1;
  53. uint32_t avoidance_mask = 1;
  54. real_t avoidance_priority = 1.0;
  55. Callable avoidance_callback;
  56. bool agent_dirty = true;
  57. uint32_t last_map_iteration_id = 0;
  58. bool paused = false;
  59. SelfList<NavAgent2D> sync_dirty_request_list_element;
  60. public:
  61. NavAgent2D();
  62. ~NavAgent2D();
  63. void set_avoidance_enabled(bool p_enabled);
  64. bool is_avoidance_enabled() { return avoidance_enabled; }
  65. void set_map(NavMap2D *p_map);
  66. NavMap2D *get_map() { return map; }
  67. bool is_map_changed();
  68. RVO2D::Agent2D *get_rvo_agent() { return &rvo_agent; }
  69. void set_avoidance_callback(Callable p_callback);
  70. bool has_avoidance_callback() const;
  71. void dispatch_avoidance_callback();
  72. void set_neighbor_distance(real_t p_neighbor_distance);
  73. real_t get_neighbor_distance() const { return neighbor_distance; }
  74. void set_max_neighbors(int p_max_neighbors);
  75. int get_max_neighbors() const { return max_neighbors; }
  76. void set_time_horizon_agents(real_t p_time_horizon);
  77. real_t get_time_horizon_agents() const { return time_horizon_agents; }
  78. void set_time_horizon_obstacles(real_t p_time_horizon);
  79. real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
  80. void set_radius(real_t p_radius);
  81. real_t get_radius() const { return radius; }
  82. void set_max_speed(real_t p_max_speed);
  83. real_t get_max_speed() const { return max_speed; }
  84. void set_position(const Vector2 &p_position);
  85. Vector2 get_position() const { return position; }
  86. void set_target_position(const Vector2 &p_target_position);
  87. Vector2 get_target_position() const { return target_position; }
  88. void set_velocity(const Vector2 &p_velocity);
  89. Vector2 get_velocity() const { return velocity; }
  90. void set_velocity_forced(const Vector2 &p_velocity);
  91. Vector2 get_velocity_forced() const { return velocity_forced; }
  92. void set_avoidance_layers(uint32_t p_layers);
  93. uint32_t get_avoidance_layers() const { return avoidance_layers; }
  94. void set_avoidance_mask(uint32_t p_mask);
  95. uint32_t get_avoidance_mask() const { return avoidance_mask; }
  96. void set_avoidance_priority(real_t p_priority);
  97. real_t get_avoidance_priority() const { return avoidance_priority; }
  98. void set_paused(bool p_paused);
  99. bool get_paused() const;
  100. bool is_dirty() const;
  101. void sync();
  102. void request_sync();
  103. void cancel_sync_request();
  104. // Updates this agent with rvo data after the rvo simulation avoidance step.
  105. void update();
  106. // RVO debug data from the last frame update.
  107. const Dictionary get_avoidance_data() const;
  108. private:
  109. void _update_rvo_agent_properties();
  110. };