Пример #1
0
        public static TerrainGen Create(World world, LayerCollection layers)
        {
            TerrainGen provider = new TerrainGen();

            provider.Init(world, layers);
            return(provider);
        }
Пример #2
0
        private void ProcessConfigs(World world, LayerCollection layers)
        {
            List <LayerConfigObject> layersConfigs = new List <LayerConfigObject>(layers.Layers);

            // 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 < layers.Layers.Length;)
            {
                LayerConfigObject config = layers.Layers[i];

                // Set layers up
                TerrainLayer layer = config.GetLayer();
                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.LayerName + ". 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.LayerName + ". 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++)
            {
                LayerConfigObject config = layersConfigs[i];
                if (config.IsStructure())
                {
                    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++)
            {
                LocalPools 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()
                    };
                }
            }
        }
Пример #3
0
 protected void Init(World world, LayerCollection layers)
 {
     // Verify all correct layers
     ProcessConfigs(world, layers);
 }