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

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

- 重要 -

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

更好地解释我想做的视频

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

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

六角坐标系(忽略红色箭头)
c# - 用损坏的六角形替换六角形-LMLPHP

c# - 用损坏的六角形替换六角形-LMLPHP

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);

        }
    }

最佳答案

代码说明在注释中:

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更改为ListVector2Int,因此您可以在Destroy游戏对象时立即将数组引用设置为null:

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;
    }
}


就移动六角形而言,我将其添加到UpdateHexCoordinates中:

float fallSpeed = 0.5f;

Vector2 goalWorldPosition = GS.CalcWorldPos(Coordinates);

transform.position = Vector2.MoveTowards(transform.position, goalWorldPosition, fallSpeed * Time.deltaTime);

关于c# - 用损坏的六角形替换六角形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57729875/

10-12 06:16