2

I'm currently making a game in Unity that's a 2D sidescroller (so all sprites are viewed from the side).

I have procedurally generated terrain (so think Terraria-esque), and I want to be able to click on one of my tiles and have a sprite path to it.

However, the sprite needs to know the following things:

  1. Whether or not the tile is accessible (aka it's not completely underground or way up randomly in the sky)
  2. What the shortest path is to get there.

I'm currently storing my tiles as a short array.

Knowing this, is A* the best algorithm for me to pursue? Does anyone have any other good info/tips of where to begin looking into this sort of thing?

My main conceptual difficulty is making it so the AI knows whether or not a tile is accessible.

Any help/pointers is appreciated!

4

1 回答 1

1

A* 是查找最短路径的好方法,但用于查找是否存在路径很麻烦。这是因为它在意识到没有路径之前正在搜索整个可访问空间。您可以使用此在线工具来使用不同的搜索算法。

您可以使用快速破解来判断每个不同空间的存储 ID 是否存在路径,并且所有节点都可以相互访问。使用遍历所有可访问节点的递归函数很容易分配此 ID。递归终止后,只需使用来自另一个尚无任何 ID 的空间的新 ID 重新启动它。希望下面的图片能阐明我的观点:

11111¤22222¤
111¤¤¤222¤¤¤
1¤¤¤3¤¤¤¤¤44
¤¤3333¤¤4444

其中¤墙壁和数字是空间的ID。当然,您需要将存储从短数组更改为其他内容,因为您需要为每个节点存储额外的短数组。

于 2014-10-28T16:32:02.457 回答