polygon_path_finder.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef POLYGON_PATH_FINDER_H
  2. #define POLYGON_PATH_FINDER_H
  3. #include "resource.h"
  4. class PolygonPathFinder : public Resource {
  5. OBJ_TYPE(PolygonPathFinder,Resource);
  6. struct Point {
  7. Vector2 pos;
  8. Set<int> connections;
  9. float distance;
  10. float penalty;
  11. int prev;
  12. };
  13. struct Edge {
  14. int points[2];
  15. _FORCE_INLINE_ bool operator<(const Edge& p_edge) const {
  16. if (points[0]==p_edge.points[0])
  17. return points[1]<p_edge.points[1];
  18. else
  19. return points[0]<p_edge.points[0];
  20. }
  21. Edge(int a=0, int b=0) {
  22. if (a>b) {
  23. SWAP(a,b);
  24. }
  25. points[0] = a;
  26. points[1] = b;
  27. }
  28. };
  29. Vector2 outside_point;
  30. Rect2 bounds;
  31. Vector<Point> points;
  32. Set<Edge> edges;
  33. bool _is_point_inside(const Vector2& p_point) const;
  34. void _set_data(const Dictionary& p_data);
  35. Dictionary _get_data() const;
  36. protected:
  37. static void _bind_methods();
  38. public:
  39. void setup(const Vector<Vector2>& p_points, const Vector<int>& p_connections);
  40. Vector<Vector2> find_path(const Vector2& p_from, const Vector2& p_to);
  41. void set_point_penalty(int p_point,float p_penalty);
  42. float get_point_penalty(int p_point) const;
  43. bool is_point_inside(const Vector2& p_point) const;
  44. Vector2 get_closest_point(const Vector2& p_point) const;
  45. Vector<Vector2> get_intersections(const Vector2& p_from, const Vector2& p_to) const;
  46. Rect2 get_bounds() const;
  47. PolygonPathFinder();
  48. };
  49. #endif // POLYGON_PATH_FINDER_H