Assets/Scripts/Digger.cs
/// <summary>
/// Creates a room centered around a location by clearing out a square area
/// </summary>
/// <param name="center">The location that should be at the center of the room</param>
/// <param name="roomSize">The dimension of the room along each side</param>
Creates a room centered around a location by clearing out a square area
Creates a room centered around a location by clearing out a square area
protected void CreateRoom(Vector2 center, int roomSize)
    {
        var left = Mathf.FloorToInt(center.x - (roomSize / 2));
        var right = Mathf.FloorToInt(center.x + (roomSize / 2));
        var top = Mathf.FloorToInt(center.y + (roomSize / 2));
        var bottom = Mathf.FloorToInt(center.y - (roomSize / 2));
        if (left < 0)
            left = 0;
        if (right >= LevelManager.instance.width)
            right = LevelManager.instance.width - 1;
        if (bottom < 0)
            bottom = 0;
        if (top >= LevelManager.instance.height)
            top = LevelManager.instance.height - 1;
        var protectCol = false;
        var protectRow = false;
        var protectIx = -1;
        for (int i = left; i <= right; i++)
        {
            for (int j = bottom; j <= top; j++)
            {
                if(LevelManager.instance.GetTileAt(i, j) == LevelManager.CELL_DOOR)
                {
                    if((i - 1 >= 0 && LevelManager.instance.GetTileAt(i - 1, j) != LevelManager.CELL_BLOCK) ||
                        (i + 1 < LevelManager.instance.width && LevelManager.instance.GetTileAt(i + 1, j) != LevelManager.CELL_BLOCK)) {
                        protectCol = true;
                        protectIx = i;
                        break;
                    }
                    else if ((j - 1 >= 0 && LevelManager.instance.GetTileAt(i, j - 1) != LevelManager.CELL_BLOCK) ||
                        (j + 1 < LevelManager.instance.height && LevelManager.instance.GetTileAt(i, j + 1) != LevelManager.CELL_BLOCK))
                    {
                        protectRow = true;
                        protectIx = j;
                        break;
                    }
                }
            }
            if (protectCol || protectRow) break;
        }  
        for (int x = left; x <= right; x++)
        {
            if (protectCol && x == protectIx) continue;
            for (int y = bottom; y <= top; y++)
            {
                if (protectRow && y == protectIx) continue;
                if(LevelManager.instance.GetTileAt(x, y) == LevelManager.CELL_BLOCK)
                {
                    LevelManager.instance.SetTileAt(x, y, LevelManager.CELL_OPEN);
                }
            }
        }
    }
protected void CreateRoom ( Vector2 center , int roomSize ) { var left = Mathf . FloorToInt ( center . x - ( roomSize / 2 ) ) ; var right = Mathf . FloorToInt ( center . x + ( roomSize / 2 ) ) ; var top = Mathf . FloorToInt ( center . y + ( roomSize / 2 ) ) ; var bottom = Mathf . FloorToInt ( center . y - ( roomSize / 2 ) ) ; if ( left < 0 ) left = 0 ; if ( right >= LevelManager . instance . width ) right = LevelManager . instance . width - 1 ; if ( bottom < 0 ) bottom = 0 ; if ( top >= LevelManager . instance . height ) top = LevelManager . instance . height - 1 ; var protectCol = false ; var protectRow = false ; var protectIx = - 1 ; for ( int i = left ; i <= right ; i ++ ) { for ( int j = bottom ; j <= top ; j ++ ) { if ( LevelManager . instance . GetTileAt ( i , j ) == LevelManager . CELL_DOOR ) { if ( ( i - 1 >= 0 && LevelManager . instance . GetTileAt ( i - 1 , j ) != LevelManager . CELL_BLOCK ) || ( i + 1 < LevelManager . instance . width && LevelManager . instance . GetTileAt ( i + 1 , j ) != LevelManager . CELL_BLOCK ) ) { protectCol = true ; protectIx = i ; break ; } else if ( ( j - 1 >= 0 && LevelManager . instance . GetTileAt ( i , j - 1 ) != LevelManager . CELL_BLOCK ) || ( j + 1 < LevelManager . instance . height && LevelManager . instance . GetTileAt ( i , j + 1 ) != LevelManager . CELL_BLOCK ) ) { protectRow = true ; protectIx = j ; break ; } } } if ( protectCol || protectRow ) break ; } for ( int x = left ; x <= right ; x ++ ) { if ( protectCol && x == protectIx ) continue ; for ( int y = bottom ; y <= top ; y ++ ) { if ( protectRow && y == protectIx ) continue ; if ( LevelManager . instance . GetTileAt ( x , y ) == LevelManager . CELL_BLOCK ) { LevelManager . instance . SetTileAt ( x , y , LevelManager . CELL_OPEN ) ; } } } }
Creates a room centered around a location by clearing out a square area
Creates a room centered around a location by clearing out a square area
// Try to find a door tile in the area we're clearing. We will protect the hallway it lives // in from being cleared out // Find the axis of the door's hallway
center
Vector2
The location that should be at the center of the room
The location that should be at the center of the room
roomSize
int
The dimension of the room along each side
The dimension of the room along each side