Пример #1
0
    private Room[] generateMainPath(Room currentRoom)
    {
        Room newRoom;
        int  length = Random.Range(MIN_MAIN_LENGTH, MAX_MAIN_LENGTH + 1);

        Room[] mainPath = new Room[length];
        for (int i = 0; i < length; i++)
        {
            Room.Exits randomExit = getRandomDirection();
            int        control    = 0;
            while (!isPossible(currentX, currentY, randomExit))
            {
                //control variable to avoid infinite loops
                control++;
                if (control == 30)
                {
                    break;
                }
                randomExit = getRandomDirection();
            }
            newRoom = new Room(currentRoom.getNextRoomCoordinate(randomExit), ROOM_SIZE, ROOM_SIZE, floorTiles, wallTiles, tunnelFloorTiles, tunnelWallTiles);
            currentRoom.connectRoom(newRoom, randomExit);
            updateGrid(currentX, currentY, newRoom, randomExit);
            currentRoom = newRoom;
            mainPath[i] = currentRoom;
        }
        currentRoom.setEnd();
        return(mainPath);
    }
Пример #2
0
    //generate shop room, will always be 1 per level.
    private Room generateShop(Room start, Room[] main, Room[] deadEnd)
    {
        int  control = 0;
        Room shop;
        Room forkRoom = pickRandomForkShop(start, main, deadEnd);

        Room.Exits randomExit = getRandomDirection();
        currentX = (int)getRoomPosition(forkRoom).x;
        currentY = (int)getRoomPosition(forkRoom).y;
        while (!isPossible(currentX, currentY, randomExit))
        {
            //control variable to avoid infinite loops
            control++;
            if (control == 30)
            {
                Debug.Log("I failed shop");
                return(generateShop(start, main, deadEnd));
            }
            randomExit = getRandomDirection();
        }
        shop = new Room(forkRoom.getNextRoomCoordinate(randomExit), ROOM_SIZE, ROOM_SIZE, floorTiles, wallTiles, tunnelFloorTiles, tunnelWallTiles);
        forkRoom.connectRoom(shop, randomExit);
        updateGrid(currentX, currentY, shop, randomExit);
        shop.setShop();
        return(shop);
    }
Пример #3
0
 /// <summary>
 /// Builds a tunnel from an Array of Vector2 in a certain direction.
 /// </summary>
 /// <param name="startingPoints"></param>
 /// <param name="tunnelTiles"></param>
 /// <param name="wallTiles"></param>
 /// <param name="direction"></param>
 public Tunnel(Vector2[] startingPoints, GameObject[] tunnelTiles, GameObject[] wallTiles, Room.Exits direction)
 {
     this.startingPoints = startingPoints;
     this.tunnelTiles    = tunnelTiles;
     this.wallTiles      = wallTiles;
     this.direction      = direction;
     this.grid           = new Dictionary <Vector2, GameObject>();
     buildTunnel();
 }
Пример #4
0
    private int updateY(int y, Room.Exits exit)
    {
        switch (exit)
        {
        case Room.Exits.LEFT:
            return(--y);

        case Room.Exits.RIGHT:
            return(++y);
        }
        Debug.Log("Something went wrong with updateY");
        return(0);
    }
Пример #5
0
    private int updateX(int x, Room.Exits exit)
    {
        switch (exit)
        {
        case Room.Exits.UP:
            return(++x);

        case Room.Exits.DOWN:
            return(--x);
        }
        Debug.Log("Something went wrong with updateX");
        return(0);
    }
Пример #6
0
    private Room[] generateDeadEnd(Room start, Room[] main)
    {
        int deadEndLength = Random.Range(1, 4);

        Room[] deadEnd = new Room[deadEndLength];
        Room   newRoom;
        int    totalRooms    = main.Length;
        int    forkRoomIndex = Random.Range(0, totalRooms);
        Room   forkRoom;

        if (forkRoomIndex >= main.Length - 1)
        {
            forkRoom = start;
        }
        else
        {
            forkRoom = main[forkRoomIndex];
        }
        //update current coords to the fork room
        currentX = (int)getRoomPosition(forkRoom).x;
        currentY = (int)getRoomPosition(forkRoom).y;
        for (int i = 0; i < deadEndLength; i++)
        {
            Room.Exits randomExit = getRandomDirection();
            int        control    = 0;
            while (!isPossible(currentX, currentY, randomExit))
            {
                //control variable to avoid infinite loops
                control++;
                if (control == 30)
                {
                    Debug.Log("I failed");
                    break;
                }

                randomExit = getRandomDirection();
            }
            if (control == 30)
            {
                //end the dead end sooner
                break;
            }
            newRoom = new Room(forkRoom.getNextRoomCoordinate(randomExit), ROOM_SIZE, ROOM_SIZE, floorTiles, wallTiles, tunnelFloorTiles, tunnelWallTiles);
            forkRoom.connectRoom(newRoom, randomExit);
            updateGrid(currentX, currentY, newRoom, randomExit);
            forkRoom = newRoom;
            forkRoom.setDeadEnd();
            deadEnd[i] = forkRoom;
        }
        return(deadEnd);
    }
Пример #7
0
    private bool isPossible(int x, int y, Room.Exits exit)
    {
        switch (exit)
        {
        case Room.Exits.UP:
        case Room.Exits.DOWN:
            x = updateX(x, exit);
            break;

        case Room.Exits.LEFT:
        case Room.Exits.RIGHT:
            y = updateY(y, exit);
            break;
        }
        return(x >= 0 && x < this.x && y >= 0 && y < this.y && !slotHasRoom(x, y));
    }
Пример #8
0
    private void updateGrid(int x, int y, Room room, Room.Exits exit)
    {
        switch (exit)
        {
        case Room.Exits.UP:
        case Room.Exits.DOWN:
            x = updateX(x, exit);
            break;

        case Room.Exits.LEFT:
        case Room.Exits.RIGHT:
            y = updateY(y, exit);
            break;
        }
        updateGrid(x, y, room);
    }