예제 #1
0
    public void BaseSetUp(LayerConfig config, World world, TerrainGen terrainGen)
    {
        this.terrainGen = terrainGen;
        layerName       = config.name;
        isStructure     = LayerConfig.IsStructure(config.structure);
        this.world      = world;
        index           = config.index;

        noise = new NoiseWrapper(world.name);
#if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && ENABLE_FASTSIMD
        noiseSIMD = new NoiseWrapperSIMD(world.name);
#endif

        foreach (var key in config.properties.Keys)
        {
            properties.Add(key.ToString(), config.properties[key].ToString());
        }

        SetUp(config);
    }
예제 #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()
                };
            }
        }
    }