Esempio n. 1
0
    private TerrainLayer[] ProcessConfigs(World world, string layerFolder)
    {
        var configLoader = new ConfigLoader <LayerConfig>(new[] { layerFolder });

        List <LayerConfig>  layersConfigs = new List <LayerConfig>(configLoader.AllConfigs());
        List <TerrainLayer> layers        = new List <TerrainLayer>(layersConfigs.Count);
        HashSet <int>       indexes       = new HashSet <int>();

        for (int i = 0; i < layersConfigs.Count;)
        {
            LayerConfig config = layersConfigs[i];

            // Ignore broken configs
            var type = Type.GetType(config.layerType + ", " + typeof(Block).Assembly, false);
            if (type == null)
            {
                Debug.LogError("Could not create layer " + config.layerType + " : " + config.name);
                layersConfigs.RemoveAt(i);
                continue;
            }

            // Set layers up
            TerrainLayer layer = (TerrainLayer)Activator.CreateInstance(type);
            layer.BaseSetUp(config, world, this);

            // Do not allow any two layers share the same index
            if (indexes.Contains(layer.index))
            {
                Debug.LogError("Could not create layer " + config.layerType + " : " + config.name + ". Index " + layer.index + " already defined");
                layersConfigs.RemoveAt(i);
                continue;
            }

            // Add layer to layers list
            layers.Add(layer);
            indexes.Add(layer.index);

            ++i;
        }

        // Call OnInit for each layer now that they all have been set up. Thanks to this, layers can
        // e.g. address other layers knowing that they will be able to access all data they need.
        for (int i = 0; i < layersConfigs.Count; i++)
        {
            LayerConfig config = layersConfigs[i];
            layers[i].Init(config);
        }

        return(layers.ToArray());
    }
Esempio n. 2
0
    private void ProcessConfigs(World world, string layerFolder)
    {
        var configLoader = new ConfigLoader <LayerConfig>(new[] { layerFolder });

        List <LayerConfig> layersConfigs = new List <LayerConfig>(configLoader.AllConfigs());

        // Terrain layers
        List <TerrainLayer> terrainLayers        = new List <TerrainLayer>();
        List <int>          terrainLayersIndexes = new List <int>(); // could be implemented as a HashSet, however, it would be insane storing hundreads of layers here

        // Structure layers
        List <TerrainLayer> structLayers        = new List <TerrainLayer>();
        List <int>          structLayersIndexes = new List <int>(); // could be implemented as a HashSet, however, it would be insane storing hundreads of layers here

        for (int i = 0; i < layersConfigs.Count;)
        {
            LayerConfig config = layersConfigs[i];

            // Ignore broken configs
            var type = Type.GetType(config.layerType + ", " + typeof(Block).Assembly, false);
            if (type == null)
            {
                Debug.LogError("Could not create layer " + config.layerType + " : " + config.name);
                layersConfigs.RemoveAt(i);
                continue;
            }

            // Set layers up
            TerrainLayer layer = (TerrainLayer)Activator.CreateInstance(type);
            layer.BaseSetUp(config, world, this);

            if (layer.isStructure)
            {
                // Do not allow any two layers share the same index
                if (structLayersIndexes.Contains(layer.index))
                {
                    Debug.LogError("Could not create structure layer " + config.layerType + " : " + config.name + ". Index " +
                                   layer.index.ToString() + " already defined");
                    layersConfigs.RemoveAt(i);
                    continue;
                }

                // Add layer to layers list
                structLayers.Add(layer);
                structLayersIndexes.Add(layer.index);
            }
            else
            {
                // Do not allow any two layers share the same index
                if (terrainLayersIndexes.Contains(layer.index))
                {
                    Debug.LogError("Could not create terrain layer " + config.layerType + " : " + config.name + ". Index " +
                                   layer.index.ToString() + " already defined");
                    layersConfigs.RemoveAt(i);
                    continue;
                }

                // Add layer to layers list
                terrainLayers.Add(layer);
                terrainLayersIndexes.Add(layer.index);
            }

            ++i;
        }

        // Call OnInit for each layer now that they all have been set up. Thanks to this, layers can
        // e.g. address other layers knowing that they will be able to access all data they need.
        int ti = 0, si = 0;

        for (int i = 0; i < layersConfigs.Count; i++)
        {
            LayerConfig config = layersConfigs[i];
            if (LayerConfig.IsStructure(config.structure))
            {
                structLayers[si++].Init(config);
            }
            else
            {
                terrainLayers[ti++].Init(config);
            }
        }

        // Sort the layers by index
        TerrainLayers = terrainLayers.ToArray();
        Array.Sort(TerrainLayers);
        StructureLayers = structLayers.ToArray();
        Array.Sort(StructureLayers);

        // Register support for noise functionality with each workpool thread
        for (int i = 0; i < Globals.WorkPool.Size; i++)
        {
            var pool = Globals.WorkPool.GetPool(i);
            pool.noiseItems = new NoiseItem[layersConfigs.Count];
            for (int j = 0; j < layersConfigs.Count; j++)
            {
                pool.noiseItems[j] = new NoiseItem
                {
                    noiseGen = new NoiseInterpolator()
                };
            }
        }
    }