navigation_2d_server.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**************************************************************************/
  2. /* navigation_2d_server.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. #ifndef NAVIGATION_2D_SERVER_H
  31. #define NAVIGATION_2D_SERVER_H
  32. #include "core/object.h"
  33. #include "core/rid.h"
  34. #include "scene/2d/navigation_polygon.h"
  35. // This server exposes the 3D `NavigationServer` features in the 2D world.
  36. class Navigation2DServer : public Object {
  37. GDCLASS(Navigation2DServer, Object);
  38. static Navigation2DServer *singleton;
  39. void _emit_map_changed(RID p_map);
  40. protected:
  41. static void _bind_methods();
  42. public:
  43. /// Thread safe, can be used across many threads.
  44. static const Navigation2DServer *get_singleton() { return singleton; }
  45. /// MUST be used in single thread!
  46. static Navigation2DServer *get_singleton_mut() { return singleton; }
  47. virtual Array get_maps() const;
  48. /// Create a new map.
  49. virtual RID map_create() const;
  50. /// Set map active.
  51. virtual void map_set_active(RID p_map, bool p_active) const;
  52. /// Returns true if the map is active.
  53. virtual bool map_is_active(RID p_map) const;
  54. /// Set the map cell size used to weld the navigation mesh polygons.
  55. virtual void map_set_cell_size(RID p_map, real_t p_cell_size) const;
  56. /// Returns the map cell size.
  57. virtual real_t map_get_cell_size(RID p_map) const;
  58. /// Set the map cell height used to weld the navigation mesh polygons.
  59. virtual void map_set_cell_height(RID p_map, real_t p_cell_height) const;
  60. virtual real_t map_get_cell_height(RID p_map) const;
  61. /// Set the map edge connection margin used to weld the compatible region edges.
  62. virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) const;
  63. /// Returns the edge connection margin of this map.
  64. virtual real_t map_get_edge_connection_margin(RID p_map) const;
  65. /// Returns the navigation path to reach the destination from the origin.
  66. virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const;
  67. virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const;
  68. virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const;
  69. virtual Array map_get_regions(RID p_map) const;
  70. virtual Array map_get_agents(RID p_map) const;
  71. virtual void map_force_update(RID p_map);
  72. /// Creates a new region.
  73. virtual RID region_create() const;
  74. /// Set the enter_cost of a region
  75. virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) const;
  76. virtual real_t region_get_enter_cost(RID p_region) const;
  77. /// Set the travel_cost of a region
  78. virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) const;
  79. virtual real_t region_get_travel_cost(RID p_region) const;
  80. virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const;
  81. /// Set the map of this region.
  82. virtual void region_set_map(RID p_region, RID p_map) const;
  83. virtual RID region_get_map(RID p_region) const;
  84. /// Set the region's layers
  85. virtual void region_set_navigation_layers(RID p_region, uint32_t p_navigation_layers) const;
  86. virtual uint32_t region_get_navigation_layers(RID p_region) const;
  87. /// Set the global transformation of this region.
  88. virtual void region_set_transform(RID p_region, Transform2D p_transform) const;
  89. /// Set the navigation poly of this region.
  90. virtual void region_set_navpoly(RID p_region, Ref<NavigationPolygon> p_nav_mesh) const;
  91. /// Get a list of a region's connection to other regions.
  92. virtual int region_get_connections_count(RID p_region) const;
  93. virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const;
  94. virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const;
  95. /// Creates the agent.
  96. virtual RID agent_create() const;
  97. /// Put the agent in the map.
  98. virtual void agent_set_map(RID p_agent, RID p_map) const;
  99. virtual RID agent_get_map(RID p_agent) const;
  100. /// The maximum distance (center point to
  101. /// center point) to other agents this agent
  102. /// takes into account in the navigation. The
  103. /// larger this number, the longer the running
  104. /// time of the simulation. If the number is too
  105. /// low, the simulation will not be safe.
  106. /// Must be non-negative.
  107. virtual void agent_set_neighbor_dist(RID p_agent, real_t p_dist) const;
  108. /// The maximum number of other agents this
  109. /// agent takes into account in the navigation.
  110. /// The larger this number, the longer the
  111. /// running time of the simulation. If the
  112. /// number is too low, the simulation will not
  113. /// be safe.
  114. virtual void agent_set_max_neighbors(RID p_agent, int p_count) const;
  115. /// The minimal amount of time for which this
  116. /// agent's velocities that are computed by the
  117. /// simulation are safe with respect to other
  118. /// agents. The larger this number, the sooner
  119. /// this agent will respond to the presence of
  120. /// other agents, but the less freedom this
  121. /// agent has in choosing its velocities.
  122. /// Must be positive.
  123. virtual void agent_set_time_horizon(RID p_agent, real_t p_time) const;
  124. /// The radius of this agent.
  125. /// Must be non-negative.
  126. virtual void agent_set_radius(RID p_agent, real_t p_radius) const;
  127. /// The maximum speed of this agent.
  128. /// Must be non-negative.
  129. virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed) const;
  130. /// Current velocity of the agent
  131. virtual void agent_set_velocity(RID p_agent, Vector2 p_velocity) const;
  132. /// The new target velocity.
  133. virtual void agent_set_target_velocity(RID p_agent, Vector2 p_velocity) const;
  134. /// Position of the agent in world space.
  135. virtual void agent_set_position(RID p_agent, Vector2 p_position) const;
  136. /// Agent ignore the Y axis and avoid collisions by moving only on the horizontal plane
  137. virtual void agent_set_ignore_y(RID p_agent, bool p_ignore) const;
  138. /// Returns true if the map got changed the previous frame.
  139. virtual bool agent_is_map_changed(RID p_agent) const;
  140. /// Callback called at the end of the RVO process
  141. virtual void agent_set_callback(RID p_agent, ObjectID p_object_id, StringName p_method, Variant p_udata = Variant()) const;
  142. /// Destroy the `RID`
  143. virtual void free(RID p_object) const;
  144. Navigation2DServer();
  145. virtual ~Navigation2DServer();
  146. };
  147. #endif // NAVIGATION_2D_SERVER_H