2016-11-01 3 views
0

그래서 특정 크기의 주어진 영역을 바닥 타일로 채우는 코드 조각이 있습니다.Unity Rectangle Overlapping

while (roomsPlaced < roomCount.maximum) 
{ 
    Vector3 randomPosition = RandomPosition(); 
    int roomHeight = GetRandomNumber(8, 15); 
    int roomWidth = GetRandomNumber(6, 15); 

    if (OutOfMap(randomPosition, roomHeight,roomWidth)) 
    { 
     continue; 
    } 

    if (roomsPlaced > 0) { 
     if (Overlaps(new Rect(randomPosition.x, randomPosition.y, roomWidth, roomHeight), roomPositions[roomPositions.Count -1])) 
      continue; 
    } 

    roomPositions.Add(new Rect(randomPosition.x, randomPosition.y, roomWidth, roomHeight)); 

    for (int x = (int)randomPosition.x; x <= (int)randomPosition.x + roomWidth; x++) 
    { 
     for (int y = (int)randomPosition.y; y <= (int)randomPosition.y + roomHeight; y++) 
     { 
      if (x == randomPosition.x || y == randomPosition.y) 
       toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)]; 

      GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject; 

      instance.transform.SetParent(boardHolder); 
     } 
    } 

    roomsPlaced++; 
} 

그리고 현재 직사각형이 목록의 마지막 사각형과 겹치지 않는지 확인해야하는 기능입니다.

bool Overlaps(Rect rA, Rect rB) 
{ 
    return (rA.x < rB.x + rB.width && rA.x + rA.width > rB.x && rA.y < rB.y + rB.height && rA.y + rA.height > rB.y); 
} 

그러나 여전히 일부 객실이 겹칠 때도 문제가 있습니다. 무엇이 잘못 되었습니까?이 문제를 해결하려면 어떻게해야합니까?

답변

0

대신 Overlaps 방법을 사용할 필요가 같은이 소리 ...

+0

나는 그것을 사용하려고했지만 같은 문제가있었습니다. –

+0

이것은 답변이 아니라 주석이어야합니다. – FakeCaleb

+0

의견을 작성하는 데 필요한 담당자가 없습니다. 아래쪽 투표를 주셔서 감사합니다 :/ –

0

Yeeah. 방금 목록에서 마지막 요소를 선택하는 대신 전체 목록을 검토하여 문제를 해결했습니다.