開發基於2D平鋪的「桌上游戲」當玩家擲骰子時,我必須做出限制(如果你着地5等,移動5個棋子)基於Tilebased的桌面遊戲雙數限制(增量算法)
- 開始在起點
- 檢查位置向兩側,上面和下面
- 檢查鄰居磚是適宜步行,如果t哎被更改,從而到達
- 轉到鄰居瓦,重複
我一直在尋找的A *和d *尋路,但它是一個新的課題,我和他們似乎更側重於從獲得點A到B,而不是「達到」,這是我所需要的。
如何通過代碼來做到這一點?
我創建從陣列將其保持我的瓦片的2D陣列(我需要tilemap的的正常陣列用於其他目的):
for(int i = 0; i < 27; i++)
{
for(int j = 0; j < 33; j++)
{
tileMap[i, j] = goTile[i * 33 + j];
}
}
我現在使用的tilemap的作爲我positiong因子,例如我的球員目前的位置是tileMap [2,4]。
然後我試圖開發一個功能:
void pathFinding(Vector2 playerPosition, int diceNumber)
{
GameObject currentPos = tileMap[(int)playerPosition.x, (int)playerPosition.y];
for (int i = 0; i < diceNumber; i++) {
if (tileMap[(int)playerPosition.x + 1, (int)playerPosition.y].tag == "walkableGrid")
{
tileMap[(int)playerPosition.x + 1, (int)playerPosition.y].gameObject.tag = "reachable";
playerPosition.x++;
}
if (tileMap[(int)playerPosition.x - 1, (int)playerPosition.y].tag == "walkableGrid")
{
playerPosition.x--;
}
if (tileMap[(int)playerPosition.x, (int)playerPosition.y + 1].tag == "walkableGrid")
{
playerPosition.y++;
}
if (tileMap[(int)playerPosition.x, (int)playerPosition.y - 1].tag == "walkableGrid")
{
playerPosition.y--;
}
}
}
但由於在完成這個(如果它甚至會工作),就需要多行代碼,我相信有使用嵌套的for循環更迅速的方法也許?
所以......你能指望我們在這裏回答使它成爲一個可選的操作方法? – Innat3
編輯帖子 –
向我們展示一些數據結構的代碼,否則很難猜測出正確的解決方案。另外,你有沒有試圖自己編碼? –