Example #1
0
    //private bool ABatchIsBeingProcessed { get; set; }

    /// <summary>
    /// This is the main method which processes the batches of chunks.
    /// It is responsible for calling the terrain generation, decorating, lighting,
    /// and mesh generation.
    /// It's running in a seperate thread, and never stops checking for more work to do.
    /// </summary>
    private void ProcessChunks()
    {
        while (ContinueProcessingChunks)
        {
            // Complete each batch before moving onto the next
            if (m_CurrentBatchBeingProcessed != null || m_ChunkProcessor.ChunksAreBeingAdded)
            {
                continue;
            }


            ChunkBatch batch = m_ChunkProcessor.GetBatchOfChunksToProcess();
            if (batch == null)
            {
                continue;
            }

            m_CurrentBatchBeingProcessed = batch;

            DateTime start = DateTime.Now;

            if (batch.BatchType == BatchType.TerrainGeneration)
            {
                m_TerrainGenerator.GenerateTerrain(batch.Chunks);
                m_CurrentBatchBeingProcessed = null;
                continue;
            }

            m_WorldDecorator.Decorate(batch.Chunks);
            m_LightProcessor.LightChunks(batch.Chunks);
            m_MeshDataGenerator.GenerateMeshData(batch.Chunks);
            m_CurrentBatchBeingProcessed = null;

            if (batch.Chunks.Count > 0)
            {
                Debug.Log("Total Time: " + (DateTime.Now - start));
            }

            Thread.Sleep(1);
        }
    }
    // Start is called before the first frame update
    private void Start()
    {
        var points = new List <Point>();

        try
        {
            Debug.Log(Application.dataPath);
            Debug.Log(Application.persistentDataPath);

            _mapObject = new GameObject("Map");

            if (LoadMapFromCache())
            {
                return;
            }

            _hexGrid                  = new HexGrid(Height, Width, HexTile);
            _map                      = new HexMap(Height, Width);
            _organisationFactory      = new OrganisationFactory();
            _mapOrganizationGenerator = new MapOrganizationGenerator(_mapObject, _organisationFactory);
            _countryGenerator         = new CountryGenerator(_organisationFactory, _mapOrganizationGenerator);

            var heightMapGenerator = new HeightMapGenerator(MountainRatio);
            _terrainGenerator            = new TerrainGenerator(heightMapGenerator);
            _terrainGenerator.DesertBelt = DesertBelt;
            _terrainGenerator.PoleBelt   = PoleBelt;

            var voronoiMap = GenerateMap();
            points = voronoiMap.Where(g => g is Site).Select(s => s.Point).ToList();

            _provinceFactory = new ProvinceFactory(_map, _lines, Instantiate, Province, _organisationFactory);
            _regions         = _provinceFactory.CreateProvinces(points);

            Debug.Log("Map check after region detection");
            CheckMap(points);

            var majorCountryNames = SettingsLoader.Instance.MajorCountryNames;
            var minorCountryNames = SettingsLoader.Instance.MinorCountryNames;
            _countryNames = majorCountryNames.Union(minorCountryNames).ToList();

            _countryGenerator.GenerateCountries(_regions, _map, MajorCountries, MinorCountries, ProvincesMajorCountries, ProvincesMinorCountries, majorCountryNames, minorCountryNames, Instantiate, Country, CountryColors);
            _mapOrganizationGenerator.GenerateContinentsList(Instantiate, Continent, _regions, _map, _mapObject);
            _terrainGenerator.GenerateTerrain(_map);

            var resources = SettingsLoader.Instance.ResourceSettings;
            ResourceService.Instance.SpreadResources(_map, resources);

            if (MapMode == MapMode.InGame)
            {
                SkinMap();
            }
            else
            {
                ColorCountries();
            }

            StoreMapIntoCache();

            var siteCommands   = string.Join(", ", points.Select(p => $"new Point({p.X}, {p.Y})"));
            var siteListComand = $"var points = new List<Point> {{ {siteCommands} }};";
            Debug.Log(siteListComand);
        }
        catch (Exception e)
        {
            Debug.LogError(e);

            LogMap();

            var siteCommands   = string.Join(", ", points.Select(p => $"new Point({p.X}, {p.Y})"));
            var siteListComand = $"var points = new List<Point> {{ {siteCommands} }};";
            Debug.Log(siteListComand);

            Debug.Log("var lines  = new List<Position>()");
            Debug.Log("{");
            foreach (var line in _lines)
            {
                Debug.Log($"new Position({line.X}, {line.Y})");
            }
            Debug.Log("}");
        }
    }