Files
godot-docs-l10n/classes/zh_CN/class_astar2d.rst
Rémi Verschelde 9581bc9bb3 Sync class reference translations with upstream 4.x
Still only zh_CN for now.
zh_TW is near 100% complete, but it has hundreds of validation errors
which need to be fixed first.
2023-11-21 16:13:45 +01:00

534 lines
26 KiB
ReStructuredText
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

:github_url: hide
.. DO NOT EDIT THIS FILE!!!
.. Generated automatically from Godot engine sources.
.. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
.. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/AStar2D.xml.
.. _class_AStar2D:
AStar2D
=======
**继承:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
A\* 的一种实现,用于查找 2D 空间中连通图上两个顶点之间的最短路径。
.. rst-class:: classref-introduction-group
描述
----
A\* 算法的一种实现,用于在 2D 空间中的连通图上找到两个顶点之间的最短路径。
有关如何使用该类的更详尽的解释,请参阅 :ref:`AStar3D<class_AStar3D>`\ 。\ **AStar2D**:ref:`AStar3D<class_AStar3D>` 的包装器,它强制执行 2D 坐标。
.. rst-class:: classref-reftable-group
方法
----
.. table::
:widths: auto
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`_compute_cost<class_AStar2D_private_method__compute_cost>` **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)** |virtual| |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`_estimate_cost<class_AStar2D_private_method__estimate_cost>` **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)** |virtual| |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`add_point<class_AStar2D_method_add_point>` **(** :ref:`int<class_int>` id, :ref:`Vector2<class_Vector2>` position, :ref:`float<class_float>` weight_scale=1.0 **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`are_points_connected<class_AStar2D_method_are_points_connected>` **(** :ref:`int<class_int>` id, :ref:`int<class_int>` to_id, :ref:`bool<class_bool>` bidirectional=true **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`clear<class_AStar2D_method_clear>` **(** **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`connect_points<class_AStar2D_method_connect_points>` **(** :ref:`int<class_int>` id, :ref:`int<class_int>` to_id, :ref:`bool<class_bool>` bidirectional=true **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`disconnect_points<class_AStar2D_method_disconnect_points>` **(** :ref:`int<class_int>` id, :ref:`int<class_int>` to_id, :ref:`bool<class_bool>` bidirectional=true **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_available_point_id<class_AStar2D_method_get_available_point_id>` **(** **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_closest_point<class_AStar2D_method_get_closest_point>` **(** :ref:`Vector2<class_Vector2>` to_position, :ref:`bool<class_bool>` include_disabled=false **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Vector2<class_Vector2>` | :ref:`get_closest_position_in_segment<class_AStar2D_method_get_closest_position_in_segment>` **(** :ref:`Vector2<class_Vector2>` to_position **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedInt64Array<class_PackedInt64Array>` | :ref:`get_id_path<class_AStar2D_method_get_id_path>` **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_point_capacity<class_AStar2D_method_get_point_capacity>` **(** **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedInt64Array<class_PackedInt64Array>` | :ref:`get_point_connections<class_AStar2D_method_get_point_connections>` **(** :ref:`int<class_int>` id **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_point_count<class_AStar2D_method_get_point_count>` **(** **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedInt64Array<class_PackedInt64Array>` | :ref:`get_point_ids<class_AStar2D_method_get_point_ids>` **(** **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedVector2Array<class_PackedVector2Array>` | :ref:`get_point_path<class_AStar2D_method_get_point_path>` **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Vector2<class_Vector2>` | :ref:`get_point_position<class_AStar2D_method_get_point_position>` **(** :ref:`int<class_int>` id **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`get_point_weight_scale<class_AStar2D_method_get_point_weight_scale>` **(** :ref:`int<class_int>` id **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`has_point<class_AStar2D_method_has_point>` **(** :ref:`int<class_int>` id **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`is_point_disabled<class_AStar2D_method_is_point_disabled>` **(** :ref:`int<class_int>` id **)** |const| |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`remove_point<class_AStar2D_method_remove_point>` **(** :ref:`int<class_int>` id **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`reserve_space<class_AStar2D_method_reserve_space>` **(** :ref:`int<class_int>` num_nodes **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`set_point_disabled<class_AStar2D_method_set_point_disabled>` **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` disabled=true **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`set_point_position<class_AStar2D_method_set_point_position>` **(** :ref:`int<class_int>` id, :ref:`Vector2<class_Vector2>` position **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`set_point_weight_scale<class_AStar2D_method_set_point_weight_scale>` **(** :ref:`int<class_int>` id, :ref:`float<class_float>` weight_scale **)** |
+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
方法说明
--------
.. _class_AStar2D_private_method__compute_cost:
.. rst-class:: classref-method
:ref:`float<class_float>` **_compute_cost** **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)** |virtual| |const|
计算两个连接点之间的成本时调用。
注意这个函数隐藏在默认的 **AStar2D** 类中。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_private_method__estimate_cost:
.. rst-class:: classref-method
:ref:`float<class_float>` **_estimate_cost** **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)** |virtual| |const|
估计一个点和路径终点之间的成本时调用。
注意这个函数隐藏在默认的 **AStar2D** 类中。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_add_point:
.. rst-class:: classref-method
void **add_point** **(** :ref:`int<class_int>` id, :ref:`Vector2<class_Vector2>` position, :ref:`float<class_float>` weight_scale=1.0 **)**
在具有给定标识符的给定位置添加一个新点。\ ``id`` 必须为 0 或更大,\ ``weight_scale`` 必须为 0.0 或更大。
在确定从相邻点到此点的一段路程的总成本时,\ ``weight_scale`` 要乘以 :ref:`_compute_cost<class_AStar2D_private_method__compute_cost>` 的结果。因此,在其他条件相同的情况下,算法优先选择 ``weight_scale`` 较低的点来形成路径。
.. tabs::
.. code-tab:: gdscript
var astar = AStar2D.new()
astar.add_point(1, Vector2(1, 0), 4) # 添加点 (1, 0)、权重为 4、ID 为 1
.. code-tab:: csharp
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(1, 0), 4); // 添加点 (1, 0)、权重为 4、ID 为 1
如果已经存在一个给定 ``id`` 的点,则它的位置和权重缩放将被更新为给定值。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_are_points_connected:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **are_points_connected** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` to_id, :ref:`bool<class_bool>` bidirectional=true **)** |const|
返回两个给定点之间是否存在连接/线段。如果 ``bidirectional````false``\ ,则返回是否可以通过此段从 ``id`` 移动到 ``to_id``\ 。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_clear:
.. rst-class:: classref-method
void **clear** **(** **)**
清除所有点和线段。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_connect_points:
.. rst-class:: classref-method
void **connect_points** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` to_id, :ref:`bool<class_bool>` bidirectional=true **)**
在给定的点之间创建一个线段。如果 ``bidirectional````false``\ ,则只允许从 ``id````to_id`` 的移动,而不允许反向移动。
.. tabs::
.. code-tab:: gdscript
var astar = AStar2D.new()
astar.add_point(1, Vector2(1, 1))
astar.add_point(2, Vector2(0, 5))
astar.connect_points(1, 2, false)
.. code-tab:: csharp
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(1, 1));
astar.AddPoint(2, new Vector2(0, 5));
astar.ConnectPoints(1, 2, false);
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_disconnect_points:
.. rst-class:: classref-method
void **disconnect_points** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` to_id, :ref:`bool<class_bool>` bidirectional=true **)**
删除给定点之间的线段。如果 ``bidirectional````false``\ ,则仅阻止从 ``id````to_id`` 的移动,并且可能会保留一个单向线段。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_available_point_id:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_available_point_id** **(** **)** |const|
返回下一个没有关联点的可用点 ID。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_closest_point:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_closest_point** **(** :ref:`Vector2<class_Vector2>` to_position, :ref:`bool<class_bool>` include_disabled=false **)** |const|
返回距离 ``to_position`` 最近的点的 ID可以选择将禁用的点考虑在内。如果点池中没有点则返回 ``-1``\ 。
\ **注意:**\ 如果有多个点距离 ``to_position`` 最近,则返回 ID 最小的那个点,以保证结果的确定性。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_closest_position_in_segment:
.. rst-class:: classref-method
:ref:`Vector2<class_Vector2>` **get_closest_position_in_segment** **(** :ref:`Vector2<class_Vector2>` to_position **)** |const|
返回最接近 ``to_position`` 的位置,该位置位于两个连接点之间的线段内。
.. tabs::
.. code-tab:: gdscript
var astar = AStar2D.new()
astar.add_point(1, Vector2(0, 0))
astar.add_point(2, Vector2(0, 5))
astar.connect_points(1, 2)
var res = astar.get_closest_position_in_segment(Vector2(3, 3)) # 返回 (0, 3)
.. code-tab:: csharp
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(0, 0));
astar.AddPoint(2, new Vector2(0, 5));
astar.ConnectPoints(1, 2);
Vector2 res = astar.GetClosestPositionInSegment(new Vector2(3, 3)); // 返回 (0, 3)
结果位于从 ``y = 0````y = 5`` 的线段中。它是线段中距给定点最近的位置。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_id_path:
.. rst-class:: classref-method
:ref:`PackedInt64Array<class_PackedInt64Array>` **get_id_path** **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)**
返回一个数组,其中包含构成由 AStar2D 在给定点之间找到的路径的点的 ID。数组从路径的起点到终点进行排序。
.. tabs::
.. code-tab:: gdscript
var astar = AStar2D.new()
astar.add_point(1, Vector2(0, 0))
astar.add_point(2, Vector2(0, 1), 1) # 默认权重为 1
astar.add_point(3, Vector2(1, 1))
astar.add_point(4, Vector2(2, 0))
astar.connect_points(1, 2, false)
astar.connect_points(2, 3, false)
astar.connect_points(4, 3, false)
astar.connect_points(1, 4, false)
var res = astar.get_id_path(1, 3) # 返回 [1, 2, 3]
.. code-tab:: csharp
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(0, 0));
astar.AddPoint(2, new Vector2(0, 1), 1); // 默认权重为 1
astar.AddPoint(3, new Vector2(1, 1));
astar.AddPoint(4, new Vector2(2, 0));
astar.ConnectPoints(1, 2, false);
astar.ConnectPoints(2, 3, false);
astar.ConnectPoints(4, 3, false);
astar.ConnectPoints(1, 4, false);
int[] res = astar.GetIdPath(1, 3); // 返回 [1, 2, 3]
如果将第2个点的权重更改为 3则结果将改为 ``[1, 4, 3]``\ ,因为现在即使距离更长,通过第 4 点也比通过第 2 点“更容易”。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_capacity:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_point_capacity** **(** **)** |const|
该函数返回支持点的数据结构的容量,可以与 :ref:`reserve_space<class_AStar2D_method_reserve_space>` 方法一起使用。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_connections:
.. rst-class:: classref-method
:ref:`PackedInt64Array<class_PackedInt64Array>` **get_point_connections** **(** :ref:`int<class_int>` id **)**
返回一个数组,其中包含与给定点形成连接的点的 ID。
.. tabs::
.. code-tab:: gdscript
var astar = AStar2D.new()
astar.add_point(1, Vector2(0, 0))
astar.add_point(2, Vector2(0, 1))
astar.add_point(3, Vector2(1, 1))
astar.add_point(4, Vector2(2, 0))
astar.connect_points(1, 2, true)
astar.connect_points(1, 3, true)
var neighbors = astar.get_point_connections(1) # 返回 [2, 3]
.. code-tab:: csharp
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(0, 0));
astar.AddPoint(2, new Vector2(0, 1));
astar.AddPoint(3, new Vector2(1, 1));
astar.AddPoint(4, new Vector2(2, 0));
astar.ConnectPoints(1, 2, true);
astar.ConnectPoints(1, 3, true);
int[] neighbors = astar.GetPointConnections(1); // 返回 [2, 3]
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_count:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_point_count** **(** **)** |const|
返回点池中当前的点数。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_ids:
.. rst-class:: classref-method
:ref:`PackedInt64Array<class_PackedInt64Array>` **get_point_ids** **(** **)**
返回所有点 ID 的数组。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_path:
.. rst-class:: classref-method
:ref:`PackedVector2Array<class_PackedVector2Array>` **get_point_path** **(** :ref:`int<class_int>` from_id, :ref:`int<class_int>` to_id **)**
返回一个数组,其中包含 AStar2D 在给定点之间找到的路径中的点。数组从路径的起点到终点进行排序。
\ **注意:**\ 该方法不是线程安全的。如果从 :ref:`Thread<class_Thread>` 调用,它将返回一个空的 :ref:`PackedVector2Array<class_PackedVector2Array>` 并打印一条错误消息。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_position:
.. rst-class:: classref-method
:ref:`Vector2<class_Vector2>` **get_point_position** **(** :ref:`int<class_int>` id **)** |const|
返回与给定 ``id`` 相关联的点的位置。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_get_point_weight_scale:
.. rst-class:: classref-method
:ref:`float<class_float>` **get_point_weight_scale** **(** :ref:`int<class_int>` id **)** |const|
返回与给定 ``id`` 关联的点的权重比例。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_has_point:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **has_point** **(** :ref:`int<class_int>` id **)** |const|
返回与给定 ``id`` 相关联的点是否存在。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_is_point_disabled:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **is_point_disabled** **(** :ref:`int<class_int>` id **)** |const|
返回用于寻路时点是否被禁用。默认情况下,所有点均被启用。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_remove_point:
.. rst-class:: classref-method
void **remove_point** **(** :ref:`int<class_int>` id **)**
从点池中移除与给定 ``id`` 关联的点。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_reserve_space:
.. rst-class:: classref-method
void **reserve_space** **(** :ref:`int<class_int>` num_nodes **)**
在内部为 ``num_nodes`` 个点保留空间,如果你想要一次性添加大量的点且数量已知,例如网格,那么就会很有用。新容量必须大于或等于旧容量。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_set_point_disabled:
.. rst-class:: classref-method
void **set_point_disabled** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` disabled=true **)**
用于寻路时禁用或启用指定的点。适用于制作临时障碍物。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_set_point_position:
.. rst-class:: classref-method
void **set_point_position** **(** :ref:`int<class_int>` id, :ref:`Vector2<class_Vector2>` position **)**
为具有给定 ``id`` 的点设置位置 ``position``\ 。
.. rst-class:: classref-item-separator
----
.. _class_AStar2D_method_set_point_weight_scale:
.. rst-class:: classref-method
void **set_point_weight_scale** **(** :ref:`int<class_int>` id, :ref:`float<class_float>` weight_scale **)**
为给定的 ``id`` 的点设置 ``weight_scale``\ 。在确定从邻接点到这个点的一段路程的总成本时,\ ``weight_scale`` 要乘以 :ref:`_compute_cost<class_AStar2D_private_method__compute_cost>` 的结果。
.. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)`
.. |const| replace:: :abbr:`const (本方法没有副作用。不会修改该实例的任何成员变量。)`
.. |vararg| replace:: :abbr:`vararg (本方法除了在此处描述的参数外,还能够继续接受任意数量的参数。)`
.. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)`
.. |static| replace:: :abbr:`static (调用本方法无需实例,所以可以直接使用类名调用。)`
.. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效操作符。)`
.. |bitfield| replace:: :abbr:`BitField (这个值是由下列标志构成的位掩码整数。)`