navigation_polygon.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* navigation_polygon.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_POLYGON_H
  31. #define NAVIGATION_POLYGON_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/resources/navigation_mesh.h"
  34. class NavigationPolygon : public Resource {
  35. GDCLASS(NavigationPolygon, Resource);
  36. PoolVector<Vector2> vertices;
  37. struct Polygon {
  38. Vector<int> indices;
  39. };
  40. Vector<Polygon> polygons;
  41. Vector<PoolVector<Vector2>> outlines;
  42. mutable Rect2 item_rect;
  43. mutable bool rect_cache_dirty = true;
  44. Mutex navmesh_generation;
  45. // Navigation mesh
  46. Ref<NavigationMesh> navmesh;
  47. protected:
  48. static void _bind_methods();
  49. void _set_polygons(const Array &p_array);
  50. Array _get_polygons() const;
  51. void _set_outlines(const Array &p_array);
  52. Array _get_outlines() const;
  53. public:
  54. #ifdef TOOLS_ENABLED
  55. Rect2 _edit_get_rect() const;
  56. bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
  57. #endif
  58. void set_vertices(const PoolVector<Vector2> &p_vertices);
  59. PoolVector<Vector2> get_vertices() const;
  60. void add_polygon(const Vector<int> &p_polygon);
  61. int get_polygon_count() const;
  62. void add_outline(const PoolVector<Vector2> &p_outline);
  63. void add_outline_at_index(const PoolVector<Vector2> &p_outline, int p_index);
  64. void set_outline(int p_idx, const PoolVector<Vector2> &p_outline);
  65. PoolVector<Vector2> get_outline(int p_idx) const;
  66. void remove_outline(int p_idx);
  67. int get_outline_count() const;
  68. void clear_outlines();
  69. void make_polygons_from_outlines();
  70. Vector<int> get_polygon(int p_idx);
  71. void clear_polygons();
  72. Ref<NavigationMesh> get_mesh();
  73. NavigationPolygon();
  74. ~NavigationPolygon();
  75. };
  76. class Navigation2D;
  77. class NavigationPolygonInstance : public Node2D {
  78. GDCLASS(NavigationPolygonInstance, Node2D);
  79. bool enabled = true;
  80. RID region;
  81. Navigation2D *navigation = nullptr;
  82. Ref<NavigationPolygon> navpoly;
  83. real_t enter_cost = 0.0;
  84. real_t travel_cost = 1.0;
  85. uint32_t navigation_layers = 1;
  86. void _navpoly_changed();
  87. void _map_changed(RID p_map);
  88. protected:
  89. void _notification(int p_what);
  90. static void _bind_methods();
  91. public:
  92. #ifdef TOOLS_ENABLED
  93. virtual Rect2 _edit_get_rect() const;
  94. virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
  95. #endif
  96. void set_enabled(bool p_enabled);
  97. bool is_enabled() const;
  98. void set_navigation_layers(uint32_t p_navigation_layers);
  99. uint32_t get_navigation_layers() const;
  100. RID get_region_rid() const;
  101. void set_enter_cost(real_t p_enter_cost);
  102. real_t get_enter_cost() const;
  103. void set_travel_cost(real_t p_travel_cost);
  104. real_t get_travel_cost() const;
  105. void set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly);
  106. Ref<NavigationPolygon> get_navigation_polygon() const;
  107. String get_configuration_warning() const;
  108. NavigationPolygonInstance();
  109. ~NavigationPolygonInstance();
  110. };
  111. #endif // NAVIGATION_POLYGON_H