Exemplo n.º 1
0
    public void createHall(int x, int y, int count, int prev)
    {
        GameObject pieceType = corridorObject;

        int[] nextHall     = chooseNextHall(x, y, 0);
        int   tileRotation = 0;

        GameObject newRoomObject;

        if (placedPrison == false)
        {
            BaseRoom prisonRoom = new BaseRoom(roomControllerScript.roomsTypes[5]);

            prisonRoom.Rotate(0);
            newRoomObject = roomControllerScript.AddRoomToList(prisonRoom, x - 2, y - 3, 0);
            placedPrison  = true;
        }

        //rotation code
        if (Mathf.Abs(nextHall[2] - previousHall[2]) == 1 || Mathf.Abs(nextHall[2] - previousHall[2]) == 3)
        {
            // corner
            pieceType    = cornerObject;
            tileRotation = nextHall[2];
            switch (previousHall[2])
            {
            // from below
            case 0:
                switch (nextHall[2])
                {
                // to the right
                case 1:
                    tileRotation = 1;
                    //print("zero one");
                    break;

                // to the left
                case 3:
                    tileRotation = 0;
                    break;
                }
                break;

            // from the left
            case 1:
                switch (nextHall[2])
                {
                // to above
                case 0:
                    tileRotation = 3;
                    break;

                // to below
                case 2:
                    tileRotation = 0;
                    break;
                }
                break;

            // from the top
            case 2:
                switch (nextHall[2])
                {
                // to the right
                case 1:
                    tileRotation = 2;
                    break;

                // to the left
                case 3:
                    tileRotation = 3;
                    break;
                }
                break;

            // from the right
            case 3:
                switch (nextHall[2])
                {
                // to above
                case 0:
                    tileRotation = 2;
                    break;

                // to below
                case 2:
                    tileRotation = 1;
                    break;
                }
                break;
            }
        }
        else
        {
            tileRotation = nextHall[2];
            // straight
            pieceType = corridorObject;
        }

        if (nextHall[2] == -1)
        {
            Debug.Log("rebooting");
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            return;
        }



        bool firstDoorBool  = false;
        bool secondDoorBool = false;
        int  endRotation    = 0;// 4-previousHall[2]+nextHall[2]==0?2:0;

        // if its the last piece, cap it off with an end piece
        if (count == hallwaySegments - 1)
        {
            switch (previousHall[2])
            {
            // from below
            case 0:
                switch (nextHall[2])
                {
                case 0:
                    endRotation = 1;
                    break;

                // to the right
                case 1:
                    endRotation = 2;
                    //print("zero one");
                    break;

                // to the left
                case 3:
                    endRotation = 0;
                    break;
                }
                break;

            // from the left
            case 1:
                switch (nextHall[2])
                {
                // to above
                case 0:
                    endRotation = 3;
                    break;

                case 1:
                    endRotation = 2;
                    break;

                // to below
                case 2:
                    endRotation = 0;
                    break;
                }
                break;

            // from the top
            case 2:
                switch (nextHall[2])
                {
                // to the right
                case 1:
                    endRotation = 2;
                    break;

                // to the left
                case 3:
                    endRotation = 3;
                    break;
                }
                break;

            // from the right
            case 3:
                switch (nextHall[2])
                {
                // to above
                case 0:
                    endRotation = 2;
                    break;

                // to below
                case 2:
                    endRotation = 3;
                    break;
                }
                break;
            }

            gridMap[x, y] = new CorridorSection(pieceType, x, y, 0, endRotation);
            Instantiate(endObject, gridMap[x, y].position, gridMap[x, y].rotation);
            return;
        }
        else
        {
            if (roomControllerScript.roomCount < roomControllerScript.maxRooms)
            {
                // try and create rooms
                int[] tryRoom = getWallFace(previousHall[2], nextHall[2]);

                // room code
                BaseRoom roomToTry = new BaseRoom(roomControllerScript.roomsTypes[Random.Range(0, 5)]);

                //for testing specific rooms
                //BaseRoom roomToTry = new BaseRoom(roomControllerScript.roomsTypes[4]);

                for (int v = 0; v < 2; v++)
                {
                    int newX             = x;
                    int newY             = y;
                    int wallDoorLocation = 0;

                    switch (tryRoom[v])
                    {
                    case 0:
                        newY += 1;

                        wallDoorLocation = 0;
                        break;

                    case 1:
                        newX += 1;

                        wallDoorLocation = 1;
                        break;

                    case 2:
                        newY -= roomToTry.length;

                        wallDoorLocation = 2;
                        break;

                    case 3:
                        newX -= roomToTry.width;

                        wallDoorLocation = 3;
                        break;
                    }
                    int canFit = roomControllerScript.CanRoomFit(newX, newY, roomToTry, 0);
                    if (canFit != -1)
                    {
                        newRoomObject = roomControllerScript.AddRoomToList(roomToTry, newX, newY, canFit);
                        if (pieceType.name == "corridorTypeOne")
                        {
                            switch (tryRoom[v])
                            {
                            case 0:
                                firstDoorBool = true;
                                break;

                            case 1:
                                firstDoorBool = true;
                                break;

                            case 2:
                                secondDoorBool = true;
                                break;

                            case 3:
                                secondDoorBool = true;
                                break;
                            }
                        }
                        else
                        {
                            int doorPlacement = tryRoom[v] + tileRotation;
                            if (doorPlacement > 3)
                            {
                                doorPlacement -= 4;
                            }

                            switch (doorPlacement)
                            {
                            case 0:
                                firstDoorBool = true;
                                break;

                            case 1:
                                secondDoorBool = true;
                                break;
                            }
                        }

                        if (newRoomObject.GetComponent <WallPlacer>() != null)
                        {
                            newRoomObject.GetComponent <WallPlacer>().RemoveWall(wallDoorLocation);
                        }
                        break;
                    }
                }
            }

            gridMap[x, y] = new CorridorSection(pieceType, x, y, 0, tileRotation);
            GameObject newHallPiece = Instantiate(gridMap[x, y].gridSectionType, gridMap[x, y].position, gridMap[x, y].rotation);
            newHallPiece.GetComponent <DoorPlacer>().AddDoor(firstDoorBool, secondDoorBool);
        }


        previousHall = nextHall;
        currentHall  = nextHall;

        if (count < hallwaySegments - 1)
        {
            createHall(nextHall[0], nextHall[1], count += 1, nextHall[2]);
        }
        else
        {
            //Debug.Log("finished creating " + count + " halls");
        }
    }