sách gpt4 ai đã đi

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

In lại 作者:太空宇宙 更新时间:2023-11-03 15:08:06 29 4
mua khóa gpt4 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();
List groupingTiles = new List();

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


Debug.Log(m_Groups.Count);
}

Bất kỳ sự giúp đỡ nào cũng sẽ được đánh giá cao!

1 Câu trả lời

我会通过在图 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;
}
khác
{
if(leftTile.Group == bottomTile.Group)
{
//third case
leftTile.Group.Tiles.Add(currentTile);
currentTile.Group = leftTile.Group;
}
khác
{
//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/

29 4 0
Bài viết được đề xuất: c# - 有没有更好的移动方式?
Bài viết được đề xuất: python - 在 cython 中将 C++ 对象转换为 python 对象?
Bài viết được đề xuất: 用于多行匹配的Python正则表达式命名组
Bài viết được đề xuất: c# - 在 UWP 上创建可拖动的 map 图钉
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com