【问题标题】:Replacing hexagons with destroyed hexagons用被破坏的六边形替换六边形
【发布时间】:2019-08-30 15:52:13
【问题描述】:

我想用其他站立的六边形替换被破坏的六边形。现有的六边形应该从顶部落下。例如,如果我在下图中破坏 (0,2) 定位的六边形,则该六边形的位置为 (0,0) 的左上六边形应该移动到 (0,2) 位置,我应该创建一个新的六边形和把它放在现在是空的 (0,0) 上,因为我们之前将 (0,0) 上的六边形移动到 (0,2)。

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

--重要--

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

更好地解释我想做什么的视频

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

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

六边形坐标系(忽略红色箭头)

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

        }
    }

【问题讨论】:

  • 所有六边形相互交换后的空白处,我们应该创建新的并从顶部填充空白处

标签: c# unity3d


【解决方案1】:

代码解释在cmets中:

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 更改为 Vector2IntList,这样您就可以在 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;
    }
}

就移动格子而言,我会在HexCoordinatesUpdate 中添加这个:

float fallSpeed = 0.5f;

Vector2 goalWorldPosition = GS.CalcWorldPos(Coordinates);

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

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 我编辑了您的代码以修复错误。我认为这一行不应该有偏移: for (int x = 0 ; x
  • @FaTaLL 哦,我没有看到偏移的长度相同。傻我。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多