navigation_server_2d.h 7.2 KB

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