Код: Выделить всё
private Room FindClosestPossibleRoomForMainRoom(Vector3Int pos1, bool top)
{
int distance, minDistance;
Room closestRoom = new();
List remainingRooms = new(rooms);
bool collapse = true;
minDistance = int.MaxValue;
int maxTry = remainingRooms.Count;
while (collapse)
{
if (maxTry == 0 || remainingRooms.Count == 0) break;
foreach (var room2 in remainingRooms)
{
distance = Math.Abs(pos1.x - room2.pos.x) + Math.Abs(pos1.y - room2.pos.y) + Math.Abs(pos1.z - room2.pos.z);
if (distance < minDistance)
{
minDistance = distance;
closestRoom = room2;
//Debug.Log(closestRoom.id + " " + maxTry);
}
}
collapse = CheckMainPath(pos1, closestRoom.pos, top);
//Debug.Log(collapse);
if (collapse) remainingRooms.Remove(closestRoom); // won't remove even if collapse is true
maxTry--;
}
//Debug.Log("---");
return closestRoom;
}
Код: Выделить всё
private class Room
{
public int id;
public Vector3Int pos, size; // pos.y = 0
private Vector3Int hallwaySize;
private bool left = false, right = false, top = false, bottom = false, celling;
private GameObject prefab;
private Material[] materials;
public Room()
{
id = -1;
}
public Room(Vector3Int pos)
{
id = -1;
this.pos = pos;
}
public Room(int id, Vector3Int pos, Vector3Int size, Vector3Int hallwaySize, GameObject prefab, Material[] materials, bool celling)
{
this.id = id;
this.pos = pos;
this.size = size;
this.prefab = prefab;
this.hallwaySize = hallwaySize;
this.materials = materials;
this.celling = celling;
}
public void SetEntrance(Way way)
{
// bla bla bla
}
public List GetBlocks()
{
// bla bla bla
}
public override bool Equals(object obj)
{
if (obj is Room other)
{
return id == other.id;
}
return false;
}
public override int GetHashCode()
{
return id.GetHashCode();
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... m-the-list
Мобильная версия