コード例 #1
0
        private static void Process(BlockGrid grid)
        {
            //var pres = grid.CalcPressure();
            //pres.Render();

            var water = grid.FindWater();

            water.AddRange(grid.Find(BlockType.WaterSource));
            var moveTo = new SortedList <int, Position>(new DuplicateKeyComparer <int>());

            foreach (var w in water)
            {
                if (grid [w.X + 1, w.Y] == BlockType.Air)
                {
                    moveTo.Add(w.Y, new Position(w.X + 1, w.Y));
                }
                if (grid [w.X, w.Y + 1] == BlockType.Air)
                {
                    moveTo.Add(w.Y + 1, new Position(w.X, w.Y + 1));
                }
                if (grid [w.X - 1, w.Y] == BlockType.Air)
                {
                    moveTo.Add(w.Y, new Position(w.X - 1, w.Y));
                }
                if (grid [w.X, w.Y - 1] == BlockType.Air)
                {
                    moveTo.Add(w.Y - 1, new Position(w.X, w.Y - 1));
                }
            }

            var       newBlock     = moveTo.Values[0];
            BlockType newBlockType = BlockType.Water;

            if (grid[newBlock.X, newBlock.Y - 1] == BlockType.Air)
            {
                newBlockType = BlockType.FallingWater;
            }
            grid.SetBlock(newBlock, newBlockType);

            // Convert falling water to water
            foreach (var w in grid.Find(BlockType.FallingWater))
            {
                if (grid[w.X + 1, w.Y] == BlockType.Water || grid[w.X - 1, w.Y] == BlockType.Water)
                {
                    grid[w.X, w.Y] = BlockType.Water;
                }
            }

            /*
             * foreach (var s in grid.Find(BlockType.WaterSource))
             * {
             *  var stream = grid.FindStream(s);
             *  var p = new List<Coord>();
             *  foreach(var c in stream)
             *  {
             *      if (pres[c.X, c.Y] == 0)
             *      {
             *          // Falling
             *          if (grid[c.X, c.Y - 1] == BlockType.Air)
             *              p.Add(new Coord(c.X, c.Y - 1));
             *          else if (grid[c.X + 1, c.Y - 1] == BlockType.Air)
             *              p.Add(new Coord(c.X + 1, c.Y - 1));
             *          else if (grid[c.X - 1, c.Y - 1] == BlockType.Air)
             *              p.Add(new Coord(c.X - 1, c.Y - 1));
             *      }
             *      else if (pres[c.X,c.Y] < 0)
             *      {
             *          // Up
             *          if (grid[c.X, c.Y + 1] == BlockType.Air)
             *              p.Add(new Coord(c.X, c.Y + 1));
             *          else if (grid[c.X + 1, c.Y + 1] == BlockType.Air)
             *              p.Add(new Coord(c.X + 1, c.Y + 1));
             *          else if (grid[c.X - 1, c.Y + 1] == BlockType.Air)
             *              p.Add(new Coord(c.X - 1, c.Y + 1));
             *      }
             *      else
             *      {
             *          // Sideways
             *          if (grid[c.X + 1, c.Y] == BlockType.Air)
             *              p.Add(new Coord(c.X + 1, c.Y));
             *          else if (grid[c.X - 1, c.Y] == BlockType.Air)
             *              p.Add(new Coord(c.X - 1, c.Y));
             *      }
             *  }
             *  grid.SetBlock(p[0], BlockType.Water);
             * }
             */


            //var water = grid.FindNeighbours(BlockType.Water);
            //water.AddRange(grid.FindNeighbours(BlockType.FallingWater));
            //foreach (var s in water)
            //{
            //    if (grid[s.X,s.Y-1] == BlockType.Dirt)
            //    {
            //        grid[s.X, s.Y] = BlockType.Water;
            //    }
            //    if (grid[s.X, s.Y - 1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y-1] = BlockType.FallingWater;
            //    }
            //}

            //foreach (var s in grid.Find(BlockType.Water))
            //{
            //    if (grid[s.X,s.Y-1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y] = BlockType.FallingWater;
            //    }
            //    else if (grid[s.X+1,s.Y-1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y] = BlockType.Air;
            //        grid[s.X + 1, s.Y - 1] = BlockType.Water;
            //    }
            //    else if (grid[s.X-1,s.Y-1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y] = BlockType.Air;
            //        grid[s.X - 1, s.Y - 1] = BlockType.Water;
            //    }
            //}

            //foreach (var s in grid.Find(BlockType.Water))
            //{
            //    if (grid[s.X, s.Y - 1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y - 1] = BlockType.FallingWater;
            //        grid[s.X, s.Y] = BlockType.Air;
            //    }
            //}
        }