Esempio n. 1
0
    public override void _Process(float delta)
    {
        int camX = int.Parse(Mathf.Stepify(Camera.Transform.origin.x / ChunkSize.x, 1).ToString());
        int camZ = int.Parse(Mathf.Stepify(Camera.Transform.origin.z / ChunkSize.z, 1).ToString());

        for (int x = -RenderDistance; x <= RenderDistance; x++)
        {
            for (int y = -RenderDistance; y <= RenderDistance; y++)
            {
                var pos = new Vector2(x + camX, y + camZ);
                if (!PreloadedChunks.ContainsKey(pos) && !LoadedChunks.ContainsKey(pos) && !toRenderPos.Contains(pos))
                {
                    toRenderPos.Enqueue(pos);
                }
            }
        }

        var temp = TemperatureManager.GetTemperature((int)(Camera.Transform.origin.x), (int)(Camera.Transform.origin.z));
        var hum  = TemperatureManager.GetHumidity((int)(Camera.Transform.origin.x), (int)(Camera.Transform.origin.z));

        InfoLabel.Clear();
        //InfoLabel.Text += "FPS: " + Engine.GetFramesPerSecond() + "\n";
        InfoLabel.Text += "PreloadedCount: " + PreloadedChunks.Count.ToString() + "\n";
        InfoLabel.Text += "LoadedChunksCount: " + LoadedChunks.Count.ToString() + "\n";
        InfoLabel.Text += "ToRenderCount: " + toRenderPos.Count.ToString() + "\n";
        InfoLabel.Text += "CamPosition: " + "X: " + camX + " Z: " + camZ + "\n";
        InfoLabel.Text += "Current Temperature: " + temp + "\n";
        InfoLabel.Text += "Current Humidity: " + hum + "% \n";
        InfoLabel.Text += "Current biome: " + BiomeManager.FindMatchingBiome(temp, hum).ToString();

        foreach (var item in LoadedChunks.Keys)
        {
            if (DistanceToChunk(item) >= RenderDistance * 2)
            {
                Chunk chunk = LoadedChunks[item];
                if (!HasNode("item.ToString()"))
                {
                    continue;
                }
                var node = GetNode(item.ToString());
                node.CallDeferred("free");
                LoadedChunks.TryRemove(item, out chunk);
            }
        }
    }
Esempio n. 2
0
    public void GetChunkData(Chunk chunk)
    {
        // Chunk global position.
        var Offset = chunk.Offset;

        // Settings !
        float         temperature     = TemperatureManager.GetTemperature(0 + (int)(Offset.x * ChunkSize.x), 0 + (int)(Offset.y * ChunkSize.z));
        float         humidity        = TemperatureManager.GetHumidity(0 + (int)(Offset.x * ChunkSize.x), 0 + (int)(Offset.y * ChunkSize.z));
        BiomeSettings CurrentSettings = BiomeManager.BestMatch(temperature, humidity);

        bool placedTree = false;

        for (int z = 0; z < ChunkSize.z; z += 1)
        {
            for (int x = 0; x < ChunkSize.x; x += 1)
            {
                temperature     = TemperatureManager.GetTemperature(x + (int)(chunk.Offset.x * ChunkSize.x), z + (int)(chunk.Offset.y * ChunkSize.z));
                humidity        = TemperatureManager.GetHumidity(x + (int)(chunk.Offset.x * ChunkSize.x), z + (int)(chunk.Offset.y * ChunkSize.z));
                CurrentSettings = BiomeManager.BestMatch(temperature, humidity);


                // Global position of the cube.
                int   gX          = ((int)Offset.x * (int)Chunk.ChunkSize.x) + x;
                int   gZ          = ((int)Offset.y * (int)Chunk.ChunkSize.z) + z;
                float noiseResult = ((Noise.GetNoise2d(gX, gZ) * CurrentSettings.TerrainAmplitude) + 1f) * (ChunkSize.y / 2);
                float height      = Mathf.Clamp(Mathf.Stepify(noiseResult, 1), 0, 254);

                // Default type
                BlockType type = CurrentSettings.DefaultBlocktype;

                // Filling under the chunk too.
                for (int i = 0; i <= height; i++)
                {
                    chunk.Voxels[x, i, z].Active = true;
                    chunk.Voxels[x, i, z].Type   = type;
                }

                // Big mountains?
                if (CurrentSettings.Mountains)
                {
                    GenerateMountains(chunk, x, z, (int)Mathf.Clamp(height, 0f, 254f));
                }

                // Add X layers of block on top of the generated rock.
                var pos = chunk.HighestAt(x, z);
                for (int i = 0; i < CurrentSettings.TopLayerThickness; i++)
                {
                    // Adding some dirt under top layer.
                    var newType = CurrentSettings.UnderLayerType;

                    // if highest block, its grass!
                    if (i == 0)
                    {
                        newType = CurrentSettings.TopLayerType;
                    }

                    // Placing block. Making sure its under 255 height.
                    chunk.Voxels[x, Mathf.Clamp(pos - i, 0, 254), z].Type = newType;
                }



                // Placing decoration
                int decorationChance = rng.RandiRange(1, 100);
                if (decorationChance < CurrentSettings.DecorationRate)
                {
                    // Placing point
                    int dy   = chunk.HighestAt(x, z) + 1;
                    var mesh = (ArrayMesh)ResourceLoader.Load(CurrentSettings.DecorationModel);
                    chunk.AddVoxelSprite(new VoxelSprite(mesh, new Vector3(x, dy, z)));
                }



                // Placing trees
                float treeChance = rng.RandfRange(1f, 100f);
                //GD.Print(treeChance + " <= " + CurrentSettings.TreeRate);
                if (treeChance < CurrentSettings.TreeRate)
                {
                    //GD.Print("Placed tree");
                    var file = (ArrayMesh)ResourceLoader.Load(CurrentSettings.TreeModel);

                    int ty = chunk.HighestAt(x, z) + 1;

                    //Placing point.

                    // Creating and setting the mesh.
                    var meshInstance = new MeshInstance();
                    meshInstance.Mesh = file;
                    meshInstance.Name = CurrentSettings.TreeModel;
                    // Adding it next frame.(safe with mutex lock)
                    CallDeferred("add_child", meshInstance);
                    // Moving it next frame(not in the tree yet :))
                    meshInstance.SetDeferred("translation", new Vector3(x + (Offset.x * ChunkSize.x) - 10, ty,
                                                                        z + (Offset.y * ChunkSize.z + 10)));
                    //meshInstance.SetDeferred("rotation_degrees", new Vector3(0, rng.RandfRange(0, 360), 0));
                }
            }
        }
    }