Exemple #1
0
    //Main map gen method
    public string GenerateGameField(GameLevelType type, string seed = " ")
    {
        Vector2 playerSpawn = Vector2.zero;

        myMapStyle             = Resources.Load <GameMapStyle>(Path.Combine("MapStyles", mapStyles.GetCurrentMapStyle(Relocation.CurrentDungeonLevel)));
        postProccesing.profile = myMapStyle.levelPostProccess;
        CreateMapHolders();
        bool useRandomSeed = seed.Equals(" ") ? true : false;

        if (type == GameLevelType.SimpleLevel)
        {
            //Map base gen
            GenerateMap(useRandomSeed, seed);
            //Noise generation
            int     noiseSeed   = Random.Range(0, 999999);
            Vector2 noiseOffset = new Vector2(Random.Range(-9999, 9999), Random.Range(-9999, 9999));
            float[,] noise = Noise.GenerateNoiseMap(mapSize.xMapSize, mapSize.yMapSize, noiseSeed, scale, octaves, persistance, lacunarity, noiseOffset);
            var groundRegions = Noise.GetClearNoiseRegions(noise, myMapStyle.GetGroundSize(), mapMask);
            for (int i = 0; i < groundRegions.Length; i++)
            {
                var region = groundRegions[i];
                for (int k = 0; k < region.tiles.Count; k++)
                {
                    Vector2Int pos = region.tiles[k];
                    int        sum = 0;
                    if (region.tiles.Contains(new Vector2Int(pos.x, pos.y + 1)))
                    {
                        sum += 1;
                    }
                    if (region.tiles.Contains(new Vector2Int(pos.x + 1, pos.y)))
                    {
                        sum += 2;
                    }
                    if (region.tiles.Contains(new Vector2Int(pos.x, pos.y - 1)))
                    {
                        sum += 4;
                    }
                    if (region.tiles.Contains(new Vector2Int(pos.x - 1, pos.y)))
                    {
                        sum += 8;
                    }
                    SpawnGround(pos.x, pos.y, region.groundLayerIndex, sum);
                }
            }
            for (int x = 0; x < mapSize.xMapSize; x++)
            {
                for (int y = 0; y < mapSize.yMapSize; y++)
                {
                    if (mapMask[x, y] == 1)                    //if walls
                    {
                        bool canSpawnWall = false;
                        for (int k = x - 1; k <= x + 1; k++)
                        {
                            for (int j = y - 1; j <= y + 1; j++)
                            {
                                try{
                                    if (mapMask[k, j] == 0)
                                    {
                                        canSpawnWall = true;
                                        break;
                                    }
                                }catch (Exception ex) {
                                    continue;
                                }
                            }
                            if (canSpawnWall)
                            {
                                break;
                            }
                        }
                        if (canSpawnWall)
                        {
                            SpawnWall(x, y);
                        }
                    }
                }
            }
            playerSpawn = GeneratePlayerPos();
            SpawnLevelMobs(playerSpawn);
            SpawnBuildings(playerSpawn);
            SpawnProps(playerSpawn);
            SpawnBlocks();
            SpawnPlayerDeathPoint();
        }
        else if (type == GameLevelType.CheckPoint)
        {
            SpawnCheckPointRoom();
            playerSpawn = Vector2.zero;
        }
        else if (type == GameLevelType.BossLevel)
        {
            SpawnBossLevel();
        }
        GameSession.SpawnPlayer(new Vector3(playerSpawn.x, playerSpawn.y, 0f));
        return(this.seed);
    }