Bu konuda biraz yardım almayı umuyordum. Tek gerçek sorun şu ki bu kod olabildiğince verimli değil. Temelde bir dizi fayans var ve etrafındaki fayansların kombinasyonuna dayanarak ne tür fayansların oluşturulacağına karar vermem gerekiyor. Aşağıdaki kodda, kontrol ettiğim karonun konumu benim haritamdaki anahtardır (ex map [sağ] sağdaki kiremittir, topLeft üst köşeye ve üst2'ye çapraz olarak bitişik karodur. Vb.) bu yönde iki boşluk. Söz konusuBir if-elseif-else ifadesiyle yapılan karşılaştırmaları kaydetmek için
kod: Ben bunun yerine, her konumunu kontrol 0-2 it a numara atamak, ondalık için tabandan 3 bu sayıyı dönüştürmek düşündüm çünkü bir tablo yapmıştı
if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.NOTHING)
{
Instantiate(wallEdgeTopLeftInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.NOTHING)
{
Instantiate(wallEdgeTopRightInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.WALL && map[top].Type == TileType.NOTHING && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeBottomLeftInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeBottomRightInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.WALL && map[top].Type == TileType.FLOOR && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeCornerTopRightTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.FLOOR && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeCornerTopLeftTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[topRight].Type == TileType.NOTHING)
{
Instantiate(wallEdgeCornerBottomLeftTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[topLeft].Type == TileType.NOTHING)
{
Instantiate(wallEdgeCornerBottomRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL))
{
Instantiate(wallTopLeftTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL))
{
Instantiate(wallTopRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallBottomLeftTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallBottomRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallMidLeftTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallMidRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[bottom2].Type == TileType.WALL))
{
Instantiate(wallTopTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallBottomTile, t.Position, Quaternion.identity);
}
else if (map[top].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeBottomTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.NOTHING && map[right].Type == TileType.FLOOR) || (map[left].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[top].Type != TileType.NOTHING))
{
Instantiate(wallEdgeRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.FLOOR && map[right].Type == TileType.NOTHING) || (map[left].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[top].Type != TileType.NOTHING))
{
Instantiate(wallEdgeLeftTile, t.Position, Quaternion.identity);
}
else if (map[bottom].Type == TileType.NOTHING && map[top].Type == TileType.FLOOR)
{
Instantiate(wallEdgeTopTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallMiddleTile, t.Position, Quaternion.identity);
}
else
{
// Should never get here, so if a white Tile is seen, something went wrong!
Instantiate(whiteTile, t.Position, Quaternion.identity);
}
, Daha sonra, hangi çini oluşturulacağını belirlemek için sonuç üzerinde bir anahtar deyimi kullanın. Sayı kodları çok doğru. Ama çok fazla kombinasyon var, daha iyi ya da daha kolay bir yol olmalı diye düşünüyorum. Herhangi bir fikir takdir edilmektedir!
Tablo: Table of conditions
Bir Anahtar deyimi kullanın http://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case – LumbusterTick
Bir anahtar ifadesinin sorunumu çözeceğimi düşünmüyorum çünkü denetlemem gerekiyor sadece belirli bir değerden ziyade değer kombinasyonları için. – user3754585
Taşı "Instantiate ([value], t.Position, Quaternion.identity)"; kontrol yapısının sonuna. [value] değerini bir değişkene ayarlayın, bu şekilde sadece "Instantiate()" ile bir satırınız olur. Ayrıca, işlenenleri tekrarlıyorsunuz (ör. Harita [sol] .Type == TileType.WALL). Her bir eşitlik karşılaştırıcısının sadece bir kez gerçekleştiği yeri bulmaya çalışın. –