用损坏的六角形替换六角形

时间:2019-08-30 15:52:13

标签: c# unity3d

我想用其他站立的六角形替换损坏的六角形。现有的六边形应从顶部掉落。例如,如果我销毁下图中的(0,2)六角形,则该六角形位置为(0,0)的左上六角形应移至(0,2)位置,并且我应创建一个新的六角形并将其放在现在为空的(0,0)上,因为我们将(0,0)上的六边形移到了(0,2)上。

我有一个二维数组,用于存储六边形的所有参考,并带有六边形坐标(x,y)的索引。

-重要-

移动对象并不重要。重要的是我们必须知道哪个六角形将被另一个六角形替换。我们必须告诉ARRAY,我们已更改了这些六边形,而刚移动或创建的六边形在其新(x,y)位置的索引中应该恰好具有一个参考。

更好地解释我想做的事情的视频

https://www.youtube.com/watch?v=QYhq0qwFmmY

任何想法或帮助将不胜感激!

六角坐标系(忽略红色箭头) Hexagon Coordinates

Hexagon Grid

public void CreateGrid(int gridWidth, int gridHeight)
    {
        for (int y = 0; y < gridHeight; y++)
        {
            for (int x = 0; x < gridWidth; x++)
            {
                GameObject Hexagon = Instantiate(HexagonPre, Vector2.zero, Quaternion.identity, HexGrid);

                    int RandColor = Random.Range(0, 5);
                    if (RandColor == 0)
                    {
                        Hexagon.GetComponent<SpriteRenderer>().color = Color.blue;
                    }
                    else if (RandColor == 1)
                    {
                        Hexagon.GetComponent<SpriteRenderer>().color = Color.red;
                    }
                    else if (RandColor == 2)
                    {
                        Hexagon.GetComponent<SpriteRenderer>().color = Color.green;
                    }
                    else if (RandColor == 3)
                    {
                        Hexagon.GetComponent<SpriteRenderer>().color = Color.yellow;
                    }
                    else if (RandColor == 4)
                    {
                        Hexagon.GetComponent<SpriteRenderer>().color = Color.cyan;
                    }

                Vector2 gridPos = new Vector2(x, y);
                Hexagon.transform.position = CalcWorldPos(gridPos);
                Hexagon.GetComponent<HexCoordinates>().Coordinates = new Vector2Int(x, y);
                Hexagon.transform.name = "X: " + x + " | Y: " + y;
            }
        }
    }

销毁六边形的代码

   if (MatchedColors == 2)
                    {
                        if(!HexToBeDestroyed.Contains(Hexagons[x, y].gameObject))
                        HexToBeDestroyed.Add(Hexagons[x, y].gameObject);

                        if (!HexToBeDestroyed.Contains(Hexagons[x - 1, y].gameObject))
                            HexToBeDestroyed.Add(Hexagons[x - 1, y].gameObject);

                        if (!HexToBeDestroyed.Contains(Hexagons[x - 1, y - 1].gameObject))
                            HexToBeDestroyed.Add(Hexagons[x - 1, y - 1].gameObject);
                    }

                    MatchedColors = 0;
                }

            }

        }
    }


    foreach (GameObject G in HexToBeDestroyed)
    {
        if (G != null)
        {
            Destroy(G.gameObject);

        }
    }

1 个答案:

答案 0 :(得分:1)

代码说明在注释中

void HexagonFall(GameObject[,] hexArray)
{
    // Handle fall for base columns and for offset columns
    for (int offset = 0 ; offset < 2 ; offset++)
    {
        // Handle fall for each column at current offset
        for (int x = 0 ; x < hexArray.GetLength(0) ; x++)
        {
            int bottomYIndex = hexArray.GetLength(1) - offset - 1;

            // List of indices of where each hexagon in that column will come from.
            // We will fill from bottom to top.
            List<Vector2Int> sourceIndices = new List<Vector2Int>();

            for (int y = bottomYIndex ; y >= 0 ; y-=2)
            {
                // HexExists returns true if the hex isn't empty. 
                // Something along the lines of ` return input!=null; `
                // depending on what "empty" hexes look like in the array

                if (HexExists(hexArray[x,y]))
                {
                    sourceIndices.Add(new Vector2Int(x,y));
                }
            }

            // We have a list of where to get each bottom hexes from, now do the move/create
            for (int y = bottomYIndex; y >= 0 ; y-=2)
            {
                if (sourceIndices.Count > 0)
                {
                    // If we have any available hexes in column,
                    // use the bottommost one (at index 0)
                    hexArray[x,y] = hexArray[sourceIndices[0].x, sourceIndices[0].y];

                    // We have now found a home for hex previously at sourceIndices[0].
                    // Remove that index from list so hex will stay put.
                    sourceIndices.RemoveAt(0);
                }
                else 
                {
                    // Otherwise, we need to generate a new hex
                    hexArray[x,y] = MakeNewHexAt(new Vector2Int(x,y));
                }

                // Tell the hex about its new home
                hexArray[x,y].GetComponent<HexCoordinates>().Coordinates = new Vector2Int(x, y);
                hexArray[x,y].transform.name = "X: " + x + " | Y: " + y;
            }            
        }
    }
}

在您的十六进制销毁代码中,我将HexToBeDestroyed更改为List的{​​{1}},因此您可以在Vector2Int游戏对象时立即将数组引用设置为null:

Destroy

就移动六角形而言,我将其添加到List<Vector2Int> HexToBeDestroyed = new List<Vector2Int>(); // ... if (MatchedColors == 2) { if(!HexToBeDestroyed.Contains(new Vector2Int(x, y)) HexToBeDestroyed.Add(new Vector2Int(x, y)); if (!HexToBeDestroyed.Contains(new Vector2Int(x - 1, y)) HexToBeDestroyed.Add(new Vector2Int(x - 1, y)); if (!HexToBeDestroyed.Contains(new Vector2Int(x - 1, y - 1))) HexToBeDestroyed.Add(new Vector2Int(x - 1, y - 1)); } // ... foreach (Vector2Int V in HexToBeDestroyed) { if (Hexagons[V.x,V.y] != null) { Destroy(Hexagons[V.x,V.y]); Hexagons[V.x,V.y] = null; } } 的{​​{1}}中:

Update