Exemple #1
0
 private void CheckPrefsKeys()
 {
     if (PlayerPrefs.HasKey(MAZE_ROWS_PREFS_KEY))
     {
         int rows = PlayerPrefs.GetInt(MAZE_ROWS_PREFS_KEY);
         if (rows >= 2)
         {
             this.rows = rows;
         }
     }
     if (PlayerPrefs.HasKey(MAZE_COLUMNS_PREFS_KEY))
     {
         int columns = PlayerPrefs.GetInt(MAZE_COLUMNS_PREFS_KEY);
         if (columns >= 2)
         {
             this.columns = columns;
         }
     }
     if (PlayerPrefs.HasKey(MAZE_ALGORITHM_PREFS_KEY))
     {
         algorithm = MazeAlgorithm.Algorithms[PlayerPrefs.GetString(MAZE_ALGORITHM_PREFS_KEY)];
     }
     else
     {
         algorithm = MazeAlgorithm.Algorithms.Values.ToArray()[0];
     }
 }
Exemple #2
0
    private void StartAlgorithm()
    {
        switch (mazeAlgorithm)
        {
        case Algorithm.BinaryTree:
            ma = new BinaryTreeAlgorithm(cells);
            break;

        case Algorithm.Sidewinder:
            ma = new SidewinderAlgorithm(cells);
            break;

        case Algorithm.HuntAndKill:
            ma = new HuntAndKillAlgorithm(cells);
            break;

        case Algorithm.GrowingTree:
            ma = new GrowingTreeAlgorithm(cells);
            break;

        default:
            Debug.Log("Unknown algorithm.");
            break;
        }
    }
Exemple #3
0
 public Maze(Transform mazeCell, int rows, int cols, MazeGenerator.GenAlgorithm alg)
 {
     this.mazeCell = mazeCell;
     this.rows     = rows;
     this.cols     = cols;
     cells         = new MazeCell[rows, cols];
     this.alg      = GetAlgorithmById(alg);
 }
Exemple #4
0
 /// <summary>
 /// Picks the algorithm to be used to generate a maze with.
 /// </summary>
 /// <param name="index">The index given to an algorithm.</param>
 private void AlgorithmPicker(int index)
 {
     switch (index)
     {
         case 0:
             _ma = new RecursiveBacktrackingAlg(_cells, GenerationStepDelay);
             break;
         case 1:
             _ma = new HuntAndKillAlg(_cells, GenerationStepDelay);
             break;
         //case 2:
         //    return _ma = new ThirdNonexistantAlgorithm(_cells, GenerationStepDelay);
         default:
             Debug.LogError("No algorithm selected.");
             break;
     }
 }
        public void BuildMap()
        {
            if (config.width > 2 && config.height > 2)
            {
                this.DestroyAllChild();
                MazeAlgorithm algorithm = (MazeAlgorithm)System.Activator.CreateInstance(System.Type.GetType("TileMazeMaker.Algorithm.Maze." + config.aglorithm.ToString()));
                algorithm.BuildMaze <MazeCell>(config.width, config.height);

                for (int iy = 0; iy < config.height; iy++)
                {
                    for (int ix = 0; ix < config.width; ix++)
                    {
                        CreateMazeCellObject(algorithm.GetAt(ix, iy));
                    }
                }
            }
            else
            {
                Debug.Log("Maze should bigger than 2X2");
            }
        }
Exemple #6
0
    public void Start()
    {
        stage = new GameObject("Stage");
        InitializeMaze();

        // Generate rooms
        GenerateRooms roomsGenerator = new GenerateRooms();
        Room          room           = new Room();

        roomsGenerator.CreateRooms(mazeCells, minRooms, maxRooms);
        room.PersonalizeRoom(mazeCells, floorMaterials);

        // Generate maze around the rooms to make corridors
        Maze maze = new MazeAlgorithm(mazeCells, size);

        maze.CreateMaze();

        // Spawn in enemies and the player
        Spawn sp = new Spawn();

        sp.SpawnEnemies(rows, columns, size, enemyPrefab, minEnemies, maxEnemies);
        sp.SpawnPlayer(playerPrefab);
    }
Exemple #7
0
        /// <summary>
        /// 不是特别完美,和map_data产生了耦合,不过只把他当成内部类,也无妨,只不过,其他类调用的时候,返回的是这个类的成员变量而不是临时变量!
        /// </summary>
        /// <returns></returns>
        public TileMapData GenerateTileMapData()
        {
            //重置
            TileMapData temp_data = new TileMapData();

            config.theme_config.RebuildTileThemeConfig();
            int tile_size_x     = config.tile_size_x;
            int tile_size_y     = config.tile_size_y;
            int total_thickness = config.wall_thickness + config.road_thickness;

            //初始化地图
            for (int iy = 0; iy < tile_size_y; iy++)
            {
                for (int ix = 0; ix < tile_size_x; ix++)
                {
                    int key = SharedUtil.PointHash(ix, iy);
                    temp_data[key] = config.theme_config.GetTilePrefabConfigIndex(config.road_name);
                }
            }

            MazeAlgorithm algorithm =
                (MazeAlgorithm)System.Activator.CreateInstance(System.Type.GetType("TileMazeMaker.Algorithm.Maze." + config.maze_algorithm.ToString()));

            //处理Mask
            algorithm.SetMazeMaskCells(config.maze_mask);

            //设置后期处理
            for (int i = 0; i < config.post_process.Count; i++)
            {
                if (config.post_process[i] != EMazePostProcess.None)
                {
                    algorithm.AddPostProcesser(config.post_process[i].ToString());
                }
            }

            algorithm.BuildMaze <MazeCell>(config.width, config.height);

            dig_list.Add(DigNorth);
            dig_list.Add(DigWest);
            dig_list.Add(DigSouth);
            dig_list.Add(DigEast);

            for (int iy = 0; iy < config.height; iy++)
            {
                for (int ix = 0; ix < config.width; ix++)
                {
                    IMazeCell cell = algorithm.GetAt(ix, iy);
                    if (cell != null)
                    {
                        TileRect out_rect = new TileRect(
                            ix * total_thickness,
                            iy * total_thickness,
                            config.road_thickness + 2 * config.wall_thickness,
                            config.road_thickness + 2 * config.wall_thickness);

                        out_rect.SetMapTileType(temp_data, config.theme_config, config.wall_name);

                        TileRect rect = new TileRect(
                            ix * total_thickness + config.wall_thickness,
                            iy * total_thickness + config.wall_thickness,
                            config.road_thickness,
                            config.road_thickness);

                        rect.SetMapTileType(temp_data, config.theme_config, config.road_name);

                        for (int i = 0; i < (int)EMazeDirection.DirectionCount; i++)
                        {
                            if (cell.IsConnectedTo((EMazeDirection)i))
                            {
                                dig_list[i](temp_data, rect);
                            }
                        }
                    }
                }
            }

            return(temp_data);
        }