2016-11-15 16 views
-1

開發基於2D平鋪的「桌上游戲」當玩家擲骰子時,我必須做出限制(如果你着地5等,移動5個棋子)基於Tilebased的桌面遊戲雙數限制(增量算法)

我嘗試使用以下邏輯: enter image description here

  • 開始在起點
  • 檢查位置向兩側,上面和下面
  • 檢查鄰居磚是適宜步行,如果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循環更迅速的方法也許?

+0

所以......你能指望我們在這裏回答使它成爲一個可選的操作方法? – Innat3

+0

編輯帖子 –

+0

向我們展示一些數據結構的代碼,否則很難猜測出正確的解決方案。另外,你有沒有試圖自己編碼? –

回答

2
//I have now edited the code to better reflect your real data 

public void ShowMoves(Vector2 playerPosition, int diceNumber, bool[] blocks) 
{ 

    int x = (int)playerPosition.x; 
    int y = (int)playerPosition.y; 

    if(tileMap.GetUpperBound(0) < x + 1) 
    { 
     if(tileMap[x + 1, y].tag == "walkableGrid" && blocks[0]) 
     { 
      /*Light up the tile*/ 
      if(diceNumber > 0) 
       ShowMoves(new Vector2(x + 1, y), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), false, y != tileMap.GetUpperBound(1), y != 0 }); 
     } 
    } 

    if(x - 1 >= 0) 
    { 
     if(tileMap[x - 1, y].tag == "walkableGrid" && blocks[1]) 
     { 
      /*Light up the tile*/ 
      if(diceNumber > 0) 
       ShowMoves(new Vector2(x - 1, y), diceNumber - 1, new bool[] { false, x != 0, y != tileMap.GetUpperBound(1), y != 0 }); 
     } 
    } 

    if(tileMap.GetUpperBound(1) < y + 1) 
    { 
     if(tileMap[x, y + 1].tag == "walkableGrid" && blocks[2]) 
     { 
      /*Light up the tile*/ 
      if(diceNumber > 0) 
       ShowMoves(new Vector2(x, y + 1), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), x != 0, y != tileMap.GetUpperBound(1), false }); 
     } 
    } 

    if(y - 1 >= 0) 
    { 
     if(tileMap[x, y - 1].tag == "walkableGrid" && blocks[3]) 
     { 
      /*Light up the tile*/ 
      if(diceNumber > 0) 
       ShowMoves(new Vector2(x, y - 1), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), x != 0, false, y != 0 }); 
     } 
    } 
} 

此代碼可能無法編譯,但它是一個例子,沿着幫助你 - 它會循環,直到沒有可移動,並且每個選項。由於blocks布爾數組,它也不會自行迴歸。輸入格式將是他們在兩個整數,一個用於x和一個y的位置,地磚可用的留在自己的側傾動作的次數,並從一開始(總是new bool[] {true, true, true, true}

塊要小心,我的代碼中可能存在錯誤,我在SO中編寫它,並且不知道它運行得如何,如果它完全運行或者什麼都運行,即使它不運行,它也應該是一個很好的起點你的邏輯和代碼,這一切

編輯:代碼已被更改,以便您的代碼,它更適應的外觀和數據類型,它使用

爲了避免總是呼籲通過輸入new bool[] {true, true, true, true};一個blocks變量,你可以通過使該方法的參數

public void ShowMoves(Vector2 playerPosition, int diceNumber, bool[] blocks = new bool[] {true, true, true, true})