簡體   English   中英

Unity 2D切片圖—按類型C#對切片進行分組

[英]Unity 2D Tile Map — Group Tiles By Type C#

我正在尋找一種解決方案,可以根據游戲圖塊的類型對游戲圖塊進行分組。 磁貼存儲在2d數組中,類型為空和水,成組的磁貼將存儲在Group類中。

所以,如果我有二維數組:

0、0、0、0、0、0、0、0,

0、1、1、1、0、0、0、0,

0,1,1,0,0,0,1,1,

0、1、0、0、0、0、1、1

如果0為空,而1為水,則會有兩個水組。

我花了整個下午努力弄清楚,這是我目前的狀況。

public void GenerateGroups(){
    //Debug.Log("Generate Groups");
    m_Groups = new List<Group>();
    List<Tile> groupingTiles = new List<Tile>();

    int groupId = 1;

    foreach(Tile t in m_Tiles){
        t.IsGrouped = false;
    }

    for(int y = 0;  y < Height; y++){
        for (int x = 0; x < Width; x++) {
            Tile tile = m_Tiles[x, y];

            if(tile.Type == TileType.Water && !tile.IsGrouped){

        //      if(m_Tiles[x+1, y].IsGrouped || m_Tiles[x, y + 1].IsGrouped){
        //          if(m_Groups.Count > 0){
        //              foreach(Group g in m_Groups){
        //                  if(g.m_Tiles.Contains(m_Tiles[x+1, y]) || g.m_Tiles.Contains(m_Tiles[x, y + 1])){
        //                      g.m_Tiles.Add(tile);
        //                      tile.IsGrouped = true;
        //                      continue;
        //                  }
        //              }
        //          }
        //      }else{
        //          groupingTiles.Add(tile);    
        //      }

                groupingTiles.Add(tile);

                tile.IsGrouped = true;

                Tile next = m_Tiles[x + 1, y];
                int pos = x + 1;

                while(next.Type == TileType.Water && !next.IsGrouped && pos < Width){
                //  Debug.Log("Going right!");
                    groupingTiles.Add(next);
                    pos++;
                    next.IsGrouped = true;
                    next = m_Tiles[pos, y];
                }

                next = m_Tiles[x, y + 1];
                pos = y + 1;

                while(next.Type == TileType.Water && !next.IsGrouped && pos < Height){
                    //Debug.Log("Going up!");
                    groupingTiles.Add(next);
                    pos++;
                    next.IsGrouped = true;
                    next = m_Tiles[x, pos];
                }

            }

            if(groupingTiles.Count > 0){
                //Debug.Log("Group Tiles: " + groupingTiles.Count);
                m_Groups.Add(new Group("Group_" + groupId, groupingTiles));
                groupId++;
                groupingTiles = new List<Tile>();
            }
        }
    }


    Debug.Log(m_Groups.Count);
}

任何幫助,將不勝感激,謝謝!

我可以通過在磁貼內部保存組的引用來解決此問題。 遇到水磚時,請檢查它的左鄰居還是底部鄰居也是水磚。 這可以在以下四種情況下結束:

  1. 左側或底部都不是水位圖(或存在):在當前位置上創建一個新組
  2. 左邊/底部是一塊水磚:將電流添加到左邊/底部的組中
  3. 左側和底部都是同一組中的水磁磚:向其組中添加電流
  4. 左側和底部都是不同組中的水磁磚:現在,這些組已建立連接,需要合並。 也將電流添加到該新組

我寫了一段代碼作為演示,但是請注意,它未經測試且經過簡化。 我認為合並兩個組有點麻煩,因為您需要更新每個組的引用。 另外,您以后必須僅用一個圖塊整理出組(如果需要)。 但這應該為您指明正確的方向:

for(int y = 0; y < Height; y++)
{
    for(int x = 0; x < Width; x++)
    {
        Tile currentTile = GetTile(x, y);

        if(!currentTile.IsWater)
            continue;

        Tile leftTile = GetLeftNeighbour(currentTile);
        Tile bottomTile = GetBottomNeighbour(currentTile);

        if(!leftTile.IsWater && !bottomTile.IsWater)
        {
            //first case
            Group newGroup = new Group();

            newGroup.Tiles.Add(currentTile);
            currentTile.Group = newGroup;                
        }
        else if(leftTile.IsWater && !bottomTile.IsWater)
        {
            //second case A
            leftTile.Group.Tiles.Add(currentTile);
            currentTile.Group = leftTile.Group;
        }
        else if(!leftTile.IsWater && bottomTile.IsWater)
        {
            //second case B
            bottomTile.Group.Tiles.Add(currentTile);
            currentTile.Group = bottomTile.Group;
        }
        else
        {
            if(leftTile.Group == bottomTile.Group)
            {
                //third case
                leftTile.Group.Tiles.Add(currentTile);
                currentTile.Group = leftTile.Group;
            }
            else
            {
                //fourth case
                leftTile.Group.Merge(bottomTile.Group);

                leftTile.Group.Tiles.Add(currentTile);
                currentTile.Group = leftTile.Group;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM