Path: blob/master/scene/resources/2d/polygon_path_finder.h
9898 views
/**************************************************************************/1/* polygon_path_finder.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/io/resource.h"3334class PolygonPathFinder : public Resource {35GDCLASS(PolygonPathFinder, Resource);3637struct Point {38Vector2 pos;39HashSet<int> connections;40float distance = 0.0;41float penalty = 0.0;42int prev = 0;43};4445union Edge {46struct {47int32_t points[2];48};49uint64_t key = 0;5051_FORCE_INLINE_ bool operator==(const Edge &p_edge) const {52return key == p_edge.key;53}54_FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {55return hash_one_uint64(p_edge.key);56}5758Edge(int a = 0, int b = 0) {59if (a > b) {60SWAP(a, b);61}62points[0] = a;63points[1] = b;64}65};6667Vector2 outside_point;68Rect2 bounds;6970Vector<Point> points;71HashSet<Edge, Edge> edges;7273bool _is_point_inside(const Vector2 &p_point) const;7475void _set_data(const Dictionary &p_data);76Dictionary _get_data() const;7778protected:79static void _bind_methods();8081public:82void setup(const Vector<Vector2> &p_points, const Vector<int> &p_connections);83Vector<Vector2> find_path(const Vector2 &p_from, const Vector2 &p_to);8485void set_point_penalty(int p_point, float p_penalty);86float get_point_penalty(int p_point) const;8788bool is_point_inside(const Vector2 &p_point) const;89Vector2 get_closest_point(const Vector2 &p_point) const;90Vector<Vector2> get_intersections(const Vector2 &p_from, const Vector2 &p_to) const;91Rect2 get_bounds() const;9293PolygonPathFinder();94};959697