Example #1
0
 public void Start()
 {
     GenerateTemplate();
     ChunkTemplates.GetObstaclesFromJson();
 }
Example #2
0
    void GenerateMapJson()
    {
        //Рекурсивно задаем гарантированный путь уровня
        GenerateMapLayout();

        //Выдаем лэйаут в консоль
        Debug.Log(mapLayout.ToDebugLogString("Map layout"));

        //Конвертируем лэйаут пути в улучшенный лэйаут, с учетом стен и прочего.
        mapLayout = ConvertLayout(mapLayout);

        //Выдаем лэйаут в консоль
        Debug.Log(mapLayout.ToDebugLogString("Converted map layout"));


        //Импортируем из джейсона
        ChunkTemplates.GetFromJson();
        ChunkTemplates.GetObstaclesFromJson();

        //create big map template, //!fill it with zeros
        int mapHeight = mapLayout.Length * ChunkTemplates.chunkHeight;
        int mapkWidth = mapLayoutLength * ChunkTemplates.chunkWidth;

        mapTemplate = new int[mapHeight][];
        for (int y = 0; y < mapHeight; y++)
        {
            mapTemplate[y] = new int[mapkWidth];
            for (int x = 0; x < mapkWidth; x++)
            {
                mapTemplate[y][x] = 0;
            }
        }

        //write all into mapTemplate
        for (int y = 0; y < mapLayout.Length; y++)
        {
            for (int x = 0; x < mapLayoutLength; x++)
            {
                int [][] templateMatrix = GenerateRandomByType(mapLayout[y][x]);
                for (int yM = 0; yM < templateMatrix.Length; yM++)
                {
                    for (int xM = 0; xM < templateMatrix[y].Length; xM++)
                    {
                        //if not obstacle space
                        if (templateMatrix[yM][xM] != 13)
                        {
                            mapTemplate[y * ChunkTemplates.chunkHeight + yM][x * ChunkTemplates.chunkWidth + xM] = templateMatrix[yM][xM];
                        }
                        //if obstacle
                        if (templateMatrix[yM][xM] == 12)
                        {
                            int[][] obstacleMatrix = ChunkTemplates.obstacleTemplatesContainer.templates.OrderBy(n => Random.value).FirstOrDefault().GetMatrix();
                            for (int yO = 0; yO < ChunkTemplates.obstacleHeight; yO++)
                            {
                                for (int xO = 0; xO < ChunkTemplates.obstacleWidth; xO++)
                                {
                                    mapTemplate[y * ChunkTemplates.chunkHeight + yM + yO][x * ChunkTemplates.chunkWidth + xM + xO] = obstacleMatrix[yO][xO];
                                }
                            }
                        }
                    }
                }
            }
        }

        //convert from BlockPlacerMatrix into BlockLibraryMatrix
        for (int y = 0; y < mapTemplate.Length; y++)
        {
            for (int x = 0; x < mapTemplate[y].Length; x++)
            {
                int type = mapTemplate[y][x];

                if (type == 0)
                {
                    continue;
                }
                bool flag = false;
                foreach (BlockPlacer.BlockChance blockChance in BlockPlacer.instance.blocks[type - 1].prefabs)
                {
                    if (Random.Range(0, blockChance.divider) == 0)
                    {
                        mapTemplate[y][x] = blockChance.blockLibraryId;
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    mapTemplate[y][x] = 0;
                }
            }
        }

        Debug.Log(mapTemplate.ToDebugLogString("Converted to BlockLibraryMatrix map template"));


        //Set monsters
        for (int y = 1; y < mapTemplate.Length - 1; y++)
        {
            for (int x = 1; x < mapTemplate[y].Length - 1; x++)
            {
                if (mapTemplate[y][x] == 0 && mapTemplate[y][x - 1] == 0 && mapTemplate[y][x + 1] == 0 && mapTemplate[y + 1][x] == 1 && mapTemplate[y + 1][x - 1] == 1 && mapTemplate[y + 1][x + 1] == 1)
                {
                    Debug.Log("Found place for enemy to spawn!!");

                    if (Random.Range(0, 10) == 0)
                    {
                        mapTemplate[y][x] = 12; //charging skeleton
                    }
                    else
                    if (Random.Range(0, 5) == 0)
                    {
                        mapTemplate[y][x] = 2;    //regular skeleton
                    }
                }
                if (mapTemplate[y][x] == 0 && mapTemplate[y + 1][x] == 1 && mapTemplate[y - 1][x] == 1 &&
                    ((mapTemplate[y][x - 1] == 1 && mapTemplate[y][x + 1] == 0) || (mapTemplate[y][x - 1] == 0 && mapTemplate[y][x + 1] == 1)))
                {
                    if (Random.Range(0, 3) == 0)
                    {
                        mapTemplate[y][x] = 5;
                    }
                }
            }
        }



        //spawn blocks
        for (int y = 0; y < mapTemplate.Length; y++)
        {
            for (int x = 0; x < mapTemplate[y].Length; x++)
            {
                SpawnBlock(x, y, mapTemplate[y][x]);
            }
        }

        PrepareColliders();
    }