Beispiel #1
0
    IEnumerator GenerateRoom()
    {
        region tempRegion;
        bool fits;
        int roomWidth, roomHeight;
        int roomX, roomY;

        for (int indx = 0; indx < roomGenIterations; indx++) {
            tempRegion = new region (regions.Count);
            fits = true;

            // Random room size
            roomWidth = Random.Range (minRoomSize, maxRoomSize)-1;
            roomHeight = Random.Range (minRoomSize, maxRoomSize)-1;

            // Random room place
            roomX = Random.Range (1, width + 1 - roomWidth);
            roomY = Random.Range (1, height + 1 - roomHeight);

            // See if room fits
            for (int pWidth = roomX; pWidth <= roomX + roomWidth; pWidth++) {
                for (int pHeight = roomY; pHeight <= roomY + roomHeight; pHeight++) {
                    if (dungeon [pWidth, pHeight].Type != CellType.empty)
                        fits = false;
                }
            }
            if (fits == true) {
                for (int pWidth = roomX; pWidth <= roomX + roomWidth; pWidth++) {
                    for (int pHeight = roomY; pHeight <= roomY + roomHeight; pHeight++) {
                        dungeon [pWidth, pHeight].Type = CellType.room;
                        dungeon [pWidth, pHeight].MyRegion = tempRegion.ID;
                        tempRegion.AddCellRefToRegion ((Vector2)dungeon [pWidth, pHeight].Pos);

                        // Remove inner walls
                        dungeon [pWidth, pHeight].walls [0] = false;
                        dungeon [pWidth, pHeight].walls [1] = false;
                        dungeon [pWidth, pHeight].walls [2] = false;
                        dungeon [pWidth, pHeight].walls [3] = false;

                        // Set room edges to Walls
                        if (pHeight == roomY + roomHeight)
                            dungeon [pWidth, pHeight].walls [0] = true;
                        if (pWidth == roomX + roomWidth)
                            dungeon [pWidth, pHeight].walls [1] = true;
                        if (pHeight == roomY)
                            dungeon [pWidth, pHeight].walls [2] = true;
                        if (pWidth == roomX)
                            dungeon [pWidth, pHeight].walls [3] = true;

                        yield return StartCoroutine (UpdateDungeon(pWidth,pHeight));
                    }
                }
                regions.Add (tempRegion);
            }
        }
    }
Beispiel #2
0
    IEnumerator GenerateMaze()
    {
        region tempRegion = new region(regions.Count);
        int selectedDir = 0;
        ArrayList possibleMoves = new ArrayList();
        ArrayList pathList = new ArrayList();
        Vector2 currentCell;

        // Check every cell for unused space to ensure whole map is filled.
        for (int pWidth = 0; pWidth <= width; pWidth++){
            for (int pHeight = 0; pHeight <= height; pHeight++){
                if (dungeon[pWidth,pHeight].Type == CellType.empty )
                {
                    currentCell = new Vector2(pWidth,pHeight);
                    pathList.Add(currentCell);

                    while (pathList.Count != 0){
                        // Assign new values to cell
                        {
                            dungeon[(int)currentCell.x,(int)currentCell.y].Type = CellType.hallway;
                            dungeon[(int)currentCell.x,(int)currentCell.y].MyRegion = tempRegion.ID;
                            tempRegion.AddCellRefToRegion(currentCell);

                            possibleMoves.Clear();
                            if (currentCell.y+1 <= height && dungeon[(int)currentCell.x,(int)currentCell.y+1].Type == CellType.empty) {
                                if (straightHalls && selectedDir == 0){
                                    possibleMoves.Add(0); possibleMoves.Add(0);
                                }
                                possibleMoves.Add(0);
                            }
                            if (currentCell.x+1 <= width && dungeon[(int)currentCell.x+1,(int)currentCell.y].Type == CellType.empty) {
                                if (straightHalls && selectedDir == 1){
                                    possibleMoves.Add(1); possibleMoves.Add(1);
                                }
                                possibleMoves.Add(1);
                            }
                            if (currentCell.y-1 >= 0 && dungeon[(int)currentCell.x,(int)currentCell.y-1].Type == CellType.empty) {
                                if (straightHalls && selectedDir == 2){
                                    possibleMoves.Add(2); possibleMoves.Add(2);
                                }
                                possibleMoves.Add(2);
                            }
                            if (currentCell.x-1 >= 0 && dungeon[(int)currentCell.x-1,(int)currentCell.y].Type == CellType.empty) {
                                if (straightHalls && selectedDir == 3){ possibleMoves.Add(3); possibleMoves.Add(3);}
                                possibleMoves.Add(3);
                            }

                            if (possibleMoves.Count == 0) {
                                pathList.RemoveAt(pathList.Count-1);
                                yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y));
                                if (pathList.Count != 0)
                                    currentCell = (Vector2)pathList[pathList.Count-1];
                            }
                            else{
                                selectedDir = (int)possibleMoves[Random.Range(0,possibleMoves.Count)];
                                switch (selectedDir){
                                case 0: // Up
                                    dungeon[(int)currentCell.x,(int)currentCell.y].walls[0] = false;
                                    dungeon[(int)currentCell.x,(int)currentCell.y+1].walls[2] = false;

                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y));
                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y+1));

                                    currentCell = new Vector2(currentCell.x,currentCell.y+1);
                                    break;
                                case 1: // Right
                                    dungeon[(int)currentCell.x,(int)currentCell.y].walls[1] = false;
                                    dungeon[(int)currentCell.x+1,(int)currentCell.y].walls[3] = false;

                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y));
                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x+1,(int)currentCell.y));

                                    currentCell = new Vector2(currentCell.x+1,currentCell.y);
                                    break;
                                case 2: // Down
                                    dungeon[(int)currentCell.x,(int)currentCell.y].walls[2] = false;
                                    dungeon[(int)currentCell.x,(int)currentCell.y-1].walls[0] = false;

                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y));
                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y-1));

                                    currentCell = new Vector2(currentCell.x,currentCell.y-1);
                                    break;
                                case 3: // Left
                                    dungeon[(int)currentCell.x,(int)currentCell.y].walls[3] = false;
                                    dungeon[(int)currentCell.x-1,(int)currentCell.y].walls[1] = false;

                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x,(int)currentCell.y));
                                    yield return StartCoroutine (UpdateDungeon((int)currentCell.x-1,(int)currentCell.y));

                                    currentCell = new Vector2(currentCell.x-1,currentCell.y);
                                    break;
                                }
                                pathList.Add(currentCell);
                            }
                        }
                    }
                }
            }
            regions.Add(tempRegion);
        }
    }