navigation_polygon.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "servers/navigation/navigation_globals.h"
  35. class NavigationPolygon : public Resource {
  36. GDCLASS(NavigationPolygon, Resource);
  37. RWLock rwlock;
  38. Vector<Vector2> vertices;
  39. Vector<Vector<int>> polygons;
  40. Vector<Vector<Vector2>> outlines;
  41. Vector<Vector<Vector2>> baked_outlines;
  42. mutable Rect2 item_rect;
  43. mutable bool rect_cache_dirty = true;
  44. Mutex navigation_mesh_generation;
  45. // Navigation mesh
  46. Ref<NavigationMesh> navigation_mesh;
  47. real_t cell_size = NavigationDefaults2D::navmesh_cell_size;
  48. real_t border_size = 0.0f;
  49. Rect2 baking_rect;
  50. Vector2 baking_rect_offset;
  51. protected:
  52. static void _bind_methods();
  53. void _validate_property(PropertyInfo &p_property) const;
  54. void _set_polygons(const TypedArray<Vector<int32_t>> &p_array);
  55. TypedArray<Vector<int32_t>> _get_polygons() const;
  56. void _set_outlines(const TypedArray<Vector<Vector2>> &p_array);
  57. TypedArray<Vector<Vector2>> _get_outlines() const;
  58. public:
  59. #ifdef DEBUG_ENABLED
  60. Rect2 _edit_get_rect() const;
  61. bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
  62. #endif // DEBUG_ENABLED
  63. enum SamplePartitionType {
  64. SAMPLE_PARTITION_CONVEX_PARTITION = 0,
  65. SAMPLE_PARTITION_TRIANGULATE,
  66. SAMPLE_PARTITION_MAX
  67. };
  68. enum ParsedGeometryType {
  69. PARSED_GEOMETRY_MESH_INSTANCES = 0,
  70. PARSED_GEOMETRY_STATIC_COLLIDERS,
  71. PARSED_GEOMETRY_BOTH,
  72. PARSED_GEOMETRY_MAX
  73. };
  74. enum SourceGeometryMode {
  75. SOURCE_GEOMETRY_ROOT_NODE_CHILDREN = 0,
  76. SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN,
  77. SOURCE_GEOMETRY_GROUPS_EXPLICIT,
  78. SOURCE_GEOMETRY_MAX
  79. };
  80. real_t agent_radius = 10.0f;
  81. SamplePartitionType partition_type = SAMPLE_PARTITION_CONVEX_PARTITION;
  82. ParsedGeometryType parsed_geometry_type = PARSED_GEOMETRY_BOTH;
  83. uint32_t parsed_collision_mask = 0xFFFFFFFF;
  84. SourceGeometryMode source_geometry_mode = SOURCE_GEOMETRY_ROOT_NODE_CHILDREN;
  85. StringName source_geometry_group_name = "navigation_polygon_source_geometry_group";
  86. void set_vertices(const Vector<Vector2> &p_vertices);
  87. Vector<Vector2> get_vertices() const;
  88. void add_polygon(const Vector<int> &p_polygon);
  89. int get_polygon_count() const;
  90. void add_outline(const Vector<Vector2> &p_outline);
  91. void add_outline_at_index(const Vector<Vector2> &p_outline, int p_index);
  92. void set_outline(int p_idx, const Vector<Vector2> &p_outline);
  93. Vector<Vector2> get_outline(int p_idx) const;
  94. void remove_outline(int p_idx);
  95. int get_outline_count() const;
  96. void set_outlines(const Vector<Vector<Vector2>> &p_outlines);
  97. Vector<Vector<Vector2>> get_outlines() const;
  98. void clear_outlines();
  99. #ifndef DISABLE_DEPRECATED
  100. void make_polygons_from_outlines();
  101. #endif // DISABLE_DEPRECATED
  102. void set_polygons(const Vector<Vector<int>> &p_polygons);
  103. Vector<Vector<int>> get_polygons() const;
  104. Vector<int> get_polygon(int p_idx);
  105. void clear_polygons();
  106. void set_sample_partition_type(SamplePartitionType p_value);
  107. SamplePartitionType get_sample_partition_type() const;
  108. void set_parsed_geometry_type(ParsedGeometryType p_geometry_type);
  109. ParsedGeometryType get_parsed_geometry_type() const;
  110. void set_parsed_collision_mask(uint32_t p_mask);
  111. uint32_t get_parsed_collision_mask() const;
  112. void set_parsed_collision_mask_value(int p_layer_number, bool p_value);
  113. bool get_parsed_collision_mask_value(int p_layer_number) const;
  114. void set_source_geometry_mode(SourceGeometryMode p_geometry_mode);
  115. SourceGeometryMode get_source_geometry_mode() const;
  116. void set_source_geometry_group_name(const StringName &p_group_name);
  117. StringName get_source_geometry_group_name() const;
  118. void set_agent_radius(real_t p_value);
  119. real_t get_agent_radius() const;
  120. Ref<NavigationMesh> get_navigation_mesh();
  121. void set_cell_size(real_t p_cell_size);
  122. real_t get_cell_size() const;
  123. void set_border_size(real_t p_value);
  124. real_t get_border_size() const;
  125. void set_baking_rect(const Rect2 &p_rect);
  126. Rect2 get_baking_rect() const;
  127. void set_baking_rect_offset(const Vector2 &p_rect_offset);
  128. Vector2 get_baking_rect_offset() const;
  129. void clear();
  130. void set_data(const Vector<Vector2> &p_vertices, const Vector<Vector<int>> &p_polygons);
  131. void set_data(const Vector<Vector2> &p_vertices, const Vector<Vector<int>> &p_polygons, const Vector<Vector<Vector2>> &p_outlines);
  132. void get_data(Vector<Vector2> &r_vertices, Vector<Vector<int>> &r_polygons);
  133. void get_data(Vector<Vector2> &r_vertices, Vector<Vector<int>> &r_polygons, Vector<Vector<Vector2>> &r_outlines);
  134. NavigationPolygon() {}
  135. ~NavigationPolygon() {}
  136. };
  137. VARIANT_ENUM_CAST(NavigationPolygon::SamplePartitionType);
  138. VARIANT_ENUM_CAST(NavigationPolygon::ParsedGeometryType);
  139. VARIANT_ENUM_CAST(NavigationPolygon::SourceGeometryMode);
  140. #endif // NAVIGATION_POLYGON_H