2016-11-15 18 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})