Exemple #1
0
    private void _ComputeFlow()
    {
        var W = Terrain.GetWidth();
        var H = Terrain.GetHeight();
        var t = Terrain.GetData();
        var f = Flow.GetData();

        t.Lock();
        f.Lock();
        for (int x = 0; x < W; x++)
        {
            for (int y = 0; y < H; y++)
            {
                var   Flow   = f.GetPixel(x, y);
                var   curr   = t.GetPixel(x, y);
                var   left   = t.GetPixel(x - 1, y);
                var   right  = t.GetPixel(x + 1, y);
                var   up     = t.GetPixel(x, y - 1);
                var   down   = t.GetPixel(x, y + 1);
                float height = curr.r + curr.g;
                float fl     = 0f;
                float fr     = 0f;
                float fu     = 0f;
                float fd     = 0f;
                if (x - 1 >= 0)
                {
                    fl = Flow.r + Mathf.Max(0, height - left.r - left.g);
                }
                if (x + 1 < W)
                {
                    fr = Flow.g + Mathf.Max(0, height - right.r - right.g);
                }
                if (y - 1 >= 0)
                {
                    fu = Flow.b + Mathf.Max(0, height - up.r - up.g);
                }
                if (y + 1 < H)
                {
                    fd = Flow.a + Mathf.Max(0, height - down.r - down.g);
                }
                float fout = fl + fr + fu + fd;
                float K    = 0f;
                if (fout != 0)
                {
                    K = Mathf.Min(1, curr.r / fout);
                }
                f.SetPixel(x, y, new Color(K * fl, K * fr, K * fu, K * fd));
            }
        }
        t.Unlock();
        f.Unlock();
    }
Exemple #2
0
    public override void _Ready()
    {
        if (Terrain == null || Terrain.GetWidth() != TileSize || Terrain.GetHeight() != TileSize)
        {
            Terrain = new ImageTexture();
            Terrain.Create(TileSize, TileSize, Image.Format.Rgbaf);
            terrainImg = new Image();
            terrainImg.Create(TileSize, TileSize, false, Image.Format.Rgbaf);
        }
        if (Mesh == null)
        {
            var mesh = new PlaneMesh();
            mesh.Size           = new Vector2(TileSize, TileSize);
            mesh.SubdivideWidth = TileSize;
            mesh.SubdivideDepth = TileSize;
            Mesh = mesh;
            var mat = new ShaderMaterial();
            mat.SetShader(TerrainShader);
            SetSurfaceMaterial(0, mat);
        }

        if (WorldPos != Vector3.Zero)
        {
            _LoadTerrainData();
        }

        hydraulics = new HydraulicSimulation(Terrain);
        terrainImg = Terrain.GetData();
    }
Exemple #3
0
 public StatusNumber(XElement xml) : this(
         uint.TryParse(xml?.Attribute("Digits")?.Value, out uint digits) ? digits : 0
         )
 {
     XML      = xml;
     Position = new Vector2(
         float.TryParse(XML?.Attribute("X")?.Value, out float x) ? x : 0,
         float.TryParse(XML?.Attribute("Y")?.Value, out float y) ? y : 0
         );
     if (XML?.Attribute("Name")?.Value is string name && !string.IsNullOrWhiteSpace(name))
     {
         Name = name;
     }
     if (XML?.Attribute("Have")?.Value is string have && !string.IsNullOrWhiteSpace(have))
     {
         Have = Assets.PicTexture(have);
     }
     if (XML?.Attribute("Empty")?.Value is string empty && !string.IsNullOrWhiteSpace(empty))
     {
         Empty = Assets.PicTexture(empty);
     }
     if (Empty != null || Have != null)
     {
         ImageTexture size = Empty ?? Have;
         Item = new Sprite()
         {
             Name     = "Item",
             Position = new Vector2(size.GetWidth() / 2, size.GetHeight() / 2),
         };
     }
     if (uint.TryParse(XML?.Attribute("Max")?.Value, out uint max))
     {
         Max = max;
     }
     if (uint.TryParse(XML?.Attribute("Init")?.Value, out uint init))
     {
         Value = init;
     }
     Visible = !XML.IsFalse("Visible");
 }
Exemple #4
0
 public HydraulicSimulation(ImageTexture terrain)
 {
     this.Terrain = terrain;
     Flow         = new ImageTexture();
     Flow.Create(Terrain.GetWidth(), Terrain.GetHeight(), Image.Format.Rgbaf);
 }