gpt4 book ai didi

c# - Unity 2D Tile Map -- Group Tiles By Type C#

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:06 27 4
gpt4 key购买 nike

我正在寻找一种解决方案,以根据游戏 block 的类型对我的游戏 block 进行分组。瓷砖存储在二维数组中,类型为空和水,分组的瓷砖将存储在组类中。

所以如果我有二维数组:

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

如有任何帮助,我们将不胜感激!

最佳答案

我会通过在图 block 中保存组的引用来解决这个问题。遇到水砖时,检查它的左边或底部邻居是否也是水砖。这可能会在四种情况下结束:

  1. 左边或底部都不是水砖(或存在):在当前砖上创建一个新组
  2. left/bottom 是一个water tile:将current添加到left/bottom组
  3. 左边和底部都是同一组的水牌:将当前牌加入他们的牌组
  4. 左边和底部都是不同组中的水砖:现在这些组有联系,需要合并。也将当前添加到该新组

我写了一段代码作为演示,但请注意它是未经测试和简化的。我认为合并两个组有点麻烦,因为您已经更新了每个组的引用。此外,您必须稍后(如果需要)只用一个图 block 对组进行分类。但它应该为您指明正确的方向:

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

关于c# - Unity 2D Tile Map -- Group Tiles By Type C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com