Skip to content

Instantly share code, notes, and snippets.

@neiled
Last active August 29, 2015 13:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save neiled/8869350 to your computer and use it in GitHub Desktop.
public void AddDoors(Level level)
{
for (int y = RoomStart.Y - 1; y <= RoomStart.Y + Height; y++)
{
for (int x = RoomStart.X-1; x <= RoomStart.X + Width; x ++)
{
if (y != RoomStart.Y - 1 && y != RoomStart.Y + Height && x != RoomStart.X - 1 && x != RoomStart.X + Width)
continue;
if (y < 0) continue;
if (y >= Level.Height) continue;
if (x < 0) continue;
if (x >= Level.Width) continue;
Area currentArea = level.Areas[y, x];
int floorCount = countFloorsAround(currentArea);
if(floorCount == 2)
currentArea.AreaType = Area.AreaTypeEnum.Water;
}
}
}
private int countFloorsAround(Area currentArea)
{
int count = 0;
if (currentArea.AreaType != Area.AreaTypeEnum.Empty)
return 0;
count++;
count += getAreaCount(currentArea, World.Direction.North);
count += getAreaCount(currentArea, World.Direction.East);
count += getAreaCount(currentArea, World.Direction.South);
count += getAreaCount(currentArea, World.Direction.West);
return count;
}
private int getAreaCount(Area currentArea, World.Direction direction)
{
Area areaToCheck = currentArea.GetDirection(direction);
if (areaWithinRoom(areaToCheck))
return 0;
if (areaToCheck != null && areaToCheck.AreaType == Area.AreaTypeEnum.Empty)
return 1;
return 0;
}
private bool areaWithinRoom(Area area)
{
if (area.Location.X < this.RoomStart.X || area.Location.Y < this.RoomStart.Y)
return false;
if (area.Location.X >= this.RoomStart.X + this.Width || area.Location.Y >= this.RoomStart.Y + this.Height)
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment