Exemplo n.º 1
0
    public LevelTemplate GenerateLevelTemplate(int cols                        = DEFAULT_LEVEL_COLS,
                                               int rows                        = DEFAULT_LEVEL_ROWS,
                                               int mainLength                  = DEFAULT_LEVEL_MAIN_LENGTH,
                                               float offshootTemperature       = DEFAULT_LEVEL_OFFSHOOT_TEMPERATURE,
                                               float offshootLengthTemperature = DEFAULT_LEVEL_OFFSHOOT_LENGTH_TEMPERATURE)
    {
        LevelTemplate levelTemplate = new LevelTemplate(cols, rows);

        levelTemplate.SetSpawn(new int[] { cols / 2, rows / 2 });
        SetFirstMain(levelTemplate);

        // connect the spawn to the first main room
        ConnectRooms(levelTemplate, levelTemplate.GetSpawnCoords(), levelTemplate.GetMainHead());

        for (int i = 0; i < mainLength; i++)
        {
            ProceedMain(levelTemplate);
        }
        levelTemplate.SetExit();

        // connect the exit to the last main room
        ConnectRooms(levelTemplate, levelTemplate.GetMainHead(), levelTemplate.GetExitCoords());

        List <int[]> mainCoords = levelTemplate.GetMainCoords();

        foreach (int[] mainCoord in mainCoords)
        {
            List <int[]> surroundingAvailableCoords = GetSurroundingAvailableCoords(levelTemplate, mainCoord);
            if (surroundingAvailableCoords.Count > 0 && rand.NextDouble() < offshootTemperature)
            {
                int[] offshootCoord = surroundingAvailableCoords[rand.Next(surroundingAvailableCoords.Count)];
                levelTemplate.SetOffshoot(offshootCoord);
                RoomTemplate mainRoom     = levelTemplate.GetMap(mainCoord);
                RoomTemplate offshootRoom = levelTemplate.GetMap(offshootCoord);
                ConnectRooms(mainRoom, offshootRoom);
                while (rand.NextDouble() < offshootLengthTemperature)
                {
                    List <int[]> availableNextOffshootCoords = GetSurroundingAvailableCoords(levelTemplate, offshootCoord);
                    if (availableNextOffshootCoords.Count == 0)
                    {
                        break;
                    }
                    int[] oldCoords = offshootCoord;
                    offshootCoord = availableNextOffshootCoords[rand.Next(availableNextOffshootCoords.Count)];
                    levelTemplate.SetOffshoot(offshootCoord);
                    RoomTemplate newOffShootRoom = levelTemplate.GetMap(offshootCoord);
                    ConnectRooms(levelTemplate.GetMap(oldCoords), newOffShootRoom);
                    List <RoomTemplate> potentialConnectRooms = GetPotentialConnectRooms(levelTemplate, offshootCoord, oldCoords);
                    if (potentialConnectRooms.Count < 2)
                    {
                        foreach (RoomTemplate potentialRoom in potentialConnectRooms)
                        {
                            ConnectRooms(newOffShootRoom, potentialRoom);
                        }
                    }
                    offshootLengthTemperature /= 2;
                }
            }
        }

        return(levelTemplate);
    }