【发布时间】: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);
}
}
【问题讨论】:
-
所有六边形相互交换后的空白处,我们应该创建新的并从顶部填充空白处