示例#1
0
    // This is used for the first room.  It does not have a Corridor parameter since there are no corridors yet.
    public void SetupRoom(GenerationParams gParams)
    {
        // Set a random width and height.
        roomWidth  = gParams.roomWidth.Random;
        roomHeight = gParams.roomHeight.Random;

        // Set the x and y coordinates so the room is roughly in the middle of the board.
        xPos = Mathf.RoundToInt(gParams.columns / 2f - roomWidth / 2f);
        yPos = Mathf.RoundToInt(gParams.rows / 2f - roomHeight / 2f);
    }
示例#2
0
    // This is an overload of the SetupRoom function and has a corridor parameter that represents the corridor entering the room.
    public void SetupRoom(GenerationParams gParams, Corridor corridor)
    {
        // Set the entering corridor direction.
        enteringCorridor = corridor.direction;

        // Set random values for width and height.
        roomWidth  = gParams.roomWidth.Random;
        roomHeight = gParams.roomHeight.Random;

        switch (corridor.direction)
        {
        // If the corridor entering this room is going north...
        case Direction.North:
            // ... the height of the room mustn't go beyond the board so it must be clamped based
            // on the height of the board (rows) and the end of corridor that leads to the room.
            roomHeight = Mathf.Clamp(roomHeight, 1, gParams.rows - corridor.EndPositionY);

            // The y coordinate of the room must be at the end of the corridor (since the corridor leads to the bottom of the room).
            yPos = corridor.EndPositionY;

            // The x coordinate can be random but the left-most possibility is no further than the width
            // and the right-most possibility is that the end of the corridor is at the position of the room.
            xPos = Random.Range(corridor.EndPositionX - roomWidth + 1, corridor.EndPositionX);

            // This must be clamped to ensure that the room doesn't go off the board.
            xPos = Mathf.Clamp(xPos, 0, gParams.columns - roomWidth);
            break;

        case Direction.East:
            roomWidth = Mathf.Clamp(roomWidth, 1, gParams.columns - corridor.EndPositionX);
            xPos      = corridor.EndPositionX;

            yPos = Random.Range(corridor.EndPositionY - roomHeight + 1, corridor.EndPositionY);
            yPos = Mathf.Clamp(yPos, 0, gParams.rows - roomHeight);
            break;

        case Direction.South:
            roomHeight = Mathf.Clamp(roomHeight, 1, corridor.EndPositionY);
            yPos       = corridor.EndPositionY - roomHeight + 1;

            xPos = Random.Range(corridor.EndPositionX - roomWidth + 1, corridor.EndPositionX);
            xPos = Mathf.Clamp(xPos, 0, gParams.columns - roomWidth);
            break;

        case Direction.West:
            roomWidth = Mathf.Clamp(roomWidth, 1, corridor.EndPositionX);
            xPos      = corridor.EndPositionX - roomWidth + 1;

            yPos = Random.Range(corridor.EndPositionY - roomHeight + 1, corridor.EndPositionY);
            yPos = Mathf.Clamp(yPos, 0, gParams.rows - roomHeight);
            break;
        }
    }
示例#3
0
    bool ChooseGenerationParams(bool playerDied)
    {
        if (playerDied)
        {
            nextLevel--;
        }

        if (nextLevel == levelGenerationParams.Length)
        {
            return(false); // No more levels to play
        }

        gParams = levelGenerationParams[nextLevel];
        nextLevel++;
        return(true);
    }
示例#4
0
    public void SetupCorridor(GenerationParams gParams, Room room, bool firstCorridor)
    {
        // Set a random direction (a random index from 0 to 3, cast to Direction).
        direction = (Direction)Random.Range(0, 4);

        // Find the direction opposite to the one entering the room this corridor is leaving from.
        // Cast the previous corridor's direction to an int between 0 and 3 and add 2 (a number between 2 and 5).
        // Find the remainder when dividing by 4 (if 2 then 2, if 3 then 3, if 4 then 0, if 5 then 1).
        // Cast this number back to a direction.
        // Overall effect is if the direction was South then that is 2, becomes 4, remainder is 0, which is north.
        Direction oppositeDirection = (Direction)(((int)room.enteringCorridor + 2) % 4);

        // If this is noth the first corridor and the randomly selected direction is opposite to the previous corridor's direction...
        if (!firstCorridor && direction == oppositeDirection)
        {
            // Rotate the direction 90 degrees clockwise (North becomes East, East becomes South, etc).
            // This is a more broken down version of the opposite direction operation above but instead of adding 2 we're adding 1.
            // This means instead of rotating 180 (the opposite direction) we're rotating 90.
            int directionInt = (int)direction;
            directionInt++;
            directionInt = directionInt % 4;
            direction    = (Direction)directionInt;
        }

        // Set a random length.
        corridorLength = gParams.corridorLength.Random;

        // Create a cap for how long the length can be (this will be changed based on the direction and position).
        int maxLength = gParams.corridorLength.m_Max;

        switch (direction)
        {
        // If the choosen direction is North (up)...
        case Direction.North:
            // ... the starting position in the x axis can be random but within the width of the room.
            startXPos = Random.Range(room.xPos, room.xPos + room.roomWidth - 1);

            // The starting position in the y axis must be the top of the room.
            startYPos = room.yPos + room.roomHeight;

            // The maximum length the corridor can be is the height of the board (rows) but from the top of the room (y pos + height).
            maxLength = gParams.rows - startYPos - gParams.roomHeight.m_Min;
            break;

        case Direction.East:
            startXPos = room.xPos + room.roomWidth;
            startYPos = Random.Range(room.yPos, room.yPos + room.roomHeight - 1);
            maxLength = gParams.columns - startXPos - gParams.roomWidth.m_Min;
            break;

        case Direction.South:
            startXPos = Random.Range(room.xPos, room.xPos + room.roomWidth);
            startYPos = room.yPos;
            maxLength = startYPos - gParams.roomHeight.m_Min;
            break;

        case Direction.West:
            startXPos = room.xPos;
            startYPos = Random.Range(room.yPos, room.yPos + room.roomHeight);
            maxLength = startXPos - gParams.roomWidth.m_Min;
            break;
        }

        // We clamp the length of the corridor to make sure it doesn't go off the board.
        corridorLength = Mathf.Clamp(corridorLength, 1, maxLength);
    }