int CreatePassage(MAP.Vector2D doorPos, MAP.DIRECTION direction)
    {
        int dir   = 0;
        int coord = 0;

        MAP.SPACE curSpace;
        if (direction == MAP.DIRECTION.UP || direction == MAP.DIRECTION.DOWN)
        {
            dir = direction == MAP.DIRECTION.UP ? -1 : 1;
            while (true)
            {
                ++coord;
                curSpace = Map[doorPos.y + (coord * dir)][doorPos.x];

                Map[doorPos.y + (coord * dir)][doorPos.x - 1] = Map[doorPos.y + (coord * dir)][doorPos.x - 1] | MAP.SPACE.PASSAGE_WALL;
                Map[doorPos.y + (coord * dir)][doorPos.x]     = Map[doorPos.y + (coord * dir)][doorPos.x] | MAP.SPACE.PASSAGE_FLOOR;
                Map[doorPos.y + (coord * dir)][doorPos.x + 1] = Map[doorPos.y + (coord * dir)][doorPos.x + 1] | MAP.SPACE.PASSAGE_WALL;

                if ((curSpace & MAP.SPACE.BLOCK) == MAP.SPACE.BLOCK)
                {
                    break;
                }

                if ((curSpace & MAP.SPACE.PASSAGE_WALL) == MAP.SPACE.PASSAGE_WALL)
                {
                    Map[doorPos.y + (coord * dir)][doorPos.x] = MAP.SPACE.PASSAGE_FLOOR;
                    ++coord;
                    break;
                }
            }
            coord = doorPos.y + (coord * dir);
        }
        else
        {
            dir = direction == MAP.DIRECTION.RIGHT ? -1 : 1;
            while (true)
            {
                ++coord;
                curSpace = Map[doorPos.y][doorPos.x + (coord * dir)];

                Map[doorPos.y - 1][doorPos.x + (coord * dir)] = Map[doorPos.y - 1][doorPos.x + (coord * dir)] | MAP.SPACE.PASSAGE_WALL;
                Map[doorPos.y][doorPos.x + (coord * dir)]     = Map[doorPos.y][doorPos.x + (coord * dir)] | MAP.SPACE.PASSAGE_FLOOR;
                Map[doorPos.y + 1][doorPos.x + (coord * dir)] = Map[doorPos.y + 1][doorPos.x + (coord * dir)] | MAP.SPACE.PASSAGE_WALL;

                if ((curSpace & MAP.SPACE.BLOCK) == MAP.SPACE.BLOCK)
                {
                    break;
                }

                if ((curSpace & MAP.SPACE.PASSAGE_WALL) == MAP.SPACE.PASSAGE_WALL)
                {
                    Map[doorPos.y][doorPos.x + (coord * dir)] = MAP.SPACE.PASSAGE_FLOOR;
                    ++coord;
                    break;
                }
            }
            coord = doorPos.x + (coord * dir);
        }
        return(coord);
    }
    void ConnectRoom(MAP.ROOM lRoom, MAP.ROOM rRoom, MAP.DIRECTION lDir, MAP.DIRECTION rDir)
    {
        bool createLDoor = lRoom.CreateDoor(lDir);
        bool createRDoor = rRoom.CreateDoor(rDir);

        MAP.Vector2D lDoor = lRoom.GetDoorPosition(lDir);
        MAP.Vector2D rDoor = rRoom.GetDoorPosition(rDir);
        Map[lDoor.y][lDoor.x] = MAP.SPACE.ROOM_DOOR;
        Map[rDoor.y][rDoor.x] = MAP.SPACE.ROOM_DOOR;

        //직선인 경우
        if (lDoor.y == rDoor.y)
        {
            int x = 1;
            do
            {
                Map[lDoor.y - 1][lDoor.x + x] = Map[lDoor.y - 1][lDoor.x + x] | MAP.SPACE.PASSAGE_WALL;
                Map[lDoor.y][lDoor.x + x]     = Map[lDoor.y][lDoor.x + x] | MAP.SPACE.PASSAGE_FLOOR;
                Map[lDoor.y + 1][lDoor.x + x] = Map[lDoor.y + 1][lDoor.x + x] | MAP.SPACE.PASSAGE_WALL;
                ++x;
            } while (lDoor.x + x != rDoor.x);
        }

        else if (lDoor.x == rDoor.x)
        {
            int y = 1;
            do
            {
                Map[lDoor.y + y][lDoor.x - 1] = Map[lDoor.y + y][lDoor.x - 1] | MAP.SPACE.PASSAGE_WALL;
                Map[lDoor.y + y][lDoor.x]     = Map[lDoor.y + y][lDoor.x] | MAP.SPACE.PASSAGE_FLOOR;
                Map[lDoor.y + y][lDoor.x + 1] = Map[lDoor.y + y][lDoor.x + 1] | MAP.SPACE.PASSAGE_WALL;
                ++y;
            } while (lDoor.y + y != rDoor.y);
        }
        //직선이 아닌 경우
        else
        {
            int pos = 0;
            if (createLDoor)
            {
                pos = CreatePassage(lDoor, lDir);
            }
            if (createRDoor)
            {
                pos = CreatePassage(rDoor, rDir);
            }

            ConnectPassage(pos, lDoor, rDoor, lDir);
        }
    }
    public bool IsClose(MAP.BLOCK b1, MAP.BLOCK b2)
    {
        MAP.Vector2D b1_RB = b1.rightBot + 2;
        MAP.Vector2D b2_LT = b2.leftTop - 2;
        //Left Right
        if (b1_RB.x - b2_LT.x == 2)
        {
            return(true);
        }
        //Up Down
        if (b1_RB.y - b2_LT.y == 2)
        {
            return(true);
        }

        return(false);
    }
    public bool GenerateRoom()
    {
        if (blockInfo.size.x < MAP.CONTANT.RoomX_Min + MAP.CONTANT.Block_Blank || blockInfo.size.y < MAP.CONTANT.RoomY_Min + MAP.CONTANT.Block_Blank)
        {
            return(false);
        }

        roomInfo = new MAP.ROOM();

        int MaxX = blockInfo.size.x > (MAP.CONTANT.RoomX_Max + MAP.CONTANT.Block_Blank) ? MAP.CONTANT.RoomX_Max : (blockInfo.size.x - MAP.CONTANT.Block_Blank);
        int MaxY = blockInfo.size.y > (MAP.CONTANT.RoomY_Max + MAP.CONTANT.Block_Blank) ? MAP.CONTANT.RoomY_Max : (blockInfo.size.y - MAP.CONTANT.Block_Blank);

        MAP.Vector2D roomSize = new MAP.Vector2D(Random.Range(MAP.CONTANT.RoomX_Min, MaxX + 1), Random.Range(MAP.CONTANT.RoomY_Min, MaxY + 1));
        //blank minimum is 2
        MAP.Vector2D blank = new MAP.Vector2D(blockInfo.size - roomSize - 1);
        roomInfo.leftTop.SetVector2D(blockInfo.leftTop.x + Random.Range(1, blank.x + 1), blockInfo.leftTop.y + Random.Range(1, blank.y + 1));
        roomInfo.rightBot.SetVector2D(roomInfo.leftTop + roomSize - 1);

        Debug.Log(index + " / " + blockInfo.leftTop.x + " , " + blockInfo.leftTop.y + " / " + roomSize.x + " / " + roomSize.y);
        //Debug.Log(blockInfo.leftTop.x + " , " + blockInfo.leftTop.y + " / " + blockInfo.rightBot.x + " , " + blockInfo.rightBot.y + " / " + blockInfo.size.x + " , " + blockInfo.size.y);
        return(true);
    }
 public NODE(int _index, int _level, MAP.Vector2D bLeftTop, MAP.Vector2D bRightBot)
 {
     blockInfo = new MAP.BLOCK(bLeftTop, bRightBot);
     index     = _index;
     level     = _level;
 }
    void ConnectPassage(int pos, MAP.Vector2D lDoor, MAP.Vector2D rDoor, MAP.DIRECTION lDir)
    {
        int coord = 0;
        int small = 0;
        int large = 0;

        //왼쪽 문 방향이 아래 = 횡으로  연결
        if (lDir == MAP.DIRECTION.DOWN)
        {
            small = lDoor.x < rDoor.x ? lDoor.x : rDoor.x;
            large = lDoor.x > rDoor.x ? lDoor.x : rDoor.x;

            if ((Map[pos - 1][small + coord] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
            {
                Map[pos - 1][small + coord] = MAP.SPACE.PASSAGE_WALL;
            }

            if ((Map[pos + 1][small + coord] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
            {
                Map[pos + 1][small + coord] = MAP.SPACE.PASSAGE_WALL;
            }

            Map[pos][small + coord] = Map[pos][small + coord] | MAP.SPACE.PASSAGE_FLOOR;
            ++coord;

            while (true)
            {
                if (small + coord == large)
                {
                    if ((Map[pos - 1][small + coord] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
                    {
                        Map[pos - 1][small + coord] = (Map[pos - 1][small + coord] | MAP.SPACE.PASSAGE_WALL);
                    }

                    if ((Map[pos + 1][small + coord] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
                    {
                        Map[pos + 1][small + coord] = (Map[pos + 1][small + coord] | MAP.SPACE.PASSAGE_WALL);
                    }

                    Map[pos][small + coord] = Map[pos][small + coord] | MAP.SPACE.PASSAGE_FLOOR;
                    break;
                }

                Map[pos - 1][small + coord] = Map[pos - 1][small + coord] | MAP.SPACE.PASSAGE_WALL;
                Map[pos][small + coord]     = Map[pos][small + coord] | MAP.SPACE.PASSAGE_FLOOR;
                Map[pos + 1][small + coord] = Map[pos + 1][small + coord] | MAP.SPACE.PASSAGE_WALL;
                ++coord;
            }
        }
        else
        {
            small = lDoor.y < rDoor.y ? lDoor.y : rDoor.y;
            large = lDoor.y > rDoor.y ? lDoor.y : rDoor.y;

            if ((Map[small + coord][pos - 1] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
            {
                Map[small + coord][pos - 1] = Map[small + coord][pos - 1] | MAP.SPACE.PASSAGE_WALL;
            }

            if ((Map[small + coord][pos + 1] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
            {
                Map[small + coord][pos + 1] = Map[small + coord][pos + 1] | MAP.SPACE.PASSAGE_WALL;
            }

            Map[small + coord][pos] = Map[small + coord][pos] | MAP.SPACE.PASSAGE_FLOOR;
            ++coord;

            while (true)
            {
                if (small + coord == large)
                {
                    if ((Map[small + coord][pos - 1] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
                    {
                        Map[small + coord][pos - 1] = Map[small + coord][pos - 1] | MAP.SPACE.PASSAGE_WALL;
                    }

                    if ((Map[small + coord][pos + 1] & MAP.SPACE.PASSAGE_FLOOR) != MAP.SPACE.PASSAGE_FLOOR)
                    {
                        Map[small + coord][pos + 1] = Map[small + coord][pos + 1] | MAP.SPACE.PASSAGE_WALL;
                    }

                    Map[small + coord][pos] = Map[small + coord][pos] | MAP.SPACE.PASSAGE_FLOOR;
                    break;
                }

                Map[small + coord][pos - 1] = Map[small + coord][pos - 1] | MAP.SPACE.PASSAGE_WALL;
                Map[small + coord][pos]     = Map[small + coord][pos] | MAP.SPACE.PASSAGE_FLOOR;
                Map[small + coord][pos + 1] = Map[small + coord][pos + 1] | MAP.SPACE.PASSAGE_WALL;
                ++coord;
            }
        }
    }