Esempio n. 1
0
    private void SetupEndRoom(int _width, int _height, int _corridorLength,
                              ref int _numRooms, ref int _availableRooms, ref int _maxRooms, ref bool[][] _gameBoard, ref List <CTRoom> _rooms, ref List <CTCorridor> _corridors)
    {
        //Safety measures
        if (nextCorridors.Count > 0)
        {
            return;
        }

        //Create Corridors
        GameObject node;
        int        startDir = Random.Range(0, (int)Direction.Size);

        for (int i = 0; i < (int)Direction.Size; ++i, ++startDir)
        {
            Direction nextDir = (Direction)(startDir % (int)Direction.Size);
            //if ((startDir % (int)Direction.Size) != (int)prevCorridor)
            {
                //Create Corridors
                if (Random.Range(0.0f, 1.0f) <= 0.5f - i * 0.1f)
                {
                    // Safety Check  if not, create a corridor for the room
                    switch (nextDir)
                    {
                    case Direction.NORTH:
                        if (coordinate.y + 1 < _gameBoard[0].Length)
                        {
                            if (!_gameBoard[coordinate.x][coordinate.y + 1])
                            {
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(CenterPoint.x - 1, yPos + roomHeight - 1, PathNodeDir.NorthL, this);
                                pathnodes.Add(PathNodeDir.NorthL, node.GetComponent <CPathNode>());
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(CenterPoint.x + 1, yPos + roomHeight - 1, PathNodeDir.NorthR, this);
                                pathnodes.Add(PathNodeDir.NorthR, node.GetComponent <CPathNode>());
                                break;
                            }
                        }
                        continue;

                    case Direction.SOUTH:
                        if (coordinate.y - 1 >= 0)
                        {
                            if (!_gameBoard[coordinate.x][coordinate.y - 1])
                            {
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(CenterPoint.x - 1, yPos, PathNodeDir.SouthL, this);
                                pathnodes.Add(PathNodeDir.SouthL, node.GetComponent <CPathNode>());
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(CenterPoint.x + 1, yPos, PathNodeDir.SouthR, this);
                                pathnodes.Add(PathNodeDir.SouthR, node.GetComponent <CPathNode>());
                                break;
                            }
                        }
                        continue;

                    case Direction.EAST:
                        if (coordinate.x + 1 < _gameBoard.Length)
                        {
                            if (!_gameBoard[coordinate.x + 1][coordinate.y])
                            {
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(xPos + roomWidth - 1, CenterPoint.y - 1, PathNodeDir.EastD, this);
                                pathnodes.Add(PathNodeDir.EastD, node.GetComponent <CPathNode>());
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(xPos + roomWidth - 1, CenterPoint.y + 1, PathNodeDir.EastU, this);
                                pathnodes.Add(PathNodeDir.EastU, node.GetComponent <CPathNode>());
                                break;
                            }
                        }
                        continue;

                    case Direction.WEST:
                        if (coordinate.x - 1 >= 0)
                        {
                            if (!_gameBoard[coordinate.x - 1][coordinate.y])
                            {
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(xPos, CenterPoint.y - 1, PathNodeDir.WestD, this);
                                pathnodes.Add(PathNodeDir.WestD, node.GetComponent <CPathNode>());
                                node = Object.Instantiate(Resources.Load("Pathnode"), Vector3.zero, Quaternion.identity) as GameObject;
                                node.GetComponent <CPathNode>().Init(xPos, CenterPoint.y + 1, PathNodeDir.WestU, this);
                                pathnodes.Add(PathNodeDir.WestU, node.GetComponent <CPathNode>());
                                break;
                            }
                        }
                        continue;
                    }

                    CTCorridor newCor = new CTCorridor();
                    newCor.SetupCorridor(this, _corridorLength, nextDir);
                    _corridors.Add(newCor);
                    nextCorridors.Add(nextDir, newCor);
                }
            }
        }

        // Create 1 End Room
        if (nextCorridors.Count <= 0)
        {
            return;
        }

        _availableRooms += nextCorridors.Count;

        // Create Next Room
        CTRoomCoordinate nextRoomCoord = new CTRoomCoordinate(0, 0);

        foreach (Direction nextdir in nextCorridors.Keys)
        {
            //if direction have a room alrdy base on the _gameboard, skip
            switch (nextdir)
            {
            case Direction.NORTH:
                if (!_gameBoard[coordinate.x][coordinate.y + 1])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x, coordinate.y + 1);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth, true);
                }
                break;

            case Direction.EAST:
                if (!_gameBoard[coordinate.x + 1][coordinate.y])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x + 1, coordinate.y);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth, true);
                }
                break;

            case Direction.WEST:
                if (!_gameBoard[coordinate.x - 1][coordinate.y])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x - 1, coordinate.y);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth, true);
                }
                break;

            case Direction.SOUTH:
                if (!_gameBoard[coordinate.x][coordinate.y - 1])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x, coordinate.y - 1);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth, true);
                }
                break;
            }
        }
    }
Esempio n. 2
0
    public int SetupAllRoom(int _boardWidth, int _boardHeight, int _roomWidth, int _roomHeight, int _corridorLength, CTRoomCoordinate _startingCoord,
                            int _maxRooms, ref bool[][] _gameBoard, ref List <CTRoom> _rooms, ref List <CTCorridor> _corridors)
    {
        /*********************
        * GENERATE 1st ROOM
        *********************/
        firstRoom = true;

        // Set a random width and height.
        roomDepth  = 0;
        roomWidth  = _roomWidth;
        roomHeight = _roomHeight;

        xPos = (int)(_boardWidth * 0.5f - roomWidth * 0.5f);
        yPos = (int)(_boardHeight * 0.5f - roomHeight * 0.5f);

        coordinate = new CTRoomCoordinate(_startingCoord);
        _gameBoard[coordinate.x][coordinate.y] = true;

        //set up pathnodes
        //pathnodes.Add(new CPathNode(CenterPoint.x, yPos + roomHeight, Direction.NORTH, this));
        //pathnodes.Add(new CPathNode(xPos, CenterPoint.y, Direction.WEST, this));
        //pathnodes.Add(new CPathNode(CenterPoint.x, yPos, Direction.SOUTH, this));
        //pathnodes.Add(new CPathNode(xPos + roomWidth, CenterPoint.y + roomWidth, Direction.EAST, this));

        generated = true;
        int currNumRooms = 1;   // 1st room

        //Debug.Log("CR_Count " + _rooms.Count + "  _numRooms: " + currNumRooms);
        //Debug.Log("Coord: " + coordinate.x + ", " + coordinate.y );

        // Create Next Corridors and pathnodes
        for (int i = 0; i < (int)Direction.Size; ++i)
        {
            //Create Corridors
            // Safety Check  if not, create a corridor for the room
            switch ((Direction)i)
            {
            case Direction.NORTH:
                //if the next room will be out of board
                if (coordinate.y + 1 < _gameBoard[0].Length)
                {
                    if (!_gameBoard[coordinate.x][coordinate.y + 1])
                    {
                        break;
                    }
                }
                continue;

            case Direction.SOUTH:
                if (coordinate.y - 1 >= 0)
                {
                    if (!_gameBoard[coordinate.x][coordinate.y - 1])
                    {
                        break;
                    }
                }
                continue;

            case Direction.EAST:
                if (coordinate.x + 1 < _gameBoard.Length)
                {
                    if (!_gameBoard[coordinate.x + 1][coordinate.y])
                    {
                        break;
                    }
                }
                continue;

            case Direction.WEST:
                if (coordinate.x - 1 >= 0)
                {
                    if (!_gameBoard[coordinate.x - 1][coordinate.y])
                    {
                        break;
                    }
                }
                continue;
            }

            CTCorridor newCor = new CTCorridor();
            newCor.SetupCorridor(this, _corridorLength, (Direction)i);
            _corridors.Add(newCor);
            nextCorridors.Add((Direction)i, newCor);
        }

        int availableRooms = nextCorridors.Count;

        // Create Next Room
        CTRoomCoordinate nextRoomCoord = new CTRoomCoordinate(0, 0);

        foreach (Direction nextdir in nextCorridors.Keys)
        {
            //if direction have a room alrdy base on the _gameboard, skip
            //if direction is same as previous corridor / corridors in list, skip
            //random a corridor except for north side and check if they got slot on gameboard
            switch (nextdir)
            {
            case Direction.NORTH:
                if (!_gameBoard[coordinate.x][coordinate.y + 1])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x, coordinate.y + 1);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_roomWidth, _roomHeight, nextRoomCoord, nextCorridors[nextdir], ref currNumRooms, ref availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, 0);
                }
                break;

            case Direction.EAST:
                if (!_gameBoard[coordinate.x + 1][coordinate.y])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x + 1, coordinate.y);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_roomWidth, _roomHeight, nextRoomCoord, nextCorridors[nextdir], ref currNumRooms, ref availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, 0);
                }
                break;

            case Direction.WEST:
                if (!_gameBoard[coordinate.x - 1][coordinate.y])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x - 1, coordinate.y);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_roomWidth, _roomHeight, nextRoomCoord, nextCorridors[nextdir], ref currNumRooms, ref availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, 0);
                }
                break;

            case Direction.SOUTH:
                if (!_gameBoard[coordinate.x][coordinate.y - 1])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x, coordinate.y - 1);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_roomWidth, _roomHeight, nextRoomCoord, nextCorridors[nextdir], ref currNumRooms, ref availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, 0);
                }
                break;
            }
        }

        //Check if its enough rooms
        while (currNumRooms < _maxRooms)
        {
            CreateEndRooms(_roomWidth, _roomHeight, _corridorLength, GetEndRooms(_rooms, _maxRooms / 4), ref currNumRooms, ref availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors);
        }

        return(currNumRooms);
    }
Esempio n. 3
0
    private void SetupRoom(int _width, int _height, CTRoomCoordinate _roomCoordinate, CTCorridor _prevCorridor,
                           ref int _numRooms, ref int _availableRooms, ref int _maxRooms, ref bool[][] _gameBoard, ref List <CTRoom> _rooms, ref List <CTCorridor> _corridors,
                           int _depth, bool _ignoreDepth = false)
    {
        //Return Mechanic / Safety
        if (_roomCoordinate.x >= _gameBoard.Length)
        {
            return;
        }

        if (_roomCoordinate.y >= _gameBoard[0].Length)
        {
            return;
        }

        if (_gameBoard[_roomCoordinate.x][_roomCoordinate.y])
        {
            return;
        }

        // Init Values
        // Set the entering corridor direction.
        nextCorridors = new Dictionary <Direction, CTCorridor>();
        prevCorridor  = _prevCorridor.direction;

        roomWidth  = _width;
        roomHeight = _height;
        roomDepth  = _depth + 1;

        coordinate = new CTRoomCoordinate(_roomCoordinate);

        //set up pathnodes
        pathnodes = new Dictionary <PathNodeDir, CPathNode>();

        //Create Room
        switch (_prevCorridor.direction)
        {
        // If the corridor entering this room is going north...
        case Direction.NORTH:
            // The y coordinate of the room must be at the end of the corridor (since the corridor leads to the bottom of the room).
            xPos = _prevCorridor.EndPositionX - (roomWidth / 2);
            yPos = _prevCorridor.EndPositionY + 1;
            break;

        case Direction.EAST:
            xPos = _prevCorridor.EndPositionX + 1;
            yPos = _prevCorridor.EndPositionY - (roomHeight / 2);
            break;

        case Direction.SOUTH:
            xPos = _prevCorridor.EndPositionX - (roomWidth / 2);
            yPos = _prevCorridor.EndPositionY - roomHeight;
            break;

        case Direction.WEST:
            xPos = _prevCorridor.EndPositionX - roomWidth;
            yPos = _prevCorridor.EndPositionY - (roomHeight / 2);
            break;
        }

        // room created successfully!
        _prevCorridor.connectedTo = true;
        generated = true;
        _gameBoard[_roomCoordinate.x][_roomCoordinate.y] = true;

        //pathnodes.Add(new CPathNode(CenterPoint.x, yPos + roomHeight, Direction.NORTH, this));
        //pathnodes.Add(new CPathNode(xPos, CenterPoint.y, Direction.WEST, this));
        //pathnodes.Add(new CPathNode(CenterPoint.x, yPos, Direction.SOUTH, this));
        //pathnodes.Add(new CPathNode(xPos + roomWidth, CenterPoint.y + roomWidth, Direction.EAST, this));

        _availableRooms--;
        _numRooms++;
        //Debug.Log("CR_Count " + _rooms.Count + "  _numRooms: " + _numRooms);
        //Debug.Log("Coord: " + coordinate.x + ", " + coordinate.y);

        if (_numRooms >= _maxRooms)
        {
            return;
        }

        int limit = _maxRooms / 4;

        if (limit < _depth && !_ignoreDepth)
        {
            return;
        }

        // Create Next Corridors and Pathnode
        int startDir = Random.Range(0, (int)Direction.Size);

        for (int i = 0; i < (int)Direction.Size; ++i, ++startDir)
        {
            Direction nextDir = (Direction)(startDir % (int)Direction.Size);
            //Create Corridors
            if (Random.Range(0.0f, 1.0f) <= 1.0f - (_numRooms + _availableRooms) / _maxRooms - i * 0.1f)
            {
                // Safety Check  if not, create a corridor for the room
                switch (nextDir)
                {
                case Direction.NORTH:
                    //if the next room will be out of board
                    if (coordinate.y + 1 < _gameBoard[0].Length)
                    {
                        if (!_gameBoard[coordinate.x][coordinate.y + 1])
                        {
                            break;
                        }
                    }
                    continue;

                case Direction.SOUTH:
                    if (coordinate.y - 1 >= 0)
                    {
                        if (!_gameBoard[coordinate.x][coordinate.y - 1])
                        {
                            break;
                        }
                    }
                    continue;

                case Direction.EAST:
                    if (coordinate.x + 1 < _gameBoard.Length)
                    {
                        if (!_gameBoard[coordinate.x + 1][coordinate.y])
                        {
                            break;
                        }
                    }
                    continue;

                case Direction.WEST:
                    if (coordinate.x - 1 >= 0)
                    {
                        if (!_gameBoard[coordinate.x - 1][coordinate.y])
                        {
                            break;
                        }
                    }
                    continue;
                }

                CTCorridor newCor = new CTCorridor();
                newCor.SetupCorridor(this, _prevCorridor.corridorLength, nextDir);
                _corridors.Add(newCor);
                nextCorridors.Add(nextDir, newCor);
            }
        }

        // Create 1 End Room
        if (nextCorridors.Count <= 0)
        {
            return;
        }

        _availableRooms += nextCorridors.Count;

        // Create Next Room
        CTRoomCoordinate nextRoomCoord = new CTRoomCoordinate(0, 0);

        foreach (Direction nextdir in nextCorridors.Keys)
        {
            //if direction have a room alrdy base on the _gameboard, skip
            switch (nextdir)
            {
            case Direction.NORTH:
                if (!_gameBoard[coordinate.x][coordinate.y + 1])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x, coordinate.y + 1);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth);
                }
                break;

            case Direction.EAST:
                if (!_gameBoard[coordinate.x + 1][coordinate.y])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x + 1, coordinate.y);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth);
                }
                break;

            case Direction.WEST:
                if (!_gameBoard[coordinate.x - 1][coordinate.y])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x - 1, coordinate.y);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth);
                }
                break;

            case Direction.SOUTH:
                if (!_gameBoard[coordinate.x][coordinate.y - 1])
                {
                    //Create next room
                    CTRoom newRoom = new CTRoom();
                    nextRoomCoord.setCoordinate(coordinate.x, coordinate.y - 1);
                    _rooms.Add(newRoom);
                    newRoom.SetupRoom(_width, _height, nextRoomCoord, nextCorridors[nextdir], ref _numRooms, ref _availableRooms, ref _maxRooms, ref _gameBoard, ref _rooms, ref _corridors, roomDepth);
                }
                break;
            }
        }
    }